i have implemented a custom CodeFixProvider that adds some XML documentation to members.
Example:
public void MyMethod() { }
will be transformed to
/// <summary></summary>
public void MyMethod() { }
The CodeFixProvider is implemented like this:
public class MyCodeFixProvider : CodeFixProvider
{
...
public async override Task RegisterCodeFixesAsync(CodeFixContext context)
{
await Task.Run(() =>
{
Diagnostics diagnostics = context.Diagnostics.First();
CodeAction codeFix = CodeAction.Create("Title", c => CreateXmlDocs(...));
context.RegisterCodeFix(codeFix, diagnostics);
}
).ConfigureAwait(false);
}
...
}
Everything is working like expected.
Now i want to add some extra functionality: After applying the code fix, the caret should be moved inside the empty summary tag.
I discovered the DocumentNavigationOperation class included in Microsoft.CodeAnalysis.Features NuGet package. This class should be able to move the caret to a specified position. But I can't find any instructions how to use this class. If i call it from inside my CreateXmlDocs method, an exception is thrown:
Navigation must be performed on the foreground thread.
Code:
private static async Task<Solution> CreateXmlDocs()
{
...
new DocumentNavigationOperation(newDocument.Id, 42)
.Apply(newDocument.Project.Solution.Workspace, cancellationToken);
...
}
I'm not sure if it makes sense to use this class inside my CreateXmlDocs method, because the new solution created inside this method isn't yet applied by Visual Studio when DocumentNavigationOperation is called.
Does anybody knows a solution to move the caret after applying a code fix?