Francesco's blog

 Sunday, November 13, 2005

Visual Studio 2005 comes with dozens of ready-to-use code snippets. You might argue on the usefulness of some of them, but for sure many of them are really well-conceived. For example, the prop expansion that creates public C# properties is a real time saver.

The Code Snippet Manager dialog box (in the Tools menu) enables you to inspect all the installed snippets, one by one, but doesn't offer the option to export a list of all the snippets, therefore you have to browse them one by one to take notice of their name, purpose, and keyboard shortcut. While I was working on chapter 4 of Programming Microsoft Visual Basic 5, I wrote this little throw-away program which decodes the snippet index and list them on a console window. Of course you can redirect the output to a file to have a document that you can use as a reference.

The program takes a parameter equal to the path of the SnippetIndex.xml (VB) or SnippetsIndex.xml (C#) file that contains the snippet index. (Oddly, this file has a slightly different name in the two languages.) If you run it without passing any argument, it uses the path of the VB snippet index in a default Visual Studio installation. A comment in the listing explains how you can use the default index for C# instead.

The output of this code is quite terse - just snippet names and shortcuts, grouped in categories - but you can easily modify the source code to extract and display more attributes.

Imports System.IO
Imports System.Xml
Imports System.Text.RegularExpressions

Module Module1
  
Dim snippetsPath As String
  
Dim catNames As New Dictionary(Of String, String)

   Sub Main(ByVal args() As String)
     
' If no argument has been provided, use default path for snippets.
     
If args.Length = 0 Then
        
args = New String() {"C:\Program Files\Microsoft Visual Studio 8\Vb\Snippets\1033\SnippetIndex.xml"}
        
' Uncomment next line to list C# snippets
        
' args = New String() {"C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\SnippetsIndex.xml"}
     
End If

      Dim snippetsFile As String = args(0)
      snippetsPath = Path.GetDirectoryName(snippetsFile)
     
' Load the snippet index file.
     
Dim xmlIndex As New XmlDocument()
      xmlIndex.Load(snippetsFile)
     
' We need two passes, because dirs and subdirs use a different XML element.
     
ParseSnippetIndex(xmlIndex, "//SnippetDir")
      ParseSnippetIndex(xmlIndex,
"//SnippetSubDir")
     
' Iterate over all the directories in the main snippet directory.
     
For Each dir As String In Directory.GetDirectories(snippetsPath)
         ParseSnippetFolder(dir,
"")
     
Next
   End Sub

   Sub ParseSnippetIndex(ByVal xmlIndex As XmlDocument, ByVal searchKey As String)
     
' Create the correspondence between relative paths and localized categories
     
For Each xmlEl As XmlElement In xmlIndex.SelectNodes(searchKey)
        
Dim elPath As XmlElement = DirectCast(xmlEl.SelectSingleNode("DirPath"), XmlElement)
        
Dim elName As XmlElement = DirectCast(xmlEl.SelectSingleNode("LocalizedName"), XmlElement)
         catNames.Add(elPath.InnerText, elName.InnerText)
     
Next
   End Sub

   Sub ParseSnippetFolder(ByVal dir As String, ByVal parentCategory As String)
     
' Retrieve the relative name of this subdirectory.
     
Dim relPath As String = dir.Substring(snippetsPath.Length)
     
' The default name for this category
     
Dim categoryName As String = parentCategory & Path.GetFileNameWithoutExtension(dir)
     
' Search this relative path in the snippet index.
     
Dim searchPath As String = "%InstallRoot%\Vb\Snippets\%LCID%" + relPath + "\"
     
If catNames.ContainsKey(searchPath) Then
        
' If found, use the localized category as appears in the index file
        
categoryName = parentCategory & catNames(searchPath)
     
End If
     
Console.WriteLine(categoryName.ToUpper())
     
' Parse individual snippets in this directory.
     
For Each file As String In Directory.GetFiles(dir, "*.snippet")
         ParseSnippetFile(file)
     
Next
     
' Parse all sub-categories
     
For Each subdir As String In Directory.GetDirectories(dir)
         ParseSnippetFolder(subdir, categoryName &
" / ")
     
Next
   End Sub

   Dim reTitle As New Regex("<Title>(.+?)</Title>")
   Dim reShortcut As New Regex("<Shortcut>(.+?)</Shortcut>")

   Sub ParseSnippetFile(ByVal snippetFile As String)
     
Dim text As String = File.ReadAllText(snippetFile)
     
' We use regexes to extract information for individual snippet files.
     
Dim maTitle As Match = reTitle.Match(text)
     
Dim maShortcut As Match = reShortcut.Match(text)
     
Dim title As String = maTitle.Groups(1).Value
     
Dim shortcut As String = maShortcut.Groups(1).Value
      Console.WriteLine(
" {0} [{1}]", title, shortcut)
  
End Sub
End
Module

Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

 
Get RSS/Atom Feed
RSS 2.0 | Atom 1.0
Search in the blog
Archive
<July 2008>
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
Categories

Powered by: newtelligence dasBlog 1.8.5223.1