3
3
namespace Microsoft.VisualStudio.FSharp.Editor
4
4
5
5
open System
6
- open System.Threading
7
6
open System.Collections .Immutable
8
7
open System.Diagnostics
9
8
@@ -20,13 +19,6 @@ open FSharp.Compiler.Text
20
19
21
20
open CancellableTasks
22
21
23
- module internal MutableCodeFixHelper =
24
- let getLineNumberAndText ( sourceText : SourceText ) position =
25
- let textLine = sourceText.Lines.GetLineFromPosition position
26
- let textLinePos = sourceText.Lines.GetLinePosition position
27
- let fcsTextLineNumber = Line.fromZ textLinePos.Line
28
- fcsTextLineNumber, textLine.ToString()
29
-
30
22
module internal UnusedCodeFixHelper =
31
23
let getUnusedSymbol textSpan ( document : Document ) ( sourceText : SourceText ) codeFixName =
32
24
let ident = sourceText.ToString textSpan
@@ -80,34 +72,30 @@ module internal CodeFixHelpers =
80
72
81
73
TelemetryReporter.ReportSingleEvent( TelemetryEvents.CodefixActivated, props)
82
74
83
- let createTextChangeCodeFix ( name : string , title : string , context : CodeFixContext , changes : TextChange seq ) =
75
+ let createTextChangeCodeFix ( codeFix , context : CodeFixContext ) =
84
76
CodeAction.Create(
85
- title ,
86
- ( fun ( cancellationToken : CancellationToken ) ->
87
- backgroundTask {
88
- let! sourceText = context.Document.GetTextAsync( cancellationToken)
89
- let doc = context.Document.WithText( sourceText.WithChanges( changes ))
90
- reportCodeFixTelemetry context.Diagnostics context.Document name [||]
77
+ codeFix.Message ,
78
+ ( fun cancellationToken ->
79
+ cancellableTask {
80
+ let! sourceText = context.Document.GetTextAsync cancellationToken
81
+ let doc = context.Document.WithText( sourceText.WithChanges( codeFix.Changes ))
82
+ reportCodeFixTelemetry context.Diagnostics context.Document codeFix.Name [||]
91
83
return doc
92
- }),
93
- name
84
+ }
85
+ |> CancellableTask.start cancellationToken),
86
+ codeFix.Name
94
87
)
95
88
96
89
[<AutoOpen>]
97
90
module internal CodeFixExtensions =
98
91
type CodeFixContext with
99
92
100
- member ctx.RegisterFsharpFix ( staticName , title , changes , ? diagnostics ) =
101
- let codeAction =
102
- CodeFixHelpers.createTextChangeCodeFix ( staticName, title, ctx, changes)
103
-
104
- let diag = diagnostics |> Option.defaultValue ctx.Diagnostics
105
- ctx.RegisterCodeFix( codeAction, diag)
106
-
107
93
member ctx.RegisterFsharpFix ( codeFix : IFSharpCodeFixProvider ) =
108
94
cancellableTask {
109
95
match ! codeFix.GetCodeFixIfAppliesAsync ctx with
110
- | ValueSome codeFix -> ctx.RegisterFsharpFix( codeFix.Name, codeFix.Message, codeFix.Changes)
96
+ | ValueSome codeFix ->
97
+ let codeAction = CodeFixHelpers.createTextChangeCodeFix ( codeFix, ctx)
98
+ ctx.RegisterCodeFix( codeAction, ctx.Diagnostics)
111
99
| ValueNone -> ()
112
100
}
113
101
|> CancellableTask.startAsTask ctx.CancellationToken
@@ -130,6 +118,15 @@ module internal CodeFixExtensions =
130
118
return RoslynHelpers.TextSpanToFSharpRange( ctx.Document.FilePath, ctx.Span, sourceText)
131
119
}
132
120
121
+ member ctx.GetLineNumberAndText position =
122
+ cancellableTask {
123
+ let! sourceText = ctx.GetSourceTextAsync()
124
+ let textLine = sourceText.Lines.GetLineFromPosition position
125
+ let textLinePos = sourceText.Lines.GetLinePosition position
126
+ let fcsTextLineNumber = Line.fromZ textLinePos.Line
127
+ return fcsTextLineNumber, textLine.ToString()
128
+ }
129
+
133
130
// This cannot be an extension on the code fix context
134
131
// because the underlying GetFixAllProvider method doesn't take the context in.
135
132
#nowarn " 3511" // state machine not statically compilable
0 commit comments