Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parenthesis and brace matching within RegExps #148612

Open
pygy opened this issue May 3, 2022 · 13 comments
Open

Parenthesis and brace matching within RegExps #148612

pygy opened this issue May 3, 2022 · 13 comments
Assignees
Labels
editor-bracket-matching Editor brace matching feature-request Request for new features or functionality
Milestone

Comments

@pygy
Copy link

pygy commented May 3, 2022

Modifying regular expressions is a pain, and a part of that pain comes from the fact that editors don't help you with the task, they treat RegExps as if they were strings.

It would be nice, when the cursor is on a parenthesis, a brace or a bracket, to have the editor highlight the one that matches, like vscode does outside RegExps.

@IllusionMH
Copy link
Contributor

Looks like duplicate of #134560

@Z-E-D
Copy link

Z-E-D commented May 3, 2022

I need this too.

I noticed that braces are highlighted inside strings in .html files, like in this one:
<meta id="vscode-workbench-web-configuration" data-settings="{{WORKBENCH_WEB_CONFIGURATION}}">

And if they could have that, I don't see a reason why Regex parenthesis, a brace or a bracket cannot have the same.

I think this is not a duplicate of the mentioned issue. @pygy is asking for a highlight of the matching braces. The other issue is asking for colorization of braces.

@IllusionMH
Copy link
Contributor

@Z-E-D matching or colorizing brackets still requires you to get proper bracket pairs first. See #134560 (comment) and #134560 (comment).

@Z-E-D
Copy link

Z-E-D commented May 3, 2022

The numerous web sites with online regexp testers prove that getting the proper bracket pairs is not an issue. Just tried ((?:[)])blah) with:
https://regex101.com/#javascript
https://regexr.com/
https://www.regexpal.com
http://regexpal.com.s3-website-us-east-1.amazonaws.com/

By the way, the colorization of Regex elements is already possible with the proper themes. Just take a look at my screenshot in the #146260 issue.

However, such colorization is not what many expect, since it doesn't have different colors for nested braces, i.e. all braces have the same color even if they are nested. Personally, I don't have a problem with that.

@IllusionMH
Copy link
Contributor

Where did I said "it's impossible" or issue that is hard to solve? Moreover first linked comment provides an option for solution, it's just not something that is available in VS Code and looks like not planned at this moment (based on comments and label).

I just pointed that this requires similar effort. You are right that changing colors based on nesting is more involved that just match braces, however if you have proper pairs then matching can be done and (nested) pairs highlight as next steps as well.

TM Grammars are used to highlight RegExps, but it doesn't look like they are used to match brackets. Would be cool if they were used for RegExps.

@pygy
Copy link
Author

pygy commented May 3, 2022

Rainbow parens may be nice too, but it's not what I'm requesting here.

This is what I mean, but within RegExps:

Screenshot 2022-05-03 at 13 39 12

@pygy
Copy link
Author

pygy commented May 3, 2022

I see that tokenizing RegExps is an issue, I'm actually working on a RegExp tokenizer for my own purposes. Hopefully it will be useful here too.

@hediet
Copy link
Member

hediet commented May 4, 2022

/duplicate #134560

TM Grammars are used to highlight RegExps

Which languages do that?
Maybe #144736 could help to get the requested feature.

@IllusionMH
Copy link
Contributor

IllusionMH commented May 4, 2022

@hediet JS/TS and looks like Python, probably other as well, but haven't checked

Screenshots

image
image

Note that it's regular part of character set inside of [] and escaped
image
image

Python
image

@hediet
Copy link
Member

hediet commented May 4, 2022

Let me reopen this to track this idea. I'm curious myself if it would work, but I don't have the time to do it at the moment.

@hediet hediet reopened this May 4, 2022
@hediet hediet added feature-request Request for new features or functionality editor-bracket-matching Editor brace matching labels May 4, 2022
@hediet hediet added this to the On Deck milestone May 4, 2022
@hediet hediet removed the *duplicate Issue identified as a duplicate of another issue(s) label May 4, 2022
@pygy
Copy link
Author

pygy commented May 4, 2022

Couldn't RegExps be treated like an embedded language?

Edit: note that, within the RegExp sub-language, character classes themselves must be treated as an embedded language.

@hediet hediet modified the milestones: On Deck, Backlog Dec 5, 2022
@RedCMD
Copy link
Contributor

RedCMD commented Mar 3, 2023

the problem isn't so much of detecting braces and matching them
but being able to handle malformed regexes, without the bracket matcher going outside the string or just marking everything as broken
as an example: /(/ + variable + /)/. would show up as invalid and possibly destroy the rest of the document

as you can see in my vscode TextMate extension

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
editor-bracket-matching Editor brace matching feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests

7 participants
@pygy @IllusionMH @connor4312 @hediet @RedCMD @Z-E-D and others