microsoft / microsoft/VSExtensibility

Feature request: Text Editor: Scroll to line

Open
#554 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
440
Forks
63
Avg merge
2d 12h
Merged PRs (30d)
1

Description

I would like to have an option to set the caret in the current textview AND scroll to the caret.

Setting the caret is already possible:

          await clientContext.Extensibility.Editor().EditAsync(batch =>
          {
              var caret = new TextPosition(textDocumentSnapshot, position);
              textViewSnapshot!.AsEditable(batch).SetSelections(
              [
                  new Selection(activePosition: caret, anchorPosition: caret, insertionPosition: caret)
              ]);
          },
          cancellationToken);

But I can't find a way to programmatically scroll to the caret.

In the old extensibility model I could do:

var documentView = await VS.Documents.GetActiveDocumentViewAsync();

var line = documentView.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(...);

documentView.TextView.Selection.Select(line.Extent, false);
documentView.TextView.ViewScroller.EnsureSpanVisible(line.Extent, EnsureSpanVisibleOptions.AlwaysCenter);

I want to use this in my CodeNav extension which shows a list of methods, properties, etc... clicking an item now sets the selection but it should also scroll to the caret, so that the user can quickly navigate all code elements in a document.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Editor().EditAsync entry point and the text view snapshot APIs shown in the issue, then compare them with the old documentView.TextView.ViewScroller.EnsureSpanVisible usage. Done means a CodeNav selection can also scroll the active text view to the caret, with behavior equivalent to the requested always-center navigation.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, developer-experience
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.