c# – Upgrading Visual Studio extension from 2019 to 2026, how to replace text?


I have a Visual Studio extension, and I’m trying to upgrade it from Visual Studio 2019 to 2026 (which should be compatible with 2022, so in theory it should work with both 2022 and 2026 when I’m done). Apparently they removed some functionality I was depending on, because this code no longer works:

var doc = dte.ActiveDocument;
if (doc == null)
    return;
var selection = doc.Selection as TextSelection;
if (selection == null)
    return;
string lines = selection.Text;
bool modified = false;
... // Edit text, result into replacementText

if (modified)
{
    selection.ReplacePattern(lines, replacementText, (int) vsFindOptions.vsFindOptionsNone);
}

The documentation for the change is found here.

This is the full example they give of how to replace text:

MutationResult result = await this.Extensibility.Editor().EditAsync(
batch =>
{
    var editor = document.AsEditable(batch);
    editor.Replace(textView.Selection.Extent, newGuidString);
},
cancellationToken);

No context, no explanation, nothing. I have no clue what type “this” is, or where document or textView came from, or what packages I need to install to get to it. I’m going in circles trying to figure out what I need to get from where to do the job.

In this document, Microsoft describes using IFindService.CreateFinderFactory, which links to here.

But I have no clue where to get an instance of IFindService. My entry point into Visual Studio is exactly one object, a DTE2. Starting with a DTE2, how do I get what I need to edit text?

Leave a Reply

Your email address will not be published. Required fields are marked *