GetComments returns all comments in the given source code.
(source []byte, lang *Language)
| 98 | |
| 99 | // GetComments returns all comments in the given source code. |
| 100 | func GetComments(source []byte, lang *Language) ([]Comment, error) { |
| 101 | var comments []Comment |
| 102 | |
| 103 | parser := sitter.NewParser() |
| 104 | parser.SetLanguage(lang.Parser) |
| 105 | |
| 106 | tree, err := parser.ParseCtx(context.Background(), nil, source) |
| 107 | if err != nil { |
| 108 | return comments, err |
| 109 | } |
| 110 | engine := NewQueryEngine(tree, lang) |
| 111 | |
| 112 | for _, query := range lang.Queries { |
| 113 | q, qErr := sitter.NewQuery([]byte(query.Expr), lang.Parser) |
| 114 | if qErr != nil { |
| 115 | return comments, qErr |
| 116 | } |
| 117 | comments = append(comments, engine.run(query.Name, q, source)...) |
| 118 | } |
| 119 | |
| 120 | if len(lang.Queries) > 1 { |
| 121 | sort.Slice(comments, func(p, q int) bool { |
| 122 | return comments[p].Line < comments[q].Line |
| 123 | }) |
| 124 | } |
| 125 | |
| 126 | return coalesce(comments), nil |
| 127 | } |
searching dependent graphs…