Setting cursor position with Visual Studio Extension
Asked Answered
A

1

9

I'm writing my own Visual Studio 2010 Extension that should help me navigating a rather large solution.
I already have a dialog based VS Extension that shows me a class name and a function name depending on some search criteria. I now can click this class/method and I then already can open the correct file and jump to the function.
What I now want to do is to set the cursor at the start of that function.
My code to jump to the function is:

Solution currentSolution = ((EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0")).Solution;
ProjectItem requestedItem = GetRequestedProjectItemToOpen(currentSolution.Projects, "fileToBeOpened");
if (requestedItem != null)
{
    // open the document
    Window window = requestedItem.Open(Constants.vsViewKindCode);
    window.Activate();

    // search for the function to be opened
    foreach (CodeElement codeElement in requestedItem.FileCodeModel.CodeElements)
    {
        // get the namespace elements
        if (codeElement.Kind == vsCMElement.vsCMElementNamespace)
        {
            foreach (CodeElement namespaceElement in codeElement.Children)
            {
                // get the class elements
                if (namespaceElement.Kind == vsCMElement.vsCMElementClass)
                {
                   foreach (CodeElement classElement in namespaceElement.Children)
                   {
                       try
                       {
                           // get the function elements
                           if (classElement.Kind == vsCMElement.vsCMElementFunction)
                           {
                               if (classElement.Name.Equals("functionToBeOpened", StringComparison.Ordinal))
                               {
                                   classElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null);
                                   this.Close();
                               }
                           }
                       }
                       catch
                       {
                       }
                   }
               }
           }
       }
   }
}

The important points here are window.Activate(); to open the correct file and classElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null); to jump to the correct function.
Unfortunately the cursor is not set to the start of the requested function. How can I do this? I'm thinking of something like classElement.StartPoint.SetCursor().
Cheers Simon

Aleut answered 21/6, 2012 at 12:2 Comment(2)
Cyclomatically complex? Also, looks like you aren't bailing out of the method when you find what you're looking for, which might have some side effects (WAG).Stutman
@Will: Yes, I know. This is just some kind of prototype code. Just for demonstrating how I open the requested class and function...Aleut
A
16

I finally got it...
You just have to use the TextSelectioninterface where you have the method MoveToPoint.
So the code from above is now:

// open the file in a VS code window and activate the pane
Window window = requestedItem.Open(Constants.vsViewKindCode);
window.Activate();

// get the function element and show it
CodeElement function = CodeElementSearcher.GetFunction(requestedItem, myFunctionName);

// get the text of the document
TextSelection textSelection = window.Document.Selection as TextSelection;

// now set the cursor to the beginning of the function
textSelection.MoveToPoint(function.StartPoint);
Aleut answered 22/6, 2012 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.