Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

featWASM Compiler: get struct field & method into completion item. #913

Merged
merged 2 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: 这里 items 好像一定不会是 nil

image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外现在这个函数叫 getScopesItems 其实已经不太合适了,因为它产生的 completion item 不一定是从 scope 来,可以考虑就叫 getCompletionItems

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的好的,马上补一个修复的pr

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
Loading