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

[api] Allow extensions to use the syntax highlighter #56356

Open
sebastian-lenz opened this issue Aug 14, 2018 · 22 comments
Open

[api] Allow extensions to use the syntax highlighter #56356

sebastian-lenz opened this issue Aug 14, 2018 · 22 comments
Labels
api feature-request Request for new features or functionality tokenization Text tokenization
Milestone

Comments

@sebastian-lenz
Copy link

It would be great if the extension API would offer a way to access the syntax highlighter of VS Code.

I am currently building an extension that offers an inline help system, I use a WebviewPanel to display the help in the editor. For consistency it would be great if we could format code samples the same way the editor does but I currently see no way to do that.

@jrieken jrieken assigned alexdima and aeschli and unassigned jrieken Aug 14, 2018
@aeschli
Copy link
Contributor

aeschli commented Aug 16, 2018

In what format would we give the syntax highlighted code give back?

@aeschli aeschli added this to the Backlog milestone Aug 16, 2018
@aeschli aeschli added the feature-request Request for new features or functionality label Aug 16, 2018
@sebastian-lenz
Copy link
Author

Anything to work with would be great.

My first intention was to search for an API that could resolve a MarkupContent or MarkedString object to html. MarkupContent is the common way extension pass text and code snippets that will be displayed to the user (e.g. from a hover or signature help provider).

This way markdown content could be rendered as well. Currently extension have to ship their own markdown parser (which VS Code already ships with at least two I know of) and if they want to provide highlighted code something like highlight.js and separate definitions for the language (or somehow integrate vscode-textmate). Before posting the issue here I asked it on the Gitter channel and another user indicated that they actually do it like so in their extension.

I also noted that the syntax color scheme of the currently activated theme is not available in the webview and cannot be accessed by extensions either.

@sebastian-lenz
Copy link
Author

So, just to add this, all I would need would be a rendered html string.

@aeschli
Copy link
Contributor

aeschli commented Aug 17, 2018

Adding @mjbvz as the webview expert.

@mjbvz
Copy link
Contributor

mjbvz commented Aug 20, 2018

@aeschli To get vscode-like syntax highlighting in a webview we would need an api like @sebastian-lenz proposes. For the markdown preview specifically, since many websites use highlightjs for highlighting code anyways, using highlightjs in the markdown preview isn't to much of a problem imo

@mjbvz mjbvz added the api label Aug 20, 2018
@mjbvz mjbvz removed their assignment Aug 20, 2018
@aeschli aeschli changed the title Allow extensions to use the syntax highlighter [api] Allow extensions to use the syntax highlighter Sep 21, 2018
@octref
Copy link
Contributor

octref commented Feb 5, 2019

You might be interested in http://shiki.matsu.io/. It doesn't use oniguruma so no native dependecy, should be quite easy to integrate into your extension.

@usernamehw
Copy link
Contributor

Can shiki be already used for markdown preview code blocks (to solve #59483)?

@octref
Copy link
Contributor

octref commented Feb 5, 2019

/cc @mjbvz

@sebastian-lenz
Copy link
Author

Thanks for the tip, but my core issue is the fact that when including a syntax highlighter in an extension, the results are not visually consistent within VS Code. Although Shiki will use the same specification, we cannot access the theme colors the user has picked.

@APerricone
Copy link

The token colors aren't even exposed as CSS variables... over 400 variables and the tokens are missing?

@DonJayamanne
Copy link
Contributor

Thought I'd mention this (even though it has been mentioned before #56356 (comment))
I'm currently using http://shiki.matsu.io and this works perfectly well (for generating HTML with styles/themes & custom tokens from VS Code & VS Code extensions).

@octref thanks for the wonderful package

@DarkTrick
Copy link

Isn't this also a little related with the question here on StackOverflow? If VSCode would provide the possibility to use the editor object - perhaps including some options like readonly - this ticket would be resolved as well?

@ylg
Copy link

ylg commented Nov 9, 2021

@DonJayamanne Would you elaborate on that a little? I don't see how Shiki solves for this (other than by coincidentally supporting themes that may or may not match a user's visual config).

@rangav
Copy link

rangav commented Jan 19, 2022

I also require this feature to be implemented, Currently unable to use themes for my extension Thunder Client in the response syntax highlighting

I have hard-coded the default theme colors for now
Screenshot 2022-01-19 at 19 08 17

@DonJayamanne
Copy link
Contributor

I don't see how Shiki solves for this (

Sorry I missed this. I had to write code to load the theme files into Shiki.

@tjpalmer
Copy link

tjpalmer commented Apr 11, 2022

The mermaid markdown example by @mjbvz shows where to plug in custom syntax highlighting in markdown preview. And vscode-textmate can be used to adapt tokens to hljs class names. It would be nice if vscode did this automatically, but it's possible to wire up manually at this point. And this works better than shiki for multiple reasons, including because hljs class names pick up matching theme colors automatically.

@Hyperclaw79
Copy link

Any progress on this?
While manually wiring in the themes would be doable for a specific language, it will be hard to support a generic case which can accept any language for syntax highlighting.
Moreover, any highlighting changes added by other language extensions will not be reflected this way.

Take a look at this generic Sticky Note which allows codeblocks to be created by copying a selection. Even after copying in the base css files like markdown.css and highlight.css, there's a lot of inconsistency between the note and the actual file.
image

@dgss
Copy link

dgss commented Sep 13, 2022

May I ask how you could make shiki work on an vscode webview extension (that uses webpack)? VSCode webview extensions are not able to make requests which seems necessary for the onig.wasm to be loaded and same for the themes. Themes can be solved somehow but i really wonder to know how you make the shiki work?

@mjbvz
Copy link
Contributor

mjbvz commented Jan 10, 2023

@aeschli @alexdima For webviews and notebook renderers, we'd like an API where we can give VS Code a string of code and a language identifier, and get back html+css with syntax highlighting for that code. This would work similarly to the existing tokenizeToString function we use in core

Some use cases:

  • For webviews, I'd like to use this to highlight code blocks in the markdown preview: Use VS Code's syntax highlighting in Markdown preview #91279

  • For notebook renderers, I had to put together a solution for this in core that uses tokenizeToString. It would be nice to move this logic out of core and also let other notebook renderers access this API

@shiqimei
Copy link

shiqimei commented Aug 2, 2023

Hi @mjbvz ! Do you know how GitHub Copilot Chat works? Since its codeblock can be adapted to the theme user selected.

image image

@dgss
Copy link

dgss commented Aug 2, 2023

Hi @mjbvz ! Do you know how GitHub Copilot Chat works? Since its codeblock can be adapted to the theme user selected.

image image

They have two different things, in the chat window they are not 100% matching the editor colors. However for the big answer blocks they are using a new editor tab which already has the editor colors as i've seen.
As i've checked last week there is still no css custom properties match with editor colors. But maybe the way they are using is more proper, instead of using a codeblock inside the extension window, using a dedicated editor tab for it. Unfortunately it doesn't fit into our needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api feature-request Request for new features or functionality tokenization Text tokenization
Projects
None yet
Development

No branches or pull requests