7

Tôi đang viết phần mở rộng Visual Studio 2010 của riêng mình, điều này sẽ giúp tôi điều hướng một giải pháp khá lớn.
Tôi đã có một hộp thoại dựa trên VS Extension cho tôi thấy một tên lớp và một tên chức năng tùy thuộc vào một số tiêu chí tìm kiếm. Bây giờ tôi có thể nhấp vào lớp/phương pháp này và sau đó tôi đã có thể mở tệp chính xác và chuyển đến hàm.
Điều tôi muốn làm là đặt con trỏ ở đầu hàm đó.
Mã của tôi để chuyển đến các chức năng là:Đặt vị trí con trỏ với 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 
         { 
         } 
        } 
       } 
      } 
     } 
    } 
} 

Những điểm quan trọng ở đây là window.Activate(); để mở tập tin chính xác và classElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null); để chuyển đến các chức năng chính xác.
Rất tiếc, con trỏ không được đặt ở đầu chức năng được yêu cầu. Tôi có thể làm cái này như thế nào? Tôi đang nghĩ về một cái gì đó như classElement.StartPoint.SetCursor().
Cheers Simon

+0

Cyclomatically complex? Ngoài ra, có vẻ như bạn không bailing ra khỏi phương pháp khi bạn tìm thấy những gì bạn đang tìm kiếm, mà có thể có một số tác dụng phụ (WAG). – Will

+0

@Will: Vâng, tôi biết. Đây chỉ là một số loại mã nguyên mẫu. Chỉ để minh họa cách tôi mở lớp và chức năng được yêu cầu ... –

Trả lời

12

Cuối cùng tôi đã nhận nó ...
Bạn chỉ cần sử dụng giao diện TextSelection, nơi bạn có phương pháp MoveToPoint.
Vì vậy, mã từ phía trên là:

// 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); 
Các vấn đề liên quan