How to define begin and end patterns which span multiple lines?
See original GitHub issueHi, 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:
but modifying the source text to bring the beginning """
to the same line as graphql request
does the trick:
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:
- Created 6 years ago
- Comments:16 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@kumarharsh For situations like this you have to make use of oniguruma lookarounds.
Here is a modification of your original example using a lookbehind:
Alternatively you can use a lookahead (although I find this is usually a worse choice):
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.
The
L:
part meansleft 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 theL:
ensures that this syntax highlighting will override the default ones.Ref: https://github.com/textmate/markdown.tmbundle/issues/15#issuecomment-18321960