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

Syntax highlight the JS in %raw #6214

Closed
Tracked by #6211
cristianoc opened this issue Apr 26, 2023 · 4 comments
Closed
Tracked by #6211

Syntax highlight the JS in %raw #6214

cristianoc opened this issue Apr 26, 2023 · 4 comments
Milestone

Comments

@cristianoc
Copy link
Collaborator

E.g.

let canUseCanvas: unit => bool = %raw(`
  function canUseCanvas() {
    return !!document.createElement('canvas').getContext;
  }
`)

could syntax highlight the JS code.

This issue could be a combination of finding an alternative surface syntax, e.g.

js`() => 3`

and adapt the editor extensions's grammar to recognise this syntax and inject a JS syntax highlighting grammar.

@cristianoc cristianoc added this to the v12 milestone Apr 26, 2023
@cristianoc
Copy link
Collaborator Author

CC @fhammerschmidt

@cristianoc
Copy link
Collaborator Author

OK write it down concisely now so I can paste it in a GitHub issue in markdown format.

To add custom syntax highlighting for embedded JavaScript code within your custom language in a VSCode extension, follow these steps:

  1. Update the tmLanguage.json file in your extension's syntaxes folder with the custom rule:

    {
      "name": "source.embedded.javascript",
      "begin": "\\bjs`",
      "end": "`",
      "beginCaptures": {
        "0": {
          "name": "punctuation.definition.string.begin.embedded-js"
        }
      },
      "endCaptures": {
        "0": {
          "name": "punctuation.definition.string.end.embedded-js"
        }
      },
      "patterns": [
        {
          "include": "source.js"
        }
      ]
    }

    This rule tells VSCode to recognize the custom syntax pattern and apply the JavaScript syntax highlighting to the code enclosed within the backticks.

  2. Test the changes in VSCode using the Extension Development Host to ensure the custom syntax highlighting works as expected and does not conflict with existing syntax rules.

  3. Once the changes are working correctly, update the version number in your package.json file and publish your updated extension to the Visual Studio Code Marketplace.

Note: This assumes you have already created a language extension with its own grammar file, and that the built-in JavaScript syntax rules are available in your environment through the source.js scope.

@cristianoc
Copy link
Collaborator Author

Screenshot 2023-04-27 at 02 06 24

@cristianoc
Copy link
Collaborator Author

Don here: rescript-lang/rescript-vscode#774

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

No branches or pull requests

1 participant