3

How do I disable the preview dialog that shows up after the light bulb in C# project?

Problem I have is, the RegisterCodeFixesAsync makes a call to database and increments the id and this is getting done twice (once during the preview and second time when the action is invoked), instead of incrementing just once, id increments twice

Lander
  • 31
  • 4
  • In fact it is getting incremented 3 times, but once I disable the preview I should be able to drill down further. Any help is appreciated. – Lander May 25 '16 at 11:29
  • Can't you refactor your code to only do the DB increment when the `CodeAction` is executed? – Tamas May 25 '16 at 11:55
  • I have this in the register codefix async context.RegisterCodeFix( CodeAction.Create( title: title, createChangedDocument: c => createItemInDB(context.Document, declaration, c), equivalenceKey: title), diagnostic); – Lander May 29 '16 at 09:45

1 Answers1

9

CodeAction has separate ComputePreviewOperationsAsync() and ComputeOperationsAsync(). Having them return different values is what I believe you're looking for. But if you use the common approach of calling CodeAction.Create(), both will return the same values.

What you can do instead is to create a custom class that inherits from CodeAction and overrides the methods the way you want. For example:

class NoPreviewCodeAction : CodeAction
{
    private readonly Func<CancellationToken, Task<Solution>> createChangedSolution;

    public override string Title { get; }

    public override string EquivalenceKey { get; }

    public NoPreviewCodeAction(
        string title, Func<CancellationToken, Task<Solution>> createChangedSolution,
        string equivalenceKey = null)
    {
        this.createChangedSolution = createChangedSolution;

        Title = title;
        EquivalenceKey = equivalenceKey;
    }

    protected override Task<IEnumerable<CodeActionOperation>> ComputePreviewOperationsAsync(
        CancellationToken cancellationToken)
    {
        return Task.FromResult(Enumerable.Empty<CodeActionOperation>());
    }

    protected override Task<Solution> GetChangedSolutionAsync(
        CancellationToken cancellationToken)
    {
        return createChangedSolution(cancellationToken);
    }
}

This version completely disables preview. Another option would be to make preview take a different path, e.g. querying the database for the next value, but not updating it.

svick
  • 236,525
  • 50
  • 385
  • 514
  • Hi @svick Thank you very much for your answer. That pretty much explains and fixes my problem. Much appreciated – Lander Jun 01 '16 at 11:22