|
| 1 | +// 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. |
| 2 | + |
| 3 | +namespacerecMicrosoft.VisualStudio.FSharp.Editor |
| 4 | + |
| 5 | +openSystem |
| 6 | +openSystem.Composition |
| 7 | +openSystem.Collections.Immutable |
| 8 | +openSystem.Threading |
| 9 | +openSystem.Threading.Tasks |
| 10 | + |
| 11 | +openMicrosoft.CodeAnalysis |
| 12 | +openMicrosoft.CodeAnalysis.Text |
| 13 | +openMicrosoft.CodeAnalysis.CodeFixes |
| 14 | +openMicrosoft.CodeAnalysis.CodeActions |
| 15 | + |
| 16 | +[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name="FixIndexerAccess"); Shared>] |
| 17 | +typeinternalFSharpFixIndexerAccessCodeFixProvider()= |
| 18 | +inherit CodeFixProvider() |
| 19 | +letfixableDiagnosticIds= set["FS3217"] |
| 20 | + |
| 21 | +letcreateCodeFix(title:string,context:CodeFixContext,textChange:TextChange)= |
| 22 | + CodeAction.Create( |
| 23 | + title, |
| 24 | +(fun(cancellationToken: CancellationToken)-> |
| 25 | +async{ |
| 26 | +let!cancellationToken= Async.CancellationToken |
| 27 | +let!sourceText= context.Document.GetTextAsync(cancellationToken)|> Async.AwaitTask |
| 28 | +return context.Document.WithText(sourceText.WithChanges(textChange)) |
| 29 | +}|> RoslynHelpers.StartAsyncAsTask(cancellationToken)), |
| 30 | + title) |
| 31 | + |
| 32 | +override__.FixableDiagnosticIds= Seq.toImmutableArray fixableDiagnosticIds |
| 33 | + |
| 34 | +override__.RegisterCodeFixesAsync context:Task= |
| 35 | +async{ |
| 36 | +letdiagnostics= |
| 37 | + context.Diagnostics |
| 38 | +|> Seq.filter(fun x-> fixableDiagnosticIds|> Set.contains x.Id) |
| 39 | +|> Seq.toList |
| 40 | +ifnot(List.isEmpty diagnostics)then |
| 41 | +let!sourceText= context.Document.GetTextAsync()|> Async.AwaitTask |
| 42 | + |
| 43 | + diagnostics |
| 44 | +|> Seq.iter(fun diagnostic-> |
| 45 | +letdiagnostics= ImmutableArray.Create diagnostic |
| 46 | + |
| 47 | +letcodefix= |
| 48 | + createCodeFix( |
| 49 | + FSComp.SR.addIndexerDot(), |
| 50 | + context, |
| 51 | + TextChange(context.Span, sourceText.GetSubText(context.Span).ToString()+".")) |
| 52 | + context.RegisterCodeFix(codefix, diagnostics)) |
| 53 | +}|> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken) |