Saturday, March 19, 2005

AccessorsCreator for VS

Imports EnvDTE
Imports System.Diagnostics

Public Module AccessorsCreator
Sub createAccessor()
Dim selected As String = DTE.ActiveDocument().Selection.text()
If (selected Is Nothing Or selected.Length = 0) Then
Exit Sub
End If

selected = selected.Replace(",", "")
selected = selected.Replace(";", "")
Dim parts() As String = selected.Split(" ")
Dim memberCount As Integer = parts.Length - 2

Dim basicOutput As String = "public $TYPE$ $PROP_NAME$ {" & vbCrLf _
& vbTab & "get { return $MEMBER_NAME$; }" & vbCrLf _
& vbTab & "set { $MEMBER_NAME$ = value; }" & vbCrLf _
& "}" & vbCrLf & vbCrLf

Dim results(memberCount) As String
For i As Integer = 2 To parts.Length - 1
Dim propName As String
propName = parts(i).Substring(0, 1).ToUpper() & parts(i).Substring(1)
results(i - 2) = basicOutput.Replace("$MEMBER_NAME$", parts(i))
results(i - 2) = results(i - 2).Replace("$PROP_NAME$", propName)
results(i - 2) = results(i - 2).Replace("$TYPE$", parts(1))
Next

Dim entryLine As Integer = DTE.ActiveDocument().Selection.BottomLine + 1
DTE.ActiveDocument().Selection.GotoLine(entryLine)

For i As Integer = 0 To results.Length - 1
DTE.ActiveDocument().Selection.Insert(results(i))
Next
End Sub
End Module

No comments: