Skip to content

Commit

Permalink
Allow whitespace lines when calculating edit indentation (#2511)
Browse files Browse the repository at this point in the history
When calculating the edit indentation we want to skip empty lines, but
whitespace lines we should actually use.

## Checklist

- [x] I have added
[tests](https://www.cursorless.org/docs/contributing/test-case-recorder/)
- [/] I have updated the
[docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and
[cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet)
- [/] I have not broken the cheatsheet

---------

Co-authored-by: Pokey Rule <[email protected]>
  • Loading branch information
AndreasArvidsson and pokey authored Jul 16, 2024
1 parent 6f05cb2 commit 08d8c0a
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 2 deletions.
37 changes: 37 additions & 0 deletions data/fixtures/recorded/actions/drinkLine2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
languageId: plaintext
command:
version: 7
spokenForm: drink line
action:
name: editNewLineBefore
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: line}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
def my_funk():
pass
selections:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 4}
marks: {}
finalState:
documentContents: |-
def my_funk():
pass
selections:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 4}
thatMark:
- type: UntypedTarget
contentRange:
start: {line: 2, character: 0}
end: {line: 2, character: 4}
isReversed: false
hasExplicitRange: true
38 changes: 38 additions & 0 deletions data/fixtures/recorded/actions/pourFunk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
languageId: typescript
command:
version: 7
spokenForm: pour funk
action:
name: editNewLineAfter
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: namedFunction}
usePrePhraseSnapshot: true
initialState:
documentContents: |2-
function foo() {
}
selections:
- anchor: {line: 1, character: 2}
active: {line: 1, character: 2}
marks: {}
finalState:
documentContents: |2-
function foo() {
}
selections:
- anchor: {line: 4, character: 4}
active: {line: 4, character: 4}
thatMark:
- type: UntypedTarget
contentRange:
start: {line: 0, character: 4}
end: {line: 2, character: 5}
isReversed: false
hasExplicitRange: true
37 changes: 37 additions & 0 deletions data/fixtures/recorded/actions/pourLine2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
languageId: plaintext
command:
version: 7
spokenForm: pour line
action:
name: editNewLineAfter
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: line}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
def my_funk():
pass
selections:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 4}
marks: {}
finalState:
documentContents: |-
def my_funk():
pass
selections:
- anchor: {line: 2, character: 4}
active: {line: 2, character: 4}
thatMark:
- type: UntypedTarget
contentRange:
start: {line: 1, character: 0}
end: {line: 1, character: 4}
isReversed: false
hasExplicitRange: true
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,19 @@ function getIndentationString(editor: TextEditor, range: Range) {
for (let i = range.start.line; i <= range.end.line; ++i) {
const line = editor.document.lineAt(i);
if (
!line.isEmptyOrWhitespace &&
line.firstNonWhitespaceCharacterIndex < length
line.range.isEmpty ||
(line.isEmptyOrWhitespace && !range.isSingleLine)
) {
// Skip empty lines. If the range is a single line, we want to include the
// indentation of the line even if it is all whitespace so that you can
// "drink line" on an indented but empty line and get the same
// indentation. If the range is not a single line, we want to skip any
// lines that are all whitespace to avoid including a random line of
// whitespace in the indentation.
continue;
}

if (line.firstNonWhitespaceCharacterIndex < length) {
length = line.firstNonWhitespaceCharacterIndex;
indentationString = line.text.slice(0, length);
}
Expand Down

0 comments on commit 08d8c0a

Please sign in to comment.