Can I turn off statement completion in a macro?

  • Thread starter Thread starter Rob Richardson
  • Start date Start date
R

Rob Richardson

Greetings!

I am trying to write a set of macros to generate Property code for private
variables. With the cursor on a line that says "Private m_MyThing as
Thing", I would run the macro and

Public Property MyThing() As Thing
Get
Return m_MyThing
End Get
Set(ByVal Value As Thing)
m_MyThing = Value
End Set
End Property

would be automatically inserted into my code.

I have a macro that automatically generates a block of code that looks like
the above. The problem is that when it is inserted into my class module,
the statement completion kicks in and I get extra End Get, End Set and End
Property lines.

I would like to have my macro check the Statement Completion setting in my
IDE, remember the current setting, make sure it is off, generate the code,
insert the code, and restore the Statement Completion setting to its
original value. Is there a way to manipulate the Statement Completion
setting inside a macro?

Thanks very much!

Rob

P.S. I will be rewriting the macro to assume Statement Completion is on.
If anybody wants it, I'll be glad to supply it. If there's enough demand,
I'll just post it here.
 
Rob Richardson said:
Public Property MyThing() As Thing
Get
Return m_MyThing
End Get
Set(ByVal Value As Thing)
m_MyThing = Value
End Set
End Property

I have a macro that automatically generates a block of code that looks like
the above. The problem is that when it is inserted into my class module,
the statement completion kicks in and I get extra End Get, End Set and End
Property lines.

I have written this macro if you want it, but I assume you want to write it
your self. What method(s) are you using to insert text and to create new
lines?

~
Jeremy
 
Jeremy,

Does your macro handle the Statement Completion problem?

I'm using "sel.text = whatever_I_want_to_write". If a CrLf is encountered,
statement completion kicks in before the rest of the text is written. I got
around this by avoiding CrLfs. I write each line one at a time. After each
line, I move down, move to the start of the line, write a single CrLf to
move whatever was there out of the way, then move up a line (to the newly
blank line) and write the next line.

Rob
 
Rob Richardson said:
Jeremy,

Does your macro handle the Statement Completion problem?
Yes.

I'm using "sel.text = whatever_I_want_to_write".

I assume "sel" is an instance of DTE.ActiveDocument.Selection, in which case
you should use "Insert", not Text. I build my entire block of text first in
a temp variable ( called OutputBuffer ), then insert it, see below:


'// first, get away from the selected text
CurrentSelection.EndOfLine( )

'// add a few lines, for good measure
CurrentSelection.NewLine(3)

'// dump the buffer
CurrentSelection.Insert( OutputBuffer )

HTH,
Jeremy
 
Here is my implementation if you are interested:
(i have also attached it as a text doc)

~
Jeremy


______________________________________


Private Function GetFirstUnderscoreIndex(ByVal strString As String) As
Integer
Dim i As Integer
Dim sChr As String

For i = 1 To Len(strString)
sChr = Mid(strString, i, 1)
If sChr = "_" Then
Return i + 1
End If
Next
End Function

Sub PropertyFromVars()
Dim Scope As String = "Public"
Dim OutputBuffer As String
Dim LineTokens(), VariableTokens() As String
Dim i, SpaceIndex As Integer
Dim SelectedVariables As String = DTE.ActiveDocument.Selection.text
Dim CurrentSelection As TextSelection = DTE.ActiveDocument.Selection

Const VAR_NAME As Short = 0
Const VAR_TYPE As Short = 1

OutputBuffer = Replace(SelectedVariables, "Private ", "", , ,
vbTextCompare)
OutputBuffer = Replace(OutputBuffer, "Public ", "", , ,
vbTextCompare)
OutputBuffer = Replace(OutputBuffer, "Friend ", "", , ,
vbTextCompare)
OutputBuffer = Replace(OutputBuffer, "Protected ", "", , ,
vbTextCompare)

LineTokens = Split(OutputBuffer, vbCrLf)

OutputBuffer = ""
For i = 0 To UBound(LineTokens)
If LineTokens(i) <> "" And Mid(Trim(LineTokens(i)), 1, 1) <> "'"
Then
VariableTokens = Split(LineTokens(i), " as ", ,
vbTextCompare)

VariableTokens(VAR_TYPE) = VariableTokens(VAR_TYPE).Trim
VariableTokens(VAR_NAME) = VariableTokens(VAR_NAME).Trim

SpaceIndex = InStr(VariableTokens(VAR_TYPE), " ")
If SpaceIndex > 0 Then
VariableTokens(VAR_TYPE) = Mid(VariableTokens(VAR_TYPE),
1, SpaceIndex - 1)
End If

OutputBuffer = OutputBuffer & Scope & " Property " &
Mid(VariableTokens(VAR_NAME),
GetFirstUnderscoreIndex(VariableTokens(VAR_NAME))).Substring(0, 1).ToUpper &
Mid(VariableTokens(VAR_NAME),
GetFirstUnderscoreIndex(VariableTokens(VAR_NAME))).Substring(1) & "() As " &
VariableTokens(VAR_TYPE) & vbCrLf
OutputBuffer = OutputBuffer & " Get" & vbCrLf
OutputBuffer = OutputBuffer & " Return " &
VariableTokens(VAR_NAME) & vbCrLf
OutputBuffer = OutputBuffer & " End Get" & vbCrLf
OutputBuffer = OutputBuffer & " Set (ByVal Value As " &
VariableTokens(1) & ")" & vbCrLf
OutputBuffer = OutputBuffer & " " &
VariableTokens(VAR_NAME) & " = Value" & vbCrLf
OutputBuffer = OutputBuffer & " End Set" & vbCrLf
OutputBuffer = OutputBuffer & "End Property" & vbCrLf &
vbCrLf
End If
Next

CurrentSelection.EndOfLine()
CurrentSelection.NewLine(3)
CurrentSelection.Insert(OutputBuffer)
End Sub
 
Back
Top