Roslyn analyser code fix - Disable preview option

人走茶凉 提交于 2019-12-02 04:54:14

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/37435740/roslyn-analyser-code-fix-disable-preview-option

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!