Skip to content

Commit

Permalink
handle completion from subpackages
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Jun 21, 2024
1 parent 5a278a1 commit e274530
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 23 deletions.
70 changes: 70 additions & 0 deletions cmd/gnols/testdata/document_completion_subpackage.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Init phase
lsp initialize input/initialize.json
lsp initialized input/initialized.json
lsp workspace/didChangeConfiguration input/didChangeConfiguration.json
lsp textDocument/didOpen input/didOpen_x.json

lsp textDocument/completion input/completion.json
cmpenv output/completion.json expected/completion.json
-- x.gno --
package foo

func Hello() {
sub.
}
-- sub/y.gno --
package sub

var X int

type SubStruct {}
-- input/initialize.json --
{
"rootUri": "file://$WORK"
}
-- input/initialized.json --
{}
-- input/didChangeConfiguration.json --
{
"settings": {
"gno": "$GOBIN/gno",
"gopls": "$GOBIN/gopls",
"root": "$GNOPATH",
"precompileOnSave": true,
"buildOnSave": true
}
}
-- input/didOpen_x.json --
{
"textDocument": {
"uri":"file://$WORK/x.gno",
"text":"${FILE_x.gno}"
}
}
-- input/completion.json --
{
"textDocument": {
"uri":"file://$WORK/x.gno"
},
"position": {
"character": 4,
"line": 3
}
}
-- expected/completion.json --
[
{
"detail": "X int",
"documentation": "",
"insertText": "X",
"kind": 6,
"label": "X"
},
{
"detail": "SubStruct",
"documentation": "",
"insertText": "SubStruct",
"kind": 7,
"label": "SubStruct"
}
]
57 changes: 34 additions & 23 deletions internal/handler/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,44 @@ func (h *handler) handleTextDocumentCompletion(ctx context.Context, reply jsonrp
func (h handler) lookupSymbols(name string, selectors []string) []gno.Symbol {
slog.Info("lookupSymbols", "name", name, "selectors", selectors)
for _, sym := range h.symbols {
if sym.Name != name {
continue
}
// we found a symbol matching name
switch sym.Kind {
case "var":
// sym is a variable, lookup for symbols matching type
return h.lookupSymbols(sym.Type, selectors)
switch {

Check failure on line 84 in internal/handler/completion.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

case "struct":
// sym is a struct, lookup for matching fields
if len(selectors) == 0 {
// no other selectors, return all symbol fields
return sym.Fields
}
var symbols []gno.Symbol
for _, f := range sym.Fields {
if f.Name == selectors[0] {
// field matches selector exactly, lookup in field type fields.
return h.lookupSymbols(f.Type, selectors[1:])
case sym.Name == name:
// we found a symbol matching name
switch sym.Kind {
case "var":
// sym is a variable, lookup for symbols matching type
return h.lookupSymbols(sym.Type, selectors)

case "struct":
// sym is a struct, lookup for matching fields
if len(selectors) == 0 {
// no other selectors, return all symbol fields
return sym.Fields
}
if strings.HasPrefix(f.Name, selectors[0]) {
// field match partially selector, append
symbols = append(symbols, f)
var symbols []gno.Symbol
for _, f := range sym.Fields {
if f.Name == selectors[0] {
// field matches selector exactly, lookup in field type fields.
return h.lookupSymbols(f.Type, selectors[1:])
}
if strings.HasPrefix(f.Name, selectors[0]) {
// field match partially selector, append
symbols = append(symbols, f)
}
}
return symbols
}

case sym.PkgPath == name:
// match a sub package, return all symbols from that package
var syms []gno.Symbol
for _, sym := range h.symbols {
if sym.PkgPath == name {
syms = append(syms, sym)
}
}
return symbols
return syms
}
}
return nil
Expand Down

0 comments on commit e274530

Please sign in to comment.