Posted in General

Visual Studio .NET “Code Complete” Macro FREE

Ok i think the version 1.0 of the Macro i was thinking my my mind is ready for using/modifcation/stealing/sharing now. What new features does it include? It will replace a shortcut(say ‘fi’) with code block (a for i loop). User can configure his own list of code blocks and shortcuts assigning them with file types. Code blocks may contain multiple variables with default values, in such cases a window will pop up asking the user for value to be replaced in case of variables. Macro will take that user entered value and replace variable with text in the code block. This will be written into the files.

Copyright? Ok this is it, anyone can use, share, copy, modify, steal and whatever you call it ! Cost?? Ok give $100 to charity, if you can, otherwise give $0 for charity. Yes, its absolutely FREE for all. I am not responsible for any miss use or failures.

The below is the code…

CodeCompletion.vb


Imports EnvDTE
Imports System.Diagnostics
Imports System.Xml
'Imports System.Drawing
'Imports System.Drawing.Design
Imports System.Data
Imports Microsoft.Win32 'Used to read registry
Imports System.Windows.Forms
Imports System.Collections
Imports System.ComponentModel

Public Module CodeCompletion

#Region "Config Settings"
'If you don't want caching, set it to false
Private Const CacheXmlInMemory As Boolean = False
Private Const CursorText As String = "~~"
#End Region

'''Completes the code by replacing your shortcut with the code block available in your configuration
Public Sub Complete()
'DTE.Events.MiscFilesEvents()
Dim selection As TextSelection = DTE.ActiveDocument.Selection
Dim startPt As EditPoint = selection.TopPoint.CreateEditPoint()
Dim endPt As TextPoint = selection.BottomPoint
'selection.MoveToDisplayColumn(endPt.Line, endPt.DisplayColumn - 2)
Dim endCol As Integer = endPt.DisplayColumn
'MessageBox.Show(selection.Text.Length)
If selection.Text.Length = 0 Then selection.WordLeft(True, 1)

'Replacement will occur here
ApplyTemplate(selection)
'startPt.MoveToAbsoluteOffset(-1) 'selection.Text.IndexOf("~~"))
End Sub

Private configForm As CodeCompletionConfig
'''Confiures your Code snippets
Public Sub Configure()
If Not configForm Is Nothing Then Exit Sub
configForm = New CodeCompletionConfig
configForm.CodeTemplateFileName = CodeTemplateFileName
configForm.LoadDataSet()
Application.Run(configForm)
configForm = Nothing
End Sub

#Region "Helper functions"
'Get Xml document from Cache
Private CodeTemplateXmlDocument As XmlDocument = Nothing

#Region "Get Code Template Xml Document"

#Region "Template File Name"
Private _codeTemplateFileName As String = ""
Private ReadOnly Property CodeTemplateFileName() As String
Get
If _codeTemplateFileName = String.Empty Then
Dim pRegKey As RegistryKey = Registry.LocalMachine
pRegKey = pRegKey.OpenSubKey("SOFTWARE\\Microsoft\\VisualStudio\\7.1")
Dim folder As Object = pRegKey.GetValue("InstallDir")
CodeTemplateXmlDocument = New XmlDocument
_codeTemplateFileName = folder & "CodeTemplates.xml"
End If
Return _codeTemplateFileName
End Get
End Property
#End Region

Private Sub GetXmlDocument()
If Not CodeTemplateXmlDocument Is Nothing Then If Not CodeTemplateXmlDocument.InnerXml = "" Then Exit Sub

CodeTemplateXmlDocument = New XmlDocument
CodeTemplateXmlDocument.Load(CodeTemplateFileName)
'I dont know why this does not load 1st time
If CodeTemplateXmlDocument.InnerXml = "" Then CodeTemplateXmlDocument.Load(CodeTemplateFileName)
End Sub
#End Region

Private Sub ApplyTemplate(ByVal selection As TextSelection)
'Get Xml Document
GetXmlDocument()

'If template is NOT available then exit
Dim codeTemplateNode As XmlNode = GetCodeTemplateNode(selection)
If codeTemplateNode Is Nothing Then Exit Sub

'If template is available then replace
Dim completeReplacementText As String = codeTemplateNode.SelectSingleNode("Code").InnerText()

Dim variableNodes As XmlNodeList = codeTemplateNode.SelectNodes("Variable")
If variableNodes.Count > 0 Then
IsAborted = True
completeReplacementText = GetReplacementText(completeReplacementText, variableNodes)
If IsAborted Then
Exit Sub
End If
End If
Dim replacementText As String = completeReplacementText.Replace(CursorText, "")
selection.Text = replacementText
If Not CacheXmlInMemory Then CodeTemplateXmlDocument = Nothing

Dim startPt As EditPoint = selection.TopPoint.CreateEditPoint()
selection.MoveToDisplayColumn(startPt.Line, startPt.DisplayColumn - (replacementText.Length - completeReplacementText.IndexOf(CursorText)))
End Sub

Private Function GetCodeTemplateNode(ByVal selection As TextSelection) As XmlNode
'Get File Extension
Dim fileName As String = DTE.ActiveDocument.Name
Dim extension As String = fileName.Substring(fileName.LastIndexOf(".") + 1)

'Prepare xPath
Dim xPath As String = String.Format("/CodeTemplates/CodeTemplateSet[FileType[@Extension='{0}']]/CodeTemplate[@ShortCut='{1}']", extension, selection.Text.Trim())
Return CodeTemplateXmlDocument.SelectSingleNode(xPath)
End Function

Private variableEntryForm As CodeCompletionVariableEntry
Private IsAborted As Boolean = True
Private Function GetReplacementText(ByVal replacementText As String, ByVal variableNodes As XmlNodeList) As String
'If Not variableEntryForm Is Nothing Then Return replacementText
variableEntryForm = New CodeCompletionVariableEntry
variableEntryForm.CodeTemplateVariablesNodes = variableNodes
variableEntryForm.ReplacementText = replacementText
variableEntryForm.AddVariableControls()
Application.Run(variableEntryForm)
replacementText = variableEntryForm.ReplacementText
If variableEntryForm.DialogResult = DialogResult.OK Then
IsAborted = False
End If
'Kill The form
variableEntryForm = Nothing
Return replacementText
End Function
#End Region

End Module

CodeCompletionConfig.vb


Imports System.Data
Imports System.Windows.Forms
Imports System.Xml

Public Class CodeCompletionConfig
Inherits System.Windows.Forms.Form

#Region "Windows Form Designer generated code"

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents LoadBtn As System.Windows.Forms.Button
Friend WithEvents SaveBtn As System.Windows.Forms.Button
Friend WithEvents CancelBtn As System.Windows.Forms.Button
Friend WithEvents ExitBtn As System.Windows.Forms.Button
Friend WithEvents Grid As System.Windows.Forms.DataGrid
Friend ds As DataSet
Private Sub InitializeComponent()
Me.ds = New System.Data.DataSet
Me.Grid = New System.Windows.Forms.DataGrid
Me.LoadBtn = New System.Windows.Forms.Button
Me.SaveBtn = New System.Windows.Forms.Button
Me.CancelBtn = New System.Windows.Forms.Button
Me.ExitBtn = New System.Windows.Forms.Button
CType(Me.ds, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.Grid, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'ds
'
Me.ds.DataSetName = "NewDataSet"
Me.ds.Locale = New System.Globalization.CultureInfo("en-AU")
'
'Grid
'
Me.Grid.DataMember = ""
Me.Grid.DataSource = Me.ds
Me.Grid.Dock = System.Windows.Forms.DockStyle.Bottom
Me.Grid.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.Grid.Location = New System.Drawing.Point(0, 46)
Me.Grid.Name = "Grid"
Me.Grid.Size = New System.Drawing.Size(736, 392)
Me.Grid.TabIndex = 0
'
'LoadBtn
'
Me.LoadBtn.Font = New System.Drawing.Font("Lucida Sans", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.LoadBtn.Location = New System.Drawing.Point(8, 8)
Me.LoadBtn.Name = "LoadBtn"
Me.LoadBtn.Size = New System.Drawing.Size(144, 32)
Me.LoadBtn.TabIndex = 1
Me.LoadBtn.Text = "&Load"
'
'SaveBtn
'
Me.SaveBtn.Font = New System.Drawing.Font("Lucida Sans", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.SaveBtn.Location = New System.Drawing.Point(200, 8)
Me.SaveBtn.Name = "SaveBtn"
Me.SaveBtn.Size = New System.Drawing.Size(144, 32)
Me.SaveBtn.TabIndex = 2
Me.SaveBtn.Text = "&Save"
'
'CancelBtn
'
Me.CancelBtn.Font = New System.Drawing.Font("Lucida Sans", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.CancelBtn.Location = New System.Drawing.Point(392, 8)
Me.CancelBtn.Name = "CancelBtn"
Me.CancelBtn.Size = New System.Drawing.Size(144, 32)
Me.CancelBtn.TabIndex = 3
Me.CancelBtn.Text = "&Cancel"
'
'ExitBtn
'
Me.ExitBtn.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.ExitBtn.Font = New System.Drawing.Font("Lucida Sans", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.ExitBtn.Location = New System.Drawing.Point(584, 8)
Me.ExitBtn.Name = "ExitBtn"
Me.ExitBtn.Size = New System.Drawing.Size(144, 32)
Me.ExitBtn.TabIndex = 4
Me.ExitBtn.Text = "E&xit"
'
'ConfigForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(7, 19)
Me.CancelButton = Me.ExitBtn
Me.ClientSize = New System.Drawing.Size(736, 438)
Me.Controls.Add(Me.ExitBtn)
Me.Controls.Add(Me.CancelBtn)
Me.Controls.Add(Me.SaveBtn)
Me.Controls.Add(Me.LoadBtn)
Me.Controls.Add(Me.Grid)
Me.Font = New System.Drawing.Font("Comic Sans MS", 10.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Name = "ConfigForm"
Me.Text = "Configuration"
CType(Me.ds, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.Grid, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

#Region "Events"
Public CodeTemplateFileName As String '= "C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE\CodeTemplates.xml"

Private Sub ExitBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExitBtn.Click
Close()
End Sub

Private Sub LoadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoadBtn.Click
LoadDataSet()
End Sub

Public Sub LoadDataSet()
ds = New DataSet
ds.ReadXml(CodeTemplateFileName)
Grid.DataSource = ds
If ds.Tables.Count > 0 Then Grid.DataMember = ds.Tables(0).TableName
End Sub

Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
ds.WriteXml(CodeTemplateFileName, XmlWriteMode.WriteSchema)
End Sub

Private Sub CancelBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelBtn.Click
ds.RejectChanges()
LoadDataSet()
End Sub
#End Region

End Class

CodeCompletionVariableEntry.vb


Imports System.Xml
Imports System.Windows.Forms

Public Class CodeCompletionVariableEntry
Inherits System.Windows.Forms.Form

#Region "Add variable Controls"
Public CodeTemplateVariablesNodes As XmlNodeList
Public ReplacementText As String = "for(int ~0~ = 0; ~0~ 0 Then Me.ActiveControl = VariablesGroupBox.Controls(1)
End Sub
#End Region

#Region "Windows Form Designer generated code"

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents OkBtn As System.Windows.Forms.Button
Friend WithEvents CancelBtn As System.Windows.Forms.Button
Friend WithEvents VariablesGroupBox As System.Windows.Forms.GroupBox
Private Sub InitializeComponent()
Me.OkBtn = New System.Windows.Forms.Button
Me.CancelBtn = New System.Windows.Forms.Button
Me.VariablesGroupBox = New System.Windows.Forms.GroupBox
Me.SuspendLayout()
'
'OkBtn
'
Me.OkBtn.DialogResult = System.Windows.Forms.DialogResult.OK
Me.OkBtn.Font = New System.Drawing.Font("Lucida Sans", 9.75!, System.Drawing.FontStyle.Bold)
Me.OkBtn.Location = New System.Drawing.Point(16, 8)
Me.OkBtn.Name = "OkBtn"
Me.OkBtn.Size = New System.Drawing.Size(128, 23)
Me.OkBtn.TabIndex = 0
Me.OkBtn.Text = "&Ok"
'
'CancelBtn
'
Me.CancelBtn.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.CancelBtn.Font = New System.Drawing.Font("Lucida Sans", 9.75!, System.Drawing.FontStyle.Bold)
Me.CancelBtn.Location = New System.Drawing.Point(160, 8)
Me.CancelBtn.Name = "CancelBtn"
Me.CancelBtn.Size = New System.Drawing.Size(112, 23)
Me.CancelBtn.TabIndex = 1
Me.CancelBtn.Text = "&Cancel"
'
'VariablesGroupBox
'
Me.VariablesGroupBox.Font = New System.Drawing.Font("Lucida Sans", 9.75!, System.Drawing.FontStyle.Bold)
Me.VariablesGroupBox.Location = New System.Drawing.Point(16, 40)
Me.VariablesGroupBox.Name = "VariablesGroupBox"
Me.VariablesGroupBox.Size = New System.Drawing.Size(264, 216)
Me.VariablesGroupBox.TabIndex = 2
Me.VariablesGroupBox.TabStop = False
Me.VariablesGroupBox.Text = "Variables"
'
'CodeCompletionVariableEntry
'
Me.AcceptButton = Me.OkBtn
Me.AccessibleDescription = ""
Me.AccessibleName = ""
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.CancelButton = Me.CancelBtn
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.VariablesGroupBox)
Me.Controls.Add(Me.CancelBtn)
Me.Controls.Add(Me.OkBtn)
Me.Name = "CodeCompletionVariableEntry"
Me.Text = "Variables Entry"
Me.ResumeLayout(False)

End Sub

#End Region

#Region "Event Handlers"
Private Sub Cancel(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelBtn.Click
Me.Close()
End Sub

Private Sub SetReplacementText(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OkBtn.Click
Dim i As Integer
For i = 0 To CodeTemplateVariablesNodes.Count - 1
ReplacementText = ReplacementText.Replace(String.Format("~{0}~", i), VariablesGroupBox.Controls(i * 2 + 1).Text)
Next
'MessageBox.Show(ReplacementText)
Me.Close()
End Sub
#End Region

End Class

CodeTemplates.xml


I will post soon, XML doesn't seem to be displayed properly here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s