Skip to content

Commit 932cde0

Browse files
Updated content range for branch (#1904)
`else if` is now part of the content range for the branch. Updated destination `constructChangeEdit` to support line this insertion delimiters with scopes where the content range doesn't take the full line. eg `} else if` ## 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
1 parent f577cee commit 932cde0

File tree

10 files changed

+77
-29
lines changed

10 files changed

+77
-29
lines changed

packages/cursorless-engine/src/languages/elseIfExtractor.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,14 @@ export function elseIfExtractor(): SelectionExtractor {
5757
}
5858

5959
// If we get here, we are part of a bigger `if` statement; extend our
60-
// removal range past our leading `else` keyword.
60+
// content range past our leading `else` keyword.
6161
const { selection } = contentRange;
6262
return {
63-
selection,
64-
context: {
65-
removalRange: new Selection(
66-
positionFromPoint(parent.child(0)!.startPosition),
67-
selection.end,
68-
),
69-
},
63+
selection: new Selection(
64+
positionFromPoint(parent.child(0)!.startPosition),
65+
selection.end,
66+
),
67+
context: {},
7068
};
7169
};
7270
}

packages/cursorless-engine/src/processTargets/targets/DestinationImpl.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,25 @@ export class DestinationImpl implements Destination {
110110

111111
private getEditRange() {
112112
const position = (() => {
113+
const contentPosition = this.isBefore
114+
? this.contentRange.start
115+
: this.contentRange.end;
116+
113117
if (this.isLineDelimiter) {
114-
const line = this.editor.document.lineAt(
115-
this.isBefore ? this.contentRange.start : this.contentRange.end,
116-
);
117-
return this.isBefore ? line.range.start : line.range.end;
118-
} else {
119-
return this.isBefore ? this.contentRange.start : this.contentRange.end;
118+
const line = this.editor.document.lineAt(contentPosition);
119+
const nonWhitespaceCharacterIndex = this.isBefore
120+
? line.firstNonWhitespaceCharacterIndex
121+
: line.lastNonWhitespaceCharacterIndex;
122+
123+
// Use the full line to include indentation
124+
if (contentPosition.character === nonWhitespaceCharacterIndex) {
125+
return this.isBefore ? line.range.start : line.range.end;
126+
}
120127
}
128+
129+
return contentPosition;
121130
})();
131+
122132
return new Range(position, position);
123133
}
124134

packages/cursorless-vscode-e2e/src/suite/fixtures/recorded/languages/rust/clearBranch5.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ finalState:
2626
documentContents: |-
2727
if n < 0 {
2828
print!("{} is negative", n);
29-
} else else {
29+
} else {
3030
print!("{} is zero", n);
3131
}
3232
selections:
33-
- anchor: {line: 2, character: 7}
34-
active: {line: 2, character: 7}
33+
- anchor: {line: 2, character: 2}
34+
active: {line: 2, character: 2}

packages/cursorless-vscode-e2e/src/suite/fixtures/recorded/languages/rust/clearBranch6.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ finalState:
2626
documentContents: |-
2727
if n < 0 {
2828
print!("{} is negative", n);
29-
} else else {
29+
} else {
3030
print!("{} is zero", n);
3131
}
3232
selections:
33-
- anchor: {line: 2, character: 7}
34-
active: {line: 2, character: 7}
33+
- anchor: {line: 2, character: 2}
34+
active: {line: 2, character: 2}

packages/cursorless-vscode-e2e/src/suite/fixtures/recorded/languages/rust/ditchBranch2.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ finalState:
2626
documentContents: |-
2727
if n < 0 {
2828
print!("{} is negative", n);
29-
} else {
29+
} else {
3030
print!("{} is zero", n);
3131
}
3232
selections:

packages/cursorless-vscode-e2e/src/suite/fixtures/recorded/languages/typescript/clearBranch4.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ finalState:
2626
documentContents: |-
2727
if (true) {
2828
const whatever = "hello";
29-
} else else {
29+
} else {
3030
const whatever = "hello";
3131
}
3232
selections:
33-
- anchor: {line: 2, character: 7}
34-
active: {line: 2, character: 7}
33+
- anchor: {line: 2, character: 2}
34+
active: {line: 2, character: 2}

packages/cursorless-vscode-e2e/src/suite/fixtures/recorded/languages/typescript/clearBranch5.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ finalState:
2626
documentContents: |-
2727
if (true) {
2828
const whatever = "hello";
29-
} else else {
29+
} else {
3030
const whatever = "hello";
3131
}
3232
selections:
33-
- anchor: {line: 2, character: 7}
34-
active: {line: 2, character: 7}
33+
- anchor: {line: 2, character: 2}
34+
active: {line: 2, character: 2}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
languageId: typescript
2+
command:
3+
version: 6
4+
spokenForm: clone branch
5+
action:
6+
name: insertCopyAfter
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: branch}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: |-
15+
if (1 > 0) {
16+
17+
} else if (1 > 2) {
18+
19+
} else {
20+
21+
}
22+
selections:
23+
- anchor: {line: 2, character: 2}
24+
active: {line: 2, character: 2}
25+
marks: {}
26+
finalState:
27+
documentContents: |-
28+
if (1 > 0) {
29+
30+
} else if (1 > 2) {
31+
32+
}
33+
else if (1 > 2) {
34+
35+
} else {
36+
37+
}
38+
selections:
39+
- anchor: {line: 5, character: 0}
40+
active: {line: 5, character: 0}

packages/cursorless-vscode-e2e/src/suite/fixtures/recorded/languages/typescript/ditchBranch.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ finalState:
2626
documentContents: |-
2727
if (true) {
2828
const whatever = "hello";
29-
} else {
29+
} else {
3030
const whatever = "hello";
3131
}
3232
selections:

packages/cursorless-vscode-e2e/src/suite/fixtures/recorded/languages/typescript/ditchBranch2.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ finalState:
2626
documentContents: |-
2727
if (true) {
2828
const whatever = "hello";
29-
} else {
29+
} else {
3030
const whatever = "hello";
3131
}
3232
selections:

0 commit comments

Comments
 (0)