Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Declarative Codesnippet Automation with T4 Templates

, 20 Apr 2011 CPOL
This article describes a technique for automating codesnippets which are associated with a class via attributes. This results in a declarative approach to the generation of boiler-plate code.
CodesnippetAutomation.zip
CodeSnippetAutomation
CodeSnippetAutomation.suo
CodeSnippetAutomation
CodeGen
Snippets
inpc.snippet
prop_inpc.snippet
CodeSnippetAutomation.csproj.user
Properties
SilverTrackProject
SilverTrack
CodeGen
Snippets
dp.snippet
inpc.snippet
prop_inpc.snippet
controls
gauge
converters
data.csv
images
addchart.png
bg.png
end.png
flag.png
info.png
playpauseicon.png
restart.png
yellowbg.png
lib
Microsoft.Expression.Controls.dll
Microsoft.Expression.Drawing.dll
Visiblox.Charts.dll
Properties
SilverTrack.csproj.user
SilverTrack.csproj.vs10x
Themes
Util
View
ViewModel
SilverTrackData
Importer
Properties
SilverTrackData.csproj.vs10x
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:s="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"
    exclude-result-prefixes="msxsl">
  <xsl:output method="text"/>

  <xsl:template match="/">
using System;

namespace Snippets
{
    <xsl:apply-templates select="//s:CodeSnippet"/>
}
  </xsl:template>

  <!-- matches a CodeSnippet element generating an attribute -->
  <xsl:template match="s:CodeSnippet">
    /// &lt;summary&gt;
    /// <xsl:value-of select="s:Header/s:Description"/>
    /// &lt;/summary&gt;
    [AttributeUsage(AttributeTargets.Class , AllowMultiple = true)]
    public class Snippet<xsl:value-of select="s:Header/s:Shortcut"/>  : Attribute
    {
    <!-- generate the attribute properties -->
    <xsl:apply-templates select="//s:Declarations/s:Literal"/>
    <!-- add the snippet code to the attribute -->
    <xsl:apply-templates select="//s:Code"/>
    }
  </xsl:template>

  <!-- generates a string property for codesnippet literal -->
  <xsl:template match="s:Literal">
        /// &lt;summary&gt;
        /// <xsl:value-of select="s:ToolTip"/>
        /// &lt;/summary&gt;
        public string <xsl:value-of select="s:ID"/> = "<xsl:value-of select="s:Default"/>";
  </xsl:template>

  <xsl:template match="s:Code">
    <!-- escape any quotes in the snippet -->
    <xsl:variable name="escaped">
      <xsl:call-template name="escapeQuot">
        <xsl:with-param name="text" select="."/>
      </xsl:call-template>
    </xsl:variable>

    <!-- add a method that returns the snippet -->
    /// &lt;summary&gt;
    /// Gets the code snippet
    /// &lt;/summary&gt;
    public string GetSnippet()
    {
    return @"<xsl:value-of select="$escaped" />";
    }
  </xsl:template>


  <!-- relpaces single quotes with double-quotes -->
  <xsl:template name="escapeQuot">
    <xsl:param name="text"/>
    <xsl:choose>
      <xsl:when test="contains($text, '&quot;')">
        <xsl:variable name="bufferBefore" select="substring-before($text,'&quot;')"/>
        <xsl:variable name="newBuffer" select="substring-after($text,'&quot;')"/>
        <xsl:value-of select="$bufferBefore"/>
        <xsl:text>""</xsl:text>
        <xsl:call-template name="escapeQuot">
          <xsl:with-param name="text" select="$newBuffer"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Colin Eberhardt
Architect Scott Logic
United Kingdom United Kingdom
I am CTO at ShinobiControls, a team of iOS developers who are carefully crafting iOS charts, grids and controls for making your applications awesome.
 
I am a Technical Architect for Visiblox which have developed the world's fastest WPF / Silverlight and WP7 charts.
 
I am also a Technical Evangelist at Scott Logic, a provider of bespoke financial software and consultancy for the retail and investment banking, stockbroking, asset management and hedge fund communities.
 
Visit my blog - Colin Eberhardt's Adventures in .NET.
 
Follow me on Twitter - @ColinEberhardt
 
-
Follow on   Twitter   Google+

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141030.1 | Last Updated 20 Apr 2011
Article Copyright 2011 by Colin Eberhardt
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid