Click here to Skip to main content
15,892,517 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.5K   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 
Generalfails in a C++ class method Pin
billdarcy10-Jul-03 5:12
billdarcy10-Jul-03 5:12 
when i run BeginningOfFunction or EndOfFunction i get the error message:
"Object reference not set to an instance of an object"

to diagnose, i broke up BeginningOfFunction to the code below. i found that "ce" is Nothing after its assignment. i dont understand why.

i tried running this when positioned in a C function and it worked. it only fails when i run it when positioned in a C++ class method. see below.

should i use something instead of vsCMElement.vsCMElementFunction?

thanks.

Bill Darcy
eCopy


------------------------
Sub BeginningOfFunction()
Dim ts As TextSelection = DTE.ActiveWindow.Selection

If (ts.IsEmpty()) Then
ts.CharLeft(True)
End If

If (Not ts.IsEmpty) Then
Dim vp As VirtualPoint
vp = ts.ActivePoint

Dim ce As CodeElement
ce = vp.CodeElement(vsCMElement.vsCMElementFunction)

Dim sp As TextPoint
sp = ce.GetStartPoint(vsCMPart.vsCMPartHeader).CreateEditPoint()

ts.MoveToPoint(sp)
End If

End Sub
----------------------------

// works when positioned in:
void f(int a)
{
int b = 0;

int c = 0;
}

// fails when positioned in:
void C::f(int a)
{
int b = 0;

int c = 0;
}
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.