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: add comment into AST, usage in definition can get document. #926

Merged
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 @@ -221,3 +221,22 @@ func getCodeSelectorExprList(file *ast.File) ([]*ast.SelectorExpr, error) {

return sv.selectorExprList, nil
}

type functionExprVisitor struct {
funcList []*ast.FuncDecl
}

func (v *functionExprVisitor) Visit(node ast.Node) ast.Visitor {
if fnExpr, ok := node.(*ast.FuncDecl); ok {
v.funcList = append(v.funcList, fnExpr)
}
return v
}

func getCodeFuncDecl(file *ast.File) ([]*ast.FuncDecl, error) {
fv := &functionExprVisitor{}

ast.Walk(fv, file)

return fv.funcList, nil
}
3 changes: 2 additions & 1 deletion tools/compiler/internal/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func GetDefinition(fileName string, fileMap map[string]string) (interface{}, err
if err != nil {
fmt.Println("Internal error: ", err)
}
definitionList := getDefinitionList(info)
fnList, _ := getCodeFuncDecl(pkg[PKG].Files[fileName])
definitionList := getDefinitionList(info, fnList)
definitionList.Position(fset)
filter := definitions{}
for _, item := range definitionList {
Expand Down
18 changes: 16 additions & 2 deletions tools/compiler/internal/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type usage struct {
InsertText string `json:"insertText"`
Params []param `json:"params"`
Type string `json:"type"`
Document string `json:"document"`
}

type param struct {
Expand Down Expand Up @@ -127,7 +128,7 @@ func (d definitions) Position(fset *token.FileSet) {
def.From.EndPosition = Position(fset.Position(token.Pos(def.From.EndPos)))
}
}
func getDefinitionList(info *typesutil.Info) definitions {
func getDefinitionList(info *typesutil.Info, fnList []*ast.FuncDecl) definitions {
var definitionList definitions

defs := info.Defs
Expand All @@ -138,7 +139,11 @@ func getDefinitionList(info *typesutil.Info) definitions {
continue
}
definition := createDefinitionItem(ident, obj)
definition.Usages = createUsages(obj, ident.Name)
definitionUsage := createUsages(obj, ident.Name)
if commentList := checkFnExist(fnList, ident); commentList != nil {
definitionUsage[0].Document = commentList.Text()
}
definition.Usages = definitionUsage
definitionList = append(definitionList, definition)
}

Expand Down Expand Up @@ -350,3 +355,12 @@ func extractParams(signature *types.Signature) (signList []string, sampleList []
}
return
}

func checkFnExist(fnList []*ast.FuncDecl, ident *ast.Ident) *ast.CommentGroup {
for _, fnDecl := range fnList {
if fnDecl.Name == ident && fnDecl.Doc != nil {
callme-taota marked this conversation as resolved.
Show resolved Hide resolved
return fnDecl.Doc
}
}
return nil
}
2 changes: 1 addition & 1 deletion tools/compiler/internal/setups.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func initSPXMod() *gopmod.Module {
func initSPXParserConf() parser.Config {
return parser.Config{
ClassKind: gopbuild.ClassKind,
Mode: parser.DeclarationErrors,
Mode: parser.AllErrors | parser.ParseComments,
}
}

Expand Down
Loading