microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
165.02k stars 29.55k forks source link

No syntax highlight in embedded language scope #117190

Closed kkeri closed 3 years ago

kkeri commented 3 years ago

Issue Type: Bug

I'm contributing to an extension that supports an embedded DSL written in Haskell. Currently the extension doesn't define a language ID and suggests adding a file association to Haskell to utilize existing syntax highlighters.

I'd like to define a proper language ID but we don't want to lose syntax highlighting, so I came up with a workaround: defining a grammar file with a single global scope and embedding Haskell into the global scope.

Prerequisites: Install a Haskell syntax highlighter (e.g. this one).

Expected behavior: highlight the source code as Haskell.

Actual behavior: I don't get syntax highlight although the languge scope utility correctly identifies the scope as haskell.

scope-sample

A .hs file in the same directory is displayed with correct syntax highlight.

scope-haskell

I made a minimal extension to reproduce the issue. I copy relevant files here.

package.json

{
    "name": "vscode-syntax-ext",
    "displayName": "Sample extension for VSCode",
    "description": "...",
    "version": "1.0.0",
    "engines": {
        "vscode": "^1.30.0"
    },
    "categories": [
        "Other"
    ],
    "contributes": {
        "languages": [
            {
                "id": "sample",
                "aliases": [
                    "sample"
                ],
                "extensions": [
                    ".sample"
                ]
            }
        ],
        "grammars": [
            {
                "language": "sample",
                "scopeName": "source.sample",
                "path": "./syntaxes/sample.tmLanguage.json",
                "embeddedLanguages": {
                    "source.sample": "haskell"
                }
            }
        ]
    }
}

syntaxes/sample.tmLanguage.json

{
    "scopeName": "source.sample",
    "patterns": [],
    "repository": {}
}

VS Code version: Code 1.53.2 (622cb03f7e070a9670c94bae1a45d78d7181fbd4, 2021-02-11T11:48:44.518Z) OS version: Linux x64 5.4.0-65-generic

System Info |Item|Value| |---|---| |CPUs|Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz (4 x 800)| |GPU Status|2d_canvas: enabled
gpu_compositing: enabled
multiple_raster_threads: enabled_on
oop_rasterization: disabled_off
opengl: enabled_on
protected_video_decode: unavailable_off
rasterization: disabled_software
skia_renderer: enabled_on
video_decode: disabled_software
vulkan: disabled_off
webgl: enabled
webgl2: enabled| |Load (avg)|1, 1, 1| |Memory (System)|7.65GB (0.17GB free)| |Process Argv|--no-sandbox --unity-launch --crash-reporter-id 222d795a-5647-4ace-82aa-10aed6154287| |Screen Reader|no| |VM|0%| |DESKTOP_SESSION|unity| |XDG_CURRENT_DESKTOP|Unity| |XDG_SESSION_DESKTOP|unity| |XDG_SESSION_TYPE|x11|
Extensions (27) Extension|Author (truncated)|Version ---|---|--- ohm|att|1.0.0 vscode-postgres|cko|1.1.17 vscode-eslint|dba|2.1.14 tslint|eg2|1.0.44 vsc-material-theme|Equ|33.1.2 vsc-material-theme-icons|equ|1.2.2 reasonml|fre|1.0.38 vscode-graphql|Gra|0.3.15 haskell|has|1.2.0 hoogle-vscode|jca|0.0.7 svn-scm|joh|2.13.5 language-haskell|jus|3.3.0 theme-karyfoundation-themes|kar|20.0.3 AWK|lug|0.0.2 gnuplot|Mar|1.0.11 template-string-converter|meg|0.4.7 vscode-docker|ms-|1.10.0 cpptools|ms-|1.2.1 java|red|0.75.0 code-spell-checker|str|1.10.2 mib|Ver|0.0.4 vscodeintellicode|Vis|1.2.11 vscode-java-debug|vsc|0.31.0 vscode-java-dependency|vsc|0.18.0 vscode-java-pack|vsc|0.12.1 vscode-java-test|vsc|0.28.0 vscode-maven|vsc|0.27.1 (4 theme extensions excluded)
A/B Experiments ``` vsliv368cf:30146710 vsreu685:30147344 python383cf:30185419 vspor879:30202332 vspor708:30202333 vspor363:30204092 pythonvsdeb440:30248342 pythonvsded773:30248341 pythonvspyt875:30259475 ```
aeschli commented 3 years ago

The TextMate grammar of the main language needs to include the syntax highlighting rules for the embedded languages. The rules can come in through an include, e.g. as seen here: https://github.com/microsoft/vscode/blob/main/extensions/perl/syntaxes/perl.tmLanguage.json#L1051 VSCode uses the standard TextMate mechanisms here.

The embeddedLanguages declaration are used by the editor to determine the language at a given positions: e.g. to know which comment tokens and brackets to use.

Let me know if I misunderstood you.