Skip to content

Commit 7ebc4e2

Browse files
Fix snippet insertion indentation (#1528)
- Fixes #1527 ## Checklist - [x] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [x] 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) - [x] I have not broken the cheatsheet --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 441a8e9 commit 7ebc4e2

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,15 @@ export default class PositionTarget extends BaseTarget {
4444
if (
4545
this.insertionDelimiter === "\n" &&
4646
this.position === "after" &&
47-
this.thatTarget.contentRange.isSingleLine
47+
this.state.thatTarget!.contentRange.isSingleLine
4848
) {
49+
// If the target that we're wrapping is not a single line, then we
50+
// want to compute indentation based on the entire target. Otherwise,
51+
// we allow the editor to determine how to perform indentation.
52+
// Note that we use `this.state.thatTarget` rather than `this.thatTarget`
53+
// because we don't really want the transitive `thatTarget` behaviour, as
54+
// it's not really the "that" target that we're after; it's the target that
55+
// we're wrapping. Should rework this stuff as part of #803.
4956
return "insertLineAfter";
5057
}
5158

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"elseStatement": {
3+
"definitions": [
4+
{
5+
"scope": {
6+
"langIds": [
7+
"python"
8+
]
9+
},
10+
"body": [
11+
"else:",
12+
"\t$body"
13+
]
14+
}
15+
],
16+
"description": "Else branch",
17+
"variables": {
18+
"body": {}
19+
},
20+
"insertionScopeTypes": ["branch"]
21+
}
22+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
languageId: python
2+
command:
3+
version: 5
4+
spokenForm: snip else after this
5+
action:
6+
name: insertSnippet
7+
args:
8+
- {type: named, name: elseStatement}
9+
targets:
10+
- type: primitive
11+
mark: {type: cursor}
12+
modifiers:
13+
- {type: position, position: after}
14+
usePrePhraseSnapshot: true
15+
initialState:
16+
documentContents: |
17+
if True:
18+
aaa = "bbb"
19+
selections:
20+
- anchor: {line: 1, character: 7}
21+
active: {line: 1, character: 7}
22+
marks: {}
23+
finalState:
24+
documentContents: |
25+
if True:
26+
aaa = "bbb"
27+
else:
28+
29+
selections:
30+
- anchor: {line: 3, character: 4}
31+
active: {line: 3, character: 4}

0 commit comments

Comments
 (0)