Skip to content

Commit

Permalink
correctly highlight go methods in interfaces & prepare for locals sup…
Browse files Browse the repository at this point in the history
…port
  • Loading branch information
topi314 committed Jun 18, 2024
1 parent bed65b9 commit 3a03405
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 106 deletions.
20 changes: 20 additions & 0 deletions config/queries/go/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@
(method_declaration
name: (field_identifier) @function.method)

(method_declaration
(parameter_list
(parameter_declaration
name: (identifier) @variable.parameter)))

(type_declaration
(type_spec
(interface_type
(method_elem
name: (field_identifier) @function.method))))

(type_declaration
(type_spec
(interface_type
(method_elem
(parameter_list
(parameter_declaration
name: (identifier) @variable.parameter))))))


; Labels

(labeled_statement
Expand Down
98 changes: 24 additions & 74 deletions config/queries/go/locals.scm
Original file line number Diff line number Diff line change
@@ -1,88 +1,38 @@
((function_declaration
name: (identifier) @local.definition.function) ; @function
)
; Scopes

[
(function_declaration)
(type_declaration)
(block)
] @local.scope

; Definitions

(type_parameter_list
(parameter_declaration
name: (identifier) @local.definition))

((method_declaration
name: (field_identifier) @local.definition.method) ; @function.method
)
(parameter_declaration (identifier) @local.definition)
(variadic_parameter_declaration (identifier) @local.definition)

(short_var_declaration
left: (expression_list
(identifier) @local.definition.var))
(identifier) @local.definition))

(var_spec
name: (identifier) @local.definition.var)

(parameter_declaration
(identifier) @local.definition.var)

(variadic_parameter_declaration
(identifier) @local.definition.var)
(identifier) @local.definition)

(for_statement
(range_clause
left: (expression_list
(identifier) @local.definition.var)))
(range_clause
left: (expression_list
(identifier) @local.definition)))

(const_declaration
(const_spec
name: (identifier) @local.definition.var))
(const_spec
name: (identifier) @local.definition))

(type_declaration
(type_spec
name: (type_identifier) @local.definition.type))
; References

; reference
(identifier) @local.reference

(type_identifier) @local.reference

(field_identifier) @local.reference

((package_identifier) @local.reference
(#set! reference.kind "namespace"))

(package_clause
(package_identifier) @local.definition.namespace)

(import_spec_list
(import_spec
name: (package_identifier) @local.definition.namespace))

; Call references
((call_expression
function: (identifier) @local.reference)
(#set! reference.kind "call"))

((call_expression
function: (selector_expression
field: (field_identifier) @local.reference))
(#set! reference.kind "call"))

((call_expression
function: (parenthesized_expression
(identifier) @local.reference))
(#set! reference.kind "call"))

((call_expression
function: (parenthesized_expression
(selector_expression
field: (field_identifier) @local.reference)))
(#set! reference.kind "call"))

; Scopes
(func_literal) @local.scope

(source_file) @local.scope

(function_declaration) @local.scope

(if_statement) @local.scope

(block) @local.scope

(expression_switch_statement) @local.scope

(for_statement) @local.scope

(method_declaration) @local.scope
(type_identifier) @local.reference
8 changes: 4 additions & 4 deletions config/themes/dark.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ hint = { underline = true, underline_style = 'curly', underline_color = '248' }
"tag" = { foreground = '9' }
"attribute" = { foreground = '3' }
"property" = { foreground = '9' }
"label" = { foreground = '11' }
"label" = { foreground = '13', italic = true }

"comment" = { foreground = '8', italic = true }
"comment.error" = { foreground = '1', bold = true }
Expand All @@ -106,16 +106,16 @@ hint = { underline = true, underline_style = 'curly', underline_color = '248' }
"punctuation.special" = { foreground = '13' }
#"punctuation.bracket" = { foreground = '11' }

"operator" = { foreground = '15' }
"operator" = { }
"special" = { foreground = '14' }
"string" = { foreground = '10', bold = true }
"string.escape" = { foreground = '14' }
"string.special" = { foreground = '3' }
"string.special.path" = { foreground = '10', bold = true }
"string.special.url" = { foreground = '4' }
"type" = { foreground = '11' }
"type" = { foreground = '11', bold = true }

"variable" = { foreground = '15' }
"variable" = { }
"variable.builtin" = { foreground = '14' }
"variable.parameter" = { foreground = '3' }

Expand Down
10 changes: 5 additions & 5 deletions config/themes/dark_simple.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ hint = { underline = true }
"tag" = { foreground = '9' }
"attribute" = { foreground = '3' }
"property" = { foreground = '9' }
"label" = { foreground = '11' }
"label" = { foreground = '13', italic = true }

"comment" = { foreground = '8', italic = true }
"comment.error" = { foreground = '1', bold = true }
Expand All @@ -106,16 +106,16 @@ hint = { underline = true }
"punctuation.special" = { foreground = '13' }
#"punctuation.bracket" = { foreground = '11' }

"operator" = { foreground = '15' }
"operator" = { }
"special" = { foreground = '14' }
"string" = { foreground = '10', bold = true }
"string.escape" = { foreground = '14' }
"string.special" = { foreground = '3' }
"string.special.path" = { foreground = '10', bold = true }
"string.special.url" = { foreground = '4' }
"type" = { foreground = '11' }
"type" = { foreground = '11', bold = true }

"variable" = { foreground = '15' }
"variable" = { }
"variable.builtin" = { foreground = '14' }
"variable.parameter" = { foreground = '3' }

Expand All @@ -135,4 +135,4 @@ hint = { underline = true }

"diff.plus" = { foreground = '10' }
"diff.delta" = { foreground = '11' }
"diff.minus" = { foreground = '9' }
"diff.minus" = { foreground = '9' }
10 changes: 5 additions & 5 deletions config/themes/dark_very_simple.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ hint = { underline = true }
"tag" = { foreground = '9' }
"attribute" = { foreground = '3' }
"property" = { foreground = '9' }
"label" = { foreground = '11' }
"label" = { foreground = '13', italic = true }

"comment" = { foreground = '8', italic = true }
"comment.error" = { foreground = '1', bold = true }
Expand All @@ -66,16 +66,16 @@ hint = { underline = true }
"punctuation.special" = { foreground = '13' }
#"punctuation.bracket" = { foreground = '11' }

"operator" = { foreground = '15' }
"operator" = { }
"special" = { foreground = '14' }
"string" = { foreground = '10', bold = true }
"string.escape" = { foreground = '14' }
"string.special" = { foreground = '3' }
"string.special.path" = { foreground = '10', bold = true }
"string.special.url" = { foreground = '4' }
"type" = { foreground = '11' }
"type" = { foreground = '11', bold = true }

"variable" = { foreground = '15' }
"variable" = { }
"variable.builtin" = { foreground = '14' }
"variable.parameter" = { foreground = '3' }

Expand All @@ -95,4 +95,4 @@ hint = { underline = true }

"diff.plus" = { foreground = '10' }
"diff.delta" = { foreground = '11' }
"diff.minus" = { foreground = '9' }
"diff.minus" = { foreground = '9' }
2 changes: 1 addition & 1 deletion docs/THEMES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The following scopes are available for styling:
- `property` - Object properties
- `tag` - Tags (e.g. `<body>` in HTML)
- `builtin` - Built-in tags (e.g. `div`, `span`, etc.)
- `label`
- `label` - Labels (e.g. `Label:` to break out of a loop in Go)
- `operator` - `||`, `+=`, `>`, `&&`, etc.
- `namespace`
- `special`
Expand Down
16 changes: 13 additions & 3 deletions gopad/editor/languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ type OutlineQuery struct {
}

type LocalsQuery struct {
Query *sitter.Query
Query *sitter.Query
ScopeCaptureID uint32
DefinitionCaptureID uint32
ReferenceCaptureID uint32
}

func GetCaptureIndexes(query *sitter.Query, captureNames []string) []*uint32 {
Expand Down Expand Up @@ -187,10 +190,17 @@ func loadTreeSitterGrammar(name string, cfg config.GrammarConfig, defaultConfigs
InjectionContentCaptureID: *indexes[0],
}
case "locals.scm":
_ = GetCaptureIndexes(query, []string{})
indexes := GetCaptureIndexes(query, []string{
"local.scope",
"local.definition",
"local.reference",
})

localsQuery = &LocalsQuery{
Query: query,
Query: query,
ScopeCaptureID: *indexes[0],
DefinitionCaptureID: *indexes[1],
ReferenceCaptureID: *indexes[2],
}
case "outline.scm":
indexes := GetCaptureIndexes(query, []string{
Expand Down
19 changes: 5 additions & 14 deletions gopad/editor/tree_sitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

type Tree struct {
Tree *sitter.Tree
Language Language
Language *Language
SubTrees map[string]*Tree
Ranges []sitter.Range
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func (f *File) updateTree() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

tree, err := parseTree(ctx, f.buffer, f.tree, *f.language, nil)
tree, err := parseTree(ctx, f.buffer, f.tree, f.language, nil)
if err != nil {
return err
}
Expand All @@ -118,7 +118,7 @@ func (f *File) updateTree() error {
return nil
}

func parseTree(ctx context.Context, buff *buffer.Buffer, oldTree *Tree, language Language, ranges []sitter.Range) (*Tree, error) {
func parseTree(ctx context.Context, buff *buffer.Buffer, oldTree *Tree, language *Language, ranges []sitter.Range) (*Tree, error) {
parser := sitter.NewParser()
parser.SetLanguage(language.Grammar.Language)
if len(ranges) > 0 {
Expand Down Expand Up @@ -159,7 +159,7 @@ func parseTree(ctx context.Context, buff *buffer.Buffer, oldTree *Tree, language
continue
}

subLanguage := getLanguageByMatch(&language, match)
subLanguage := getLanguageByMatch(language, match)
if subLanguage == nil || subLanguage.Config.Grammar == nil {
continue
}
Expand All @@ -182,7 +182,7 @@ func parseTree(ctx context.Context, buff *buffer.Buffer, oldTree *Tree, language
oldSubTree = oldTree.SubTrees[subLanguage.Name]
}

subTree, err := parseTree(ctx, buff, oldSubTree, *subLanguage, subRanges)
subTree, err := parseTree(ctx, buff, oldSubTree, subLanguage, subRanges)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -489,12 +489,3 @@ func (f *File) OutlineTree() []OutlineItem {

return items
}

type Local struct {
Name string
Properties map[string]string
}

func (f *File) localsTree() []Local {
return nil
}

0 comments on commit 3a03405

Please sign in to comment.