Skip to content

Commit

Permalink
Added Workspace handling and some debug stuff dotnet#15562
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianAtWork committed Nov 20, 2023
1 parent c87e14a commit 9d94a18
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,25 @@ type internal AddExplicitReturnType [<ImportingConstructor>] () =
&& 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)
}
)
Expand Down Expand Up @@ -103,7 +104,7 @@ type internal AddExplicitReturnType [<ImportingConstructor>] () =
| 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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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


[<Fact>]
Expand All @@ -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<CodeAction>()

let mutable refactorContext =
CodeRefactoringContext(document, span, Action<CodeActions.CodeAction>(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(),"")

()
}

0 comments on commit 9d94a18

Please sign in to comment.