Visual Studio user-defined keywords highlighting
February 6, 2010 3 Comments
- What it is all about ?
- Have you ever coded in assembly language ? for me the answer is “yes I did”, I used to write assembly programs using MASM through Visual studio 2008 which is a very rich IDE, unfortunately Visual Studio supports highlighting C/C++ , .NET languages, XML only, this mean that it doesn’t support assembly code highlighting which may be very important for a readable assembly code in order to distinguish between an instruction and its operands (MOV EAX, 8).
- There is no doubt that code highlighting makes your eyes more comfortable and debugging operation easier, but how to highlight keywords that Visual Studio cannot recognize ? this suggests 2 ways to achieve so, lets call them “Newbie” and“Visual Studio Geek” way.
- The “Newbie” way:
- This suggests using an open source text editor like “Notepad++” which supports code highlighting for a variant of languages including (C/C++, C#, Assembly, Java, PHP, Python).
- One of “Notepad++” hottest features is that it allows for a user defined syntax highlighting, where you can define your own language and its highlighting, this sounds awesome !
- Advantage:
- Easy to apply, you just need the text editor home page, check http://notepad-plus.sourceforge.net/uk/site.htm
- Doesn’t require any extra technical skills assuming you know how to download and setup a program !.
- Disadvantage:
- Each time you try to compile your code you need to do the following time wasting operation:
- Press Ctrl+A, Ctrl+C, which means copying your code from your text editor.
- Press Alt+Tab to navigate to visual studio, Pres Ctrl+V to paste your code in Visual Studio code area, then Compile.
- The separation between the area where the code is being compiled and the area where the code get highlighted is not so far from a real programmer nightmare, which is the “Debugging monster” .. not scared yet ? watch this:
- you are compiling a mass of code, a “syntax error” appears, simply you go to the error pane and navigate to the error line through clicking on the error description.
- Reaching the error line, you find a mass of black text, and you should scan it with your eyes up and down to figure out the error and fix it, which is somehow scary.
- It gets worse when you need to debug a logical error.
- Now we will strive to getting our anonymous code highlighted and compiled in the same place, and here shines a light, a“Geek light”.
- Each time you try to compile your code you need to do the following time wasting operation:
- The “Visual Studio Geek” way:
- We will not be able to do exactly what “Notepad++” does, but we will try to get closer as much as we can.
- For assembly language case:
- Check this link http://kipirvine.com/asm/gettingStarted/index.htm#syntax
- It describes how to highlight assembly keywords like registers names (e.i EAX), assembly types (e.i DWORD), MASM instructions (e.i OFFSET).
- Hey ! enough bullets, I don`t write assembly, this post is useless.
- It is expected that one will say so, assembly case was just an example that gets us closer to the solution.
- Lets not talk about assembly anymore, I will talk about a personal experience with Windows Programming through Win32 API which I hope will make everything clear.
- Win32 API nightmare
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { MessageBox(0, TEXT("Hello World!"), TEXT("Hello"), 0); return 0; }
- LPSTR is a pointer to a long string of characters, you needn’t know about Windows Programming right now, we just focus on the bigger concept right here.
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { MessageBox(0, TEXT("Hello World!"), TEXT("Hello"), 0); return 0; }
- this is the file which Irvine told to put in C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE
- Open this file using Notepad, Surprise ! the file is no more than assembly keywords, and here is a screen shot
- For those who are not familiar with assembly “ah, al, ax, bh” are names of assembly registers used in the code.
- This file should contain the user-defined keywords which Visual Studio should highlight.
- Each keyword should be provided on a separate line, with no spaces inside the keyword.
- Assume that we want Visual Studio to highlight the following C++ class names when they appear in our code: cin, cout, string
- Do the following:
- Open a Visual Studio C++ console application project and write the following:
int main() { string str; getline(cin, str); cout << str; return 0; }
- Open “usertype.dat” using Notepad.
- Write cin, cout, string each on a seperate line.
- Press Ctrl+S to save changes to the text file and close it.
- Open Visual Studio, then from the main menu choose Tools –> Options –> Fonts and Colors.
- From the “Display items” list, choose “User Keywords”, now you should see the following.
- Change the color of user keyword from “Item foreground” list box, and then press OK.
- If you are using a custom color with Red = 43, Green = 145, Blue = 175 then your code should now looks like the following:
int main() { string str; getline(cin, str); cout << str; return 0; }
- Open a Visual Studio C++ console application project and write the following:
- The same can be done with Windows Programming environment, you can define keywords and types such as HINSTANCE, WINAPI or LPSTR.
- We can now see our keywords highlighted and complied at the same place.
- You can highlight the keywords of a library you use (e.i Windows programming, STL).
- It is easy to notice that Visual Studio doesn’t provide a powerful custom highlighting capabilities compared to those provided by “Notepad++”.
- User defined keywords are to be added manually, and this is very boring for large libraries to include all their types.
- There is always a decision to make, most of the time we find that not all choices are equal, and this is life.
- Here we have to choose between 2 things:
- Localizing code highlighting and compiling inside Visual Studio.
- Code highlighting and compiling are separated, you compile in Visual Studio and watch your code in “Notepad++”, but you get more powerful highlighting capabilities.
- Don’t let others decide for you, simply because in each situation you know what is the most proper choice, and this what is called decision making.