Roslyn analyser code fix - Disable preview option

前端 未结 1 1950
自闭症患者
自闭症患者 2021-01-20 04:30

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 incr

1条回答
  •  -上瘾入骨i
    2021-01-20 04:55

    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> createChangedSolution;
    
        public override string Title { get; }
    
        public override string EquivalenceKey { get; }
    
        public NoPreviewCodeAction(
            string title, Func> createChangedSolution,
            string equivalenceKey = null)
        {
            this.createChangedSolution = createChangedSolution;
    
            Title = title;
            EquivalenceKey = equivalenceKey;
        }
    
        protected override Task> ComputePreviewOperationsAsync(
            CancellationToken cancellationToken)
        {
            return Task.FromResult(Enumerable.Empty());
        }
    
        protected override Task 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.

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