Code Bookmark

  • Thread starter Thread starter Diarmuid
  • Start date Start date
D

Diarmuid

Where is this in VB.net?
Its in VB6, where you can jump from one bookmark to another, to help
navigate your code.
 
Diarmuid,

Do you see those blue flags in top of your IDE?

Cor
 
Ok, thanks.
Now that I know where they are, I added them to my toolbar by going
View/Toolbars/Text Editor
 
I set keyboard shortcuts to mine. Ctrl-Alt-B toggles the bookmark on
and off and Ctrl-B and Ctrl-Shift-B jumps to the next/previous
bookmarks.

I also wrote a macro that allows me to jump to the definition of a
method or variable and at the same time, leave a bookmark. That way I
can jump to a definition and hit a hot key and jump right back where I
was.
 
Hey, thats cool. Didn't even know you could write macros like that.
Any chance you'd sent it me? Its diarmuidq at yahoo dot com
 
Here is the code. The code is pretty simple and there minimal error
checking.
It was cobbled together quickly so it could probably be improved, but
it seems to work. It will only work if called while in a text
document.

I have a hotkey assigned to execute the GotoDefinition sub. That sub
in turn pushes a marker onto a stack (which is a class defined below).
It then executes the IDE's Edit.GoToDefinition command.

Then another hotkey executes the PopMarker sub which retrieves the
bookmark from the stack and jumps to it.

Sub GotoDefinition()
PushMarker()
DTE.ExecuteCommand("Edit.GoToDefinition")
End Sub

Sub PushMarker()

Dim txtDoc As TextDocument =
CType(DTE.ActiveDocument.Object("TextDocument"), TextDocument)

Dim edPt As EditPoint = txtDoc.StartPoint.CreateEditPoint

Dim sel As TextSelection = CType(DTE.ActiveWindow.Selection,
TextSelection)

edPt.MoveToLineAndOffset(sel.CurrentLine, sel.CurrentColumn)
edPt.SetBookmark()

MarkerStack.Push(New Marker(edPt, sel.CurrentLine,
sel.CurrentColumn))

End Sub

Public Sub PopMarker()
Try

Dim m As Marker = MarkerStack.Pop
If Not m Is Nothing Then
Dim txtDoc As TextDocument =
CType(DTE.ActiveDocument.Object("TextDocument"), TextDocument)
Dim objSel As TextSelection =
CType(DTE.ActiveDocument.Selection, TextSelection)
Dim edPt As EditPoint = m.ep

If edPt.TryToShow(vsPaneShowHow.vsPaneShowCentered) Then
edPt.ClearBookmark()
objSel.MoveToLineAndOffset(m.line, m.col)
End If
End If
Catch ex As Exception
Debug.WriteLine(ex.Message & ": " & ex.StackTrace)
End Try
End Sub

End Module

Public Class MarkerStack
Private Shared MarkerS As New Stack

Shared Sub Push(ByVal m As Marker)
MarkerS.Push(m)
End Sub

Shared Function Pop() As Marker
If MarkerS.Count > 0 Then
Return DirectCast(MarkerS.Pop, Marker)
Else
Return Nothing
End If
End Function

Shared Sub ClearStack()
MarkerS.Clear()
End Sub
End Class

Public Class Marker
Public Sub New(ByVal e As EditPoint, ByVal l As Integer, ByVal c As
Integer)
ep = e
line = l
col = c
End Sub

Public ep As EditPoint
Public line As Integer
Public col As Integer
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top