diff --git a/vsintegration/src/FSharp.Editor/Refactor/AddExplicitReturnType.fs b/vsintegration/src/FSharp.Editor/Refactor/AddExplicitReturnType.fs index c603939f9fe..1b019803a04 100644 --- a/vsintegration/src/FSharp.Editor/Refactor/AddExplicitReturnType.fs +++ b/vsintegration/src/FSharp.Editor/Refactor/AddExplicitReturnType.fs @@ -32,24 +32,25 @@ type internal AddExplicitReturnType [] () = && not (parseFileResults.IsTypeAnnotationGivenAtPosition symbolUse.Range.Start) - static member refactor (context:CodeRefactoringContext) (symbolUse:FSharpSymbolUse,memberFunc:FSharpMemberOrFunctionOrValue,symbolSpan) = + static member refactor (context:CodeRefactoringContext) (symbolUse:FSharpSymbolUse,memberFunc:FSharpMemberOrFunctionOrValue,symbolSpan:TextSpan,textLine:TextLine) = let typeString = memberFunc.FullType.FormatWithConstraints symbolUse.DisplayContext let title = SR.AddExplicitReturnTypeAnnotation() let getChangedText (sourceText: SourceText) = - let debugInfo = $"{sourceText} : {typeString} : {symbolSpan}" + let debugInfo = $"{sourceText} : {typeString} : {symbolSpan} : {textLine}" debugInfo - let sub = sourceText.ToString(symbolSpan) + let sub = sourceText.ToString(textLine.Span) let newSub = sub.Replace("=", $" :{memberFunc.ReturnParameter.Type.TypeDefinition.DisplayName}=") - sourceText.Replace(symbolSpan, newSub) + sourceText.Replace(textLine.Span, newSub) let codeActionFunc = (fun (cancellationToken: CancellationToken) -> - backgroundTask { + task { let! sourceText = context.Document.GetTextAsync(cancellationToken) let changedText = getChangedText sourceText + context.Document.Project.Solution.Id return context.Document.WithText(changedText) } ) @@ -103,7 +104,7 @@ type internal AddExplicitReturnType [] () = | Some span -> Some(symbolUse,memberFunc,span) | None -> None ) - |> Option.map(fun (symbolUse,memberFunc,textSpan) -> AddExplicitReturnType.refactor context (symbolUse,memberFunc,textSpan)) + |> Option.map(fun (symbolUse,memberFunc,textSpan) -> AddExplicitReturnType.refactor context (symbolUse,memberFunc,textSpan,textLine)) return res } diff --git a/vsintegration/tests/FSharp.Editor.Tests/Refactors/AddExplicitReturnType.fs b/vsintegration/tests/FSharp.Editor.Tests/Refactors/AddExplicitReturnType.fs index 14495b8ce07..9ee0f116aae 100644 --- a/vsintegration/tests/FSharp.Editor.Tests/Refactors/AddExplicitReturnType.fs +++ b/vsintegration/tests/FSharp.Editor.Tests/Refactors/AddExplicitReturnType.fs @@ -17,6 +17,9 @@ open FSharp.Editor.Tests.Helpers open Microsoft.CodeAnalysis.CodeRefactorings open NUnit.Framework +open Microsoft.CodeAnalysis.CodeActions +open System.Collections.Generic +open Microsoft.VisualStudio.LanguageServices [] @@ -29,24 +32,35 @@ let ``Refactor changes something`` () = let! ct = Async.CancellationToken - let sourceText = SourceText.From code - let document = RoslynTestHelpers.GetFsDocument code + let solution = RoslynTestHelpers.CreateSolution(code) + let workspace = solution.Workspace + let document = RoslynTestHelpers.GetSingleDocument solution + let spanStart = code.IndexOf "sum" let span = TextSpan(spanStart, 3) + + + let refactorProvider = AddExplicitReturnType() + let refactoringActions= new List() let mutable refactorContext = - CodeRefactoringContext(document, span, Action(fun a -> ()), ct) + CodeRefactoringContext(document, span, (fun a -> refactoringActions.Add (a)), ct) - let refactorProvider = AddExplicitReturnType() - let expected = None do! refactorProvider.ComputeRefactoringsAsync refactorContext - let newText = match refactorContext.TextDocument.TryGetText() with - | true,result -> result - | false,_ -> sourceText - let! text = refactorContext.TextDocument.GetTextAsync( ct) - Assert.AreNotEqual(sourceText.ToString(),newText.ToString(),"") + let! operations = refactoringActions[0].GetOperationsAsync(ct) + + for operation in operations do + operation.Apply (workspace,ct) + + + + let! refactorText = refactorContext.TextDocument.GetTextAsync ct + refactorText + let! text = document.GetTextAsync ct + solution.Id + Assert.AreNotEqual(code,text.ToString(),"") () }