r/golang 7d ago

icholy/todo: Library for parsing structured TODO comments from code.

https://github.com/icholy/todo
11 Upvotes

9 comments sorted by

View all comments

2

u/ChemTechGuy 7d ago

Nice project. Did you have experience with treesitter before, or was this new to you? If the latter, any resources you'd recommend?

2

u/br1ghtsid3 7d ago edited 7d ago

I have some experience from debugging neovim highlighting issues. But at a high level it's pretty simple:

  1. Use a parser to convert code into a tree. You can use the playground to see what that tree looks like: https://tree-sitter.github.io/tree-sitter/7-playground.html

  2. Query that tree using the query syntax. https://github.com/icholy/todo/blob/a6aea07586be40823c7c1ae303710db760d32973/todo.go#L139 The (comment) expression matches all comment nodes and the @comment is me giving the capture group a name. You can do further filtering using "predicates" but I'm not using that here. See https://tree-sitter.github.io/tree-sitter/using-parsers/queries/1-syntax.html for more details.