microsoft / vscode-textmate

A library that helps tokenize text using Text Mate grammars.
MIT License
584 stars 115 forks source link

Add Option to Format in often used match expression parts #178

Open Hendiadyoin1 opened 2 years ago

Hendiadyoin1 commented 2 years ago

Languages often have patterns that reappear in multiple places, like identifiers or argument-lists. These can have quite some length to it and may make the match-expression as a whole less readable.

To solve this you could add a top-level dict to the grammar description that contains name-regex pairs which are substituted into the requested places at runtime.

A good solution would be to add all defined helper functions as a preamble to each match in a named group with 0 repetitions like (?<Name>...){0}


Another possible solution might be PCRE's (?&name) (and (?(DEFINE)...)) syntax this could be emulated via preprocessing of the patterns and string superstition, this would not break any old code because (?&...) is illegal in ECMA's regex subset

If a switch to PCRE happens or native support is added you would just need to add a list of (?(DEFINE)...)s to the start of each match

RedCMD commented 2 years ago

Probably should request this at https://github.com/textmate/textmate