Skip to content

syntax/c: highlight the header name in #include as a string, not a type - #4236

Open
Londopy wants to merge 2 commits into
micro-editor:masterfrom
Londopy:fix-c-include-highlight
Open

Londopy wants to merge 2 commits into
micro-editor:masterfrom
Londopy:fix-c-include-highlight

Conversation

@Londopy

@Londopy Londopy commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

#include <float.h> highlighted float as a type (and <, ., > as operators) because the #include line was only matched by a pattern rule, so the other top-level patterns still ran over the header name.

This makes #include lines a preproc region (like scad.yaml does), which stops the type/identifier patterns from matching inside it. The region's inner rules highlight <...> and "..." as constant.string — matching how #include "float.h" was already rendered — and keep trailing // and /* */ comments as comments. Inner rules are patterns rather than nested regions, since nested regions get top-level patterns leaked into them by highlightRegion.

Checked with pkg/highlight directly:

input result
#include <float.h> #include:preproc, <float.h>:constant.string
#include "float.h" unchanged from before
# include <stdint.h> // TODO: drop <stdint.h>:constant.string, // TODO: drop:comment
#include <bool.h> /* int */ /* int */:comment (int no longer a type)
#include MY_HEADER all preproc
float x = 1.0f; / int y = a < b > c; unchanged
#include <float.h> followed by float z; second line still highlights float as type (region closes at EOL)

Closes #3930

@Andriamanitra Andriamanitra left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice little improvement, works as expected.

You could remove "include" from the preproc rule on line 17 since it's now handled by the region.

@Londopy

Londopy commented Sep 20, 2026

Copy link
Copy Markdown
Contributor Author

Done! Dropped include from the pattern. While retesting I noticed that left #include_next unhighlighted (the old pattern was partially matching it), so the region start now also accepts _next.

@JoeKar

JoeKar commented Sep 21, 2026

Copy link
Copy Markdown
Member

The same is valid for the cpp.yaml (maybe even more like objc.yaml), right?

@Andriamanitra

Copy link
Copy Markdown
Collaborator

The same is valid for the cpp.yaml (maybe even more like objc.yaml), right?

Good point! We have syntaxes for 6 different variants of C:

  • arduino
  • c
  • cpp
  • cuda
  • hc
  • objc

It would probably make sense to use include: "c" in at least some of these to inherit from the C syntax, but I'm not sure which ones are true supersets of C. But for this PR simply copying the rule to all of them would be fine too.

Grepping for #.*include also found three others that are not C but I guess can use a similar preprocessor (I don't think we need to touch these):

  • ats
  • fortran
  • zscript

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

Successfully merging this pull request may close these issues.

C syntax highlighting.

3 participants