-
Notifications
You must be signed in to change notification settings - Fork 144
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
Fall back to 'Plain Text' if a referenced syntax is missing #427
Conversation
The consequence of this change is that if `syntect` encounters a syntax that references another syntax that is not present, `syntect` will fallback to Plain Text syntax instead of erroring/panicking. Falling back to Plain Text in cases like this seems to be what Sublime Text is doing too. For example, `bat` has a syntax for Vue, but Vue references a syntax with scope `text.pug`, which is not included in bat (by default). So if `bat` encounters the following `vue-with-pug.vue` file: ``` <template lang='pug'> #container.col p This shall be formated as Plain Text as long as a Pug syntax definition is missing </template> ``` `bat` currently panics with: ``` % bat ~/Desktop/vue-with-pug.vue ───────┬──────────────────────────────────────────────────────────────── │ File: /Users/martin/Desktop/vue-with-pug.vue │ Size: 140 B ───────┼──────────────────────────────────────────────────────────────── thread 'main' panicked at 'Can only call resolve on linked references: ByScope { scope: <text.pug>, sub_context: None }', /Users/martin/.cargo/registry/src/github.com-1ecc6299db9ec823/syntect-4.6.0/src/parsing/syntax_definition.rs:191:18 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ``` With this change, instead of panicking, `bat` will print the file with the pug parts highlighted as Plain Text.
@@ -767,6 +767,32 @@ impl SyntaxSetBuilder { | |||
} | |||
} | |||
|
|||
fn with_plain_text_fallback<'a>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried putting this logic inside of Self::find_id()
directly, which would simplify the code. But rustc complained about reached the recursion limit while instantiating 'func::<[closure]>'
. This is because the predicate
parameter is generic, and calling find_id()
inside of itself causes recursion without a base case. This problem is explained in detail here.
This was the most straightforward way I could come up with to work around that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering about that, thanks for the explanation :)
The consequence of this change is that if
syntect
encounters a syntaxthat references another syntax that is not present,
syntect
willfallback to Plain Text syntax instead of erroring/panicking. Falling
back to Plain Text in cases like this seems to be what Sublime Text is
doing too.
For example,
bat
has a syntax for Vue, but Vue references a syntaxwith scope
text.pug
, which is not included in bat (by default).So if
bat
encounters the followingvue-with-pug.vue
file:bat
currently panics with:With this change, instead of panicking,
bat
will print the file withthe pug parts highlighted as Plain Text:
Closes #421