Skip to content

Commit

Permalink
wip(completion): address func_ret case
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Aug 6, 2024
1 parent d201322 commit c5cb7bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
8 changes: 4 additions & 4 deletions cmd/gnols/testdata/document_completion_func_ret.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type MyType struct {
Baz bool
}

func Bar() MyType {}
func NewMyType() MyType {}

func Hello() {
Bar().B // completion here, should return some fields of MyType
NewMyType().B // completion here, should return some fields of MyType
}
-- input/initialize.json --
{
Expand Down Expand Up @@ -49,14 +49,14 @@ func Hello() {
"uri":"file://$WORK/x.gno"
},
"position": {
"character": 7,
"character": 14,
"line": 11
}
}
-- expected/completion_x.json --
[
{
"detail": "Bar int",
"detail": "Bar string",
"documentation": "",
"insertText": "Bar",
"kind": 5,
Expand Down
18 changes: 16 additions & 2 deletions internal/handler/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ func (h *handler) handleTextDocumentCompletion(ctx context.Context, reply jsonrp
}

case *ast.SelectorExpr:
// look up in subpackages (TODO also add imported packages)
if pkg := nodeName(n.X); pkg != "" {
switch x := n.X.(type) {
case *ast.Ident:
// look up in subpackages (TODO also add imported packages)
pkg := x.Name
// look up pkg in subpkgs
for _, sub := range h.subPkgs {
if sub.Name == pkg {
Expand All @@ -83,6 +85,10 @@ func (h *handler) handleTextDocumentCompletion(ctx context.Context, reply jsonrp
}
}
}

case *ast.CallExpr:
// this a call, find return type
syms = symbolFinder{h.currentPkg.Symbols}.find(selectors)
}

case *ast.FuncDecl:
Expand Down Expand Up @@ -260,6 +266,7 @@ func (s symbolFinder) findIn(symbols []gno.Symbol, selectors []string) []gno.Sym
return symbols
}
name := selectors[0]
name = strings.TrimSuffix(name, "()") // TODO add case like func_ret with func arguementsj
var syms []gno.Symbol
for _, sym := range symbols {
switch {
Expand All @@ -270,6 +277,7 @@ func (s symbolFinder) findIn(symbols []gno.Symbol, selectors []string) []gno.Sym
case "var", "field":
if sym.Type == "" {
// sym is an inline struct, returns fields
// TODO ensure that works when there's still other selectors
return sym.Fields
}
// lookup for symbols matching type in baseSymbols
Expand All @@ -278,6 +286,12 @@ func (s symbolFinder) findIn(symbols []gno.Symbol, selectors []string) []gno.Sym
case "struct", "interface":
// sym is a struct or an interface, lookup in fields/methods
return s.findIn(sym.Fields, selectors[1:])

case "func":
if sym.Type != "" {
return s.findIn(s.baseSymbols, append([]string{sym.Type}, selectors[1:]...))
}

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

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
}

case strings.HasPrefix(sym.Name, name): // partial match
Expand Down

0 comments on commit c5cb7bc

Please sign in to comment.