Roslyn Code Action: How to check if preview or real execution?

前端 未结 2 444
醉话见心
醉话见心 2020-12-21 14:35

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

相关标签:
2条回答
  • 2020-12-21 14:42

    You can choose to override ComputePreviewOperationsAsync to have different behavior for Previews from regular code.

    0 讨论(0)
  • 2020-12-21 15:00

    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.

    0 讨论(0)
提交回复
热议问题