highlight.js icon indicating copy to clipboard operation
highlight.js copied to clipboard

Fix highlight problem in preprocessor

Open akhtarmdsaad opened this issue 2 years ago • 3 comments

The highlighting of preprocessor goes wrong. Everything after the define line has no highlighting.

This PR resolves the Issue #3505

Changes

There was a simple change in cpp.js file. path: src/languages/cpp.js

Before: const PREPROCESSOR = { className: 'meta', begin: /#\s*[a-z]+\b/, end: /$/, <----------------------------- see this keywords: { keyword: 'if else elif endif define undef warning error line ' + 'pragma _Pragma ifdef ifndef include' }, contains: ...... image

After const PREPROCESSOR = { className: 'meta', begin: /#\s*[a-z]+\b/, end: /\s/, <----------------------- the change keywords: { keyword: 'if else elif endif define undef warning error line ' + 'pragma _Pragma ifdef ifndef include' }, contains: ......

image

*The header files <....> are treated as html elements and hence not shown

Checklist

  • [x] Added markup tests, or they don't apply here because...
  • [x] Updated the changelog at CHANGES.md

akhtarmdsaad avatar Sep 20 '23 01:09 akhtarmdsaad

Should I add the contains property or end the preprocessor after "\s" ? This ending doesn't seems to me a proper way so adding the contains might be a good approach. can you suggest me

akhtarmdsaad avatar Sep 20 '23 05:09 akhtarmdsaad

Thanks for the attempt here, but I think this one might be a bit more complex.

If this does fix 3505, it's a very "round-about" fix... the stated issue there is that lines following a pre-processor line are not highlighted at all (because of how we handling templating), not that the content on the same line immediately following a pre-processor directive is not highlighted.

The current behavior seems an intentional choice. True, it's not what VS Code does, but seems unclear to me that VS Code is superior. The text that follows any random compiler directive is not necessarily C code, and thus using those same rules to highlight it (as normal C code) seems quite wrong in some ways.

I'll leave this open for now to see if it draws any further discussion/opinions.

joshgoebel avatar Sep 21 '23 01:09 joshgoebel

Github highlighting (just for curiosity):

// this is a continued\
comment.
#include <iostream>
#include <iostream>
#define what do{ cout << "Hello"; } while(0);
#define PI 3.14;
#pragma warning(disable : 1234) 
#if !defined(CONFIG_OPTION)
#error "CONFIG_OPTION is not defined."
#endif
#line 42 "mycode.cpp"
#undef PI

Looks like it tries to parse some directives and highlight them special and the rest just get the macro highlighted and the rest entirely untouched.

joshgoebel avatar Sep 21 '23 02:09 joshgoebel