문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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
                        {
                        }
                    }
                }
            }
        }
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top