Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Amber: A code search and replace tool (github.com/dalance)
124 points by bpierre on May 23, 2024 | hide | past | favorite | 40 comments


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


Reminded me of https://github.com/ast-grep/ast-grep and I wonder how it compares in performance and functionality.


ast-grep find/replace Abstract Syntax Tree. Amber matches on regex.

It seems ast-grep is more powerful if you don't exactly know how the code is structured.


ast-grep is based on tree-sitter parsers and most of the time is spent on parsing.

There is some optimization to [skip parsing](https://github.com/ast-grep/ast-grep/blob/9f8ed5fb2abf35d928...) but generally I don't expect ast-grep can beat ripgrep or amber. (but sometimes ast-grep can beat grep due to parallel processing)


This used the more sound approach of treating code as code


At this rate of naming projects after the same valuable stones, we are going to need namespacing for the CLI tools.


Off the top of my head amber can refer to: an in browser small talk implementation, a typed to bash compiled language, a text replacement tool, a crystal mvc web framework, and a medical simulation software.


Dang I made my post and now I see I really do need a heart attack, I only knew the first 3, and now you throw two more in the mix. Yikes.


There's an Amber Smalltalk and Amber Bash Scripting Language and this. I just discovered these literally the past week.

"Namespace collisions" in the real world get my goat in general, but in this case, I'm about to have a heart attack from the discovery of all 3 in one week.

I get why for all cases : amber=electron/ήλεκτρο in Ancient Greek.* But still.

* One of my favorite albums is Amber by Autechre, and where I learned this nugget (also - it's a pile of Sand Dunes on the cover, so silicon). Clever boys.


Use amber to search through your amber code


Amber is the ex who keeps digging up the past.


This project has been around for a while.


related, you might want to consider changing your username - when I saw your comment I thought you were the Madrid sporting organization and it took me a while to realize.


but mine can be read backwards


I used to use https://comby.dev/ which is amazing when it works but I kept having issues where it wouldn't accept my input or it'd error during processing. It was very flaky. So I've resorted to using https://github.com/piranha/goreplace as backup which is much simpler but reliable.

I'll check this one out...


I used comby as well but it was difficult to write proper recipes.

ast-grep and gritql are both much better.


Feels similar to `sd` (https://github.com/chmln/sd)

which in my mind was the first “replace” version of ripgrep

grep -> ripgrep

sed -> sd


Most of the time if I'm doing this, it's in a git repo and I only want to affect the checked-in files so that I can easily diff my changes. So I use a combination of `git grep`, `xargs`, and `perl` via a script I named `git-gsr`:

https://gist.github.com/jaysoffian/0eda35a6a41f500ba5c458f02...

Put it in `PATH` and then: `git gsr <old> <new>`


https://news.ycombinator.com/item?id=40431835

EDIT: Opps same name different project.


Seems 'amber' a popular name. A dynamics package, an hypothetical operating system, a framework, a Java enhancing project, a VM, a language, now a tool.


I'm glad that people make tools like this, but this is a task that I need to do so rarely that I'm likely to forget it exists, which means that I'll likely roll my own (inferior!) version as a one-time thing.


    find . [ ... find args ] | sed -e 's/nee\(dl[aeiou]\)/stack\1/g'


this is cool! i do the same thing[1], wanted ag/rg to be able to search and replace.

1. https://github.com/nathants/agr


Can it do multiline search and replace?


ast-grep and gritql can.


Is it possible to store my config files under ~.config/amber/ on MacOS?


Not to be confused with this Amber

https://amber-lang.com/


not to be confused with Amber, the crystal lang web framework


Or Amber, the programming language that compiles to Bash, posted here just the other day.



The author specifically mentions, and benchmarks against, ripgrep in the linked content.


ripgrep is brilliant and I use it a lot but it has no knowledge of the languages it works with so I don't use it for searching anything more than names of functions or stuff.

If you really need to search for code constructs that can span multiple lines (but not always do) then Treesitter-aware tools like ast-grep and gritql are superior.


Ripgrep lacks the “& replace” bit.


Nah.

   $ rg 'sear' -r 'repl' file.txt


That only replaces in the output. ambr seems to actually modify the file contents, like `sed -i`. ripgrep never modifies the contents of files.


Right, can use --passthru and > or sponge to write it to disk.

Edit: Oh, it's you. Thanks for a brilliant tool, I use it every workday and have been for years.




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

Search: