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

feat: fallback to regex highlighting when treesitter is not available #462

Closed
wants to merge 2 commits into from

Conversation

mikavilpas
Copy link
Contributor

@mikavilpas mikavilpas commented Dec 6, 2024

Issue

Documentation for completions can include markdown fenced code blocks
that should be highlighted with the appropriate syntax. When a
treesitter parser is not available for the language used in the
codeblock, the code inside is not highlighted correctly.

Incorrect highlighting example:

image

This can be an issue when using
https://github.com/mikavilpas/blink-ripgrep.nvim which displays ripgrep
(rg) matches for all the files in the project. Many big projects include
a large variety of programming languages, and the user may not have all
the treesitter parsers installed for all the languages.

Solution

Right now, neovim comes bundled with a regex-based syntax highlighter
that supports 743 languages. We can use this syntax highlighter as a
fallback when a treesitter parser is not available for the language.

Improved highlighting example:

image

The solution is not perfect but an 80% solution. The entire
documentation text is highlighted in the syntax of the first codeblock.
This means the code will be displayed correctly, but any markdown syntax
in the documentation will look weird.

The idea is to provide a "better than nothing" experience out of the box, but if the user wants better highlighting, they should install the parser(s) for the languages they are interested in 🙂

Related issue: #336

Issue
=====

Documentation for completions can include markdown fenced code blocks
that should be highlighted with the appropriate syntax. When a
treesitter parser is not available for the language used in the
codeblock, the code inside is not highlighted correctly.

This can be an issue when using
https://github.com/mikavilpas/blink-ripgrep.nvim which displays ripgrep
(rg) matches for all the files in the project. Many big projects include
a large variety of programming languages, and the user may not have all
the treesitter parsers installed for all the languages.

Solution
========

Right now, neovim comes bundled with a regex-based syntax highlighter
that supports 743 languages. We can use this syntax highlighter as a
fallback when a treesitter parser is not available for the language.

The solution is not perfect but an 80% solution. The entire
documentation text is highlighted in the syntax of the first codeblock.
This means the code will be displayed correctly, but any markdown syntax
in the documentation will look weird.

Related issue: Saghen#336
mikavilpas added a commit to mikavilpas/blink-ripgrep.nvim that referenced this pull request Dec 6, 2024
@mikavilpas
Copy link
Contributor Author

Hmm I have been thinking that my use case might be quite special and it probably isn't the best at addressing regex based highlighting in blink in general.

Maybe there's another way to solve this. For example, to get colored highlights, I could provide a function in blink-ripgrep that blink could call when the documentation needs to be highlighted.

That way I could also add other decorations that are not supported by regex (or treesitter). For example, I would like to highlight the matched word with a different background color.

Let me know what you would prefer!

@Saghen
Copy link
Owner

Saghen commented Dec 10, 2024

I think a function makes sense. It could look something like fn highlight_documentation(win, buf, ns). In #465, I'll move all the blink specific properties on the items into a blink field and we can revisit this

@mikavilpas
Copy link
Contributor Author

Sounds good 👍🏻

I'll follow that PR, it looks like it enables a huge number of things that I like.

@mikavilpas
Copy link
Contributor Author

This was implemented in #650 using a different approach.

@mikavilpas mikavilpas closed this Dec 19, 2024
mikavilpas added a commit to mikavilpas/blink-ripgrep.nvim that referenced this pull request Dec 20, 2024
mikavilpas added a commit to mikavilpas/blink-ripgrep.nvim that referenced this pull request Dec 20, 2024
When a result is found for a file whose filetype does not have a
treesitter parser installed, fall back to regex based highlighting that
is bundled in Neovim. This allows highlighting many more file types out
of the box.

See here for the blink side implementation and a mini demo:

Saghen/blink.cmp#462
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.

2 participants