Greetings,
I wanted a $ParameterList$ without default values wrapped in C-style comments, and wanted each parameter to be on its own line as well (Company style). I found it a rather common request (at least the remove-defaults one), so I came up with these macros:
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Imports System.Text.RegularExpressions
Public Module MyMacros
'' Strips all C-style comments from the current line
Public Sub StripCStyleComments()
Dim selection As TextSelection
selection = DTE.ActiveDocument.Selection
selection.SelectLine()
Dim text As String
text = selection.Text
Dim re As Regex
re = New Regex("\\s/\\*.*?\\*/")
text = re.Replace(text, "")
selection.Insert(text)
End Sub
'' Splits the parameter declarations for the function in the current line into several lines,
'' one per parameter, selects the newly inserted text and formats it (utilizing the IDE's
'' language specific smart formatting)
''
'' For example, this:
''
'' void SomeClass::SomeFunction(int x, int y, float z, std::string foo) const
''
'' Becomes:
''
'' void SomeClass::SomeFunction(
'' int x,
'' int y,
'' float z,
'' std::string foo) const
''
Public Sub SplitParameterLine()
Dim selection As TextSelection = DTE.ActiveDocument.Selection
Dim startLine As Integer = selection.ActivePoint.Line
selection.SelectLine()
' If there are no parameters, bail out
If selection.Text.IndexOf(",") = -1 Then Exit Sub
Dim leftBracketPos = selection.Text.IndexOf("(")
' If this is not a function declarator, bail out
If leftBracketPos = -1 Then Exit Sub
Dim preamble = Left(selection.Text, leftBracketPos + 1) ' left bracket included in preamble
Dim parameterPortion = Mid(selection.Text, leftBracketPos + 2) 'and excluded in parameterPortion
Dim parameters = parameterPortion.Split(",")
selection.Insert(preamble + vbCrLf + String.Join("," + vbCrLf, parameters))
' Select whatever we inserted, and format it
Dim currentLine As Integer = selection.TopPoint.Line
Dim numLines = currentLine - startLine + 1
Dim i
For i = 1 To numLines
selection.LineUp(True)
Next
selection.SmartFormat()
End Sub
'' A wrapper for VAssistX.RefactorCreateImplementation that adds the following:
'' - All C-style comments are removed from the function declarator (i.e. no default values),
'' using <StripCStyleComments>
'' - The parameters are split, each on its own line, using <SplitParameterLine>
''
Public Sub CreateImplementation()
DTE.ExecuteCommand("VAssistX.RefactorCreateImplementation")
' After CreateImplementation is done, the situation is as follows:
' FunctionDeclarator
' {
' <CURSOR>
' }
'
' So we move up two lines, and call StripCStyleComments, followed by SplitParameterLine
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.LineUp(False, 2)
StripCStyleComments()
selection.LineUp()
SplitParameterLine()
End Sub
End Module
Macros similar to CreateImplementation can be created if you feel like it--I only did that one because it's what I use frequently. You can remove the last 2 lines of CreateImplementation if you don't want to split the parameters into separate lines.
This fixes issues for the following threads: $ParameterList$ without defaults, Split function / params to fit certain col width (partially), and Move implementation, not default parameters (partially--he'll need to wrap VAssistX.RefactorMoveImplementation).
I'll add notifications in the aforementioned threads linking to this one for further discussion/customization.