Re:Amber: A code search and replace tool
by saurik on Thursday May 23, 2024 @01:21PM
(#40457376)
I don't understand what makes this a "code" search and replace tool as opposed to a general text search and replace tool (a la the many many existing options for such). I was kind of expecting some kind of AST structure or grammar spec or at least a quick tokenizer to be able to handle situations like "don't replace this text if it appears in a string constant", but it doesn't mention anything like that.
[ Parent
| on HN ]
The tool you are describing is what I am trying to build at https://github.com/alexpovel/srgn . The idea is a compromise between regex (think ripgrep) and grammar awareness (through tree-sitter).
Even without a grammar spec, there's lots of things common in _most_ languages: quoted strings, whitespaces, matching braces/parens, etc.
Re:Amber: A code search and replace tool
by herrington_d on Thursday May 23, 2024 @05:05PM
(#40459912)
Hey! ast-grep author here. I believe what you want is probably what https://github.com/ast-grep/ast-grep can help you.
More specifically for the "string constant" search/replace, you can see the playground example!
https://ast-grep.github.io/playground.html#eyJtb2RlIjoiUGF0Y...
Yeah it doesn't look like this is AST-aware at all.
If you want to do queries that understand the grammar, https://github.com/getgrit/gritql is closer to what you're looking for. (Disclaimer: I'm the author.)
Ex. here's how you would search for your example of looking for a string unless it's inside a string constant. [0]
grit apply '"this_string" => `new_string` where $match <: not within string()'