|
| 1 | +package rust |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + sitter "github.com/smacker/go-tree-sitter" |
| 7 | + "github.com/smacker/go-tree-sitter/rust" |
| 8 | + "github.com/studyzy/codei18n/core/domain" |
| 9 | + "github.com/studyzy/codei18n/core/utils" |
| 10 | +) |
| 11 | + |
| 12 | +// extractComments processes the AST and extracts all comments matching the query. |
| 13 | +func (a *RustAdapter) extractComments(root *sitter.Node, src []byte, file string) ([]*domain.Comment, error) { |
| 14 | + q, err := sitter.NewQuery([]byte(rustCommentQuery), rust.GetLanguage()) |
| 15 | + if err != nil { |
| 16 | + return nil, err |
| 17 | + } |
| 18 | + |
| 19 | + qc := sitter.NewQueryCursor() |
| 20 | + qc.Exec(q, root) |
| 21 | + defer qc.Close() // Ensure cursor is closed |
| 22 | + |
| 23 | + var comments []*domain.Comment |
| 24 | + |
| 25 | + for { |
| 26 | + m, ok := qc.NextMatch() |
| 27 | + if !ok { |
| 28 | + break |
| 29 | + } |
| 30 | + |
| 31 | + for _, c := range m.Captures { |
| 32 | + node := c.Node |
| 33 | + content := node.Content(src) |
| 34 | + |
| 35 | + // Find Owner and Symbol Path |
| 36 | + owner := FindOwnerNode(node, src) |
| 37 | + symbolPath := ResolveSymbolPath(owner, src) |
| 38 | + |
| 39 | + comment := &domain.Comment{ |
| 40 | + SourceText: content, |
| 41 | + Symbol: symbolPath, |
| 42 | + File: file, |
| 43 | + Language: "rust", |
| 44 | + Range: domain.TextRange{ |
| 45 | + StartLine: int(node.StartPoint().Row) + 1, |
| 46 | + StartCol: int(node.StartPoint().Column) + 1, |
| 47 | + EndLine: int(node.EndPoint().Row) + 1, |
| 48 | + EndCol: int(node.EndPoint().Column) + 1, |
| 49 | + }, |
| 50 | + Type: getDomainCommentType(content), |
| 51 | + } |
| 52 | + comment.ID = utils.GenerateCommentID(comment) |
| 53 | + comments = append(comments, comment) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return comments, nil |
| 58 | +} |
| 59 | + |
| 60 | +// getDomainCommentType maps raw comment content to domain.CommentType |
| 61 | +func getDomainCommentType(content string) domain.CommentType { |
| 62 | + if strings.HasPrefix(content, "/*") { |
| 63 | + return domain.CommentTypeBlock |
| 64 | + } |
| 65 | + if strings.HasPrefix(content, "///") || strings.HasPrefix(content, "//!") { |
| 66 | + return domain.CommentTypeDoc |
| 67 | + } |
| 68 | + return domain.CommentTypeLine |
| 69 | +} |
0 commit comments