How to change the color of Preprocessor Conditionals such as #if in Vistual Studio?

Issue

Is there a method to change the color of Preprocessor Conditionals such as,

#if
#ifdef
#ifndef
#endif

I do work on large C libraries which use plenty of Preprocessor Conditionals and as you can see in the example below, this really limits the readability of the code as C programmers conventionally do not indent these conditionals.
Graying out these lines using Keywork Highlighting would be a big help on not confusing these un-indented lines as the end of long if statements etc.

Example code,

    ...
   if( ... )
    {

        for( uint8_t i = 0, k = 0; i < FOO; i += 16, k++ )
        {
            for( uint8_t j = 0; j < 16; j++ )
            {
#if defined( BAZ ) || defined( BAR )
                if( ... )
#else
                if( ... )
#endif
                {
                    if( ... )
                    { 
                        continue;
                    }
#if defined( BAZ_ ) || defined( BAR_ ) || defined( FOO_ )
                    if( ... )
                    {
                        if( ... )
                        {
                            continue;
                        }
                    }
#endif
                    if( ... ) 
                    { 
                        continue;
                    }
                    if( ... )
                    { 
                        foo++;
                        continue;
                    }
                }
            }
        }
    }
    ...

Note: I have phrased the question for Visual Studio, however, I will be using the solution in Atmel Studio which is basically a re-badged version of the same software. I have successfully install most of the VS add-ons to Atmel Studio in the past but tbh I haven’t given a R# a go, if your solution is going to include it.

Update:

After posting the question I actually noticed the Syntax Highlighting on Stackoverflow does an amazing job doing exactly what I have in mind. The # conditions are grayed out and the rest of the code reads beautifully. I would like to be able to do the same on VS/AS.

Solution

A year or two ago I ran across the solution: Naggy. Among several other features (which I find useful), it grays out preprocessor conditionals just like you expect and want.

In Atmel Studio 7.0 it’s available in the built-in Extensions Gallery. Install from there, restart AS, and you’re good to go. There are no options or configuration settings; it’s very simple, and works great.

I also had it working in Atmel Studio 6.2, but seems like I had to install it manually or something. This AVR Freaks thread should help.

Answered By – JBurger

Answer Checked By – David Goodson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.