[英]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.