Skip to content

Commit

Permalink
wip(completion): address other cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Jul 31, 2024
1 parent 61903bc commit 0cec8df
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
7 changes: 1 addition & 6 deletions cmd/gnols/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,7 @@ func call(ts *testscript.TestScript, method string, paramFile string) {
)
_, err := conn.Call(context.Background(), method, params, &response)
if err != nil {
if jerr, ok := err.(*jsonrpc2.Error); ok { //nolint:errorlint
// output jsonrpc2.Error in json
response = map[string]any{"error": jerr}
} else {
ts.Fatalf("Error Call: %v", err)
}
response = map[string]any{"error": err}
}
if err := writeJSON(ts, filepath.Base(paramFile), response); err != nil {
ts.Fatalf("writeJSON: %v", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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
cmp output/completion_x.json expected/completion_x.json
-- x.gno --
package foo

Expand Down
2 changes: 1 addition & 1 deletion internal/gno/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func ParsePackages(wd, rootDir string) ([]Package, error) {
if err != nil {
return nil, err
}
var pkgs []Package
pkgs := []Package{}
for _, dir := range dirs {
files, err := getFiles(dir)
if err != nil {
Expand Down
60 changes: 60 additions & 0 deletions internal/handler/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,34 @@ func (h *handler) handleTextDocumentCompletion(ctx context.Context, reply jsonrp
pos := gotoken.Pos(doc.PositionToOffset(params.Position))
nodes, _ := astutil.PathEnclosingInterval(doc.Pgf.File, pos, pos)
spew.Dump("ENCLOSING NODES", nodes)
// TODO use this func in other places (check for ast.Ident usage)
nodeName := func(n ast.Node) string {
if id, ok := n.(*ast.Ident); ok {
return id.Name
}
return ""
}
for _, n := range nodes {
var syms []gno.Symbol
switch n := n.(type) {

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

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

case *ast.BlockStmt:
// Check if selectors[0] has been assigned here
for _, t := range n.List {
switch t := t.(type) {
case *ast.AssignStmt:
if nodeName(t.Lhs[0]) == selectors[0] && t.Tok == gotoken.DEFINE {
// found selectors[0] assignment, now fetch type
if cl, ok := t.Rhs[0].(*ast.CompositeLit); ok {
typ := nodeName(cl.Type)
// FIXME typ can come from an other package
syms = symbolFinder{h.currentPkg.Symbols}.find(append([]string{typ}, selectors[1:]...))
break
}
}
}
}

case *ast.SelectorExpr:
if id, ok := n.X.(*ast.Ident); ok {
// look up in subpackages (TODO also add imported packages)
Expand Down Expand Up @@ -116,6 +140,42 @@ func (h *handler) handleTextDocumentCompletion(ctx context.Context, reply jsonrp
}
}
}

case *ast.File:
// final node, check for global variable declaration that could match
// selectors[0]
for _, t := range n.Decls {
if len(syms) > 0 {
break
}
if d, ok := t.(*ast.GenDecl); ok {
for _, t := range d.Specs {
if v, ok := t.(*ast.ValueSpec); ok {
// FIXME loop over all names in case there's multiple assignement
if v.Names[0].Name == selectors[0] {
if len(v.Values) > 0 {
switch vs := v.Values[0].(type) {

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

View workflow job for this annotation

GitHub Actions / lint

singleCaseSwitch: should rewrite switch statement to if statement (gocritic)
case *ast.CompositeLit:
typ := nodeName(vs.Type)
syms = symbolFinder{h.currentPkg.Symbols}.find(append([]string{typ}, selectors[1:]...))
}
} else {
// TODO address when len(selectors)>1
for _, field := range v.Type.(*ast.StructType).Fields.List {
syms = append(syms, gno.Symbol{
Name: field.Names[0].Name,
Kind: "field",
Doc: strings.TrimSpace(field.Doc.Text()),
Signature: doc.Content[field.Pos()-1 : field.End()-1],
})
}
}
}
}
}
}
}

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

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
}

if len(syms) > 0 {
Expand Down

0 comments on commit 0cec8df

Please sign in to comment.