I am currently experimenting with Roslyn and Code Actions, more specific Code Refactorings. It feels kind of easy, but I have a difficulty I cannot solve.
Code actio
You can choose to override ComputePreviewOperationsAsync to have different behavior for Previews from regular code.
I've found the solution to my problem by digging deeper and trial and error after Keven Pilch's answer. He bumped me in the right direction.
The solution was to override both the ComputePreviewOperationsAsync and the GetChangedSolutionAsync methods in my own CodeAction.
Here the relevant part of my CustomCodeAction
, or full gist here.
private readonly Func<CancellationToken, bool, Task<Solution>> _createChangedSolution;
protected override async Task<IEnumerable<CodeActionOperation>> ComputePreviewOperationsAsync(CancellationToken cancellationToken)
{
const bool isPreview = true;
// Content copied from http://sourceroslyn.io/#Microsoft.CodeAnalysis.Workspaces/CodeActions/CodeAction.cs,81b0a0866b894b0e,references
var changedSolution = await GetChangedSolutionWithPreviewAsync(cancellationToken, isPreview).ConfigureAwait(false);
if (changedSolution == null)
return null;
return new CodeActionOperation[] { new ApplyChangesOperation(changedSolution) };
}
protected override Task<Solution> GetChangedSolutionAsync(CancellationToken cancellationToken)
{
const bool isPreview = false;
return GetChangedSolutionWithPreviewAsync(cancellationToken, isPreview);
}
protected virtual Task<Solution> GetChangedSolutionWithPreviewAsync(CancellationToken cancellationToken, bool isPreview)
{
return _createChangedSolution(cancellationToken, isPreview);
}
The code to create the action stays quite similar, except the bool
is added and I can check against it then:
public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
// [...]
context.RegisterRefactoring(CustomCodeAction.Create("MyAction",
(c, isPreview) => DoMyAction(context.Document, dec, c, isPreview)));
}
private static async Task<Solution> DoMyAction(Document document, MethodDeclarationSyntax method, CancellationToken cancellationToken, bool isPreview)
{
// some - for the question irrelevant - roslyn changes, like:
// [...]
// now the DTE magic
if (!isPreview)
{
// [...]
}
return document.Project.Solution;
}
Why those two?
The ComputePreviewOperationsAsync
calls the the normal ComputeOperationsAsync
, which internally calls ComputeOperationsAsync
. This computation executes GetChangedSolutionAsync
. So both ways - preview and not - end up at GetChangedSolutionAsync
. That's what I actually want, calling the same code, getting a very similar solution, but giving a bool
flag if it is preview or not too.
So I've written my own GetChangedSolutionWithPreviewAsync
which I use instead. I have overriden the default GetChangedSolutionAsync
using my custom Get function, and then ComputePreviewOperationsAsync
with a fully customized body. Instead of calling ComputeOperationsAsync
, which the default one does, I've copied the code of that function, and modified it to use my GetChangedSolutionWithPreviewAsync
instead.
Sounds rather complicated in written from, but I guess the code above should explain it quite well.
Hope this helps other people.