Do any programming editors have a language aware b...
# linking-together
g
Do any programming editors have a language aware balanced bracket regex engine? Thinking about it right it seems like it would eminently useful to be able to say
Copy code
s/someFuncName(?open()(.?)(?close)(?open{)(.*?)(?close)/balbalbal(\1){\2}/
or something to that effect where
(?open<char>)
means start a language aware match for <char> (ps: I get that search and replace above is boring but I can't count the times I've wanted to be able to grab the content of a function or a code block in a regex but couldn't. Usually I got around it with keyboard macros which sometimes is a win and sometimes a lose. Most old text editors just edit based on text and nothing else. Slickedit, the editor I used most of my life until recently, has the ability to search based on the its colorization engine meaning that given the fact that it knows a string from a comment from code for coloring differently you could search/replace for things only in comments or only in strings etc... But it didn't have any kind of language aware bracket matching patterns added to its regex language. At the moment that seems like a ridiculously obvious enhancement to a text editor but it requires the editor to be able to parse the language and somehow combine that knowledge with its regex engine.
👍 5
k
This reminds me of Vim text objects[1] and structural regular expressions [2]. But I suspect you're already aware of those. [1] http://vimdoc.sourceforge.net/htmldoc/motion.html#object-select [2] http://doc.cat-v.org/bell_labs/structural_regexps
r
I empathize with you on this one! As a Vim user, I usually end up using vim macros like you describe or text objects like Kartik mentioned. If that isn't sufficient I often resort to massaging regex capture groups. It feels primitive in a lot of ways. Code editors have these powerful syntax matching engines they use for highlighting, and yet they provide no easy way for the end user to access that machinery! Because highlighting is the only thing you ever want to do, right? /Sarcasm 😛
j
I'm not 100% sure I understand what you seek, but one thing I really like is "expand/contract region" combined with search/replace inside the region:

https://www.youtube.com/watch?v=_RvHz3vJ3kA&amp;feature=emb_title

g
yeah, i had this in vim for a while until i got a new computer and forgot to update my dotfiles. i think vim-surround does this: https://github.com/tpope/vim-surround and it even works with html tags
1
i think emmet is an extension of similar ideas: https://emmet.io/ but mostly for html/css
g
If I understand correctly those features, vim-surround, emmet (who's bracket matching is broken ATM), expand/contract region in emacs are not part of regular expression in those editors are they? They are commands you can add to a string of commands if you're either recording a keyboard macro or writing a manual macro. Right?
j
(I can only speak to the emacs case because I'm unfamiliar with the other implementations.) The thing I was gesturing towards is that regex operations can be restricted to the current region (i.e. area of selection), which can be combined
expand-region
-like functions to get something like what you're talking about above. This would not be integrated into the grammar of regular expressions, though, so you would need to write a few more lines of elisp to get the behavior I think you're after.
g
r
A somewhat related tool I have used for Python is https://pybowler.io/ though it's more of a useful sdk that adds some macro like capabilities to Python than a proper tool with a nice UX.