4 ms·
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 fo
by saurik 2y ago
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.
- zeroxfe 2y agoEven without a grammar spec, there's lots of things common in _most_ languages: quoted strings, whitespaces, matching braces/parens, etc.
- alexpovel 2y agoThe tool you are describing is what I am trying to build at https://github.com/alexpovel/srgn https://github.com/alexpovel/srgn . The idea is a compromise between regex (think ripgrep) and grammar awareness (through tree-sitter).
- herrington_d 2y agoHey! ast-grep author here. I believe what you want is probably what https://github.com/ast-grep/ast-grep 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#eyJtb2RlIjoiUGF0Y2giLCJsYW5nIjoidHlwZXNjcmlwdCIsInF1ZXJ5IjoidmFyaWFibGUiLCJyZXdyaXRlIjoiIiwiY29uZmlnIjoiaWQ6IGludGVybmFsLW1ldGhvZC1ub3QtYW5ub3RhdGVkIFxucnVsZTpcbiAga2luZDogbWV0aG9kX2RlZmluaXRpb25cbiAgaGFzOlxuICAgIGZpZWxkOiBuYW1lIFxuICAgIHJlZ2V4OiBeLipJbnRlcm5hbCRcbiAgbm90OlxuICAgIGZvbGxvd3M6XG4gICAgICBwYXR0ZXJuOiAvKiogQGludGVybmFsICovIFxuIiwic291cmNlIjoiY29uc29sZS5sb2codmFyaWFibGUpXG5jb25zb2xlLmxvZygndmFyaWFibGUgaW4gc3RyaW5nJykifQ== https://ast-grep.github.io/playground.html#eyJtb2RlIjoiUGF0Y...
- morgante 2y agoYeah 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 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 https://app.grit.io/studio?key=v8iA8zIbHs2uCykNpSF_G