Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix null encoding in new document in move type action #74623

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeRefactorings.MoveType;
using Microsoft.CodeAnalysis.Contracts.EditAndContinue;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
Expand Down Expand Up @@ -3949,6 +3950,87 @@ static void F()
ExitBreakState(debuggingSession);
}

/// <summary>
/// Scenario:
/// - F5
/// - edit source and apply code fix to move type to file XYZ.cs
/// - continue execution
/// </summary>
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/72984")]
public async Task EditAndContinueAfterApplyingMoveTypeToFileCodeFix()
{
const string movedType = "AnotherClass";

const string source = $$"""
class {{movedType}}
{
public const bool True = true;
}

class Test
{
static bool B() => {{movedType}}.True;
static void G() { while (B()); }

static void F()
{
G();
}
}
""";

const string modifiedSource = $$"""
class Test
{
static bool A() => {{movedType}}.True;
static bool B() => A();
static void G() { while (B()); }

static void F()
{
H();
}

static void H()
{
G();
}
}
""";

var moduleId = EmitAndLoadLibraryToDebuggee(source);

using var workspace = CreateWorkspace(out var solution, out var service);
(solution, var document) = AddDefaultTestProject(solution, source);
var documentId = document.Id;
var oldProject = document.Project;

var debuggingSession = await StartDebuggingSessionAsync(service, solution);

// Apply code fix: Move type to AnotherClass.cs

var moveTypeService = document.GetLanguageService<IMoveTypeService>();
var root = await document.GetSyntaxRootAsync();
var span = root.DescendantTokens()
.Where(s => s.Text is movedType)
.FirstOrDefault()
.Span;
var modifiedSolution = await moveTypeService.GetModifiedSolutionAsync(document, span, MoveTypeOperationKind.MoveType, cancellationToken: default);

// Apply edit on remaining document: source after code fix -> modifiedSource

modifiedSolution = modifiedSolution.WithDocumentText(document.Id, CreateText(modifiedSource));

var newProject = modifiedSolution.GetProject(oldProject.Id);
Assert.Equal(1, oldProject.DocumentIds.Count);
Assert.Equal(2, newProject.DocumentIds.Count);

var (updates, emitDiagnostics) = await EmitSolutionUpdateAsync(debuggingSession, modifiedSolution);
Assert.Empty(emitDiagnostics);
Assert.False(updates.Updates.IsEmpty);
Assert.Equal(ModuleUpdateStatus.Ready, updates.Status);
}

[Fact]
public async Task MultiSession()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,8 @@ internal DocumentState UpdateTree(SyntaxNode newRoot, PreservationMode mode)
}
else
{
// the existing encoding was never observed so is unknown.
encoding = null;
// the existing encoding was never observed so we try to use the one from the desired root.
encoding = newRoot.SyntaxTree.Encoding;
Rekkonnect marked this conversation as resolved.
Show resolved Hide resolved
}

var syntaxTreeFactory = LanguageServices.GetRequiredService<ISyntaxTreeFactoryService>();
Expand Down
Loading