Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate Go func, func name, and lambda to treesitter #1756

Merged
merged 3 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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() {\n\t// body\n}"
josharian marked this conversation as resolved.
Show resolved Hide resolved
selections:
- anchor: {line: 2, character: 1}
active: {line: 2, character: 1}
marks:
default.b:
start: {line: 1, character: 4}
end: {line: 1, character: 8}
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() {\n\t// body\n}"
pokey marked this conversation as resolved.
Show resolved Hide resolved
selections:
- anchor: {line: 2, character: 1}
active: {line: 2, character: 1}
marks:
default.b:
start: {line: 1, character: 4}
end: {line: 1, character: 8}
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() {\n\t// body\n}"
selections:
- anchor: {line: 2, character: 1}
active: {line: 2, character: 1}
marks:
default.f:
start: {line: 0, character: 0}
end: {line: 0, character: 4}
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() {\n\t// body\n}"
selections:
- anchor: {line: 2, character: 1}
active: {line: 2, character: 1}
marks:
default.f:
start: {line: 0, character: 0}
end: {line: 0, character: 4}
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 g()
selections:
- anchor: {line: 0, character: 8}
active: {line: 0, character: 8}
marks:
default.f:
start: {line: 0, character: 0}
end: {line: 0, character: 4}
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 gust
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: namedFunction}
mark: {type: decoratedSymbol, symbolColor: default, character: g}
usePrePhraseSnapshot: true
initialState:
documentContents: func g()
selections:
- anchor: {line: 0, character: 8}
active: {line: 0, character: 8}
marks:
default.g:
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() {\n\t// body\n}"
selections:
- anchor: {line: 2, character: 1}
active: {line: 2, character: 1}
marks:
default.b:
start: {line: 1, character: 4}
end: {line: 1, character: 8}
finalState:
documentContents: "func () {\n\t// body\n}"
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 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 (t T) f() {\n\t// body\n}"
selections:
- anchor: {line: 2, character: 1}
active: {line: 2, character: 1}
marks:
default.b:
start: {line: 1, character: 4}
end: {line: 1, character: 8}
finalState:
documentContents: "func (t T) () {\n\t// body\n}"
selections:
- anchor: {line: 0, character: 11}
active: {line: 0, character: 11}
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 g()
selections:
- anchor: {line: 0, character: 8}
active: {line: 0, character: 8}
marks:
default.f:
start: {line: 0, character: 0}
end: {line: 0, character: 4}
finalState:
documentContents: func ()
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 lambda fine
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: anonymousFunction}
mark: {type: decoratedSymbol, symbolColor: default, character: f}
usePrePhraseSnapshot: true
initialState:
documentContents: _ = func() { /* stuff */ }
selections:
- anchor: {line: 0, character: 26}
active: {line: 0, character: 26}
marks:
default.f:
start: {line: 0, character: 4}
end: {line: 0, character: 8}
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 lambda sun
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: anonymousFunction}
mark: {type: decoratedSymbol, symbolColor: default, character: s}
usePrePhraseSnapshot: true
initialState:
documentContents: _ = func() { /* stuff */ }
selections:
- anchor: {line: 0, character: 26}
active: {line: 0, character: 26}
marks:
default.s:
start: {line: 0, character: 16}
end: {line: 0, character: 21}
finalState:
documentContents: "_ = "
selections:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
23 changes: 23 additions & 0 deletions queries/go.scm
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,26 @@
) @list @map
(#not-parent-type? @list composite_literal)
)

;; Functions

;; function declaration, generic function declaration, function stub
;; func foo() {}
;; func foo[]() {}
;; func foo()
(function_declaration
name: (_) @functionName
pokey marked this conversation as resolved.
Show resolved Hide resolved
body: (_)? @namedFunction.interior
pokey marked this conversation as resolved.
Show resolved Hide resolved
) @namedFunction @functionName.domain
pokey marked this conversation as resolved.
Show resolved Hide resolved

;; method declaration
;; func (X) foo() {}
(method_declaration
name: (_) @functionName
body: (_) @namedFunction.interior
) @namedFunction @functionName.domain

;; func literal
(func_literal
josharian marked this conversation as resolved.
Show resolved Hide resolved
body: (_) @namedFunction.interior
pokey marked this conversation as resolved.
Show resolved Hide resolved
) @anonymousFunction
Loading