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

[json] colorize string literals based on language information coming from schemas #224581

Open
voxpelli opened this issue Aug 2, 2024 · 10 comments
Assignees
Labels
feature-request Request for new features or functionality help wanted Issues identified as good community contribution opportunities json JSON support issues
Milestone

Comments

@voxpelli
Copy link

voxpelli commented Aug 2, 2024

The built in json-language-features extension provides plenty of nice features for JSON Schemas, such as markdownDescription and defaultSnippets.

I would love for it to also enable indicating that the value of a property is an embedded language.

Specifically I would love for the built in npm extension to be able to indicate that values within the "scripts" property of package.json files are shellscript values.

Would make it easier to interpret and author complex npm-scripts setup such as this:

Skärmavbild 2024-08-02 kl  14 19 11

Implementation brainstorming

The current schema that the npm extension uses is json.schemastore.org/package:

"jsonValidation": [
{
"fileMatch": "package.json",
"url": "https://json.schemastore.org/package"
},

It actually already contains something similar that's aimed at IntelliJ – "x-intellij-language-injection": "Shell Script", which IntelliJ added in JetBrains/intellij-community@2e490ce

Here's how it looks in the package.json schema:

"scriptsInstallAfter": {
  "description": "Run AFTER the package is installed.",
  "type": "string",
  "x-intellij-language-injection": "Shell Script"
},

Maybe something similar, such as "x-vscode-language-injection": "shellscript" could work? Or, considering that Shell Script is an alias of shellscript, consume x-intellij-language-injection wherever it is available?

Related discussions

Mostly stumbled upon people requesting this for yaml:

@voxpelli
Copy link
Author

voxpelli commented Aug 2, 2024

Here's the PR that added the "x-vscode-language-injection": "shellscript" to the package.json JSON Schema: SchemaStore/schemastore#2195

@hyperupcall
Copy link
Contributor

This would be a great addition! As someone that co-maintains SchemaStore, I lean towards the shared use of the x-intellij-language-injection property, assuming languages have aliases that generally include the Intellij ids. Otherwise, all schemas that have properties of x-intellij-language-injection will also need to add an extra x-vscode-language-injection, and we'd have to check to ensure the values are in sync.

@aeschli
Copy link
Contributor

aeschli commented Aug 6, 2024

Annotating the schema is only a first, small step, the challenge is how to do the coloring.

Syntax highlighting happens on the renderer, through TextMate grammars and grammar injections. It has no access to the JSON schema which is loaded and evaluated from the JSON language extension, running in a process on the extension host. We don't have a way to dynamically inject rules on runtime.

What could be done is to use semantic highlighting, in the JSON language extension, but that would mean implementing a parser for shell script.

Help is welcome...

@aeschli aeschli added this to the Backlog Candidates milestone Aug 6, 2024
@aeschli aeschli added json JSON support issues feature-request Request for new features or functionality labels Aug 6, 2024
Copy link

This feature request is now a candidate for our backlog. The community has 60 days to upvote the issue. If it receives 20 upvotes we will move it to our backlog. If not, we will close it. To learn more about how we handle feature requests, please see our documentation.

Happy Coding!

@aeschli aeschli changed the title Extend json-language-features to support injecting embedded languages with JSON Schemas [json] colorize string literals based on language information coming from schemas Aug 6, 2024
@voxpelli
Copy link
Author

voxpelli commented Aug 6, 2024

@aeschli I'm thinking it would work similarly to how CSS and JS can be embedded in HTML, is that a lot different?

@aeschli
Copy link
Contributor

aeschli commented Aug 7, 2024

With HTML, the HTML grammar includes the CSS and TypeScript grammars. when ever it sees <style> is starts using the rules for CSS.

The difference is that the coloring that you want is only for certain files and properties and only at runtime (once we have loaded and evaluated the schema) we know which ones. We have no mechanism to redefine/customize grammars at runtime.
We can do this with semantic highlighting, which goes on top of syntax highlighting.

Copy link

🙂 This feature request received a sufficient number of community upvotes and we moved it to our backlog. To learn more about how we handle feature requests, please see our documentation.

Happy Coding!

@aeschli aeschli added the help wanted Issues identified as good community contribution opportunities label Aug 20, 2024
@RedCMD
Copy link
Contributor

RedCMD commented Aug 26, 2024

I could make an extension to do this
but does the JSON language extension provide any api for other extensions to use?
location of strings/objects and the current schema in use
it would nice to not have to re-compute the entire schema and JSON document

I could then provide semantic syntax highlighting using TextMate or tree-sitter

I don't see how it could be possible to provide a JSON TextMate grammar to do it perfectly (built-in TextMate syntax highlighting)
as its impossible for TextMate to lookahead multiple lines (which could be required by some schemas)
however, it would allow for extra language features like bracket highlighting etc

@aeschli
Copy link
Contributor

aeschli commented Aug 26, 2024

@RedCMD No the JSON language extension does not have such an API. It's unclear to me how such an API would look like. Also I'd rather avoid dependencies between extensions. Maintaining APIs is a lot of work.

  • The semantic highlighting feature could be implemented in the JSON extension itself
  • Or the extension that needs schema information can use the vscode-json-languageservice node module. For parsing JSON use the node-jsonc-parser node module.

I would recommend not to worry about schema support first, but just look at e.g. syntax highlighting in package.json script tags. Its already challenging enough.

@RedCMD
Copy link
Contributor

RedCMD commented Aug 27, 2024

I've created up a mock extension https://marketplace.visualstudio.com/items?itemName=RedCMD.json-embedded-languages
Simply just a TextMate injection to target a root level "scripts" object

image

"x-intellij-language-injection" lang:json -path:**/globalStorage** -path:**/package.json -path:**/package.schema.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features or functionality help wanted Issues identified as good community contribution opportunities json JSON support issues
Projects
None yet
Development

No branches or pull requests

4 participants