简体   繁体   English

Visual Studio 可扩展性:移动到 TextDocument 中的行

[英]Visual Studio Extensibility: Move to line in a TextDocument

I am in the focus of a ToolWindow.我在 ToolWindow 的焦点。 By doing dobleclick on a TreeView node, the cursor has to move to a particular line within the opened source code document.通过在 TreeView 节点上执行 dobleclick,cursor 必须移动到打开的源代码文档中的特定行。 I solved this issue by calling the Edit.GoTo Line command like this:我通过调用 Edit.GoTo Line 命令解决了这个问题,如下所示:

var commandName = "Edit.GoTo " + lineNumber;
_dte.ExecuteCommand(commandName);

However I am not quite convinient with that as I lose the focus of the toolwindow.但是,我对此不太满意,因为我失去了工具窗口的焦点。 Is there another way to move to a line by using the Automation API?是否有另一种方法可以使用自动化 API 移动到生产线?

Use the IViewScroller.EnsureSpanVisible(SnapshotSpan span, EnsureSpanVisibleOptions options)使用IViewScroller.EnsureSpanVisible(SnapshotSpan span, EnsureSpanVisibleOptions options)

To create a span, use:要创建跨度,请使用:

var lines = view.VisualSnapshot.Lines;

var startLine = lines.FirstOrDefault(a => a.LineNumber == fromLine - 1);
var endLine = lines.FirstOrDefault(a => a.LineNumber == toLine - 1);

if (startLine == null || endLine == null)
    return;

var startPosition = startLine.Start;
var endPosition = endLine.Start;

var span = new SnapshotSpan(view.TextSnapshot, Span.FromBounds(startPosition, endPosition));

And to scroll to the span:并滚动到跨度:

layer.TextView.ViewScroller.EnsureSpanVisible(span,
    EnsureSpanVisibleOptions.AlwaysCenter);

Where view is the IWpfTextView provided by your adorner ( IWpfTextViewCreationListener ) view是您的装饰器提供的IWpfTextView ( IWpfTextViewCreationListener )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM