1

I'm kind off new to the Visual Studio Extensions. Is there a way to interact to code window from Visual Studio 2010 tool window. I have a Datagrid hosted on the VisualStudio tool window. DataGrid contains ClassName, MethodName e.tc. On the click of className/MethodName need to acheive the following

  1. Open the particular .cs file containing className/MethodName
  2. HighLight the particular className/MethodName.

I know this acheivable using "IWpfTextView" class, but not sure how. Did a lot of googling but in vain.Even the link below remains to be un-answered link.

Any help on the above will be greatly appreciated.

Community
  • 1
  • 1
shishi
  • 107
  • 1
  • 2
  • 8
  • there are already extensions present which highlight all occurrences of selected word. Are you trying to make that? – fhnaseer Mar 13 '13 at 11:11
  • Thanks for the comment Faisal. No, I'm not doing that. I just need to hightlight class name/ Method Name for the particular file available in solution explorer. – shishi Mar 13 '13 at 11:14
  • Don't have any idea about that. – fhnaseer Mar 13 '13 at 11:15

2 Answers2

2

I actually did almost the same thing. You can see complete source code on Visual Localizer.

Basically you need to do two things:

  1. Obtain IVsTextView instance for the file
  2. call its SetSelection() method which takes range (start line, end line, start column, end column) as parameters. You may also want to call the EnsureSpanVisible() to scroll down.

Obtaining IVsTextView is quite easy as well. In my project (Visual Localizer) there's a class called DocumentViewsManager, located in VLlib/components that has fairly straightforward method called GetTextViewForFile(), which takes only file path as an argument. It does the following:

  1. use VsShellUtilities.IsDocumentOpen method to obtain IVsWindowFrame for given file path
  2. pass this to the VsShellUtilities.GetTextView method, which returns IVsTextView

Hope it helps.

cre8or
  • 387
  • 3
  • 10
0

Thanks cre8or. I found an alternative to do the same.

You need to use "TextSelection" class to highlight the above code line.

    foreach (CodeElement codeElement in projectItem.FileCodeModel.CodeElements)// search for the function to be opened
    {
        // get the namespace elements
        if (codeElement.Kind == vsCMElement.vsCMElementNamespace)
        {
            foreach (CodeElement namespaceElement in codeElement.Children)
            {
                // get the class elements
                if (namespaceElement.Kind == vsCMElement.vsCMElementClass || namespaceElement.Kind == vsCMElement.vsCMElementInterface)
                {
                    foreach (CodeElement classElement in namespaceElement.Children)
                    {
                        try
                        {
                            // get the function elements to highlight methods in code window
                            if (classElement.Kind == vsCMElement.vsCMElementFunction)
                            {
                                if (!string.IsNullOrEmpty(highlightString))
                                {
                                    if (classElement.Name.Equals(highlightString, StringComparison.Ordinal))
                                    {
                                        classElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null);

                    classElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null);

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

                        // now set the cursor to the beginning of the function
                        textSelection.MoveToPoint(classElement.StartPoint);
                        textSelection.SelectLine();

                                    }
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }
    }
shishi
  • 107
  • 1
  • 2
  • 8
  • 2
    Sure, that works. But don't forget you need a valid FileCodeModel to do it - some files don't even have it, some files have it broken (weird errors when you least expect it) and it is definitely slower. – cre8or Apr 17 '13 at 08:21
  • @cre8or since you mentioned "slower" this post came back in a Google search, I was wondering if you could look at my question over at https://stackoverflow.com/questions/45359130/vs-extension-textpoint-greaterthan-lessthan-very-slow-for-large-files ? Would you happen to know an alternative to (or a way to speed up) `element.StartPoint.GreaterThan()`? Thanks – Adam Plocher Aug 10 '17 at 08:01