7

かなり大きなソリューションをナビゲートするのに役立つ自分のVisual Studio 2010 Extensionを作成しています。
私はVS拡張機能のダイアログベースを既に持っており、いくつかの検索条件に応じてクラス名と関数名を表示しています。私は今このクラス/メソッドをクリックすることができますし、私はすでに正しいファイルを開き、関数にジャンプすることができます。
ここでは、カーソルをその関数の先頭に設定します。関数にジャンプする
私のコードは次のとおりです。Visual Studio Extensionでカーソル位置を設定する

Solution currentSolution = ((EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0")).Solution; 
ProjectItem requestedItem = GetRequestedProjectItemToOpen(currentSolution.Projects, "fileToBeOpened"); 
if (requestedItem != null) 
{ 
    // open the document 
    Window window = requestedItem.Open(Constants.vsViewKindCode); 
    window.Activate(); 

    // search for the function to be opened 
    foreach (CodeElement codeElement in requestedItem.FileCodeModel.CodeElements) 
    { 
     // get the namespace elements 
     if (codeElement.Kind == vsCMElement.vsCMElementNamespace) 
     { 
      foreach (CodeElement namespaceElement in codeElement.Children) 
      { 
       // get the class elements 
       if (namespaceElement.Kind == vsCMElement.vsCMElementClass) 
       { 
        foreach (CodeElement classElement in namespaceElement.Children) 
        { 
         try 
         { 
          // get the function elements 
          if (classElement.Kind == vsCMElement.vsCMElementFunction) 
          { 
           if (classElement.Name.Equals("functionToBeOpened", StringComparison.Ordinal)) 
           { 
            classElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null); 
            this.Close(); 
           } 
          } 
         } 
         catch 
         { 
         } 
        } 
       } 
      } 
     } 
    } 
} 

ここで重要なポイントは正しい関数にジャンプするには、正しいファイルとclassElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null);を開くためにwindow.Activate();です。
残念ながら、カーソルは要求された機能の開始に設定されていません。これどうやってするの?私はclassElement.StartPoint.SetCursor()のようなものを考えています。
私は最終的にそれを得た 乾杯サイモン

+0

Cyclomaticallyコンプレックス?また、あなたが探しているものが見つかったときに、そのメソッドから救済することができないように見えます。これにはいくつかの副作用(WAG)があるかもしれません。 – Will

+0

@ウィル:はい、私は知っています。これはプロトタイプコードの一種です。私が要求されたクラスと関数をどのように開いているかをデモするために... –

答えて

12

...
あなたはちょうどあなたがメソッドMoveToPointを持ってTextSelectionインタフェースを使用する必要があります。
だから、上記のコードは次のとおりです。

// open the file in a VS code window and activate the pane 
Window window = requestedItem.Open(Constants.vsViewKindCode); 
window.Activate(); 

// get the function element and show it 
CodeElement function = CodeElementSearcher.GetFunction(requestedItem, myFunctionName); 

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

// now set the cursor to the beginning of the function 
textSelection.MoveToPoint(function.StartPoint); 
関連する問題