-
Notifications
You must be signed in to change notification settings - Fork 790
/
ReplaceWithSuggestion.fs
66 lines (51 loc) · 2.8 KB
/
ReplaceWithSuggestion.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
58
59
60
61
62
63
64
65
66
// 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.Threading.Tasks
open System.Collections.Immutable
open Microsoft.CodeAnalysis.Text
open Microsoft.CodeAnalysis.CodeFixes
open FSharp.Compiler.Diagnostics
open FSharp.Compiler.EditorServices
open FSharp.Compiler.Syntax
open FSharp.Compiler.Text
open CancellableTasks
[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = CodeFix.ReplaceWithSuggestion); Shared>]
type internal ReplaceWithSuggestionCodeFixProvider [<ImportingConstructor>] (settings: EditorOptions) =
inherit CodeFixProvider()
override _.FixableDiagnosticIds = ImmutableArray.Create("FS0039", "FS1129", "FS0495")
override _.RegisterCodeFixesAsync context : Task =
asyncMaybe {
do! Option.guard settings.CodeFixes.SuggestNamesForErrors
let document = context.Document
let! parseFileResults, checkFileResults =
document.GetFSharpParseAndCheckResultsAsync(nameof (ReplaceWithSuggestionCodeFixProvider))
|> CancellableTask.start context.CancellationToken
|> Async.AwaitTask
|> liftAsync
// This is all needed to get a declaration list
let! sourceText = document.GetTextAsync(context.CancellationToken)
let unresolvedIdentifierText = sourceText.GetSubText(context.Span).ToString()
let pos = context.Span.End
let caretLinePos = sourceText.Lines.GetLinePosition(pos)
let caretLine = sourceText.Lines.GetLineFromPosition(pos)
let fcsCaretLineNumber = Line.fromZ caretLinePos.Line
let lineText = caretLine.ToString()
let partialName =
QuickParse.GetPartialLongNameEx(lineText, caretLinePos.Character - 1)
let declInfo =
checkFileResults.GetDeclarationListInfo(Some parseFileResults, fcsCaretLineNumber, lineText, partialName)
let addNames (addToBuffer: string -> unit) =
for item in declInfo.Items do
addToBuffer item.NameInList
for suggestion in CompilerDiagnostics.GetSuggestedNames addNames unresolvedIdentifierText do
let replacement = PrettyNaming.NormalizeIdentifierBackticks suggestion
do
context.RegisterFsharpFix(
CodeFix.ReplaceWithSuggestion,
CompilerDiagnostics.GetErrorMessage(FSharpDiagnosticKind.ReplaceWithSuggestion suggestion),
[| TextChange(context.Span, replacement) |]
)
}
|> Async.Ignore
|> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)