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 line change calculation to account for source-generated documents #51701

Merged
1 commit merged into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1168,12 +1168,12 @@ public async Task BreakMode_RudeEdits()
public async Task BreakMode_RudeEdits_SourceGenerators()
{
var sourceV1 = @"
// GENERATE: class G { int X1 => 1; }
/* GENERATE: class G { int X1 => 1; } */

class C { int Y => 1; }
";
var sourceV2 = @"
// GENERATE: class G { int X2 => 1; }
/* GENERATE: class G { int X2 => 1; } */

class C { int Y => 2; }
";
Expand Down Expand Up @@ -2210,7 +2210,8 @@ static string EncLogRowToString(EditAndContinueLogEntry row)

private static void GenerateSource(GeneratorExecutionContext context)
{
const string Marker = "// GENERATE: ";
const string OpeningMarker = "/* GENERATE:";
const string ClosingMarker = "*/";

foreach (var syntaxTree in context.Compilation.SyntaxTrees)
{
Expand All @@ -2231,12 +2232,12 @@ private static void GenerateSource(GeneratorExecutionContext context)

void Generate(string source, string fileName)
{
var index = source.IndexOf(Marker);
var index = source.IndexOf(OpeningMarker);
if (index > 0)
{
index += Marker.Length;
var eoln = source.IndexOf('\n', index) + 1;
context.AddSource($"Generated_{fileName}", source[index..eoln]);
index += OpeningMarker.Length;
var closing = source.IndexOf(ClosingMarker, index);
context.AddSource($"Generated_{fileName}", source[index..closing].Trim());
}
}
}
Expand All @@ -2245,12 +2246,12 @@ void Generate(string source, string fileName)
public async Task BreakMode_ValidSignificantChange_SourceGenerators_DocumentUpdate_GeneratedDocumentUpdate()
{
var sourceV1 = @"
// GENERATE: class G { int X => 1; }
/* GENERATE: class G { int X => 1; } */

class C { int Y => 1; }
";
var sourceV2 = @"
// GENERATE: class G { int X => 2; }
/* GENERATE: class G { int X => 2; } */

class C { int Y => 2; }
";
Expand Down Expand Up @@ -2288,14 +2289,77 @@ class C { int Y => 2; }
EndDebuggingSession(service);
}

[Fact]
public async Task BreakMode_ValidSignificantChange_SourceGenerators_DocumentUpdate_GeneratedDocumentUpdate_LineChanges()
{
var sourceV1 = @"
/* GENERATE:
class G
{
int M()
{
return 1;
}
}
*/
";
var sourceV2 = @"
/* GENERATE:
class G
{

int M()
{
return 1;
}
}
*/
";

var generator = new TestSourceGenerator() { ExecuteImpl = GenerateSource };

using var workspace = CreateWorkspace();
var project = AddDefaultTestProject(workspace, sourceV1, generator);

var moduleId = EmitLibrary(sourceV1, generator: generator);
LoadLibraryToDebuggee(moduleId);

var service = CreateEditAndContinueService(workspace);
var debuggingSession = StartDebuggingSession(service);
var editSession = StartEditSession(service, loadedModules: _loadedModulesProvider);

// change the source (valid edit):
var document1 = workspace.CurrentSolution.Projects.Single().Documents.Single();
workspace.ChangeDocument(document1.Id, SourceText.From(sourceV2, Encoding.UTF8));

// validate solution update status and emit:
var (updates, emitDiagnostics) = await service.EmitSolutionUpdateAsync(workspace.CurrentSolution, s_noSolutionActiveSpans, CancellationToken.None).ConfigureAwait(false);
Assert.Empty(emitDiagnostics);
Assert.Equal(ManagedModuleUpdateStatus.Ready, updates.Status);

// check emitted delta:
var delta = updates.Updates.Single();
Assert.Empty(delta.ActiveStatements);

var lineUpdate = delta.SequencePoints.Single();
AssertEx.Equal(new[] { "3 -> 4" }, lineUpdate.LineUpdates.Select(edit => $"{edit.OldLine} -> {edit.NewLine}"));
Assert.NotEmpty(delta.ILDelta);
Assert.NotEmpty(delta.MetadataDelta);
Assert.NotEmpty(delta.PdbDelta);
Assert.Empty(delta.UpdatedMethods);

EndEditSession(service);
EndDebuggingSession(service);
}

[Fact]
public async Task BreakMode_ValidSignificantChange_SourceGenerators_DocumentUpdate_GeneratedDocumentInsert()
{
var sourceV1 = @"
partial class C { int X = 1; }
";
var sourceV2 = @"
// GENERATE: partial class C { int Y = 2; }
/* GENERATE: partial class C { int Y = 2; } */

partial class C { int X = 1; }
";
Expand Down Expand Up @@ -2341,10 +2405,10 @@ class C { int Y => 1; }
";

var additionalSourceV1 = @"
// GENERATE: class G { int X => 1; }
/* GENERATE: class G { int X => 1; } */
";
var additionalSourceV2 = @"
// GENERATE: class G { int X => 2; }
/* GENERATE: class G { int X => 2; } */
";

var generator = new TestSourceGenerator() { ExecuteImpl = GenerateSource };
Expand Down
11 changes: 6 additions & 5 deletions src/Features/Core/Portable/EditAndContinue/EditSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -869,12 +869,13 @@ public async Task<SolutionUpdate> EmitSolutionUpdateAsync(Solution solution, Sol
// since we already checked that no changed document is out-of-sync above.
var oldActiveExceptionRegions = await GetBaseActiveExceptionRegionsAsync(solution, cancellationToken).ConfigureAwait(false);

var lineEdits = projectChanges.LineChanges.SelectAsArray((lineChange, project) =>
var lineEdits = await projectChanges.LineChanges.SelectAsArrayAsync(async lineChange =>
{
var filePath = project.GetDocument(lineChange.DocumentId)!.FilePath;
Contract.ThrowIfNull(filePath);
return new SequencePointUpdates(filePath, lineChange.Changes);
}, newProject);
var document = await newProject.GetDocumentAsync(lineChange.DocumentId, includeSourceGenerated: true, cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(document);
Contract.ThrowIfNull(document.FilePath);
return new SequencePointUpdates(document.FilePath, lineChange.Changes);
}).ConfigureAwait(false);

using var pdbStream = SerializableBytes.CreateWritableStream();
using var metadataStream = SerializableBytes.CreateWritableStream();
Expand Down