-
-
Notifications
You must be signed in to change notification settings - Fork 143
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
MEGA-ISSUE: Syntax highlighting in language-c
(C/C++)
#877
Comments
In general, if you're using a built-in syntax theme, we intend for the new grammars to make your code look similar to how it looked before. If you're using a community syntax theme, we can't make any such guarantees, but we'll tell you exactly how to apply the syntax highlighting you expect via overrides in your user stylesheet. |
Using this file as a reference and testing with the built-in One Dark theme, I can attest that most of the syntax highlighting differences I saw were not intentional choices, and the ones that were intentional were not strongly held.
The changes have been made to both C and C++ and are described in this commit that is now part of #859. |
FWIW things appear more broken with the built-in Base16 Tomorrow Dark theme (for example user-defined types are no longer highlighted). Is there an easy way to test #859 without rebuilding everything (which I'm afraid I can't do in a reasonable amount of time on my machine)? |
You can set
Aha! I always forget about those. Thank god some people understand CI better than I do. |
Is #859 supposed to fix the other themes too, or are you only taking care of One Dark at first? Also, with One Dark, is highlighting variables when declaring or assigning them expected? |
It's supposed to fix the other built-in themes. I admit I only fixed One Dark and One Light earlier, but I'll make a note to revisit the others.
Yes. Let me know if that isn't the case. Long version: In C-style languages without a sigil (like My way of resolving this was to say that variables are scoped as So — not just for @Tamaranch, but also for other readers of this issue:
|
Testing the latest #859 for the various built-in themes, I realize that now only One Dark doesn't highlight function parameters (in function declaration and definition). Perhaps it would be a good thing to do so here too, if we think of it as a kind of variable declaration? |
I'm not sure why they made that decision. All the syntax themes are due for revisions, but that's a large project. I'm not sure that One Dark should be the default, but if I were to change it now, experience tells me I'd need to offer a concession for folks who liked the way it already was. My instinct is that we'd evolve those themes but offer the originals as optional packages. |
This could be a bug: structure members are not highlighted in this definition (whatever the theme): static const struct wl_registry_listener registry_listener =
{
.global = registry_global,
.global_remove = registry_global_remove,
}; |
Fixed in this commit. Thanks again for such great feedback! |
After some agonizing and vacillating, I just made a change that affects C/C++: rather than draw a confusing distinction between built-in value types like
Since this new convention is exactly how legacy Tree-sitter did things in C/C++, this should only reduce the highlighting differences between legacy modern Tree-sitter grammars. But let me know if you notice any unusual side-effects. |
Not sure if it's only since the latest changes, but pointer of pointer declarations (such as |
And you're on a recent build? That's odd — I added support for that a few days ago. For reference, if you're unsure if something is theme-dependent or not, run the Editor: Log Cursor Scope command with your cursor inside the token in question. In this example… int main (char **strv;) {
} … |
Yes, I'm using the appimage from the latest build (Pulsar-1.113.2024012204.AppImage). As a function parameter, highlighting works, but not as a variable or structure member. After checking, this isn't new, it's also the case with 1.113 and at least with the penultimate build I have locally (Pulsar-1.113.2024012105.AppImage). |
Not sure it should count, but I note in passing that array item assignments are not highlighted: strv[n] = "str"; |
OK, that goes on the TODO list. Can you give me a code example for a scenario where highlighting isn't present? |
Yeah, this one isn't a regression, but you can certainly make the argument. Not a major priority right now, but it's worth remembering. |
struct {
char **strv;
};
void f (void) {
char **strv = NULL;
char **strv1;
} |
Pointer-of-pointer variable declarations are addressed in this commit. |
@SirTomofAto Yep, that is some weird behavior there. I'll look into it. Thanks for the report! |
@SirTomofAto Addressed in this commit in #906. The Thanks again for reporting this! |
Great! Thanks for the quick response. And yeah, I completely get the need for |
It seems that this commit also fixes a bug I was about to report, concerning the highlighting of function parameters whose name ends in void f (int p_, int p_1, int p_12); It's reproducible with rolling release |
Ha! It is an unwanted side effect. My pattern for detecting This stopped happening for parameters because the recent changes stopped applying the int table[foo_]; I'll push a fix for that, too. So glad you mentioned it! EDIT: Here's the commit. |
Looks like it's still an issue in C++ (pulsar 1.114.0), see e.g. https://gitlab.xfce.org/panel-plugins/xfce4-docklike-plugin/-/blob/fcbe8325871adea32cede57212ff50977a3be156/src/Wnck.cpp#L98 |
Oof! I try pretty hard to keep things in sync between the two grammars. I'll take a look. |
Goto labels are no longer highlighted, either when "calling" them via void f (void) {
/* some code */
goto cleanup;
/* some code */
cleanup:
/* cleanup */
} |
I've encountered a strange crash that only happens when using the new tree-sitter implementation, and only occurs for me when on Linux, not Windows (I typically use Pulsar within an Ubuntu VM development environment).
The crash happens consistently when there is a number and then two non-numbers, with the non-numbers encapsulated in parenthesis. |
I can reproduce this. The symptoms are identical to those I got when I tried to build a Thanks for the report! |
OK, I think I've managed to solve this by upgrading our custom version of This fix should go out in nine days or so with the 1.115 release, but if you want to make sure it works, you can grab a nightly build from the artifacts of this CI job (once it's done in a half-hour or so). Thanks again, @SirTomofAto! |
Yeah, looks like it. tree-sitter-tools will help to discern whether these are problems with Pulsar or with the underlying Tree-sitter parser.
Hey, that's useful. It reveals a bug in our injection handling. Highlighting of macro values is a crapshoot for the reasons explained here. Everything after On the second line the On the third line, the I suspect that this has something to do with the arcane historical differences between comment types in C, but I'm unwilling to spend any time in my finite lifespan on finding out. The takeaway here is that we need to be injecting into |
Thanks for the explanation, interesting to learn about. |
No problem. The fix is now present in #941, by the way. |
Structure members are not highlighted when there is no pointer dereference or assignment: in if (s.m) {}
if (s->m) {} it is highlighted. |
C isn't my strong suit, so I'll ask: can you think of any scenarios in which |
Sounds good to me. |
OK, gonna handle both of these. First one was just an oversight. As for the second one: the legacy tree-sitter grammar scoped goto labels — in both definition and usage — as Mirroring the definition/mention distinction that exists in functions, a label definition will be |
OK, just added to #914. We're running a bit late on the monthly release, but I'm hoping we can get it out this coming week. |
Is this new behavior? I'm not sure I follow — can you do a screencast or otherwise explain exactly what's happening? |
No it's not new, it was already the case in Atom, but it's annoying. Here is a video of it: Capture.video.du.2024-08-06.20-22-37.mp4 |
Yeah, that's a tough one. I think there's a way to describe that style of indentation with the new query system such that it always does the right thing, but in general that's a blind spot for any editor that follows from the TextMate school of thinking. (Which generally preserves indentation from one line to the next, except for certain patterns that increase or decrease indentation by a single increment.) Feel free to file a separate issue on this so we can keep track of it; it's a feature request, but it's a fair one. I'm tempted to take this on just to prove that it's possible to detect these “hanging-indent” scenarios and do the right thing. It might not be something that lands in core, but a community package should at least be able to substitute its own indentation logic. |
@mibi88, I think I've got something that would satisfy you. The only caveat is that you have to start typing before the indentation fixes itself: un-hang-indent.movThere isn't a fantastic way to customize the built-in indentation queries, but until I fix that, you can drop this into your {
let disposable = atom.grammars.onDidAddGrammar(
async (grammar) => {
if (grammar.scopeName !== 'source.c' || grammar.constructor.name !== 'WASMTreeSitterGrammar') {
return;
}
disposable.dispose();
await grammar.loadQueryFiles(grammar.grammarFilePath, grammar.queryPaths);
let existing = grammar.indentsQuery;
grammar.indentsQuery = `
${existing}
(
(expression_statement)
.
(_) @match
(#set! indent.matchIndentOf previousSibling.startPosition)
)
`;
}
)
} This isn't comprehensive, but it certainly addresses the example you gave. Similar queries could be added (if needed) to handle If it works well, we could even add it to |
Ok, I'll make one.
Yes, it's a lot better like that. |
IMPORTANT: Some issues have already been fixed!
If you’re still on the regular 1.113 release, you might be suffering from a problem that has already been fixed. Many fixes landed on
master
in #859. You are encouraged to download the latest rolling release — both (a) to see whether what you’re reporting has been fixed, and (b) so that you can enjoy the fixes that have been reported since the 1.113 release.This will serve as a catch-all issue for any problems you may encounter with syntax highlighting in C or C++ files (
language-c
). If you have problems with the new syntax highlighting (or folds, or indents) that are specific toc
,cpp
, or related files (h
,cc
, etc.), keep reading.Something isn't highlighting correctly!
If you’ve found any highlighting regressions since 1.113:
First, please scroll down and see if someone else has reported the issue. If so, then you need only sit back and wait for a fix — most issues will be fixed in version 1.114!
If not, please comment with
I want to go back to the old highlighting!
You can easily opt out of the new Tree-sitter highlighting for any language, but first please:
To revert to the old Tree-sitter grammar for this language only, add the following to your
config.cson
:To revert to the TextMate-style grammar for this language only, add the following to your
config.cson
:The text was updated successfully, but these errors were encountered: