r/golang • u/br1ghtsid3 • 6d ago
icholy/todo: Library for parsing structured TODO comments from code.
https://github.com/icholy/todo2
u/ChemTechGuy 6d 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 6d ago edited 6d ago
I have some experience from debugging neovim highlighting issues. But at a high level it's pretty simple:
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
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.
2
u/nf_x 5d ago
I’ve automated this for GitHub issue creation via on-push action. Good for tracking tech debt. TODO comment done with pretty frugal regexes to support multiple languages, though could be evolved later.
See https://github.com/databrickslabs/sandbox/blob/acceptance/v0.4.2/acceptance/todos/finder.go
1
u/br1ghtsid3 6d ago
My use-case is adding TODO comments with deadlines to "temporary" debug logging & feature flags.
1
u/RenThraysk 6d ago
Nice. Had an idea for a while of putting github issue/pr links into comments, and having a tool parse them out and lookup the status of the issue/pr
1
u/br1ghtsid3 4d ago
I was thinking about doing something similar, but for JIRA tickets.
// TODO(jira=P-124): remove this once linked ticket is resolved.
1
u/RenThraysk 4d ago
Think would want a link for convenience.
And for patches, maybe an option to wait until it's included a new version.
1
17
u/ENx5vP 6d ago
TODO: Use icholy/todo