Skip to content
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

Merged
merged 2 commits into from Mar 13, 2022

Conversation

Enselic
Copy link
Collaborator

@Enselic Enselic commented Mar 12, 2022

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:

Skärmavbild 2022-03-12 kl  08 03 16

Closes #421

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>(
Copy link
Collaborator Author

@Enselic Enselic Mar 12, 2022

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.

Copy link
Collaborator

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

panic on a svelte file
3 participants