AddNewKeywordToDisposableConstructorInvocation.fs 1.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright (c) Microsoft Corporation.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

namespace rec Microsoft.VisualStudio.FSharp.Editor

open System.Composition
open System.Collections.Immutable
open System.Threading
open System.Threading.Tasks

open Microsoft.CodeAnalysis.Text
open Microsoft.CodeAnalysis.CodeFixes
open Microsoft.CodeAnalysis.CodeActions

J
Jared Hester 已提交
14
[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = "AddNewKeyword"); Shared>]
15 16 17
type internal FSharpAddNewKeywordCodeFixProvider() =
    inherit CodeFixProvider()

18
    override __.FixableDiagnosticIds = ImmutableArray.Create "FS0760"
19 20 21

    override this.RegisterCodeFixesAsync context : Task =
        async {
22
            let title = SR.AddNewKeyword.Value
23 24 25 26 27
            context.RegisterCodeFix(
                CodeAction.Create(
                    title,
                    (fun (cancellationToken: CancellationToken) ->
                        async {
28
                            let! sourceText = context.Document.GetTextAsync()
29
                            return context.Document.WithText(sourceText.WithChanges(TextChange(TextSpan(context.Span.Start, 0), "new ")))
J
Jared Hester 已提交
30
                        } |> RoslynHelpers.StartAsyncAsTask(cancellationToken)),
31
                    title), context.Diagnostics |> Seq.filter (fun x -> this.FixableDiagnosticIds.Contains x.Id) |> Seq.toImmutableArray)
J
Jared Hester 已提交
32
        } |> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)
33