Skip to content

Commit

Permalink
featWASM Compiler: get struct field & method into completion item. (g…
Browse files Browse the repository at this point in the history
…oplus#913)

* feat: get struct field & method into completion item .

* fix: fix the bug of wrong if statements.
  • Loading branch information
callme-taota committed Sep 19, 2024
1 parent 6bcd9d1 commit 24a3301
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
19 changes: 19 additions & 0 deletions tools/compiler/internal/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,22 @@ func getCodeFunctionList(file *ast.File) (funcList, error) {

return fv.funcList, nil
}

type selectorExprVisitor struct {
selectorExprList []*ast.SelectorExpr
}

func (v *selectorExprVisitor) Visit(node ast.Node) ast.Visitor {
if selectorExpr, ok := node.(*ast.SelectorExpr); ok {
v.selectorExprList = append(v.selectorExprList, selectorExpr)
}
return v
}

func getCodeSelectorExprList(file *ast.File) ([]*ast.SelectorExpr, error) {
sv := &selectorExprVisitor{}

ast.Walk(sv, file)

return sv.selectorExprList, nil
}
4 changes: 1 addition & 3 deletions tools/compiler/internal/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ func GetCompletions(fileName string, fileMap map[string]string, line, column int
if err != nil {
fmt.Println("Internal error: ", err)
}
items := goKeywords
items = append(items, list...)
return items, nil
return list, nil
}

func GetTokenDetail(token, pkgPath string) (interface{}, error) {
Expand Down
45 changes: 41 additions & 4 deletions tools/compiler/internal/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ func getScopesItems(fileName string, fileMap map[string]string, line, column int
}

items := &completionList{}
selector, find := getSelector(file, cursorPos)
if find {
selectorInfoGetter(info, selector, items)
if items != nil {
return *items, nil
}
}

*items = goKeywords

smallScopes := findSmallestScopesAtPosition(info, cursorPos, fset)
for _, scope := range smallScopes {
Expand All @@ -201,6 +210,37 @@ func getScopesItems(fileName string, fileMap map[string]string, line, column int
return *items, nil
}

func selectorInfoGetter(info *typesutil.Info, selectorExpr *ast.SelectorExpr, items *completionList) {
if childSelectorExpr, ok := selectorExpr.X.(*ast.SelectorExpr); ok {
ident := childSelectorExpr.Sel
obj := info.Uses[ident]
if obj == nil {
return
}
handleStruct(obj.Type(), obj.Name(), items)
} else if ident, ok := selectorExpr.X.(*ast.Ident); ok {
obj := info.Uses[ident]
if obj == nil {
return
}
handleStruct(obj.Type(), obj.Name(), items)
}
}

func getSelector(file *ast.File, cursorPos token.Pos) (*ast.SelectorExpr, bool) {
list, _ := getCodeSelectorExprList(file)
for _, node := range list {
if checkPositionInRange(cursorPos, node.Pos(), node.End()) {
return node, true
}
}
return nil, false
}

func checkPositionInRange(pos, from, end token.Pos) bool {
return pos > from && pos < end
}

func findSmallestScopesAtPosition(info *typesutil.Info, pos token.Pos, fset *token.FileSet) []*types.Scope {
var scopeList []*types.Scope
var smallList []*types.Scope
Expand Down Expand Up @@ -274,10 +314,7 @@ func handleStruct(T types.Type, name string, items *completionList) {
if stru.Field(i).Embedded() {
handleStruct(stru.Field(i).Type(), name, items)
}
if stru.Field(i).Exported() {
addCompletionItem(stru.Field(i), name, items)
}
if stru.Field(i).Pkg().Path() == PKG {
if stru.Field(i).Exported() || stru.Field(i).Pkg().Path() == PKG {
addCompletionItem(stru.Field(i), stru.Field(i).Name(), items)
}
}
Expand Down

0 comments on commit 24a3301

Please sign in to comment.