[HN Gopher] Amber: A code search and replace tool
       ___________________________________________________________________
        
       Amber: A code search and replace tool
        
       Author : bpierre
       Score  : 114 points
       Date   : 2024-05-23 14:48 UTC (1 days ago)
        
 (HTM) web link (github.com)
 (TXT) w3m dump (github.com)
        
       | laerus wrote:
       | At this rate of naming projects after the same valuable stones,
       | we are going to need namespacing for the CLI tools.
        
         | ape4 wrote:
         | Use amber to search through your amber code
        
         | tazjin wrote:
         | This project has been around for a while.
        
         | ecshafer wrote:
         | 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.
        
           | wormius wrote:
           | 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.
        
         | slimsag wrote:
         | 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.
        
           | laerus wrote:
           | but mine can be read backwards
        
         | pizzafeelsright wrote:
         | Amber is the ex who keeps digging up the past.
        
         | wormius wrote:
         | 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/elektro 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.
        
       | danilonc wrote:
       | Reminded me of https://github.com/ast-grep/ast-grep and I wonder
       | how it compares in performance and functionality.
        
         | danilonc wrote:
         | 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.
        
         | eviks wrote:
         | This used the more sound approach of treating code as code
        
         | herrington_d wrote:
         | 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)
        
       | mcepl wrote:
       | https://github.com/BurntSushi/ripgrep ???
        
         | zsyllepsis wrote:
         | The author specifically mentions, and benchmarks against,
         | ripgrep in the linked content.
        
         | kstrauser wrote:
         | Ripgrep lacks the "& replace" bit.
        
           | cess11 wrote:
           | Nah.                  $ rg 'sear' -r 'repl' file.txt
        
             | burntsushi wrote:
             | That only replaces in the output. ambr seems to actually
             | modify the file contents, like `sed -i`. ripgrep never
             | modifies the contents of files.
        
               | cess11 wrote:
               | 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.
        
         | pdimitar wrote:
         | 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.
        
       | VagabundoP wrote:
       | https://news.ycombinator.com/item?id=40431835
       | 
       | EDIT: Opps same name different project.
        
         | forgotpwd16 wrote:
         | 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.
        
       | jes5199 wrote:
       | 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.
        
       | Ringz wrote:
       | Is it possible to store my config files under ~.config/amber/ on
       | MacOS?
        
       | saurik wrote:
       | 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 wrote:
         | Even without a grammar spec, there's lots of things common in
         | _most_ languages: quoted strings, whitespaces, matching
         | braces/parens, etc.
        
         | alexpovel wrote:
         | 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).
        
         | herrington_d wrote:
         | 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...
        
         | morgante wrote:
         | 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
        
       | dmix wrote:
       | 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...
        
         | pdimitar wrote:
         | I used comby as well but it was difficult to write proper
         | recipes.
         | 
         | ast-grep and gritql are both much better.
        
       | PaulDavisThe1st wrote:
       | find . [ ... find args ] | sed -e 's/nee\(dl[aeiou]\)/stack\1/g'
        
       | Singletoned wrote:
       | Can it do multiline search and replace?
        
         | pdimitar wrote:
         | ast-grep and gritql can.
        
       | malthejorgensen wrote:
       | Feels similar to `sd` (https://github.com/chmln/sd)
       | 
       | which in my mind was the first "replace" version of ripgrep
       | 
       | grep -> ripgrep
       | 
       | sed -> sd
        
       | sam0x17 wrote:
       | not to be confused with Amber, the crystal lang web framework
        
         | replwoacause wrote:
         | Or Amber, the programming language that compiles to Bash,
         | posted here just the other day.
        
       | js2 wrote:
       | 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>`
        
       | nathants wrote:
       | 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
        
       | harryquach wrote:
       | Not to be confused with this Amber
       | 
       | https://amber-lang.com/
        
       ___________________________________________________________________
       (page generated 2024-05-24 23:02 UTC)