65.9K
CodeProject is changing. Read more.
Home

Source Navigation Macros

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3 votes)

Dec 28, 2000

CPOL
viewsIcon

46181

Three simple macros that aid in navigating in and between your source code files.

Since I wrote these simple macros about a year ago I can't manage without. I hope you will find them useful as well.

  • QuickFind - finds current selection in the opened document or pops up a regular DevStudio's find dialog if current selection is empty.
  • GoToLastChange - scrolls current document up or down to ensure the last edited line is visible.
  • CPPvH - toggles between CPP and H files in both directions with QuickFind feature built-in. it's especcially useful when you want to jump from declaration of function to its definition and vice-versa.
'============== Navigation Macros By Michael Pelts ========================

Sub QuickFind () 
'DESCRIPTION: finds current selection in the opened document or pops up a regular 
'             DevStudio's find dialog if current selection is empty.
    Dim doc
    set doc = ActiveDocument

    ' Be sure active document is a text document
    if doc Is Nothing Then
        Exit Sub
    elseif doc.Type <> "Text" Then
        Exit Sub
    End If

    lookFor = doc.Selection
    curLine = doc.Selection.CurrentLine
    curCol = doc.Selection.CurrentColumn

    if Len(lookFor) = 0 then
        ExecuteCommand "FindNext"
        if Len(doc.Selection) = 0 then
            ExecuteCommand "Find"
        end if
    else
        doc.Selection.Cancel
        doc.Selection.MoveTo curLine, curCol 
        doc.Selection.FindText lookFor
    End if
End Sub

'==================================================================

Sub GoToLastChange () 
'DESCRIPTION: Scrolls current document up or down to ensure the last edited line is visible.
    ' Be sure active document is a text document
    if ActiveDocument Is Nothing Then
        Exit Sub
    elseif ActiveDocument.Type <> "Text" Then
        Exit Sub
    End If

    if ActiveDocument.Undo = True Then
        ActiveDocument.Redo
    else
        MsgBox("No changes found")
    End if
End Sub

'==================================================================

Sub CPPvH ()
'DESCRIPTION: toggles between CPP and H files in both directions with QuickFind feature built-in.
    on error resume next
    Dim doc
    set doc = ActiveDocument

    ' Be sure active document is a text document
    if doc Is Nothing Then
        Exit Sub
    elseif doc.Type <> "Text" Then
        Exit Sub
    End If

    lookFor = doc.Selection
    fileName = doc.FullName
    dotPos = InstrRev(fileName , ".", -1, vbTextCompare)
    
    fileType = Right(fileName, Len(fileName) - dotPos)
    fileName = Left(fileName, dotPos)

    if UCase(fileType) = UCase("cpp") then
        fileName = fileName + "h"
    elseif UCase(fileType) = UCase("h") then
        fileName = fileName + "cpp"
    else
        MsgBox("Unsupported file format: " + fileType)
        Exit Sub
    End If

    fileName = LCase(fileName)    '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
        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
    End if
End Sub

'=======================================================================