locked
Highlighting code window text from Visual studio tool window using VSPackage RRS feed

  • Question

  • 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.

    Wednesday, March 13, 2013 10:22 AM

Answers

  • I finally found the answer to my post based on lot of googling. Hope this helps for others.

    1.  to open the project item

     Window window = projectItem.Open(EnvDTE.Constants.vsViewKindCode);
     window.Activate();

    2. 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
                                {
                                }
                            }
                        }
                    }
                }
            }
    • Marked as answer by Shishi1988 Tuesday, April 16, 2013 1:36 PM
    Tuesday, April 16, 2013 1:36 PM

All replies

  • Is your tool window control a win form?

    If yes, we can add the click event and make it highlight. I don't think this is a VSX case.


    崖山之後無中國,明亡之後無華夏

    Thursday, March 14, 2013 8:35 AM
  • Hi Disley,

    Thanks for the reply.

    Its a VisualStudio ToolWindow created using VSPackage.

    I need some thing similar to FindResults Window. After user has selected one of the results,appropriate file gets opened with the text hightlighted. Is there any way in which visual studio permits me to acheive the same?

    Thursday, March 14, 2013 9:45 AM
  • Any solution for the above??

    Monday, March 18, 2013 9:23 AM
  • I finally found the answer to my post based on lot of googling. Hope this helps for others.

    1.  to open the project item

     Window window = projectItem.Open(EnvDTE.Constants.vsViewKindCode);
     window.Activate();

    2. 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
                                {
                                }
                            }
                        }
                    }
                }
            }
    • Marked as answer by Shishi1988 Tuesday, April 16, 2013 1:36 PM
    Tuesday, April 16, 2013 1:36 PM