Skip to content

Commit

Permalink
migrate Go func, func name, and lambda to treesitter (#1756)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
josharian authored Aug 15, 2023
1 parent bee6ed7 commit f23ca12
Show file tree
Hide file tree
Showing 21 changed files with 547 additions and 3 deletions.
29 changes: 29 additions & 0 deletions data/playground/go/funcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package p

func plainOldFunction() {
go func() {
// anonymous function
}()
_ = func() string {
return "x"
}
}

func genericFunction[T int | int64](x T) T {
if x == 0 {
panic("zero")
}
return x
}

func (a A) method() {
defer func() {
recover()
}()
[]func(){
func() { panic(0) },
func() { panic(1) },
}[a[0].(int)]()
}

func stub() string
3 changes: 0 additions & 3 deletions packages/cursorless-engine/src/languages/go.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ const nodeMatchers: Partial<
ifStatement: "if_statement",
functionCall: ["call_expression", "composite_literal"],
functionCallee: ["call_expression[function]", "composite_literal[type]"],
namedFunction: ["function_declaration", "method_declaration"],
type: [
"pointer_type",
"qualified_type",
"type_identifier",
"function_declaration[result]",
"method_declaration[result]",
],
functionName: ["function_declaration[name]", "method_declaration[name]"],
anonymousFunction: "func_literal",
condition: conditionMatcher("*[condition]"),
argumentOrParameter: cascadingMatcher(
argumentMatcher("argument_list", "parameter_list"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
languageId: go
command:
version: 6
spokenForm: change funk
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: namedFunction}
usePrePhraseSnapshot: true
initialState:
documentContents: _ = func() { /* body */ }
selections:
- anchor: {line: 0, character: 25}
active: {line: 0, character: 25}
marks: {}
finalState:
documentContents: "_ = "
selections:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
languageId: go
command:
version: 6
spokenForm: change funk air
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: namedFunction}
mark: {type: decoratedSymbol, symbolColor: default, character: a}
usePrePhraseSnapshot: true
initialState:
documentContents: func f[X any]() { /* body */ }
selections:
- anchor: {line: 0, character: 30}
active: {line: 0, character: 30}
marks:
default.a:
start: {line: 0, character: 9}
end: {line: 0, character: 12}
finalState:
documentContents: ""
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
languageId: go
command:
version: 6
spokenForm: change funk bat
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: namedFunction}
mark: {type: decoratedSymbol, symbolColor: default, character: b}
usePrePhraseSnapshot: true
initialState:
documentContents: func f() { /* body */ }
selections:
- anchor: {line: 0, character: 18}
active: {line: 0, character: 18}
marks:
default.b:
start: {line: 0, character: 14}
end: {line: 0, character: 18}
finalState:
documentContents: ""
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
languageId: go
command:
version: 6
spokenForm: change funk bat
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: namedFunction}
mark: {type: decoratedSymbol, symbolColor: default, character: b}
usePrePhraseSnapshot: true
initialState:
documentContents: func (t T) f() { /* body */ }
selections:
- anchor: {line: 0, character: 29}
active: {line: 0, character: 29}
marks:
default.b:
start: {line: 0, character: 20}
end: {line: 0, character: 24}
finalState:
documentContents: ""
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
languageId: go
command:
version: 6
spokenForm: change funk fine
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: namedFunction}
mark: {type: decoratedSymbol, symbolColor: default, character: f}
usePrePhraseSnapshot: true
initialState:
documentContents: func f() { /* body */ }
selections:
- anchor: {line: 0, character: 18}
active: {line: 0, character: 18}
marks:
default.f:
start: {line: 0, character: 5}
end: {line: 0, character: 6}
finalState:
documentContents: ""
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
languageId: go
command:
version: 6
spokenForm: change funk fine
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: namedFunction}
mark: {type: decoratedSymbol, symbolColor: default, character: f}
usePrePhraseSnapshot: true
initialState:
documentContents: func (t T) f() { /* body */ }
selections:
- anchor: {line: 0, character: 29}
active: {line: 0, character: 29}
marks:
default.f:
start: {line: 0, character: 11}
end: {line: 0, character: 12}
finalState:
documentContents: ""
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
languageId: go
command:
version: 6
spokenForm: change funk fine
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: namedFunction}
mark: {type: decoratedSymbol, symbolColor: default, character: f}
usePrePhraseSnapshot: true
initialState:
documentContents: func f()
selections:
- anchor: {line: 0, character: 8}
active: {line: 0, character: 8}
marks:
default.f:
start: {line: 0, character: 5}
end: {line: 0, character: 6}
finalState:
documentContents: ""
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
languageId: go
command:
version: 6
spokenForm: change funk name bat
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: functionName}
mark: {type: decoratedSymbol, symbolColor: default, character: b}
usePrePhraseSnapshot: true
initialState:
documentContents: func f() { /* body */ }
selections:
- anchor: {line: 0, character: 18}
active: {line: 0, character: 18}
marks:
default.b:
start: {line: 0, character: 14}
end: {line: 0, character: 18}
finalState:
documentContents: func () { /* body */ }
selections:
- anchor: {line: 0, character: 5}
active: {line: 0, character: 5}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
languageId: go
command:
version: 6
spokenForm: change funk name fine
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: functionName}
mark: {type: decoratedSymbol, symbolColor: default, character: f}
usePrePhraseSnapshot: true
initialState:
documentContents: func f() { /* body */ }
selections:
- anchor: {line: 0, character: 18}
active: {line: 0, character: 18}
marks:
default.f:
start: {line: 0, character: 5}
end: {line: 0, character: 6}
finalState:
documentContents: func () { /* body */ }
selections:
- anchor: {line: 0, character: 5}
active: {line: 0, character: 5}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
languageId: go
command:
version: 6
spokenForm: change funk trap
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: namedFunction}
mark: {type: decoratedSymbol, symbolColor: default, character: t}
usePrePhraseSnapshot: true
initialState:
documentContents: func (t T) f() { /* body */ }
selections:
- anchor: {line: 0, character: 29}
active: {line: 0, character: 29}
marks:
default.t:
start: {line: 0, character: 8}
end: {line: 0, character: 9}
finalState:
documentContents: ""
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
languageId: go
command:
version: 6
spokenForm: change inside funk
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- {type: interiorOnly}
- type: containingScope
scopeType: {type: namedFunction}
usePrePhraseSnapshot: true
initialState:
documentContents: "func (t T) f() {\n\t_ = 1\n}"
selections:
- anchor: {line: 1, character: 6}
active: {line: 1, character: 6}
marks: {}
finalState:
documentContents: "func (t T) f() {\n\t\n}"
selections:
- anchor: {line: 1, character: 1}
active: {line: 1, character: 1}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
languageId: go
command:
version: 6
spokenForm: change inside funk bat
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- {type: interiorOnly}
- type: containingScope
scopeType: {type: namedFunction}
mark: {type: decoratedSymbol, symbolColor: default, character: b}
usePrePhraseSnapshot: true
initialState:
documentContents: func f() { /* body */ }
selections:
- anchor: {line: 0, character: 18}
active: {line: 0, character: 18}
marks:
default.b:
start: {line: 0, character: 14}
end: {line: 0, character: 18}
finalState:
documentContents: func f() { }
selections:
- anchor: {line: 0, character: 11}
active: {line: 0, character: 11}
Loading

0 comments on commit f23ca12

Please sign in to comment.