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 actions are executed once against a dummy workspace as a "preview" option, so that you can see the actual changes before you click the action and execute it against the real workspace.
Now I am dealing with some things Roslyn can't really do (yet), so I am doing some changes via EnvDTE
. I know, it's bad, but I couldn't find another way.
So the issue here is:
When I hover over my code action, the code gets executed as preview, and it should NOT do the EnvDTE
changes. Those should only be done when the real execute happens.
I have created a gist with a small example of my code. It doesn't really makes sense, but should show what I want to achieve. Do some modifications via roslyn, then do something via EnvDTE
, like changing Cursor position. But of course only on the real execution.
The relevant part for those who can't click the gist:
public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(continueOnCapturedContext: false);
var node = root.FindNode(context.Span);
var dec = node as MethodDeclarationSyntax;
if (dec == null)
return;
context.RegisterRefactoring(CodeAction.Create("MyAction", c => DoMyAction(context.Document, dec, c)));
}
private static async Task<Solution> DoMyAction(Document document, MethodDeclarationSyntax method, CancellationToken cancellationToken)
{
var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken);
var root = await syntaxTree.GetRootAsync(cancellationToken);
// some - for the question irrelevant - roslyn changes, like:
document = document.WithSyntaxRoot(root.ReplaceNode(method, method.WithIdentifier(SyntaxFactory.ParseToken(method.Identifier.Text + "Suffix"))));
// now the DTE magic
var preview = false; // <--- TODO: How to check if I am in preview here?
if (!preview)
{
var requestedItem = DTE.Solution.FindProjectItem(document.FilePath);
var window = requestedItem.Open(Constants.vsViewKindCode);
window.Activate();
var position = method.Identifier.GetLocation().GetLineSpan().EndLinePosition;
var textSelection = (TextSelection) window.Document.Selection;
textSelection.MoveTo(position.Line, position.Character);
}
return document.Project.Solution;
}
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 theGetChangedSolutionAsync
methods in my own CodeAction.Here the relevant part of my
CustomCodeAction
, or full gist here.The code to create the action stays quite similar, except the
bool
is added and I can check against it then:Why those two?
The
ComputePreviewOperationsAsync
calls the the normalComputeOperationsAsync
, which internally callsComputeOperationsAsync
. This computation executesGetChangedSolutionAsync
. So both ways - preview and not - end up atGetChangedSolutionAsync
. That's what I actually want, calling the same code, getting a very similar solution, but giving abool
flag if it is preview or not too.So I've written my own
GetChangedSolutionWithPreviewAsync
which I use instead. I have overriden the defaultGetChangedSolutionAsync
using my custom Get function, and thenComputePreviewOperationsAsync
with a fully customized body. Instead of callingComputeOperationsAsync
, which the default one does, I've copied the code of that function, and modified it to use myGetChangedSolutionWithPreviewAsync
instead.Sounds rather complicated in written from, but I guess the code above should explain it quite well.
Hope this helps other people.
You can choose to override
ComputePreviewOperationsAsync
to have different behavior for Previews from regular code.