Search code examples
c++eclipseeclipse-cdtconstexpr

Is there a way to syntax highlight constexpr variables in Eclipse CDT?


When I create an enum, the members of it are in blue and italics in my code. I would like something similiar for a constexpr variable (C++ 14 or later). Is that possible somehow?


Solution

  • I don't think you can get exactly what you're looking for.

    But, in this statement:

    constexpr double MY_VARIABLE;
    

    ...you can change the formatting of constexpr, double, or MY_VARIABLE independently, with side effects affecting other variables and keywords too. Here's how:


    I just tested this in Eclipse Version 2023-03 (4.27.0), and it works:

    Window --> Preferences --> C/C++ --> Editor --> Syntax Coloring.

    In the "Element" box, expand "Code" --> choose Keywords. Change its color. That affects conxtexpr and all other keywords, such as:

    • if
    • else
    • typedef
    • using
    • auto
    • return
    • template
    • class
    • etc.

    To see the colors and formatting for enums, look for Enum Classes, Enumerators, and Enums in the same "Element" box, so you can borrow those settings if you like.

    If your goal is to change the double part, for instance, in constexpr double, that is controlled by the Built-in types option in the "Element" box. But, again, that changes all built-in types, not just those preceded by constexpr.

    If you'd like to change the MY_VARIABLE part of, for instance, constexpr double MY_VARIABLE, when it is a global variable, that is controlled by the Global variables element. You can make it Italic too:

    enter image description here

    I don't think there's a way to affect just constexpr but no other keywords, without a custom extension or custom syntax highlighting scheme or something.

    Here's a screenshot. You can see in the "Preview" section other keywords affected and now white in my case too. I verified in my main editor that this changed constexpr.

    enter image description here

    Switch to VSCode

    My last bit of advice to you is that unless you need the amazing indexing capabilities of Eclipse, since Eclipse is the clear winner here for absolutely gigantic projects and code bases (ex: 1 GB ~ 100 GB repos such as those I've used at my previous two companies), switch to VSCode. As a long-time Eclipse user, I've switched to VSCode and find it overall much better. I still keep Eclipse installed, however, for when I need to track down some code and need Eclipse's far superior indexer.

    References

    1. I used my document here, under my section "Now update your doxygen syntax highlighting colors", to remind myself how to do this: Eclipse setup instructions on a new Linux (or other OS) computer.

    2. GitHub Copilot also says the same thing if you simply ask it, Is there a way to syntax highlight constexpr variables in Eclipse CDT?. Other AIs to check with include Bing Chat, ChatGPT, and Google Bard. But, remember, Stack Exchange warns you each time you add an answer:

      Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more.

      And, a lot of people utterly despise AIs and may "fart in your general direction" if you try to use or talk about them.

      But, I use them every single day in professional programming, and will never program again without them, because they are so useful! They are amazing. They do not replace me; they enhance me.