-
Notifications
You must be signed in to change notification settings - Fork 763
/
FixIndexerAccess.fs
57 lines (46 loc) · 2.45 KB
/
FixIndexerAccess.fs
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
57
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace Microsoft.VisualStudio.FSharp.Editor
open System
open System.Composition
open System.Collections.Immutable
open System.Threading.Tasks
open Microsoft.CodeAnalysis
open Microsoft.CodeAnalysis.Text
open Microsoft.CodeAnalysis.CodeFixes
open SymbolHelpers
[<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 span = ref context.Span
// skip all braces and blanks until we find [
while
(!span).End < sourceText.Length &&
let t = TextSpan((!span).Start,(!span).Length + 1)
let s = sourceText.GetSubText(t).ToString()
s.[s.Length-1] <> '[' do
span := TextSpan((!span).Start,(!span).Length + 1)
!span,sourceText.GetSubText(!span).ToString()
with
| _ -> context.Span,sourceText.GetSubText(context.Span).ToString()
let codefix =
createTextChangeCodeFix(
FSComp.SR.addIndexerDot(),
context,
(fun () -> asyncMaybe.Return [| TextChange(span, replacement.TrimEnd() + ".") |]))
context.RegisterCodeFix(codefix, diagnostics))
} |> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)