Skip to content

Commit

Permalink
wip(completion): address some cases, add others
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Jul 29, 2024
1 parent 7d7f9b8 commit ea9fb33
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 8 deletions.
6 changes: 3 additions & 3 deletions cmd/gnols/testdata/document_completion_func_args3.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ cmpenv output/completion_x.json expected/completion_x.json
package foo

func Hello(x interface { Foo() int }) {
x. // completion here, should return all fields of MyType
x. // completion here, should return all methods of inline interface
}
-- input/initialize.json --
{
Expand Down Expand Up @@ -48,10 +48,10 @@ func Hello(x interface { Foo() int }) {
-- expected/completion_x.json --
[
{
"detail": "Foo int",
"detail": "Foo() int",
"documentation": "",
"insertText": "Foo",
"kind": 5,
"kind": 2,
"label": "Foo"
}
]
59 changes: 59 additions & 0 deletions cmd/gnols/testdata/document_completion_func_args3b.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 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_x.json
cmpenv output/completion_x.json expected/completion_x.json
-- x.gno --
package foo

func Hello(x I) {
x. // completion here, should return all methods of I
}

type I interface { Foo() int }
-- 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_x.json --
{
"textDocument": {
"uri":"file://$WORK/x.gno"
},
"position": {
"character": 3,
"line": 3
}
}
-- expected/completion_x.json --
[
{
"detail": "Foo() int",
"documentation": "",
"insertText": "Foo",
"kind": 2,
"label": "Foo"
}
]
63 changes: 63 additions & 0 deletions cmd/gnols/testdata/document_completion_func_args3c.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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_x.json
cmpenv output/completion_x.json expected/completion_x.json
-- x.gno --
package foo

func Hello(x I) {
x.Foo(). // completion here, should return all fields of X
}

type X struct {
Bar int
}

type I interface { Foo() X }
-- 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_x.json --
{
"textDocument": {
"uri":"file://$WORK/x.gno"
},
"position": {
"character": 9,
"line": 3
}
}
-- expected/completion_x.json --
[
{
"detail": "Bar int",
"documentation": "",
"insertText": "Bar",
"kind": 5,
"label": "Bar"
}
]
72 changes: 72 additions & 0 deletions cmd/gnols/testdata/document_completion_func_ret.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# 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_x.json
cmpenv output/completion_x.json expected/completion_x.json
-- x.gno --
package foo

type MyType struct {
Foo int
Bar string
Baz bool
}

func Bar() MyType {}

func Hello() {
Bar().B // completion here, should return some fields of MyType
}
-- 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_x.json --
{
"textDocument": {
"uri":"file://$WORK/x.gno"
},
"position": {
"character": 7,
"line": 11
}
}
-- expected/completion_x.json --
[
{
"detail": "Bar int",
"documentation": "",
"insertText": "Bar",
"kind": 5,
"label": "Bar"
},
{
"detail": "Baz bool",
"documentation": "",
"insertText": "Baz",
"kind": 5,
"label": "Baz"
}
]
18 changes: 13 additions & 5 deletions internal/handler/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,21 @@ func (h *handler) handleTextDocumentCompletion(ctx context.Context, reply jsonrp
syms = symbolFinder{sub.Symbols}.find(append([]string{typ}, selectors[1:]...))
}
}

case *ast.InterfaceType:
// TODO address when len(selectors)>1
for _, method := range t.Methods.List {
syms = append(syms, gno.Symbol{
Name: method.Names[0].Name,
Kind: "method",
Doc: method.Comment.Text(),
Signature: doc.Content[method.Pos()-1 : method.End()-1],
})
}

default:
panic("FIXME cannot find type")
}
// if len(syms) == 0 {
// syms = symbolFinder{h.subPkgs}.find(append([]string{typ}, selectors[1:]...))
// }

spew.Dump("FOUND", syms)
for _, f := range syms {
Expand Down Expand Up @@ -176,8 +184,8 @@ func (s symbolFinder) findIn(symbols []gno.Symbol, selectors []string) []gno.Sym
// lookup for symbols matching type in baseSymbols
return s.findIn(s.baseSymbols, append([]string{sym.Type}, selectors[1:]...))

case "struct":
// sym is a struct, lookup in fields
case "struct", "interface":
// sym is a struct or an interface, lookup in fields/methods
return s.findIn(sym.Fields, selectors[1:])
}

Expand Down

0 comments on commit ea9fb33

Please sign in to comment.