EvgeniyPeshkov / syntax-highlighter

Syntax Highlighter extension for Visual Studio Code (VSCode). Based on Tree-sitter.
https://marketplace.visualstudio.com/items?itemName=evgeniypeshkov.syntax-highlighter
MIT License
210 stars 43 forks source link

[C/C++] . and -> and :: as punctuation #12

Closed alekseyt closed 5 years ago

alekseyt commented 5 years ago

I think it makes sense to tag . and -> as punctuation. Comma (,) is an operator in C++ too, but no one highlighting it because it doesn't make sense to assign color to every ,. By extension it doesn't make a lot of sense to highlight every .. Since -> essentially does the same thing as . but on pointers instead of references, for consistency reasons it also should go to punctuation IMO.

Example code:

this->x
(this)->x
(*this).x

For C++ it also would be nice to move :: to punctuation because it looks weird in something like this:

const std::string s = "yo";

Alternatively all such "questionable" operators might go into their own category like syntax.punctuation.operator or syntax.operator.punctuation. Alas styles in this extension do not work hierarchically, e.g. syntax.operator won't assign color to syntax.operator.punctuation, so syntax.operator.punctuation is kind of misleading name. Another name might be something like syntax.access, idk really.

Here is the patch for moving ., -> and :: to punctuation:

diff --git a/grammars/c.json b/grammars/c.json
index 39da5af..a299197 100644
--- a/grammars/c.json
+++ b/grammars/c.json
@@ -49,8 +49,6 @@
         "\"typedef\"": "control",

         "\"sizeof\"": "operator",
-        "\".\"": "operator",
-        "\"->\"": "operator",
         "\"*\"": "operator",
         "\"-\"": "operator",
         "\"+\"": "operator",
@@ -98,6 +96,8 @@
         "preproc_directive": "directive",
         "preproc_arg": "directive",

+        "\".\"": "punctuation",
+        "\"->\"": "punctuation",
         "\";\"": "punctuation",
         "\"[\"": "punctuation",
         "\"]\"": "punctuation",
diff --git a/grammars/cpp.json b/grammars/cpp.json
index 4c2d39a..10b1f2a 100644
--- a/grammars/cpp.json
+++ b/grammars/cpp.json
@@ -69,8 +69,6 @@
         "\"sizeof\"": "operator",
         "\"new\"": "operator",
         "\"delete\"": "operator",
-        "\".\"": "operator",
-        "\"->\"": "operator",
         "\"*\"": "operator",
         "\"-\"": "operator",
         "\"+\"": "operator",
@@ -105,7 +103,6 @@
         "\"^=\"": "operator",
         "\"|=\"": "operator",
         "\"?\"": "operator",
-        "\"::\"": "operator",

         "\"#if\"": "directive",
         "\"#ifdef\"": "directive",
@@ -117,6 +114,9 @@
         "\"#include\"": "directive",
         "preproc_directive": "directive",

+        "\".\"": "punctuation",
+        "\"->\"": "punctuation",
+        "\"::\"": "punctuation",
         "\";\"": "punctuation",
         "\":\"": "punctuation",
         "\",\"": "punctuation",

What do you think?

alekseyt commented 5 years ago

Also i think ->* and .* are missing in syntax. It's not a big deal, but if this fix is made then someone may complain that in

a->*b

There are 1) two operators while it's one operator really 2) they may or may not be highlighted differently. But it won't be noticeable if -> and * are in the same category.

EvgeniyPeshkov commented 5 years ago

Greeting @alekseyt , welcome. C++ is my very primary language, and almost the only one. So I've put my best efforts to highlight it as good as I can a long time ago. While comma is indeed operator in C++, it's rarely used this way. Only the last part of for-loop condition comes to mind. And even then it's more like a delimiter. So in my opinion it's better to leave comma in punctuation as other delimiters. Speaking of ., ->, ::. I don't see a reason to not color them as operators. const std::string s = "yo"; doesn't look weird to me either. Just think of punctuation category as delimiters. The main goal is to provide for readable highlighting, calm, simple, easy to customize.

alekseyt commented 5 years ago

Nevermind then.