microsoft / monaco-editor

A browser based code editor
https://microsoft.github.io/monaco-editor/
MIT License
40.18k stars 3.58k forks source link

[Bug] Wrong syntax highlighting in c++'s attribute specifiers #3395

Open Sickeroni opened 1 year ago

Sickeroni commented 1 year ago

Reproducible in vscode.dev or in VS Code Desktop?

Reproducible in the monaco editor playground?

Monaco Editor Playground Code

[edit]

monaco.editor.create(document.getElementById('container'), {
    value: "[[nodiscard(\"is the word using highlighted? Yes it is and alignas too.\")]]\nint test() {\n    return 42;\n}",
    language: 'cpp'
});

[/edit]

Reproduction Steps

[[nodiscard("is the word using highlighted? Yes it is")]]
int test() {
    return 42;
}

or the extended

#include <iostream>

[[nodiscard("is the word using highlighted? Yes it is")]]
int test() {
    return 42;
    std::cout << "but here it is okay to use the word using \n";
}

or just use this link https://godbolt.org/z/4cc31Wf4Y

Actual (Problematic) Behavior

Syntax highlighting is wrong when using specific keywords in string literals in attribute specifiers. Found only the word "using" but properly there are more. [edit]same for alignas[/edit]

The following (key)words don't have the problem:

just see the color of the word "using"
image same for the extended code image

the other (key)words: image

and for completion here the vscode version

image

Expected Behavior

Since it is a string literal, it should be the same color as other parts of the text.

Additional Context

When using godbolt, this bug appeared. The issue based on it is to be found at https://github.com/compiler-explorer/compiler-explorer/issues/4239
The suggestion was to create a issue here, since their syntax highlighting is monaco based.

relevant quotes:

RubenRBS Hi! Thanks for the report. While we can probably patch this in our end, we ultimately rely on Microsof's own Monaco editor syntax highlighting for C++. Can you report this issue in https://github.com/microsoft/monaco-editor? If anyone wants to try and fix it, the offending file can be found in https://github.com/microsoft/monaco-editor/blob/main/src/basic-languages/cpp/cpp.ts

mattgodbolt In fairness I think I patched support for this kind of stuff upstream in monaco 😊 so it could still be my fault... but yeah, probably an expert there (ie not me) should take a look.

partouf commented 1 year ago

The code to use in the playground would be:

monaco.editor.create(document.getElementById('container'), {
    value: "[[nodiscard(\"is the word using highlighted? Yes it is\")]]\nint test() {\n    return 42;\n}",
    language: 'cpp'
});

This reproduces this situation.

Sickeroni commented 1 year ago

Updated the playground part thanks to to https://github.com/partouf

FelixDombek-TomTom commented 1 year ago

This looks fairly trivial, maybe as simple as adding a line that matches strings before https://github.com/microsoft/monaco-editor/blob/main/src/basic-languages/cpp/cpp.ts#L400, such as [/(")([^"]*)(")/, 'string'],.