Skip to content

Commit 77a34ab

Browse files
josharianpokey
andauthored
migrate Go scopes statement, comment, string, and textFragment (#1685)
This begins the Go migration to treesitter. In addition to migrating scopes, this commit includes two bug fixes. (1) It adds a statement scope for expression statements. This fixes #1592. Note that block statements are omitted. There are many block statements in a normal Go program, most of which are not of interest. For example, in: if x() { // nada } there are two statements, one that is the entire if statement, and one block statement that runs from brace to brace. As the typical block statement includes other statements, and is usually part of other named statements, it will not typically be helpful during editing. (2) It fixes an existing bug in the handling of paired delimiters when used with treesitter-based scopes. When looking for paired delimiters from inside a string, we first search inside the string only, then outside the string only. This lets us find the parens in both "(hello)" and ("hello") given command "take round odd". However, that fallback logic did not get replicated to the treesitter-based scope flow. As a result, we never searched outside the string. Co-authored-by: Pokey Rule <[email protected]> ## 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: Pokey Rule <[email protected]>
1 parent 4adc68a commit 77a34ab

File tree

15 files changed

+335
-34
lines changed

15 files changed

+335
-34
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ const textFragmentExtractors: Record<
135135
"css",
136136
scssStringTextFragmentExtractor,
137137
),
138-
go: constructDefaultTextFragmentExtractor("go"),
139138
html: constructDefaultTextFragmentExtractor(
140139
"html",
141140
htmlStringTextFragmentExtractor,

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

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,14 @@ import {
88
import { NodeMatcherAlternative } from "../typings/Types";
99
import { SimpleScopeTypeType } from "@cursorless/common";
1010

11-
// Generated by the following command:
12-
// `curl https://raw.githubusercontent.com/tree-sitter/tree-sitter-go/master/src/node-types.json | jq '[.[] | select(.type == "_statement" or .type == "_simple_statement") | .subtypes[].type]'`
13-
const STATEMENT_TYPES = [
14-
"_expression",
15-
"assignment_statement",
16-
"dec_statement",
17-
"inc_statement",
18-
"send_statement",
19-
"short_var_declaration",
20-
"_simple_statement",
21-
"break_statement",
22-
"const_declaration",
23-
"continue_statement",
24-
"defer_statement",
25-
"empty_statement",
26-
"expression_switch_statement",
27-
"fallthrough_statement",
28-
"for_statement",
29-
"go_statement",
30-
"goto_statement",
31-
"if_statement",
32-
"labeled_statement",
33-
"return_statement",
34-
"select_statement",
35-
"type_declaration",
36-
"type_switch_statement",
37-
"var_declaration",
38-
];
39-
4011
const nodeMatchers: Partial<
4112
Record<SimpleScopeTypeType, NodeMatcherAlternative>
4213
> = {
4314
map: "composite_literal",
4415
list: ["composite_literal", "slice_type", "array_type"],
45-
statement: STATEMENT_TYPES,
46-
string: ["interpreted_string_literal", "raw_string_literal"],
4716
ifStatement: "if_statement",
4817
functionCall: ["call_expression", "composite_literal"],
4918
functionCallee: ["call_expression[function]", "composite_literal[type]"],
50-
comment: "comment",
5119
namedFunction: ["function_declaration", "method_declaration"],
5220
type: [
5321
"pointer_type",

packages/cursorless-engine/src/processTargets/modifiers/surroundingPair/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,25 @@ function processSurroundingPairCore(
8686
);
8787

8888
if (containingScope != null) {
89-
return findSurroundingPairTextBased(
89+
const surroundingRange = findSurroundingPairTextBased(
9090
editor,
9191
range,
9292
containingScope[0].contentRange,
9393
delimiters,
9494
scopeType,
9595
);
96+
if (surroundingRange != null) {
97+
// Found the pair within this text fragment or comment, e.g. "(abc)"
98+
return surroundingRange;
99+
}
100+
// Search in the rest of the file, to find e.g. ("abc")
101+
return findSurroundingPairTextBased(
102+
editor,
103+
range,
104+
null,
105+
delimiters,
106+
scopeType,
107+
);
96108
}
97109
}
98110

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
languageId: go
2+
command:
3+
version: 6
4+
spokenForm: change state plex
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: statement}
12+
mark: {type: decoratedSymbol, symbolColor: default, character: x}
13+
usePrePhraseSnapshot: true
14+
initialState:
15+
documentContents: x = 1
16+
selections:
17+
- anchor: {line: 0, character: 5}
18+
active: {line: 0, character: 5}
19+
marks:
20+
default.x:
21+
start: {line: 0, character: 0}
22+
end: {line: 0, character: 1}
23+
finalState:
24+
documentContents: ""
25+
selections:
26+
- anchor: {line: 0, character: 0}
27+
active: {line: 0, character: 0}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
languageId: go
2+
command:
3+
version: 6
4+
spokenForm: change round
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: surroundingPair, delimiter: parentheses}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: _ = ("abc")
15+
selections:
16+
- anchor: {line: 0, character: 9}
17+
active: {line: 0, character: 9}
18+
marks: {}
19+
finalState:
20+
documentContents: "_ = "
21+
selections:
22+
- anchor: {line: 0, character: 4}
23+
active: {line: 0, character: 4}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
languageId: go
2+
command:
3+
version: 6
4+
spokenForm: change round
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: surroundingPair, delimiter: parentheses}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: _ = "(abc)"
15+
selections:
16+
- anchor: {line: 0, character: 9}
17+
active: {line: 0, character: 9}
18+
marks: {}
19+
finalState:
20+
documentContents: _ = ""
21+
selections:
22+
- anchor: {line: 0, character: 5}
23+
active: {line: 0, character: 5}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
languageId: go
2+
command:
3+
version: 6
4+
spokenForm: change state plex
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: statement}
12+
mark: {type: decoratedSymbol, symbolColor: default, character: x}
13+
usePrePhraseSnapshot: true
14+
initialState:
15+
documentContents: const x = 1
16+
selections:
17+
- anchor: {line: 0, character: 11}
18+
active: {line: 0, character: 11}
19+
marks:
20+
default.x:
21+
start: {line: 0, character: 6}
22+
end: {line: 0, character: 7}
23+
finalState:
24+
documentContents: ""
25+
selections:
26+
- anchor: {line: 0, character: 0}
27+
active: {line: 0, character: 0}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
languageId: go
2+
command:
3+
version: 6
4+
spokenForm: change state plex
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: statement}
12+
mark: {type: decoratedSymbol, symbolColor: default, character: x}
13+
usePrePhraseSnapshot: true
14+
initialState:
15+
documentContents: x--
16+
selections:
17+
- anchor: {line: 0, character: 3}
18+
active: {line: 0, character: 3}
19+
marks:
20+
default.x:
21+
start: {line: 0, character: 0}
22+
end: {line: 0, character: 1}
23+
finalState:
24+
documentContents: ""
25+
selections:
26+
- anchor: {line: 0, character: 0}
27+
active: {line: 0, character: 0}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
languageId: go
2+
command:
3+
version: 6
4+
spokenForm: change state
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: statement}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: fmt.Print("hi")
15+
selections:
16+
- anchor: {line: 0, character: 13}
17+
active: {line: 0, character: 13}
18+
marks: {}
19+
finalState:
20+
documentContents: ""
21+
selections:
22+
- anchor: {line: 0, character: 0}
23+
active: {line: 0, character: 0}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
languageId: go
2+
command:
3+
version: 6
4+
spokenForm: change state plex
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: statement}
12+
mark: {type: decoratedSymbol, symbolColor: default, character: x}
13+
usePrePhraseSnapshot: true
14+
initialState:
15+
documentContents: if x {}
16+
selections:
17+
- anchor: {line: 0, character: 6}
18+
active: {line: 0, character: 6}
19+
marks:
20+
default.x:
21+
start: {line: 0, character: 3}
22+
end: {line: 0, character: 4}
23+
finalState:
24+
documentContents: ""
25+
selections:
26+
- anchor: {line: 0, character: 0}
27+
active: {line: 0, character: 0}

0 commit comments

Comments
 (0)