Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


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...


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.


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()'

[0] https://app.grit.io/studio?key=v8iA8zIbHs2uCykNpSF_G




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: