|
| 1 | +// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. |
| 2 | + |
| 3 | +namespace Microsoft.VisualStudio.FSharp.Editor |
| 4 | + |
| 5 | +open System |
| 6 | +open System.Composition |
| 7 | +open System.Threading |
| 8 | + |
| 9 | +open FSharp.Compiler |
| 10 | +open FSharp.Compiler.CodeAnalysis |
| 11 | +open FSharp.Compiler.Symbols |
| 12 | +open FSharp.Compiler.Text |
| 13 | +open FSharp.Compiler.Syntax |
| 14 | + |
| 15 | +open Microsoft.CodeAnalysis.Text |
| 16 | +open Microsoft.CodeAnalysis.CodeRefactorings |
| 17 | +open Microsoft.CodeAnalysis.CodeActions |
| 18 | + |
| 19 | +[<ExportCodeRefactoringProvider(FSharpConstants.FSharpLanguageName, Name = "ChangeDerefToValue"); Shared>] |
| 20 | +type internal FSharpChangeDerefToValueRefactoring |
| 21 | + [<ImportingConstructor>] |
| 22 | + ( |
| 23 | + checkerProvider: FSharpCheckerProvider, |
| 24 | + projectInfoManager: FSharpProjectOptionsManager |
| 25 | + ) = |
| 26 | + inherit CodeRefactoringProvider() |
| 27 | + |
| 28 | + static let userOpName = "ChangeDerefToValue" |
| 29 | + |
| 30 | + override _.ComputeRefactoringsAsync context = |
| 31 | + asyncMaybe { |
| 32 | + let document = context.Document |
| 33 | + let! parsingOptions, _ = projectInfoManager.TryGetOptionsForEditingDocumentOrProject(document, context.CancellationToken, userOpName) |
| 34 | + let! sourceText = context.Document.GetTextAsync(context.CancellationToken) |
| 35 | + let! parseResults = checkerProvider.Checker.ParseFile(document.FilePath, sourceText.ToFSharpSourceText(), parsingOptions, userOpName=userOpName) |> liftAsync |
| 36 | + |
| 37 | + let selectionRange = RoslynHelpers.TextSpanToFSharpRange(document.FilePath, context.Span, sourceText) |
| 38 | + let! derefRange = parseResults.TryRangeOfRefCellDereferenceContainingPos selectionRange.Start |
| 39 | + let! exprRange = parseResults.TryRangeOfExpressionBeingDereferencedContainingPos selectionRange.Start |
| 40 | + |
| 41 | + let combinedRange = Range.unionRanges derefRange exprRange |
| 42 | + let! combinedSpan = RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, combinedRange) |
| 43 | + let replacementString = |
| 44 | + // Trim off the `!` |
| 45 | + sourceText.GetSubText(combinedSpan).ToString().[1..] + ".Value" |
| 46 | + |
| 47 | + let title = SR.UseValueInsteadOfDeref() |
| 48 | + |
| 49 | + let getChangedText (sourceText: SourceText) = |
| 50 | + sourceText.WithChanges(TextChange(combinedSpan, replacementString)) |
| 51 | + |
| 52 | + let codeAction = |
| 53 | + CodeAction.Create( |
| 54 | + title, |
| 55 | + (fun (cancellationToken: CancellationToken) -> |
| 56 | + async { |
| 57 | + let! sourceText = context.Document.GetTextAsync(cancellationToken) |> Async.AwaitTask |
| 58 | + return context.Document.WithText(getChangedText sourceText) |
| 59 | + } |> RoslynHelpers.StartAsyncAsTask(cancellationToken)), |
| 60 | + title) |
| 61 | + context.RegisterRefactoring(codeAction) |
| 62 | + } |
| 63 | + |> Async.Ignore |
| 64 | + |> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken) |
0 commit comments