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

Add operator detecting for c/cpp

Open TOMWT-qwq opened this issue 1 year ago • 17 comments

  • Add operator detecting for c/cpp.
  • Fix FUNCTION_DECLARATION not matching overloading operator.

Changes

  • Defined OPERATOR constant and added it somewhere.
  • Modified something related to FUNCTION_DECLARATION.

TOMWT-qwq avatar Jan 13 '24 16:01 TOMWT-qwq

When you say symbol do you mean operator? If so we'd do this by first putting all the operators (including multi-character ones like !=) into an array then using our own regex.either function to build the actual regex.

joshgoebel avatar Jan 14 '24 03:01 joshgoebel

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

7 files changed

Total change +573 B

View Changes
file base pr diff
es/core.min.js 8.17 KB 8.17 KB +2 B
es/highlight.min.js 8.17 KB 8.17 KB +2 B
es/languages/arduino.min.js 4.61 KB 4.75 KB +144 B
es/languages/cpp.min.js 2.59 KB 2.73 KB +140 B
highlight.min.js 8.2 KB 8.21 KB +2 B
languages/arduino.min.js 4.61 KB 4.76 KB +144 B
languages/cpp.min.js 2.6 KB 2.73 KB +139 B

github-actions[bot] avatar Jan 14 '24 20:01 github-actions[bot]

I cleaned up the PR a bit to better match our coding standards... but I think the reason this hasn't been done before if the issue with <... sometimes it's an operator, sometimes it's the start of a template... so that case would need to be properly handled. (or maybe this will prove to be simpler in C++ since we only use it inside FUNCTION_TYPE_RE)... not sure.

Right now there are a ton of failing markup tests... you'll need to go thru them and see which are operator related (which will simply need to be updated) and which are template related.

There is a line (you can uncomment) in the test/markup test code that will just "replace" the test result files with whatever we generate - it can be helpful for updating result files but if you do this please proof all the changes by hand to guarantee they are as expected.

joshgoebel avatar Jan 14 '24 21:01 joshgoebel

Nothing is matched according to the tests. Is regex.escape(x) working properly? I can't find its defination from the package downloaded here

TOMWT-qwq avatar Jan 15 '24 04:01 TOMWT-qwq

So sorry I forgot to push the main file that does the linking... if pull and build it should work now.

joshgoebel avatar Jan 16 '24 01:01 joshgoebel

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

7 files changed

Total change +731 B

View Changes
file base pr diff
es/core.min.js 8.17 KB 8.22 KB +47 B
es/highlight.min.js 8.17 KB 8.22 KB +47 B
es/languages/arduino.min.js 4.61 KB 4.76 KB +151 B
es/languages/cpp.min.js 2.59 KB 2.73 KB +146 B
highlight.min.js 8.2 KB 8.25 KB +45 B
languages/arduino.min.js 4.61 KB 4.76 KB +150 B
languages/cpp.min.js 2.6 KB 2.74 KB +145 B

github-actions[bot] avatar Jan 16 '24 01:01 github-actions[bot]

ok I'll deal with those tests

TOMWT-qwq avatar Jan 16 '24 04:01 TOMWT-qwq

Those tests are strong and I've found some bugs :)

Some of the failed tests are related to Arduino.

tomwt-awa avatar Jan 16 '24 09:01 tomwt-awa

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

7 files changed

Total change +748 B

View Changes
file base pr diff
es/core.min.js 8.17 KB 8.22 KB +47 B
es/highlight.min.js 8.17 KB 8.22 KB +47 B
es/languages/arduino.min.js 4.61 KB 4.76 KB +154 B
es/languages/cpp.min.js 2.59 KB 2.74 KB +150 B
highlight.min.js 8.2 KB 8.25 KB +47 B
languages/arduino.min.js 4.61 KB 4.77 KB +154 B
languages/cpp.min.js 2.6 KB 2.74 KB +149 B

github-actions[bot] avatar Jan 16 '24 23:01 github-actions[bot]

I set its relevance to 0 to make autodetect working.

Finally I've dealt with most of them except the template <>

tomwt-awa avatar Jan 17 '24 10:01 tomwt-awa

Is -> an operator?

TOMWT-qwq avatar Jan 17 '24 15:01 TOMWT-qwq

I don't think it's a good idea to deal with these two cases in this PR. They are not related with operator.

  • :: and ->(and : in some special case, I found one in the tests) I think they should be detected as punctuation.
  • template <>. I think they should be detected as quotes or smth else.

Anyway let's finish this stage of my work. I'll deal with them in another two PRs.

tomwt-awa avatar Jan 20 '24 08:01 tomwt-awa

@allejo Any thoughts on whether it might be OK to just accept that <> as part of templates are going to be highlighted as operators (because it's too hard to distinguish) - for the increased fidelity of getting operators highlighted in general? I've long resisted this, but maybe in cases where it wouldn't break highlighting in general we should just embrace it?

I'll deal with them in another two PRs.

Let's see what allejo says. My concern is the template stuff may be very hard or even impossible to fix - so to accept this PR (separately) we'd have to be willing to accept that risk. So we need to decide we're open to that type of brokeness long-term or if we even want to still consider it broken.

joshgoebel avatar Jan 20 '24 16:01 joshgoebel

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

9 files changed

Total change +1.03 KB

View Changes
file base pr diff
es/core.min.js 8.17 KB 8.22 KB +43 B
es/highlight.min.js 8.17 KB 8.22 KB +43 B
es/languages/arduino.min.js 4.61 KB 4.76 KB +158 B
es/languages/c.min.js 1.86 KB 2 KB +140 B
es/languages/cpp.min.js 2.59 KB 2.74 KB +154 B
highlight.min.js 8.21 KB 8.25 KB +43 B
languages/arduino.min.js 4.61 KB 4.77 KB +157 B
languages/c.min.js 1.87 KB 2.01 KB +139 B
languages/cpp.min.js 2.6 KB 2.75 KB +153 B

github-actions[bot] avatar Jan 20 '24 17:01 github-actions[bot]

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

9 files changed

Total change +1.04 KB

View Changes
file base pr diff
es/core.min.js 8.17 KB 8.22 KB +43 B
es/highlight.min.js 8.17 KB 8.22 KB +43 B
es/languages/arduino.min.js 4.61 KB 4.77 KB +160 B
es/languages/c.min.js 1.86 KB 2.01 KB +144 B
es/languages/cpp.min.js 2.59 KB 2.75 KB +156 B
highlight.min.js 8.21 KB 8.25 KB +41 B
languages/arduino.min.js 4.61 KB 4.77 KB +159 B
languages/c.min.js 1.87 KB 2.01 KB +143 B
languages/cpp.min.js 2.6 KB 2.75 KB +154 B

github-actions[bot] avatar Jan 21 '24 19:01 github-actions[bot]

I'm going to ask you what are we going to do next btw.

  • I'm planning to add punctuation detect for ; and ::.
  • I'm planning to add namespace :: and template <> to the defination of a type and a function. I hope it can work for a structure of a::A<b::B<c::C>>>.

Any suggestion?

tomwt-awa avatar Jan 22 '24 09:01 tomwt-awa

You know, a powerful part of hljs is contains: ["self"]. However, a regex is not that powerful. I've tried to do smth, but it doesn't work as expected. I don't think we can recognize everything clearly like a complier now.

TOMWT-qwq avatar Jan 28 '24 05:01 TOMWT-qwq