question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to define begin and end patterns which span multiple lines?

See original GitHub issue

Hi, I’m trying to expand the support for my plugin, graphql-for-vscode, to support gherkin feature files, but I’m stuck while defining the grammer. The begin regexp seems to not do any matching when I specify a newline (\n):

Specifically:

With this syntax definition:

{
  "fileTypes": ["feature"],
  "scopeName": "text.gherkin.feature.graphql",
  "injectionSelector": "L:text -comment",
  "patterns": [
    {
      "begin": "graphql request\\s+\"\"\"",
      "end": "\"\"\"",
      "patterns": [
        {
          "name": "featureGQL",
          "include": "source.graphql"
        }
      ]
    }
  ]
}

I don’t get any matches: image

but modifying the source text to bring the beginning """ to the same line as graphql request does the trick:

image

I tried modifying the begin regexp to be: "begin": "graphql request\\n\\s+\"\"\"", but it didn’t help - in fact, it stopped highlighting anything within quotes.

I’ve spent some time browsing other syntaxes in vscode and textmate, but could only find \n to be used in match regexp sections, but none yet in begin or end sections.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
ghostcommented, Jan 17, 2018

@kumarharsh For situations like this you have to make use of oniguruma lookarounds.

Here is a modification of your original example using a lookbehind:

{
  "injectionSelector": "L:text -comment",
  "patterns": [
    {
      "begin": "graphql request\\s*$",  // STAGE_1
      "end": "(?<=\"\"\")", // end if the last token consumed was the closing """
      "patterns": [
        {
          "begin": "^\\s*(\"\"\")$",  // STAGE_2
          "end": "^\\s*(\"\"\")",
          "patterns": [
            { "include": "source.graphql" }
          ]
        }
      ]
    },
  ]
  ...
}

Alternatively you can use a lookahead (although I find this is usually a worse choice):

{
  "injectionSelector": "L:text -comment",
  "patterns": [
    {
      "begin": "graphql request\\s*$",  // STAGE_1
      "end": "(\"\"\")$",
      "patterns": [
        {
          "begin": "^\\s*(\"\"\")$",  // STAGE_2
          "end": "^\\s*(?=\"\"\")",
          "patterns": [
            { "include": "source.graphql" }
          ]
        }
      ]
    },
  ]
  ...
}

One pattern you will want to use to match multi-line syntactic constructs accurately is as follows. This is what I was referring to earlier about chaining “begin”/“end” matches. You might want to adapt your example to this style depending on how many stages there will be after the first quoted block.

{
  "patterns": [
    {
      "begin": "A",
      "end": "B",
    },
    {
      "begin": "(?<=B)",
      "end": "C",
    },
    {
      "begin": "(?<=C)",
      "end": "D",
    },
    …
  ]
}
4reactions
kumarharshcommented, Nov 27, 2017

The L: part means left injection, i.e., the grammar rules are injected to the left of the existing rules for the scope being highlighted. When doing syntax highlighting, the left-most rule has higher precedence than the rules to it’s right. So the L: ensures that this syntax highlighting will override the default ones.

Ref: https://github.com/textmate/markdown.tmbundle/issues/15#issuecomment-18321960

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to find patterns in text spanning multiple lines?
I have a text file where these appear as a "collection" that span over multiple lines and the collections are grouped by {....
Read more >
Manage multiline messages | Filebeat Reference [8.5] - Elastic
This configuration merges any line that begins with whitespace up to the previous line. In this example, the pattern matches the following lines:...
Read more >
How can I "grep" patterns across multiple lines?
Replace bar with the ending part of the pattern. N appends the next line to the active buffer ( sed calls this the...
Read more >
Spanning multiple lines - Xinuos
Multiline records are a suitable solution for handling data that is regular in form, ... The BEGIN section is used to define a...
Read more >
re — Regular expression operations — Python 3.11.1 ...
Both patterns and strings to be searched can be Unicode strings ( str) as well as 8- ... Matches the start of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found