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

Oli*_*gel 6 .net visual-studio

我是ToolWindow的焦点.通过在TreeView节点上执行dobleclick,游标必须移动到打开的源代码文档中的特定行.我通过调用Edit.GoTo Line命令解决了这个问题,如下所示:

var commandName = "Edit.GoTo " + lineNumber;
_dte.ExecuteCommand(commandName);
Run Code Online (Sandbox Code Playgroud)

然而,当我失去工具窗口的焦点时,我并不十分方便.有没有其他方法可以使用Automation API转移到一行?

Cla*_*sen 5

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

要创建跨度,请使用:

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));
Run Code Online (Sandbox Code Playgroud)

并滚动到跨度:

layer.TextView.ViewScroller.EnsureSpanVisible(span,
    EnsureSpanVisibleOptions.AlwaysCenter);
Run Code Online (Sandbox Code Playgroud)

哪里viewIWpfTextView你的装饰器提供(IWpfTextViewCreationListener)