-
Notifications
You must be signed in to change notification settings - Fork 789
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a codefix that suggests replacements for unknown identifiers
- Loading branch information
Showing
4 changed files
with
61 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
vsintegration/src/FSharp.Editor/CodeFix/ReplaceWithSuggestion.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace rec Microsoft.VisualStudio.FSharp.Editor | ||
|
||
open System | ||
open System.Composition | ||
open System.Collections.Immutable | ||
open System.Threading | ||
open System.Threading.Tasks | ||
|
||
open Microsoft.CodeAnalysis | ||
open Microsoft.CodeAnalysis.Text | ||
open Microsoft.CodeAnalysis.CodeFixes | ||
open Microsoft.CodeAnalysis.CodeActions | ||
|
||
[<ExportCodeFixProvider(FSharpCommonConstants.FSharpLanguageName, Name = "ReplaceWithSuggestion"); Shared>] | ||
type internal FSharpReplaceWithSuggestionCodeFixProvider() = | ||
inherit CodeFixProvider() | ||
let fixableDiagnosticIds = ["FS0039"; "FS1129"; "FS0495"] | ||
let maybeString = FSComp.SR.undefinedNameSuggestionsIntro() | ||
|
||
let createCodeFix (title: string, context: CodeFixContext, textChange: TextChange) = | ||
CodeAction.Create( | ||
title, | ||
(fun (cancellationToken: CancellationToken) -> | ||
async { | ||
let! sourceText = context.Document.GetTextAsync() |> Async.AwaitTask | ||
return context.Document.WithText(sourceText.WithChanges(textChange)) | ||
} |> CommonRoslynHelpers.StartAsyncAsTask(cancellationToken)), | ||
title) | ||
|
||
override __.FixableDiagnosticIds = fixableDiagnosticIds.ToImmutableArray() | ||
|
||
override __.RegisterCodeFixesAsync context : Task = | ||
async { | ||
context.Diagnostics | ||
|> Seq.filter (fun x -> fixableDiagnosticIds |> List.contains x.Id) | ||
|> Seq.iter (fun x -> | ||
let message = x.GetMessage() | ||
let splitted = message.Split([|maybeString|], StringSplitOptions.None) | ||
if splitted.Length > 1 then | ||
let suggestions = | ||
splitted.[1].Split([|' '; '\r'; '\n'|], StringSplitOptions.RemoveEmptyEntries) | ||
|> Array.map (fun s -> s.Trim()) | ||
|
||
let diagnostics = [| x |].ToImmutableArray() | ||
|
||
for suggestion in suggestions do | ||
let codefix = | ||
createCodeFix( | ||
FSComp.SR.replaceWithSuggestion suggestion, | ||
context, | ||
TextChange(TextSpan(context.Span.Start, context.Span.End), suggestion)) | ||
context.RegisterCodeFix(codefix, diagnostics)) | ||
} |> CommonRoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters