Skip to content

Fall back to 'Plain Text' if a referenced syntax is missing#427

Merged
trishume merged 2 commits into
trishume:masterfrom
Enselic:plain-text-fallback
Mar 13, 2022
Merged

Fall back to 'Plain Text' if a referenced syntax is missing#427
trishume merged 2 commits into
trishume:masterfrom
Enselic:plain-text-fallback

Conversation

@Enselic

@Enselic Enselic commented Mar 12, 2022

Copy link
Copy Markdown
Collaborator

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.
Comment thread src/parsing/syntax_set.rs
}
}

fn with_plain_text_fallback<'a>(

@Enselic Enselic Mar 12, 2022

Copy link
Copy Markdown
Collaborator Author

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

Comment thread src/parsing/syntax_set.rs
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