7

के साथ कर्सर स्थिति सेट करना मैं अपना खुद का विजुअल स्टूडियो 2010 एक्सटेंशन लिख रहा हूं जो मुझे एक बड़े समाधान को नेविगेट करने में मदद करनी चाहिए।
मेरे पास पहले से ही एक संवाद आधारित वीएस एक्सटेंशन है जो मुझे कुछ खोज मानदंडों के आधार पर एक वर्ग का नाम और एक फ़ंक्शन नाम दिखाता है। अब मैं इस वर्ग/विधि पर क्लिक कर सकता हूं और फिर मैं सही फ़ाइल खोल सकता हूं और फ़ंक्शन पर जा सकता हूं।
अब मैं क्या करना चाहता हूं कि कर्सर को उस फ़ंक्शन की शुरुआत में सेट करना है।
समारोह में जाने के लिए मेरे कोड है:विजुअल स्टूडियो एक्सटेंशन

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

महत्वपूर्ण बिंदुओं यहाँ window.Activate(); हैं सही फ़ाइल को खोलने के लिए और classElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null); सही कार्य करने के लिए कूद करने के।
दुर्भाग्य से कर्सर अनुरोधित फ़ंक्शन की शुरुआत में सेट नहीं है। मैं यह कैसे कर सकता हूँ? मैं classElement.StartPoint.SetCursor() जैसे कुछ के बारे में सोच रहा हूं।
चीयर्स साइमन

+0

Cyclomatically जटिल? साथ ही, ऐसा लगता है कि जब आप जो खोज रहे हैं उसे ढूंढने के लिए आप विधि से बाहर नहीं आ रहे हैं, जिसके कुछ साइड इफेक्ट्स (डब्ल्यूएजी) हो सकते हैं। – Will

+0

@Will: हाँ, मुझे पता है। यह सिर्फ कुछ प्रकार का प्रोटोटाइप कोड है। सिर्फ यह दिखाने के लिए कि मैं अनुरोधित वर्ग और कार्य कैसे खोलता हूं ... –

उत्तर

12

मैं अंत में यह मिल गया ...
तुम बस TextSelection इंटरफ़ेस जहां विधि MoveToPoint है का उपयोग करने के लिए है।
तो ऊपर से कोड है:

// 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); 

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^