0

I'm building a VSpackage extension to create "VisualStudio Tool Window". I have a grid inside tool window, consisting of numbers. If a user selects a particular row of the grid. That particular line of code should be highlighted.

To be more clear, Suppose my grid contains:

row 1 - 10, row 2 - 15, row 3 - 14,

if user selects row 1, then 10th line in the code window should get highlighted. Is this feature possible using VisualStudio package. I have a strong feeling that this is possible.Because most of the search results work that way.

Any help on the same is greatly appreciated!!

shishi
  • 107
  • 1
  • 2
  • 8

2 Answers2

0

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

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
0

You can also use simpler solution; see below

  int lineNo = 3;
  if (!isProjectItemOpen)
  {
       Window win = projectItem.Open();
       win.Visible = true;
       Document doc = win.Document;
       doc.Activate();
       var ts = dte.ActiveDocument.Selection;
       ts.GotoLine(lineNo, true);
  }
slfan
  • 8,950
  • 115
  • 65
  • 78