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

Syntax causes tokenization to break for remainder of file #488

Closed
1 of 5 tasks
alexr00 opened this issue Jul 13, 2020 · 10 comments
Closed
1 of 5 tasks

Syntax causes tokenization to break for remainder of file #488

alexr00 opened this issue Jul 13, 2020 · 10 comments

Comments

@alexr00
Copy link

alexr00 commented Jul 13, 2020

Checklist

  • This problem exists even with the setting "C_Cpp.enhancedColorization": "Disabled"
  • This bug exists for C
  • This bug exists for C++
  • This bug exists for Objective-C
  • This bug exists for Objective-C++

The code with a problem is:

Test::Test() : Base ("{}") {
   // Broken syntax highlight
}

It looks like:

image

It should look like:

It looks like things start to go wrong on the first curly brace, which ends up with these scopes:

punctuation.section.block.begin.bracket.curly.function.definition.special.constructor.cpp
meta.head.function.definition.special.constructor.cpp
meta.function.definition.special.constructor.cpp
source.cpp
@jeff-hykin jeff-hykin added High Priority Common or breaks highlighting of more than just itself and removed High Priority Common or breaks highlighting of more than just itself labels Jul 14, 2020
@jeff-hykin
Copy link
Owner

jeff-hykin commented Jul 14, 2020

Does this code use macros?

There's probably a more graceful way the syntax can handle this, but if the code isn't using utilizing the preprocessor I'd be very surprised if that code was valid.

@marzojr
Copy link

marzojr commented Jul 14, 2020

The code does not use macros or the preprocessor in any way and is perfectly valid in all versions of c++ ever; here is a full example that compiles:

struct Base {
    Base(const char *s_);
    const char *s;
};

struct Test : public Base {
    Test();
};

Base::Base(const char *s_) : s(s_) {}

Test::Test() : Base ("{}") {
   // Broken syntax highlight
}

You can place it on a c++ file and compile with any compiler.

Edit: live example.

@matter123
Copy link
Collaborator

@jeff-hykin constructors are allowed to call other constructors or the base constructor in the initializer list.

This is no different than

class Test {
std::string _member;
Test();
}

Test::Test(): _member("{}") {
}

Though both highlight correctly for me (if you ignore bracket highlighter getting confused)

Screen Shot 2020-07-14 at 11 10 26

@marzojr
Copy link

marzojr commented Jul 14, 2020

@matter123 It will break highlight if you add a space between _member and (. It seems like adding a space there combines with the curly braces in the string to break the highlighting.

@matter123
Copy link
Collaborator

@marzojr It seems that this may have been fixed sometime between the most recently published version and the version that vscode is using. You can manually install the "Better C++ Syntax" extension, to fix for now.

I finally, somewhat, have some time I can put into looking into #460, so hopefully, at some point, the version vscode is using should be able to be updated again.

Screen Shot 2020-07-14 at 11 16 27

@marzojr
Copy link

marzojr commented Jul 14, 2020

@matter123 I tried the extension you mentioned, and it does make the problem much better -- but it is still there.

Note that the curly braces in your screenshot are all highlighted as brackets, not as characters inside a character literal. Also, the closing parenthesis of the initializer, as well as the curly braces of the constructor, are incorrectly highlighted.

Exploring a bit, most of the syntax highlighting is correct with that extension, but curly braces, square brackets, and parenthesis, are all incorrectly highlighted for the remainder of the file -- they highlight as brackets while inside strings, and are not highlighted outside of strings.

Interestingly, changing from {} to {}[] in the character literal will fix the highlighting, probably because the grammar probably identifies it as a lambda introducer instead of as a scope.

@matter123
Copy link
Collaborator

matter123 commented Jul 14, 2020

Are your brackets colored via your color theme or via an extension like "Bracket Pair Colorizer 2"?

If you use Developer: Inspect Editor Tokens and Scopes (in the command palate), you should see that the grammar correctly marks the "{}" as string.quoted.double.cpp

@marzojr
Copy link

marzojr commented Jul 14, 2020

Hm, nice catch; the incorrect bracket highlighting is coming from "Bracker Pair Colorizer 2", and with it disabled, it seems that "Better C++ Syntax" fixes this issue.

@matter123
Copy link
Collaborator

Closing this as this is fixed in a released version.

@jeff-hykin
Copy link
Owner

jeff-hykin commented Jul 15, 2020

Thanks for taking care of this.

And well, I guess consider me surprised, I didn't account for the case of an external initializer list. I guess I'm a bit too used to inline constructors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants