Showing posts with label templates. Show all posts
Showing posts with label templates. Show all posts

2007-10-11

Changing Visual Studio Item Templates

So, Visual Studio is pretty great right? It makes a lot of things really easy, really automated... saves a lot of typing, etc... However, there are still some areas where I find myself repetatively doing the same thing in certain scenarios.

For example -- Making a new class.

Now, I usually just right-click on my project list, Add..., New Item, then I'm presented with an array of the available Item Templates. So, I'll select Class, rename it, and then I'm presented with this:


using System;
using System.Collections.Generic;
using System.Text;

namespace MyNamespace
{
class Class1
{
}
}


And of course, the first thing I will do, is change the class name, set it to public, create an empty constructor, define code regions to keep things organized... and end up with something like this:


using System;
using System.Collections.Generic;
using System.Text;

namespace MyNamespace
{
public class Class1
{

#region Constructors
public Class1()
{
Init();
}

///
/// Initializes field values.
///

private void Init()
{
}
#endregion Constructors

#region Fields
#endregion Fields

#region Properties
#endregion Properties

#region Public Methods
#endregion Public Methods

#region Private Methods
#endregion Private Methods
}
}


WHOA! That takes quite a few minutes of typing... FOR EVERY CLASS I WRITE!!

So, I decided to go figure out how those templates work.

Where are those files at?

The templates are stored in your Visual Studio installation directory. If you're like me, and running a fairly recent version of Visual Studio 2005, installed with default configuration, your install directory will probably be:

C:\Program Files\Microsoft Visual Studio 8\

and the templates are stored in:


C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates


Under that directory you'll find subdirectories for each classification of template (CSharp, JSharp, VisualBasic, etc.. ), and in each of those subdirectories, you'll find a zip file for each template.

For what I want to fix, I am looking for this file:


C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Class.zip

*sidenote: Notice that the last subdirectory in the path is the language code 1033 (English) and if you've installed windows/vs with a different language, this will be different.. this is one reason you may not see your default templates when working across languages (ie, windows is in spanish, and visual studio is installed from the English language version, but is configured to be in spanish.. templates will be missing!!


In the Class.zip file, you will find two files; Class.cs and Class.vstemplate

Class.vstemplate
Class.vstemplate is just an XML file. This contians information about the template, such as what GUID to lookup for the icon to display in the wizard screen, what the sorting order is, what assemblies it references, etc.. For the most part, unless your doing something more complicated than what I want to do, you won't need to edit this. One tag to pay attention to is:

<ProjectItem ReplaceParameters="true">Class.cs</ProjectItem<

This tag says that VS should make a new project item based on Class.cs, and it should parse the file and replace the parameter tokens in it with the appropriate values... So let's look at Class.cs:


Class.cs
Class.cs is the templated code file. The default file looks like this:


using System;
using System.Collections.Generic;
using System.Text;

namespace $rootnamespace$
{
class $safeitemrootname$
{
}
}


So this is the normal new empty class, and the tokens $rootnamespace and $safeitemrootname$ are what will get replaced when VS parses the file and passes in the parameters.


Well, I don't know anything about those parameters... So I'm not going to mess with them. However, I did go make a list of the parameters I found in the default templates ( I could not find a list of these paramaters on the net anywhere...)


$rootnamespace$
$safeitemrootname$
$registeredorganization$
$year$
$guid1$
$ContentTags$
$MasterPage$
$fileinputname$
$classname$
$safeitemname$


So I modified Class.cs to be:


using System;
using System.Collections.Generic;
using System.Text;

namespace $rootnamespace$
{
public class $safeitemrootname$
{
#region Constructors
public $safeitemrootname$()
{
Init();
}

/// <summary>
/// Initializes field values.
/// </summary>
private void Init()
{
}
#endregion Constructors

#region Fields
#endregion Fields

#region Properties
#endregion Properties

#region Public Methods
#endregion Public Methods

#region Private Methods
#endregion Private Methods
}
}

Yay! my fingers are saved... but what you say? all my base are NOT belong to us? True. There is a cache.


Refreshing the Visual Studio Template Cache


The template files are cached in a folder called TemplateItemCache in the same location as the template folder. So..


  1. Close all Visual Studio Windows
  2. Clear contents of TemplateCache folder
  3. Open a DOS prompt (normal one, or Visual Studio 2005 Command Prompt)
  4. Run devenv /installvstemplates (if that doesn't work, run devenv /setup)


Now, when you restart Visual Studio, your new templates will be installed...


BUT! the fun doesn't stop here! The same templating structure for items also applies to projects! But that's next post...

2007-05-09

Java, NetBeans, and Templates, OH MY!

Well, having recently sparked an interest in moving towards a Open Source, cross platform, but still as cool as c#/VS2005 development platform, I of course landed in the middle of NetBeans 5.5 and Java.

Having never programmed in Java before, but understanding it's really similar to c# (or I should say c# is really similar to Java), I immediately started fiddling about as if I were writing c# code. So, it's easy to get past typing uppercase String, not lowercase, and also not too hard to grok "extends" instead of ":" for inheritance. The one-class-per-file thing, well, I guess it will just make me a more organized programmer, however annoying it is. But the things that really erked me was properties.

In c# I can do this:

...
private string _name;

public string Name
{
get
{
return this._name;
}
set
{
this._name = value;
}
}
...

but in Java, that looks like:

...
private String _name;

public String getName()
{
return this._name;
}

public void setName(string value)
{
this._name = value;
}
...


Wow. Extremely obnoxious. Furthermore, I have finally gotten myself broken in with the VS2005 IDE to type "prop" + TAB to get a nice template for my properties. Well, since there is no such thing in Java, this macro also does not exist. So, I proceeded to make a NetBeans code template called "prop", which functions the same way the VS2005 "prop" code snippet does.

So, for all you c# coders who are venturing into the foreign lands of Java, here's a little tutorial on how to add this little cultural comfort into NetBeans.


Property Code Template Installation Instructions:

1. Select menu item "Tools->Options".
2. Click on "Editor" sidebar button.
3. Click on "Code Templates" tab.
4. Select "Java" from languages combo-box.
5. Click "New", and then enter "prop" as the Abbreviation in the dialog.
6. Click "Ok".
7. Make sure "prop" is the selected template, and in the text box below the list, enter these lines:

private ${int} ${_prop};

public ${int} get${Property}()
{
return this.${_prop};
}

public void set${Property}(${int} value)
{
this.${_prop} = value;
}

8. Select "Tab" from "Expand On" combo box.
9. Click "OK".


Now you've got it installed.. Feel free to go to the code and give it a whirl! Have a look at the other macros in the list to see what's built in, and once you figure out the syntax of the template notation, make your own templates!