Skip to content

Commit f23ca12

Browse files
authored
migrate Go func, func name, and lambda to treesitter (#1756)
The previous definitions were mostly correct, although they were missing the possibility of declaring a function without a body. (Understandably. They are rare.) See the end of https://go.dev/ref/spec#Function_declarations. ## 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 bee6ed7 commit f23ca12

21 files changed

+547
-3
lines changed

data/playground/go/funcs.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package p
2+
3+
func plainOldFunction() {
4+
go func() {
5+
// anonymous function
6+
}()
7+
_ = func() string {
8+
return "x"
9+
}
10+
}
11+
12+
func genericFunction[T int | int64](x T) T {
13+
if x == 0 {
14+
panic("zero")
15+
}
16+
return x
17+
}
18+
19+
func (a A) method() {
20+
defer func() {
21+
recover()
22+
}()
23+
[]func(){
24+
func() { panic(0) },
25+
func() { panic(1) },
26+
}[a[0].(int)]()
27+
}
28+
29+
func stub() string

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@ const nodeMatchers: Partial<
1414
ifStatement: "if_statement",
1515
functionCall: ["call_expression", "composite_literal"],
1616
functionCallee: ["call_expression[function]", "composite_literal[type]"],
17-
namedFunction: ["function_declaration", "method_declaration"],
1817
type: [
1918
"pointer_type",
2019
"qualified_type",
2120
"type_identifier",
2221
"function_declaration[result]",
2322
"method_declaration[result]",
2423
],
25-
functionName: ["function_declaration[name]", "method_declaration[name]"],
26-
anonymousFunction: "func_literal",
2724
condition: conditionMatcher("*[condition]"),
2825
argumentOrParameter: cascadingMatcher(
2926
argumentMatcher("argument_list", "parameter_list"),
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 funk
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: namedFunction}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: _ = func() { /* body */ }
15+
selections:
16+
- anchor: {line: 0, character: 25}
17+
active: {line: 0, character: 25}
18+
marks: {}
19+
finalState:
20+
documentContents: "_ = "
21+
selections:
22+
- anchor: {line: 0, character: 4}
23+
active: {line: 0, character: 4}
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 funk air
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: namedFunction}
12+
mark: {type: decoratedSymbol, symbolColor: default, character: a}
13+
usePrePhraseSnapshot: true
14+
initialState:
15+
documentContents: func f[X any]() { /* body */ }
16+
selections:
17+
- anchor: {line: 0, character: 30}
18+
active: {line: 0, character: 30}
19+
marks:
20+
default.a:
21+
start: {line: 0, character: 9}
22+
end: {line: 0, character: 12}
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 funk bat
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: namedFunction}
12+
mark: {type: decoratedSymbol, symbolColor: default, character: b}
13+
usePrePhraseSnapshot: true
14+
initialState:
15+
documentContents: func f() { /* body */ }
16+
selections:
17+
- anchor: {line: 0, character: 18}
18+
active: {line: 0, character: 18}
19+
marks:
20+
default.b:
21+
start: {line: 0, character: 14}
22+
end: {line: 0, character: 18}
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 funk bat
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: namedFunction}
12+
mark: {type: decoratedSymbol, symbolColor: default, character: b}
13+
usePrePhraseSnapshot: true
14+
initialState:
15+
documentContents: func (t T) f() { /* body */ }
16+
selections:
17+
- anchor: {line: 0, character: 29}
18+
active: {line: 0, character: 29}
19+
marks:
20+
default.b:
21+
start: {line: 0, character: 20}
22+
end: {line: 0, character: 24}
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 funk fine
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: namedFunction}
12+
mark: {type: decoratedSymbol, symbolColor: default, character: f}
13+
usePrePhraseSnapshot: true
14+
initialState:
15+
documentContents: func f() { /* body */ }
16+
selections:
17+
- anchor: {line: 0, character: 18}
18+
active: {line: 0, character: 18}
19+
marks:
20+
default.f:
21+
start: {line: 0, character: 5}
22+
end: {line: 0, character: 6}
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 funk fine
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: namedFunction}
12+
mark: {type: decoratedSymbol, symbolColor: default, character: f}
13+
usePrePhraseSnapshot: true
14+
initialState:
15+
documentContents: func (t T) f() { /* body */ }
16+
selections:
17+
- anchor: {line: 0, character: 29}
18+
active: {line: 0, character: 29}
19+
marks:
20+
default.f:
21+
start: {line: 0, character: 11}
22+
end: {line: 0, character: 12}
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 funk fine
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: namedFunction}
12+
mark: {type: decoratedSymbol, symbolColor: default, character: f}
13+
usePrePhraseSnapshot: true
14+
initialState:
15+
documentContents: func f()
16+
selections:
17+
- anchor: {line: 0, character: 8}
18+
active: {line: 0, character: 8}
19+
marks:
20+
default.f:
21+
start: {line: 0, character: 5}
22+
end: {line: 0, character: 6}
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 funk name bat
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: functionName}
12+
mark: {type: decoratedSymbol, symbolColor: default, character: b}
13+
usePrePhraseSnapshot: true
14+
initialState:
15+
documentContents: func f() { /* body */ }
16+
selections:
17+
- anchor: {line: 0, character: 18}
18+
active: {line: 0, character: 18}
19+
marks:
20+
default.b:
21+
start: {line: 0, character: 14}
22+
end: {line: 0, character: 18}
23+
finalState:
24+
documentContents: func () { /* body */ }
25+
selections:
26+
- anchor: {line: 0, character: 5}
27+
active: {line: 0, character: 5}

0 commit comments

Comments
 (0)