I am writing my own extension to Visual Studio 2010, which should help me move on to a fairly large solution.
I already have a VS Extension dialog box that shows me the class name and function name depending on some search criteria. Now I can click this class / method, and then I can already open the correct file and go to the function.
Now I want to position the cursor at the beginning of this function.
My code to go to the function:
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 go to the correct function.
Unfortunately, the cursor is not set to the beginning of the requested function. How can i do this? I am thinking of something like classElement.StartPoint.SetCursor() .
Cheers simon
Simon linder
source share