簡體   English   中英

使用Visual Studio擴展設置光標位置

[英]Setting cursor position with Visual Studio Extension

我正在編寫自己的Visual Studio 2010 Extension,它可以幫助我導航一個相當大的解決方案。
我已經有一個基於VS 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
                       {
                       }
                   }
               }
           }
       }
   }
}

這里的重點是window.Activate(); 打開正確的文件和classElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null); 跳轉到正確的功能。
遺憾的是,游標未設置為所請求函數的開頭。 我怎樣才能做到這一點? 我在想類似classElement.StartPoint.SetCursor()的東西。
干杯西蒙

我終於明白了......
您只需使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM