microsoft / vscode

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

DocumentSemanticTokensProvider fails for the first untitled editor created with showTextDocument since v1.76 #191286

Open yy0931 opened 1 year ago

yy0931 commented 1 year ago

I've been developing a VSCode extension to syntax highlight a language using semantic highlight, and I've noticed that vscode.DocumentSemanticTokensProvider.provideDocumentSemanticTokens is never called for the untitled document created by vscode.window.showTextDocument(await vscode.workspace.openTextDocument(...)) if the created untitled document is the first document created after opening a window.

The issue does not exist in VSCode v1.60-v1.75 but it does exist in v1.76-1.81 (current version).

Here is a minimal reproducible code. A .vsix file is also included in it. semantic-highlight-test-2.zip

extension.js

const vscode = require("vscode")

exports.activate = (/** @type {vscode.ExtensionContext} */context) => {
    const legend = new vscode.SemanticTokensLegend(["keyword"])
    context.subscriptions.push(
        // Highlight the entire file of languageId "test-lang" as "keyword"
        vscode.languages.registerDocumentSemanticTokensProvider({ language: "test-lang" }, {
            provideDocumentSemanticTokens(document) {
                const builder = new vscode.SemanticTokensBuilder(legend)
                for (let i = 0; i < document.lineCount; i++) {
                    builder.push(document.lineAt(i).range, "keyword")
                }
                return builder.build()
            }
        }, legend),

        // Register "semantic-highlighting-test", which opens a new document with languageId "test-lang"
        vscode.commands.registerCommand("semantic-highlighting-test", async () => {
            await vscode.window.showTextDocument(await vscode.workspace.openTextDocument({
                language: "test-lang",
                content: "foo\nbar"
            }))
        }),
    )
}

package.json

{
    "name": "semantic-highlighting-test",
    "displayName": "semantic-highlighting-test",
    "description": "",
    "version": "1.0.0",
    "publisher": "test",
    "engines": {
        "vscode": "^1.60.0"
    },
    "activationEvents": [
        "onCommand:semantic-highlighting-test",
        "onLanguage:test-lang"
    ],
    "main": "./extension.js",
    "browser": "./extension.js",
    "contributes": {
        "commands": [
            {
                "command": "semantic-highlighting-test",
                "title": "semantic-highlighting-test"
            }
        ],
        "languages": [
            {
                "id": "test-lang"
            }
        ],
        "configurationDefaults": {
            "[test-lang]": {
                "editor.semanticHighlighting.enabled": true
            }
        }
    },
    "scripts": {},
    "devDependencies": {
        "@types/vscode": "^1.60.0",
        "@vscode/vsce": "^2.20.1"
    }
}

Does this issue occur when all extensions are disabled?: Yes

Steps to Reproduce:

  1. Open VSCode.
  2. Install the extension.
  3. Close the VSCode window.
  4. Reopen a VSCode window.
  5. Open a folder (this step is necessary to prevent VSCode from opening an untitled editor on startup).
  6. Close all editor tabs.
  7. Close the VSCode window.
  8. Reopen a VSCode window. At this point, there should be no editor tabs.
  9. Execute "semantic-highlighting-test" from the command palette

Screencast:

recording.webm

yy0931 commented 1 year ago

I noticed that I should have added "onLanguage:test-lang" to activationEvents, so I updated the code and the zip file, but the issue still persists.

deser commented 11 months ago

I have similar issue with custom editor. The use case is the next:

Prerequisite

  1. Have custom editor implemented for some file extension (let's say .meow).
  2. Have semantic tokens provider implemented for .meow.

Steps to reproduce

  1. Open 2 tabs: 1 is code editor for 1.meow and 2nd is custom editor for the 1.meow.
  2. Focus on custom editor tab
  3. Close vscode and re-open
  4. Move focus to code editor tab.
  5. Observe that provideDocumentSemanticTokens is not called. Editing this file does not call provideDocumentSemanticTokens as well