better-cpp-syntax icon indicating copy to clipboard operation
better-cpp-syntax copied to clipboard

C++20 keywords don't seem to be highlighted

Open cjdb opened this issue 6 years ago • 29 comments

Checklist

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

The code with a problem is:

module example;
import <vector>
import some.other.module;

export template<typename From, typename To>
concept convertible_to =
	std::is_convertible_v<From, To> and
	requires(From (&f)()) {
		static_cast<To>(f());
	};

In the above example, module, import, <vector>, some.other.module, concept, and requires should all be highlighted, but they're not.

It looks like:

image

Theme: peaceshi

image

Theme: XD

image

Theme: One Monokai

It should look like:

  • module, import, concept, and requires should all be highlighted like the other keywords. (Note: module is contextually-dependent.)
  • <vector> and some.other.module should probably look like <vector> in #include <vector>

cjdb avatar Oct 11 '19 01:10 cjdb

I'd be okay with some.other.module not being highlighted.

cjdb avatar Oct 11 '19 02:10 cjdb

Duplicate of #302. Current focus is on published standards.

matter123 avatar Oct 11 '19 02:10 matter123

Just keyword highlighting, no context awareness, could be added in easily enough. Import as an alternative to #include should also be easy enough to add.

matter123 avatar Oct 11 '19 02:10 matter123

I've been trying to work out how to do this locally, but without success. Looks like the above words are already in the JSON file, which has confused me.

cjdb avatar Oct 11 '19 02:10 cjdb

Often those are in negative lookbehinds and lookaheads.

import: the import keyword needs to be pulled out of the check for #, But if the semicolon is required it might be best if its pulled into its own pattern.

Adding keywords: This should be just as simple as adding the correct flags to the keywords in https://github.com/jeff-hykin/cpp-textmate-grammar/blob/master/source/languages/cpp/tokens.rb#L261-L274

matter123 avatar Oct 11 '19 02:10 matter123

Thanks. I was definitely barking up the wrong tree, trying to add what's below to generate.rb.

grammar[:concept_definition] = Pattern.new(
        tag_as: "meta.declaration.concept",
        should_fully_match: ["concept A = B;"]
        match: Pattern.new(
                match:/concept/,
                tag_as: "keyword.other.concept",
            ).maybe(@spaces).then(
                identifier
            ).maybe(@spaces).then(
                assignment_operator
            ).maybe(@spaces).then(
                /[^;]+/
            ).maybe(@spaces).then(@semicolon.or(/\n/)),
    )

cjdb avatar Oct 11 '19 03:10 cjdb

Looks good, couple changes.

  1. use .then(std_space) rather than .maybe(@spaces). This is for a couple reasons:
    • std_space supports inline comments
    • std_space is automatically optional
    • .maybe(@spaces) can catastrophically backtrack.
  2. .then(/[^;]+/) should be .oneOrMoreOf(match: /[^;]/, dont_backtrack?: true)
  3. use @end_of_line instead of /\n/
  4. You still want a fallback to highlight /concept/ if the context aware pattern fails.

matter123 avatar Oct 11 '19 03:10 matter123

Do concept, requires, and import need logic like what's above? I get module probably does, but the others aren't context-dependent keywords.

cjdb avatar Oct 11 '19 03:10 cjdb

If a keyword doesn't influence any other highlighting, then all that is need is to add the correct flags inside tokens.rb. In the new PR, I added support for import.

matter123 avatar Oct 11 '19 03:10 matter123

I think this is a rookie mistake, but I'm having issues with npm run build.

cjdb: npm test

> [email protected] pretest /mnt/c/Users/Chris/projects/cpp-textmate-grammar
> npm run build


> [email protected] build /mnt/c/Users/Chris/projects/cpp-textmate-grammar
> ruby scripts/generate.rb

scripts/generate.rb:6: warning: Insecure world writable dir /mnt/c/Users/Chris/projects/cpp-textmate-grammar/node_modules/.bin in PATH, mode 040777
Traceback (most recent call last):
        4: from /mnt/c/Users/Chris/projects/cpp-textmate-grammar/source/languages/c/generate.rb:3:in `<main>'
        3: from /mnt/c/Users/Chris/projects/cpp-textmate-grammar/source/languages/c/generate.rb:3:in `require_relative'
        2: from /mnt/c/Users/Chris/projects/cpp-textmate-grammar/source/textmate_tools.rb:4:in `<top (required)>'
        1: from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
/usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- deep_clone (LoadError)


Generating the syntax for 'c' failed: 256
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `ruby scripts/generate.rb`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/cjdb/.npm/_logs/2019-10-11T04_10_51_315Z-debug.log
npm ERR! Test failed.  See above for more details.

Are there any prerequisites I should be installing? I've run npm install and npm install top to no avail.

cjdb avatar Oct 11 '19 04:10 cjdb

Yes bundle install, I should probably add a post install hook.

matter123 avatar Oct 11 '19 04:10 matter123

Yes bundle install, I should probably add a post install hook.

I went ahead and added that to the CONTRIBUTING.md for next time. Sorry, about that @cjdb, and thanks for taking the initiative to get things running on your own machine.

jeff-hykin avatar Oct 15 '19 23:10 jeff-hykin

I've noticed that since pulling 9f50b88, my custom requires highlighting has stopped working. Highlighting doesn't show even when it's configured identical to consteval, but interestingly, it doesn't highlight at all when I try to use it like a normal function.

cjdb avatar Oct 18 '19 00:10 cjdb

It is expected that requires is not usable as a function name due to, avoid_invalid_function_names = @cpp_tokens.lookBehindToAvoidWordsThat(:isWord, not(:isPreprocessorDirective), not(:isValidFunctionName)).

Can you share what you have for the requires highlighting? As well as what context you added it to.

matter123 avatar Oct 18 '19 01:10 matter123

Can you share what you have for the requires highlighting?

# source/language/cpp/tokens.rb
{ representation: "consteval", name: "consteval", isLambdaSpecifier: true },
{ representation: "requires",  name: "requires",  isLambdaSpecifier: true },

Same as consteval for now. Figured I'd start small, and work my way up.

As well as what context you added it to.

[]() consteval x {}; // consteval highlighted
[]() requires x {}; // requires not highlighted

cjdb avatar Oct 18 '19 01:10 cjdb

If you are using the debugger could you check that you are using the "Launch and Build All" configuration, the default "Launch Extension" does not usually rebuild the syntax files.

matter123 avatar Oct 18 '19 01:10 matter123

I've been manually running npm test, but I just tried Launch and Build All: no difference.

cjdb avatar Oct 18 '19 01:10 cjdb

That is very strange, If I add the requires token directly after consteval (line 221) everything seems to work.

Screen Shot 2019-10-17 at 18 47 57

Could you remove the other requires definition (on line 263) just to be sure?

Is the lambda definition inside of a macro?

@jeff-hykin Any thoughts on why it wouldn't work?

matter123 avatar Oct 18 '19 01:10 matter123

Still nothing. Is there any chance that the customised file isn't being loaded, and the default one is? image

cjdb avatar Oct 18 '19 01:10 cjdb

There shouldn't be, npm test has no knowledge of what vscode loads normally, and the extension development host replaces the extension, so that the default one should never be loaded.

matter123 avatar Oct 18 '19 02:10 matter123

Is the pattern being recognized as a lambda? (Is the scope at { meta.function.definition.body.lambda.cpp when using Developer: Inspect TM Scopes

matter123 avatar Oct 18 '19 02:10 matter123

Could you try to reproduce this from a clean clone of the repo to try to determine if this is a build issue or if other changes might be affecting it?

matter123 avatar Oct 18 '19 02:10 matter123

Could you try to reproduce this from a clean clone of the repo to try to determine if this is a build issue or if other changes might be affecting it?

This didn't fix it. Perhaps we should jump on an IM channel? I'm available on cpplang.slack or #include<C++> Discord under @cjdb.

Is the pattern being recognized as a lambda? (Is the scope at { meta.function.definition.body.lambda.cpp when using Developer: Inspect TM Scopes

See attached. image

cjdb avatar Oct 18 '19 03:10 cjdb

I've been manually running npm test, but I just tried Launch and Build All: no difference.

Is it opening a separate window? Running npm test will generate the files, but it won't change any visuals. To see the changes VS Code has to compile the extension, create a new window, and then sudo-install the newly compiled extension inside that window. And most times: when a change is made to the code the entire new-window needs to be reloaded.

Then new visuals and Developer: Inspect TM Scopes will only work inside that new window. Everywhere else will be the old version.

This didn't fix it. Perhaps we should jump on an IM channel?

Not a bad idea. Although if other people have this problem, we do want it to be findable.

jeff-hykin avatar Oct 18 '19 14:10 jeff-hykin

just an update on this problem: in v1.14.20 (pushed just now) should add at least basic highlighting for those keywords

jeff-hykin avatar Jan 21 '20 19:01 jeff-hykin

just an update on this problem: in v1.14.20 (pushed just now) should add at least basic highlighting for those keywords

Awesome, thanks!

(Note: it does not highlight the second requires keyword from this example (the one that comes from a requires-clause rather than a requires-expression), but perhaps that's a known issue.)

HighCommander4 avatar Jan 21 '20 22:01 HighCommander4

yeah... thanks for pointing that out. That case in particular is more of a templating problem see #246: Template definition needs multiple fixes

jeff-hykin avatar Jan 22 '20 04:01 jeff-hykin

related to #573 related to #575

soroshsabz avatar Oct 15 '21 00:10 soroshsabz