In a VisualStudio.Extensibility Extension project (VS2022), I can modify the currently edited Document from a Command by running
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
using var textView = await context.GetActiveTextViewAsync(cancellationToken);
await Extensibility.Editor().EditAsync(batch =>
{
var doc = textView.Document.AsEditable(batch);
...
}
}
But I din't find any way to change the caret location, the ITextViewSnapshot.Selection object is read-only.
However according to the documentation it's supposed to be possible:
For the initial release of the new Visual Studio extensibility model, only the following capabilities are supported
...
* Performing text edits and selection/caret changes.
My question is how to move the caret?
Edit
The code below adds a new caret at the specified location, however I can't find a way to remove the initial one; it seems SetSelections is actually adding selections
return Extensibility.Editor().EditAsync(batch =>
{
var snapshot = args.AfterTextView;
var document = snapshot.Document.AsEditable(batch);
int offset = snapshot.Selection.InsertionPosition.Offset;
document.Insert(offset, "-");
var position = new TextPosition(snapshot.Document, offset);
var selections = new List<Selection>
{
new (position, position, position)
};
var textView = snapshot.AsEditable(batch);
textView.SetSelections(selections);
},
cancellationToken);