vscode-textmate icon indicating copy to clipboard operation
vscode-textmate copied to clipboard

Matching multiline text

Open Nv7-GitHub opened this issue 2 years ago • 3 comments

My language has syntax that is structured as follows:

<Group 1>
<Group 2>
<Group 2>

<Group 1>
<Group 2>
<Group 2>
<Group 2>

...

There are sets of one "Group 1" followed by any number of "Group 2" before a blank line, after which another set starts. I want Group 1 highlighted differently than Group 2. There are no identifying features within the line, just its place within the file. I tried using newlines in a regex to capture these but it didn't work, and it seems that VSCode only goes one line at a time for the regex.

I can't use a language server since this needs to work in VSCode web, how can I achieve this highlighting?

Nv7-GitHub avatar Sep 18 '23 15:09 Nv7-GitHub

(I'm just a random guy that does a lot of highlighting not a VS Code contributor btw)

how can I achieve this highlighting

There's only two tools in Textmate parsers (VS Code and many editors use Textmate grammars for highlighting).

  • The pattern (which can only match one line)
  • The pattern range (which has a start pattern, which can only match one line, and an end pattern, which can only match one line)

Thats it.

So you could get creative, use a pattern range, have it start with group 1, have it end when it sees a blank line, and then restrict what patterns are allowed inside of the pattern range.

Here's some code snippets to show a little bit of what I mean.

"What if the start-trigger/start-pattern requires multiple lines to be identified?"

Well then you're straight outta luck. Textmate parsers are hacky, very-limited things. Its actually more amazing that they work at all. Stuff like the C++ pre-processor just straight up break it with no good workaround. Its sad but that's how it is. One day (maybe) VS Code will switch to the Tree Sitter Parser and these problems won't exist.

jeff-hykin avatar Sep 18 '23 17:09 jeff-hykin

I can't use a language server since this needs to work in VSCode web, how can I achieve this highlighting?

You could try using semantic tokens. There you can look at the entire file contents when creating them, not just one line at a time, looking for patterns. You can create an extension that uses a nodejs language server and our libraries for that. They are fit for running in a web worker, and creating a new worker on the web or a new nodejs process on the desktop. In our codebase, you can take a look at css.

alexdima avatar Sep 18 '23 17:09 alexdima