Fall back to 'Plain Text' if a referenced syntax is missing#427
Merged
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.
Enselic
commented
Mar 12, 2022
| } | ||
| } | ||
|
|
||
| fn with_plain_text_fallback<'a>( |
Collaborator
Author
There was a problem hiding this comment.
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.
Collaborator
There was a problem hiding this comment.
I was wondering about that, thanks for the explanation :)
keith-hall
reviewed
Mar 12, 2022
keith-hall
approved these changes
Mar 12, 2022
trishume
approved these changes
Mar 13, 2022
This was referenced Mar 27, 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The consequence of this change is that if
syntectencounters a syntaxthat references another syntax that is not present,
syntectwillfallback 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,
bathas a syntax for Vue, but Vue references a syntaxwith scope
text.pug, which is not included in bat (by default).So if
batencounters the followingvue-with-pug.vuefile:batcurrently panics with:With this change, instead of panicking,
batwill print the file withthe pug parts highlighted as Plain Text:
Closes #421