2013-03-13 17 views
1

我對Visual Studio Extensions不熟悉。有沒有一種方法可以從Visual Studio 2010工具窗口與代碼窗口進行交互。 我有VisualStudio工具窗口上承載的Datagrid。 DataGrid包含ClassName,MethodName e.tc.在類名/方法名的點擊需要acheive以下使用VSPackage突出顯示來自Visual Studio工具窗口的代碼窗口文本

  1. 打開特定的.cs含類名/方法名文件
  2. 突出特定的類名/方法名。

我知道這個使用「IWpfTextView」的類,但不知道如何。做了大量的谷歌搜索,但徒勞無功。即使下面的鏈接仍未得到回答link

以上任何幫助將不勝感激。

+0

已經有擴展名存在,其中突出顯示選定單詞的所有出現。你想要做那個嗎? – 2013-03-13 11:11:01

+0

感謝您的評論費薩爾。不,我沒有那樣做。我只需要在解決方案資源管理器中使用特定文件的類名/方法名稱。 – shishi 2013-03-13 11:14:30

+0

對此沒有任何想法。 – 2013-03-13 11:15:38

回答

2

我其實做了幾乎相同的事情。你可以在Visual Localizer上看到完整的源代碼。

基本上你需要做兩件事情:

  1. 獲取IVsTextView實例文件
  2. 調用其爲setSelection()方法,這需要範圍(起始行,終止行,起始列,結束列)作爲參數。您可能還想調用EnsureSpanVisible()向下滾動。

獲得IVsTextView也很容易。在我的項目(Visual Localizer)中,有一個名爲DocumentViewsManager的類,位於VLlib/components中,它具有相當直接的方法GetTextViewForFile(),該方法僅將文件路徑作爲參數。它具有以下功能:

  1. 使用VsShellUtilities.IsDocumentOpen方法來獲得IVsWindowFrame給定文件路徑
  2. 它傳遞給VsShellUtilities.GetTextView方法,它返回IVsTextView

希望它能幫助。

0

謝謝cre8or。 我發現了一個替代方案來做同樣的事情。

您需要使用「TextSelection」類來突出顯示上面的代碼行。

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 
         { 
         } 
        } 
       } 
      } 
     } 
    } 
+2

當然,這有效。但是不要忘了你需要一個有效的FileCodeModel來完成它 - 一些文件甚至沒有它,一些文件已經被破壞(當你最不期待的時候會發生奇怪的錯誤),而且肯定會變慢。 – cre8or 2013-04-17 08:21:52

+0

@ cre8or既然你提到「慢」這篇文章回來了谷歌搜索,我想知道你是否可以看看我的問題在https://stackoverflow.com/questions/45359130/vs-extension-textpoint-greaterthan-小文件很慢 - 對大文件?你會碰巧知道一個替代(或加快)'element.StartPoint.GreaterThan()'?謝謝 – 2017-08-10 08:01:35

相關問題