Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect string syntax highlighting in attributes in VS Code #6042

Closed
brendanzab opened this issue Sep 20, 2020 · 8 comments
Closed

Incorrect string syntax highlighting in attributes in VS Code #6042

brendanzab opened this issue Sep 20, 2020 · 8 comments
Labels
E-unknown It's unclear if the issue is E-hard or E-easy without digging in S-actionable Someone could pick this issue up and work on it right now

Comments

@brendanzab
Copy link
Member

When using logos, I get the following broken syntax highlighting:

Screen Shot 2020-09-21 at 9 40 44 am

Original source for replication purposes:

#[derive(Debug, Clone, Logos)]
enum Quoted<'source> {
    #[regex(r#"[^\\"']+"#)]
    Text(&'source str),
    #[token("\\")]
    StartEscape,
    #[token("\'", |_| Quote::Single)]
    #[token("\"", |_| Quote::Double)]
    End(Quote),

    #[error]
    Error,
}

#[derive(Debug, Clone, Logos)]
enum Escape {
    #[token("\\", |_| '\\')]
    #[token("n", |_| '\n')]
    #[token("r", |_| '\r')]
    #[token("t", |_| '\t')]
    #[token("0", |_| '\0')]
    #[token("\'", |_| '\'')]
    #[token("\"", |_| '\"')]
    Single(char),
    #[token("u")]
    StartUnicodeEscape,
    #[token("x")]
    StartAsciiEscape,

    #[error]
    Error,
}

#[derive(Debug, Clone, Logos)]
enum UnicodeEscape<'source> {
    #[regex(r"\{[0-9a-fA-F]*\}", |lexer| &lexer.slice()[1..(lexer.slice().len() - 1)])]
    CharCode(&'source str),
    #[token("\'", |_| Quote::Single)]
    #[token("\"", |_| Quote::Double)]
    End(Quote),

    #[error]
    Error,
}

A workaround (suggested by @Kixiron and seen in the wild in Crunch) is to add // workaround highlighting: " to the offending lines:

 #[derive(Debug, Copy, Clone, PartialEq)]
 enum Quote {
     Single,
@@ -33,7 +61,7 @@ enum QuotedLiteral {

 #[derive(Debug, Clone, Logos)]
 enum Quoted<'source> {
-    #[regex(r#"[^\\"']+"#)]
+    #[regex(r#"[^\\"']+"#)] // workaround highlighting: "
     Text(&'source str),
     #[token("\\")]
     StartEscape,
@@ -53,7 +81,7 @@ enum Escape {
     #[token("t", |_| '\t')]
     #[token("0", |_| '\0')]
     #[token("\'", |_| '\'')]
-    #[token("\"", |_| '\"')]
+    #[token("\"", |_| '\"')] // workaround highlighting: "
     Single(char),
     #[token("u")]
     StartUnicodeEscape,
@brendanzab brendanzab changed the title Confused string syntax highlighting in attributes in VS Code Incorrect string syntax highlighting in attributes in VS Code Sep 21, 2020
@bjorn3
Copy link
Member

bjorn3 commented Sep 21, 2020

Does this also happen when the rust-analyzee extension is disabled? If so then this issue is likely caused by an incorrect regex in the language definition of vscode.

@brendanzab
Copy link
Member Author

Ahh yeah, it seems like it is still doing something similar with rust-analyzer disabled:
Screen Shot 2020-09-21 at 4 07 43 pm

@lnicola
Copy link
Member

lnicola commented Sep 21, 2020

I think there's another issue about the same thing. But is the built-in grammar still used if semantic highlighting is enabled?

@bjorn3
Copy link
Member

bjorn3 commented Sep 21, 2020

The rust-analyzer extension ships with a grammar that is I believe adapted from the built-in grammar of vscode. I think it only has some tweaks to better make it match what semantic highlighting would output. (#4397) I am not sure if semantic highlighting enhances or replaces the output of the vscode grammar.

@sloane-shark
Copy link

Hi all! I'm not sure if this problem is necessarily caused by rust-analyzer, but it seems like maybe rust-analyzer could fix it? VS Code ships with a rust grammar that is pulled from atom which incorrectly excludes raw string literals from attributes. I have opened a PR here, but as far as I can tell it hasn't been noticed or picked up. I'm using a local version of this change to make my own syntax highlighting work properly. Perhaps this could be implemented in rust-analyzer with semantic highlighting?

@brendanzab
Copy link
Member Author

Yeah, seems like the project is looking for maintainers? zargony/atom-language-rust#144

@matklad
Copy link
Member

matklad commented Oct 15, 2020

@dustypomerleau is this fixed by the new grammar?

@matklad matklad added E-unknown It's unclear if the issue is E-hard or E-easy without digging in S-actionable Someone could pick this issue up and work on it right now labels Oct 15, 2020
@dustypomerleau
Copy link
Contributor

@matklad Nearly. Cases 2 and 3 are ok:

Screen Shot 2020-10-15 at 22 24 16

Case 1 still needs a little work:

Screen Shot 2020-10-15 at 22 25 56

The way that strings and chars are included in attributes is allowing them to gobble up the closing ] of the attribute itself. I will have a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-unknown It's unclear if the issue is E-hard or E-easy without digging in S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

No branches or pull requests

6 participants