clangd / vscode-clangd

Visual Studio Code extension for clangd
https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd
MIT License
592 stars 97 forks source link

Inactive Region Highlights Persist Through `clangd.restart` #600

Closed Baricus closed 3 months ago

Baricus commented 3 months ago

Since version 0.1.25 (which replaced the old behavior of highlighting regions with the default comment color) the vscode-clangd extension does not clear either background highlights or lowered opacity of inactive code before reloading the server. Background highlights or opacity are re-applied to the code (additively in case of the background highlight). Additionally, if the inactive code changed during the reload due to using a new compile_commands.json or edits to the existing one, code can be highlighted as inactive even while it is active.

As an example, consider the following C++ program:

#include <iostream>

#ifdef A
#define MESSAGE "Howdy A!"
#else
#ifdef B
#define MESSAGE "Hello B!"
#else
#error "A or B must be defined"
#endif
#endif

int main() {
    std::cout << MESSAGE << std::endl;
}

and the following (handwritten for simplicity) compile_commands.json:

[
    {
        "arguments": [
            "/usr/bin/g++",
            "-DA",
            "main.cpp"
        ],
        "directory": "/path/to/folder/containing/both/files/",
        "file": "main.cpp",
        "output": "a.out"
    }
]

When loading a folder containing these two files in VSCode, vscode-clangd properly marks the inactive regions: image If we now change -DA to -DB in compile_commands.json and run clangd.restart the highlights from -DA persist: image For completeness, this also works on the "error" branch if we drop the define entirely: image

This behaves similarly if the clangd.inactiveRegions.useBackgroundHighlight setings is enabled, but the highlights appear to stack, getting darker as you repeatedly reload, even without changing the compile_commands.json file.

My hunch is that this occurs because there is no code to clear the regions in the dispose method, but I'm very new to VSCode extension development so I am unsure I'm understanding the root cause properly.

Logs clangd.log (Covers walking through the same steps as shown in the example)

System information Clangd version (from the log, or clangd --version): 17.0.3 clangd extension version: 0.1.27-0.1.25 (tested with all 3 versions) Operating system: Linux x86-64, using remote development through a Windows 10 machine (though I think that isn't relevant here?)

HighCommander4 commented 3 months ago

Thanks for the report. I've noticed this as well.

A workaround until the bug is fixed is to close and reopen the affected source file in the editor.