| < Free Open Study > |
|
The following code retrieves a whole procedure from a module. The function expects that the cursor lies somewhere within the procedure to be retrieved. You can find this code in the Utilities class in the code for Chapter 12. It uses the TextSelection and EditPoint objects. It will retrieve any comments that immediately precede the procedure definition line.
Public Function GetWholeProc() As String Dim ts As TextSelection = oVB.ActiveWindow().Selection Dim ep As EditPoint = ts.ActivePoint.CreateEditPoint Dim sLine As String Dim i As Integer Dim sCommentChar As String Try sCommentChar = Me.GetCommentCharForDoc(oVB.ActiveDocument) sCommentChar = Left(sCommentChar, 1) If sCommentChar = "/" Then If Len(ts.Text) = 0 Then MsgBox("For a C#/C++ project you must select the whole proc.") Return "" End If End If ' if the user has selected the whole proc, ' then just return it ' otherwise select it for them... If Len(ts.Text) > 0 Then If (InStr(1, ts.Text, "Sub ", 1) > 0 Or _ InStr(1, ts.Text, "Function ", 1) > 0) And _ (InStr(1, ts.Text, "End Sub", 1) > 0 Or _ InStr(1, ts.Text, "End Function", 1) > 0) _ Then Return ts.Text End If GoTo SelectTheProc Else SelectTheProc: " Get the start of the proc ep.MoveToPoint(ep.CodeElement(EnvDTE.vsCMElement. vsCMElementFunction).GetStartPoint (vsCMPart.vsCMPartWhole)) ' move selection start point to top of proc ts.MoveToPoint(ep, False) ' back up to previous line looking for comments i = 0 Do ep.LineUp() ts.MoveToPoint(ep, False) ts.SelectLine() sLine = ts.Text If Left(Trim(sLine), 1) <> sCommentChar Then ep.LineDown() ts.MoveToPoint(ep, False) Exit Do End If i = i + 1 Loop ' if the count of comment lines > 0 the ' ts point is set properly ' else we must move it back to the original ep.LineDown(i + 1) ' move to bottom of proc ep.MoveToPoint(ep.CodeElement(EnvDTE.vsCMElement. vsCMElementFunction).GetEndPoint( vsCMPart.vsCMPartWhole)) ' select the proc ts.MoveToPoint(ep, True) Return ts.Text End If Catch e As System.Exception System.Windows.Forms.MessageBox.Show( "You must either select " & _ "the whole procedure or your cursor must be within the procedure " & _ "to be selected. " & e.Message) Return "" End Try End Function
| < Free Open Study > |
|