dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.85k stars 669 forks source link

A Closing Curly Brace Inside a Char Literal or String (In a File-Scoped Namespace) Messes Up Syntax Highlighting For The Line #4912

Closed colejohnson66 closed 2 years ago

colejohnson66 commented 2 years ago

Issue Description

The highlighting algorithm says that, in this specific case, the comma after the enum entry is an error. The interesting thing is that this only happens with file scoped namespaces (not block scoped ones) (see screenshots below), and only with the right brace in the single quotes.

Curiously, inserting a (single) single quote after the comma causes the syntax highlighter to interpret the comma as being inside single quotes. A syntax error is, correctly, generated in this instance.

Basically, it's a visual bug only affecting syntax highlighting.

Steps to Reproduce

Paste the following into a new file in VS Code:

namespace TestNamespace;

public enum Test
{
    RightBrace = '}',
}

Expected Behavior

The comma after the enum entry is left white

Actual Behavior

The comma is actually red

Logs

OmniSharp log

Post the output from Output-->OmniSharp log here

C# log

Post the output from Output-->C# here

Environment information

VSCode version: 1.62.3 C# Extension: 1.23.16

Dotnet Information .NET SDK (reflecting any global.json): Version: 6.0.100 Commit: 9e8b04bbff Runtime Environment: OS Name: Windows OS Version: 10.0.22000 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\6.0.100\ Host (useful for support): Version: 6.0.0 Commit: 4822e3c3aa .NET SDKs installed: 3.1.415 [C:\Program Files\dotnet\sdk] 5.0.403 [C:\Program Files\dotnet\sdk] 6.0.100-rc.1.21458.32 [C:\Program Files\dotnet\sdk] 6.0.100-rc.1.21463.6 [C:\Program Files\dotnet\sdk] 6.0.100-rc.2.21505.57 [C:\Program Files\dotnet\sdk] 6.0.100 [C:\Program Files\dotnet\sdk] .NET runtimes installed: Microsoft.AspNetCore.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.0-rc.1.21452.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.0-rc.2.21480.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.0-rc.1.21451.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.0-rc.2.21480.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.0-rc.1.21451.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.0-rc.2.21501.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] To install additional .NET runtimes or SDKs: https://aka.ms/dotnet-download
Visual Studio Code Extensions |Extension|Author|Version| |---|---|---| |better-cpp-syntax|jeff-hykin|1.15.10| |better-toml|bungcip|0.3.2| |code-spell-checker|streetsidesoftware|2.0.13| |cpptools|ms-vscode|1.7.1| |cpptools-extension-pack|ms-vscode|1.1.0| |crates|serayuzgur|0.5.10| |csharp|ms-dotnettools|1.23.16| |gitlens|eamodio|11.7.0| |godot-tools|geequlim|1.1.2| |hexeditor|ms-vscode|1.8.2| |indent-rainbow|oderwat|8.2.2| |language-x86-64-assembly|13xforever|3.0.0| |markdown-preview-enhanced|shd101wyy|0.6.1| |material-icon-theme|PKief|4.11.0| |midl-webidl-syntax|dave-deletethis|0.0.1| |mono-debug|ms-vscode|0.16.2| |npm-intellisense|christian-kohler|1.4.0| |nugetpackagemanagergui|aliasadidev|1.1.9| |platformio-ide|platformio|2.4.0| |python|ms-python|2021.11.1422169775| |remote-ssh|ms-vscode-remote|0.66.1| |remote-ssh-edit|ms-vscode-remote|0.66.1| |remote-wsl|ms-vscode-remote|0.58.5| |rust-analyzer|matklad|0.2.826| |sort-imports|amatiasq|6.2.6| |tabnine-vscode|TabNine|3.5.1| |v4pvhdlforprofessionals|ViDE-Software|1.1.2| |vscode-eslint|dbaeumer|2.2.2| |vscode-firefox-debug|firefox-devtools|2.9.5| |vscode-npm-script|eg2|0.3.23| |vscode-nuget-package-manager|jmrog|1.1.6| |vscode-print|pdconsec|0.9.14| |vscode-pull-request-github|GitHub|0.32.0| |vscode-pylance|ms-python|2021.11.2| |vscode-tailwindcss|bradlc|0.7.2| |vscode-typescript-next|ms-vscode|4.6.20211126| |vscode-yaml|redhat|1.2.1| |vscodeintellicode|VisualStudioExptTeam|1.2.14| |vsliveshare|ms-vsliveshare|1.0.5090| |vsliveshare-audio|ms-vsliveshare|0.1.91| |wavetrace|wavetrace|1.1.2|;
colejohnson66 commented 2 years ago

Screenshots

The comma being highlighted red with file-scoped namespaces:

The comma being highlighted white (correctly) with block-scoped namespaces:

The comma being interpreted as a char when a (single) single quote is placed after it (with appropriate syntax error squigle):

colejohnson66 commented 2 years ago

I have also discovered that the bug is not present if there's no namespace declaration at all (file- or block-scoped)

colejohnson66 commented 2 years ago

This is also happening with curly braces inside strings. Again, a block scoped namespace fixes this.

namespace TestNamespace;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("{0}\t{1}", "abc", "def");
    }
}

In the first WriteLine argument, the '1' appears to be treated as part of an interpolated string. Everything after the closing quote on that argument is highlighted as if it were a string. The semicolon is colored red.

Placing a closing double quote after the semicolon will cause it to be highlighted as if it were a string as well, but a syntax error appears.

In all (syntactically correct) cases, Intellisense works fine, and the examples compile with no issues. Only the highlighting is bad, and only with file-scoped namespaces.

filipw commented 2 years ago

Could you please move this issue to https://github.com/dotnet/csharp-tmLanguage - thanks!

Syntax highlighting rules for C# are not part of this repo, they are part of the the textmate language grammar that ships as a built-in part of VS Code independently of this extension.

If you are looking for this extension to help you with syntax highlighting, you can enable semantic highlighting

    "editor.semanticHighlighting.enabled": true,
    "csharp.semanticHighlighting.enabled": true

which will address the problems you listed here.

colejohnson66 commented 2 years ago

I do have semantic highlighting enabled. However, the issue is still present if I turn them off. Should I still post an issue there?

filipw commented 2 years ago

Ah you are right - this is indeed happening for both non-semantic and semantic highlighting. I think it should still be opened under https://github.com/dotnet/csharp-tmLanguage because these are two separate highlighting implementations.

I will reopen this. @JoeRobich do you know if this is a classification bug in Roslyn?

colejohnson66 commented 2 years ago

Cross posted.

JoeRobich commented 2 years ago

Using the "Developer: Inspect Editor Tokens and Scopes" command we can see the semantic token for this span is string as expected. However, It appears that the editor is applying the textmate scope and color regardless.

image

Since this snipped uses File Scoped namespaces I wanted to see how it worked in VS Code insiders which has updated their C# textmate grammar. We can see here the proper highlighting behavior.

image

I believe this issue will be resolved in the next VS Code release, since it will have the updated grammar with file scoped namespace support.

filipw commented 2 years ago

Thanks for looking it this!

I was also thinking along these lines but I was not sure if the updated grammar shipped already or not, the last VS Code patch release was 10 days ago, and the grammar update commit is from ~4 weeks ago. However, it was done without a PR so it's hard to track what goes with which release.

colejohnson66 commented 2 years ago

VS Code Insiders does indeed fix the issue.