使用 Visual Studio 扩展设置光标位置 [英] Setting cursor position with Visual Studio Extension
问题描述
我正在编写自己的 Visual Studio 2010 扩展,它应该可以帮助我浏览一个相当大的解决方案.
我已经有一个基于对话框的 VS 扩展,它根据某些搜索条件向我显示类名和函数名.我现在可以点击这个类/方法,然后我就可以打开正确的文件并跳转到该函数.
我现在想要做的是在该函数的开头设置光标.
我跳转到函数的代码是:
I'm writing my own Visual Studio 2010 Extension that should help me navigating a rather large solution.
I already have a dialog based VS Extension that shows me a class name and a function name depending on some search criteria. I now can click this class/method and I then already can open the correct file and jump to the function.
What I now want to do is to set the cursor at the start of that function.
My code to jump to the function is:
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()
.
干杯西蒙
The important points here are window.Activate();
to open the correct file and classElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null);
to jump to the correct function.
Unfortunately the cursor is not set to the start of the requested function. How can I do this? I'm thinking of something like classElement.StartPoint.SetCursor()
.
Cheers Simon
推荐答案
我终于明白了...
您只需要使用 TextSelection
接口,其中包含方法 MoveToPoint
.
所以上面的代码现在是:
I finally got it...
You just have to use the TextSelection
interface where you have the method MoveToPoint
.
So the code from above is now:
// 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);
这篇关于使用 Visual Studio 扩展设置光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!