Click here to Skip to main content
15,881,725 members
Articles / Programming Languages / C++

'Find Function headers' macro for Visual Studio .NET

Rate me:
Please Sign up or sign in to vote.
3.67/5 (3 votes)
28 May 2002CPOL 71.4K   283   11   4
This macro will help you when you want to quickly move between function headers in your code.

Introduction

As you know there isn't a function like WBGoToNext and WBGoToPrevious in Visual Studio .NET.
So this macro will help you when you want to quickly move between function headers in your code.

It is very simple, so there is no need comment!

VB
' FindFunction macro
'
' Writer : [Asia MVP] Smile Seo
' E-mail : seaousak@hotmail.com
'

Imports EnvDTE
Imports System.Diagnostics

Public Module FindFunction
    Sub BeginningOfFunction()
        Dim ts As TextSelection = DTE.ActiveWindow.Selection
        ts.MoveToPoint(ts.ActivePoint.CodeElement(_
               vsCMElement.vsCMElementFunction).GetStartPoint(vsCMPart.vsCMPartHeader))
    End Sub

    Sub EndOfFunction()
        Dim ts As TextSelection = DTE.ActiveWindow.Selection
        ts.MoveToPoint(ts.ActivePoint.CodeElement( _
                 vsCMElement.vsCMElementFunction).GetEndPoint(vsCMPart.vsCMPartHeader))
    End Sub

    Sub GotoFunctionHeaderUp()
        On Error GoTo ErrorHandler

        BeginningOfFunction()

        ActiveDocument().Selection.FindText("}", vsFindOptions.vsFindOptionsBackwards)
        ActiveDocument().Selection.LineUp()
        BeginningOfFunction()

ErrorHandler:
    End Sub

    Sub GotoFunctionHeaderDown()
        On Error GoTo ErrorHandler

        EndOfFunction()

        ActiveDocument().Selection.FindText("{")
        ActiveDocument().Selection.LineDown()

        BeginningOfFunction()

ErrorHandler:
    End Sub


End Module

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Korea (Republic of) Korea (Republic of)
Woo Seok Seo have been a Microsoft MVP for 7 years and have translated several books into Korean. Author of C# Programming for Beginner (DevPress, 2001), he is interested in Debugging techniques and .NET technology. Get in touch with Woo Seok Seo at wooseok.seo@gmail.com

Comments and Discussions

 
GeneralThis works pretty well... Pin
Thomas Lunsford6-Aug-03 14:06
Thomas Lunsford6-Aug-03 14:06 
Thanks for the article! The following modification works pretty well. It keeps incrmenting or decrementing the current line until it is either in a function/method or it is a the beginning/end of the file. It could use some improvement, such as turning off painting the screen while it works, but I'll have to get around to that later. I think it could cause problems if you have a text selection instead of just a point (ts.LineUp will move the line instead of the cursor).

  Sub GotoFunctionHeaderUp()<br />
    On Error GoTo ErrorHandler<br />
<br />
    Dim ts As TextSelection = DTE.ActiveWindow.Selection<br />
    Dim ce As CodeElement<br />
<br />
    Do<br />
      ts.LineUp()<br />
      ce = ts.ActivePoint.CodeElement(vsCMElement.vsCMElementFunction)<br />
    Loop While (ce Is Nothing And ts.TopPoint.Line > 1)<br />
<br />
    If (Not ce Is Nothing) Then<br />
      ts.MoveToPoint(ce.GetStartPoint(vsCMPart.vsCMPartHeader))<br />
    End If<br />
<br />
ErrorHandler:<br />
  End Sub<br />
<br />
  Sub GotoFunctionHeaderDown()<br />
    On Error GoTo ErrorHandler<br />
<br />
    Dim ts As TextSelection = DTE.ActiveWindow.Selection<br />
    Dim ce As CodeElement<br />
    Dim prevLine As Integer<br />
    Dim currLine As Integer<br />
<br />
    ce = ts.ActivePoint.CodeElement(vsCMElement.vsCMElementFunction)<br />
<br />
    If (Not ce Is Nothing) Then<br />
      ts.MoveToPoint(ce.GetEndPoint(vsCMPart.vsCMPartHeader))<br />
      ts.LineDown()<br />
    End If<br />
<br />
    Do<br />
      prevLine = ts.TopPoint.Line<br />
      ts.LineDown()<br />
      currLine = ts.TopPoint.Line<br />
<br />
      If (prevLine = currLine) Then<br />
        Exit Do<br />
      End If<br />
<br />
      ce = ts.ActivePoint.CodeElement(vsCMElement.vsCMElementFunction)<br />
    Loop While (ce Is Nothing)<br />
<br />
    If (Not ce Is Nothing) Then<br />
      ts.MoveToPoint(ce.GetStartPoint(vsCMPart.vsCMPartHeader))<br />
    End If<br />
<br />
    ts.TopPoint.TryToShow(vsPaneShowHow.vsPaneShowCentered) ' would be nice to move the point to about 1/3 from the top instead of centered<br />
ErrorHandler:<br />
  End Sub

Generalfails in a C++ class method Pin
billdarcy10-Jul-03 5:12
billdarcy10-Jul-03 5:12 
QuestionHow to bind Pin
Robin4-Jun-02 7:47
Robin4-Jun-02 7:47 
AnswerRe: How to bind Pin
S Fewings8-Jul-03 5:15
S Fewings8-Jul-03 5:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.