-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move comment processing to tree-sitter
- Loading branch information
Showing
20 changed files
with
321 additions
and
438 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package code | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
sitter "github.com/smacker/go-tree-sitter" | ||
"github.com/smacker/go-tree-sitter/golang" | ||
"github.com/smacker/go-tree-sitter/python" | ||
"github.com/smacker/go-tree-sitter/rust" | ||
) | ||
|
||
// Language represents a supported programming language. | ||
// | ||
// NOTE: What about haskell, less, perl, php, powershell, r, sass, swift? | ||
type Language struct { | ||
Delimiters []string | ||
Sitter *sitter.Language | ||
Query string | ||
} | ||
|
||
// Comment represents an in-code comment (line or block). | ||
type Comment struct { | ||
Text string | ||
Line int | ||
Offset int | ||
Scope string | ||
} | ||
|
||
func getLanguageFromExt(ext string) (*Language, error) { | ||
switch ext { | ||
case ".go": | ||
return &Language{ | ||
Delimiters: []string{"//", "/*", "*/"}, | ||
Sitter: golang.GetLanguage(), | ||
Query: "(comment)+ @comment", | ||
}, nil | ||
case ".rs": | ||
return &Language{ | ||
Delimiters: []string{"///", "//"}, | ||
Sitter: rust.GetLanguage(), | ||
Query: "(line_comment)+ @comment", | ||
}, nil | ||
case ".py": | ||
return &Language{ | ||
Delimiters: []string{"#"}, | ||
Sitter: python.GetLanguage(), | ||
Query: `(comment) @comment`, | ||
}, nil | ||
default: | ||
return nil, fmt.Errorf("unsupported extension: '%s'", ext) | ||
} | ||
} | ||
|
||
func getComments(source []byte, lang *Language) ([]Comment, error) { | ||
var comments []Comment | ||
|
||
parser := sitter.NewParser() | ||
parser.SetLanguage(lang.Sitter) | ||
|
||
tree, err := parser.ParseCtx(context.Background(), nil, source) | ||
if err != nil { | ||
return comments, err | ||
} | ||
n := tree.RootNode() | ||
|
||
q, err := sitter.NewQuery([]byte(lang.Query), lang.Sitter) | ||
if err != nil { | ||
return comments, err | ||
} | ||
|
||
qc := sitter.NewQueryCursor() | ||
qc.Exec(q, n) | ||
|
||
for { | ||
m, ok := qc.NextMatch() | ||
if !ok { | ||
break | ||
} | ||
|
||
for _, c := range m.Captures { | ||
text := c.Node.Content(source) | ||
|
||
for _, d := range lang.Delimiters { | ||
text = strings.Trim(text, d) | ||
} | ||
|
||
scope := "text.comment.line" | ||
if strings.Count(text, "\n") > 1 { | ||
scope = "text.comment.block" | ||
} | ||
|
||
comments = append(comments, Comment{ | ||
Line: int(c.Node.StartPoint().Row) + 1, | ||
Offset: int(c.Node.StartPoint().Column), | ||
Scope: scope, | ||
Text: text, | ||
}) | ||
} | ||
} | ||
|
||
return comments, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package code | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func toJSON(comments []Comment) string { | ||
j, _ := json.MarshalIndent(comments, "", " ") | ||
return string(j) | ||
} | ||
|
||
func TestComments(t *testing.T) { | ||
var cleaned []fs.DirEntry | ||
|
||
cases, err := os.ReadDir("../../testdata/comments/in") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
for _, f := range cases { | ||
if f.Name() == ".DS_Store" { | ||
continue | ||
} | ||
cleaned = append(cleaned, f) | ||
} | ||
|
||
for i, f := range cleaned { | ||
b, err1 := os.ReadFile(fmt.Sprintf("../../testdata/comments/in/%s", f.Name())) | ||
if err1 != nil { | ||
t.Error(err1) | ||
} | ||
|
||
lang, err2 := getLanguageFromExt(filepath.Ext(f.Name())) | ||
if err2 != nil { | ||
t.Fatal(err2) | ||
} | ||
|
||
comments, err3 := getComments(b, lang) | ||
if err3 != nil { | ||
t.Fatal(err3) | ||
} | ||
comments = coalesce(comments) | ||
|
||
b2, err4 := os.ReadFile(fmt.Sprintf("../../testdata/comments/out/%d.json", i)) | ||
if err4 != nil { | ||
t.Fatal(err4) | ||
} | ||
|
||
markup := toJSON(comments) | ||
if markup != string(b2) { | ||
bin := filepath.Join("..", "..", "bin", fmt.Sprintf("%d.json", i)) | ||
_ = os.WriteFile(bin, []byte(markup), os.ModePerm) | ||
t.Errorf("%s", markup) | ||
} | ||
} | ||
} |
Oops, something went wrong.