Hack 12. Customize Syntax Coloring
Specify the keywords you want Visual Studio to
colorize, even in PHP!
When
working with Visual Studio, you will
notice that language keywords are highlighted in blue. This coloring
makes code more readable. If you ever want to see the impact that
this coloring has, open a code file in Notepad and notice how much
less readable it is.
Unfortunately, most languages, including VB.NET and C#,
won't let you change how
keywords
are colored in Visual Studio. One language that does provide a method
for specifying your own keywords is C++. For example, you
can add the word "DateTime" as a
keyword to C++ with a few simple steps. First, you need to create a
file named
usertype.dat. This is a simple text file that contains
all of the words that you want to be colored as keywords.
Start by creating a new text file in your favorite text editor, add
the word DateTime to the top line of that file,
then save that file with the name of usertype
and the extension of .dat. Here is what your
file should look like:
DateTime
Next, copy this file to the following directory: <Visual
Studio Directory>\Common7\IDE (for example,
C:\Program Files\Microsoft Visual Studio .NET
2003\Common7\IDE).
Before Visual Studio will read this file, you need to restart the
IDE; after the restart, you can open up a C++ source code file, and
you should see DateTime colored as a keyword. You
can then add additional keywords, each on its own line, and the IDE
will pick these words up and treat them as keywords. Customizing the
words that are colored is a great way to get confirmation that you
typed the correct word and also increases the readability of your
code.
2.8.1. Hacking the Hack
If you wanted all of your
classes to be colored, it would take a
quite a bit of time to add all of them to the
usertype.dat file. Steve King has written a nice
add-in that will look through your entire solution, add all of your
classes to a temporary usertype.dat file, then
load that file with Visual Studio. This way, you can have all of your
classes colored without the labor of doing it yourself. The only
downside to this add-in is that this file is not saved when you close
the IDE, so you will need to run this command every time you open the
IDE. (It was written this way to avoid overwriting any changes to the
original usertype.dat file.)
This add-in actually does a number of other things that might be of
interest to C++ developers, including adding regions, dependency and
inheritance graphs, and the ability to search the Web.
The add-in can be downloaded from http://www.codeproject.com/macro/kingstools.asp.
2.8.2. Add Coloring for Other Languages
Many people
use
Visual Studio to edit languages that it was never intended to edit.
Out of the box, Visual Studio won't do syntax
highlighting for these languages, but you can fix this with a small
registry hack and a custom usertype.dat file.
The first part of this hack is to hack the registry and tell Visual
Studio to associate the language's filename
extension with C++. It may seem a little strange to add the extension
as a C++ extension, but this is the only way to get the IDE to use
the custom
usertype.dat file, since only C++ will use the
usertype.dat file.
This example adds PHP as a supported language. First, you
need to edit the registry and add the .php
extension to the list of C++ file extensions.
|
Always back up your registry by exporting the key you are modifying
before making modifications to it, or use the alternate registry
method [Hack #30] .
|
|
To add the .php extension, create a new string
value key under the File Extensions folder located here:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\7.1\
Languages\File Extensions
|
Replace 7.1 with
the version of Visual Studio that you are working with:
7.0 for Visual Studio .NET 2002, 7.1 for Visual Studio .NET 2003, and
8.0 for Visual Studio 2005.
|
|
The key needs to be named .php and the value of
the key should be:
{B2F072B0-ABC1-11D0-9D62-00C04FD9DFD9}. (Make this
easy on yourself by copying this GUID from the .c
key.) After this key is added, the .php
extension will be treated as a C++ file, and any keywords entered in
the usertype.dat file will be appropriately
highlighted.
The next step is to add all of the PHP language keywords to the
usertype.dat file. These keywords are shown in
Table 2-2.
Table 2-2. PHP keywords list
abstract
|
array
|
as
|
boolean
|
break
|
case
|
class
|
default
|
define
|
do
|
echo
|
else
|
elseif
|
empty
|
eval
|
exit
|
extends
|
false
|
for
|
foreach
|
function
|
global
|
if
|
implements
|
in
|
include
|
include_once
|
instanceof
|
interface
|
new
|
null
|
php
|
php3
|
phpinfo
|
print
|
private
|
protected
|
public
|
require
|
require_once
|
return
|
static
|
switch
|
true
|
unset
|
var
|
while
| |
You will need to add each of these keywords on its own line to
usertype.dat, along with any other C++ keywords
you may have defined. After you create this file and save it to the
<Visual Studio Directory>\Common7\IDE
directory, all the PHP language keywords will be highlighted whenever
you open a .php file in Visual Studio.
Thanks to a blogger named David Cumps for discovering this method.
You can also download a .reg file to create the
registry key and a prefilled usertype.dat file
from his blog, located at http://weblogs.asp.NET/cumpsd/archive/2004/02/22/77926.aspx.
|
No comments:
Post a Comment