-
Notifications
You must be signed in to change notification settings - Fork 872
Expand file tree
/
Copy pathFixIndexerAccess.fs
More file actions
56 lines (44 loc) · 2.49 KB
/
Copy pathFixIndexerAccess.fs
File metadata and controls
56 lines (44 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace Microsoft.VisualStudio.FSharp.Editor
open System.Composition
open System.Collections.Immutable
open System.Threading.Tasks
open Microsoft.CodeAnalysis.Text
open Microsoft.CodeAnalysis.CodeFixes
open FSharp.Compiler.SourceCodeServices
[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = "FixIndexerAccess"); Shared>]
type internal FSharpFixIndexerAccessCodeFixProvider() =
inherit CodeFixProvider()
let fixableDiagnosticIds = set ["FS3217"]
override __.FixableDiagnosticIds = Seq.toImmutableArray fixableDiagnosticIds
override __.RegisterCodeFixesAsync context : Task =
async {
let diagnostics =
context.Diagnostics
|> Seq.filter (fun x -> fixableDiagnosticIds |> Set.contains x.Id)
|> Seq.toList
if not (List.isEmpty diagnostics) then
let! sourceText = context.Document.GetTextAsync() |> Async.AwaitTask
diagnostics
|> Seq.iter (fun diagnostic ->
let diagnostics = ImmutableArray.Create diagnostic
let span,replacement =
try
let mutable span = context.Span
let notStartOfBracket (span: TextSpan) =
let t = TextSpan(span.Start, span.Length + 1)
let s = sourceText.GetSubText(t).ToString()
s.[s.Length-1] <> '['
// skip all braces and blanks until we find [
while span.End < sourceText.Length && notStartOfBracket span do
span <- TextSpan(span.Start, span.Length + 1)
span,sourceText.GetSubText(span).ToString()
with
| _ -> context.Span,sourceText.GetSubText(context.Span).ToString()
let codefix =
CodeFixHelpers.createTextChangeCodeFix(
CompilerDiagnostics.getErrorMessage AddIndexerDot,
context,
(fun () -> asyncMaybe.Return [| TextChange(span, replacement.TrimEnd() + ".") |]))
context.RegisterCodeFix(codefix, diagnostics))
} |> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)