【问题标题】:Setting cursor position with Visual Studio Extension使用 Visual Studio 扩展设置光标位置
【发布时间】:2012-06-21 12:02:45
【问题描述】:

我正在编写自己的 Visual Studio 2010 扩展,它应该可以帮助我浏览一个相当大的解决方案。
我已经有一个基于对话框的 VS 扩展,它根据某些搜索条件向我显示类名和函数名。我现在可以单击此类/方法,然后我已经可以打开正确的文件并跳转到该函数。
我现在要做的是将光标设置在该函数的开头。
我跳转到函数的代码是:

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().
干杯西蒙

【问题讨论】:

  • 循环复杂?此外,当您找到所需内容时,您似乎并没有放弃该方法,这可能会产生一些副作用 (WAG)。
  • @Will:是的,我知道。这只是某种原型代码。只是为了演示我如何打开请求的类和函数...

标签: c# visual-studio-2010 envdte visual-studio-extensions visual-studio-sdk


【解决方案1】:

我终于明白了...
你只需要使用TextSelectioninterface,你有方法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);

【讨论】:

    猜你喜欢
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多