/
ChangePrefixNegationToInfixSubtraction.fs
55 lines (39 loc) · 1.99 KB
/
ChangePrefixNegationToInfixSubtraction.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
// 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.Threading.Tasks
open Microsoft.CodeAnalysis.Text
open Microsoft.CodeAnalysis.CodeFixes
[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = "ChangePrefixNegationToInfixSubtraction"); Shared>]
type internal FSharpChangePrefixNegationToInfixSubtractionodeFixProvider() =
inherit CodeFixProvider()
let fixableDiagnosticIds = set ["FS0003"]
override _.FixableDiagnosticIds = Seq.toImmutableArray fixableDiagnosticIds
override _.RegisterCodeFixesAsync context : Task =
asyncMaybe {
let diagnostics =
context.Diagnostics
|> Seq.filter (fun x -> fixableDiagnosticIds |> Set.contains x.Id)
|> Seq.toImmutableArray
let! sourceText = context.Document.GetTextAsync(context.CancellationToken)
let mutable pos = context.Span.End + 1
// This won't ever actually happen, but eh why not
do! Option.guard (pos < sourceText.Length)
let mutable ch = sourceText.GetSubText(TextSpan(pos, 1)).ToString().[0]
while pos < sourceText.Length && Char.IsWhiteSpace(ch) do
pos <- pos + 1
let text = sourceText.GetSubText(TextSpan(pos, 1))
ch <- text.ToString().[0]
// Bail if this isn't a negation
do! Option.guard (ch = '-')
let title = SR.ChangePrefixNegationToInfixSubtraction()
let codeFix =
CodeFixHelpers.createTextChangeCodeFix(
title,
context,
(fun () -> asyncMaybe.Return [| TextChange(TextSpan(pos, 1), "- ") |]))
context.RegisterCodeFix(codeFix, diagnostics)
}
|> Async.Ignore
|> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)