Introduction
Insert the following code to your macro dsm file and run the AccessorMutator macro to use it.
Function FileType (ByVal doc)
ext = doc.Name
FileType = 0
pos = Instr(ext, ".")
if pos > 0 then
Do While pos <> 1
ext = Mid(ext, pos, Len(ext) - pos + 1)
pos = Instr(ext, ".")
Loop
ext = LCase(ext)
end if
If ext = ".rc" Or ext = ".rc2" Then
FileType = 4
ElseIf ext = ".h" Then
FileType = 1
ElseIf ext = ".cpp" Then
FileType = 2
ElseIf doc.Language = dsJava Then
FileType = 3
ElseIf doc.Language = dsIDL Then
FileType = 4
ElseIf doc.Language = dsHTML_IE3 Or doc.Language = dsHTML_RFC1866 Then
FileType = 5
ElseIf doc.Language = dsVBSMacro Then '
FileType = 6
ElseIf ext = ".def" Then
FileType = 7
Else
FileType = 0
End If
End Function
Function GetClassName()
'----------------------------------------
'DESCRIPTION: Gets the name of the class.
'Made by Vitaly Belman
'----------------------------------------
set Doc = ActiveDocument.Selection
if Doc.FindText("class \:w$\~;", dsMatchRegExp) = False then 'Regular classes
Doc.FindText("class \:w\:b+"), dsMatchRegExp 'Derived classes
else
Doc.CharRight
end if
Doc.CharRight
Doc.CharLeft
Doc.WordLeft DsExtend
GetClassName = Doc
End Function
Sub Jump()
'DESCRIPTION: toggles between CPP and H files in both directions with QuickFind
'I used Michael Pelts' code for it.
on error resume next
Dim myDocument
Dim a
Dim b
Dim Flag
Dim Flag1
lookFor = ActiveDocument.Selection
Flag1 = 0
Flag = 1
a = ActiveDocument.FullName
tmp = InStr(a, ".cpp")
If tmp Then
b = Left(a, Len(a) - 3) + "h"
Flag1 = 1
Else
tmp = InStr(a, ".h")
If tmp Then
b = Left(a, Len(a) - 1) + "cpp"
Flag1 = 1
End If
End If
fileName = LCase(b)'first try lower case
Documents.Open fileName
if Err.Number <> 0 then
Err.Clear
fileName = UCase(fileName)'try upper case
Documents.Open fileName
if Err.Number <> 0 then
MsgBox Err.Description, 0 , "Error"
exit sub
End If
End If
if Len(lookFor) > 0 then
curLine = ActiveDocument.Selection.CurrentLine
curCol = ActiveDocument.Selection.CurrentColumn
lookForDef = "::" + lookFor
ActiveDocument.Selection.FindText lookForDef, dsMatchCase + dsMatchWord
if ActiveDocument.Selection = lookForDef then
curLine = ActiveDocument.Selection.CurrentLine
curCol = ActiveDocument.Selection.CurrentColumn - Len(ActiveDocument.Selection)
End if
ActiveDocument.Selection.Cancel
ActiveDocument.Selection.MoveTo curLine, curCol
ActiveDocument.Selection.FindText lookFor, dsMatchCase + dsMatchWord
End if
End Sub
Sub AccessorMutator
'----------------------------------------
'DESCRIPTION: Creates the Acc/Mut text.
'Made by Vitaly Belman
'----------------------------------------
if FileType(ActiveDocument) <> 1 then msgbox("You can use this macro only when header file is the active ActiveDocument.Selectionument."):Exit Sub
VarName = InputBox("Enter variable name.")
VarKind = InputBox("Enter variable type.")
ClassName = GetClassName()
ActiveDocument.Selection.SelectAll
ActiveDocument.Selection.LineDown
ActiveDocument.Selection.NewLine
ActiveDocument.Selection = VarKind & " " & VarName & "(); //Accessor"
ActiveDocument.Selection.NewLine
ActiveDocument.Selection = "void Set" & VarName & "(" & VarKind & " " & VarName & "); //Mutator"
Jump()
ActiveDocument.Selection.SelectAll
ActiveDocument.Selection.LineDown
ActiveDocument.Selection.NewLine
ActiveDocument.Selection = VarKind & " " & ClassName & "::" & VarName & "() //Accessor"
ActiveDocument.Selection.NewLine
ActiveDocument.Selection = "{"
ActiveDocument.Selection.NewLine
ActiveDocument.Selection.Indent
ActiveDocument.Selection = "return m_" & VarName & ";"
ActiveDocument.Selection.NewLine
ActiveDocument.Selection.BackSpace
ActiveDocument.Selection = "}"
ActiveDocument.Selection.NewLine
ActiveDocument.Selection.NewLine
ActiveDocument.Selection = "void " & " " & ClassName & "::" & "Set" & VarName & "(" & VarKind & " " & VarName & "); //Mutator"
ActiveDocument.Selection.NewLine
ActiveDocument.Selection = "{"
ActiveDocument.Selection.NewLine
ActiveDocument.Selection.Indent
ActiveDocument.Selection = "m_" & VarName & " = " & VarName & ";"
ActiveDocument.Selection.NewLine
ActiveDocument.Selection.BackSpace
ActiveDocument.Selection = "}"
End Sub
Please notice that I am using the Default Indent option in Visual C++ and not the Smart Indent like many people do, thus it might cause some weird indents. I suggest switching to Default Indent or removing the unneeded indents in the code.
Vitaly Belman
|