Skip to content

Commit 694abf3

Browse files
committed
Don't crash if we produce bad data in document generation
1 parent 82755ca commit 694abf3

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/FormattingUtilities.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,11 @@ public static void GetOriginalDocumentChangesFromLineInfo(FormattingContext cont
407407
? originalLine.End - originalStart
408408
: lineInfo.FormattedLength;
409409
var formattedStart = formattedLine.Start + formattedIndentation + lineInfo.FormattedOffset;
410-
formattingChanges.Add(new TextChange(new TextSpan(originalStart, length), formattedText.ToString(TextSpan.FromBounds(formattedStart, formattedLine.End - lineInfo.FormattedOffsetFromEndOfLine))));
410+
var formattedEnd = formattedLine.End - lineInfo.FormattedOffsetFromEndOfLine;
411+
if (formattedEnd > formattedStart)
412+
{
413+
formattingChanges.Add(new TextChange(new TextSpan(originalStart, length), formattedText.ToString(TextSpan.FromBounds(formattedStart, formattedEnd))));
414+
}
411415

412416
if (lineInfo.CheckForNewLines)
413417
{

src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/Formatting/FormattingLogTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ public async Task RanOutOfOriginalLines()
8888
await GetFormattingEditsAsync(contents, htmlChangesFile);
8989
}
9090

91+
[Fact]
92+
[WorkItem("https://developercommunity.visualstudio.com/t/Razor-Formatting-Feature-internal-error/11041869#T-ND11043454")]
93+
public async Task MultiLineLambda()
94+
{
95+
var contents = GetResource("InitialDocument.txt");
96+
97+
var document = CreateProjectAndRazorDocument(contents);
98+
99+
var options = new RazorFormattingOptions();
100+
101+
var formattingService = (RazorFormattingService)OOPExportProvider.GetExportedValue<IRazorFormattingService>();
102+
formattingService.GetTestAccessor().SetFormattingLoggerFactory(new TestFormattingLoggerFactory(TestOutputHelper));
103+
104+
await GetFormattingEditsAsync(document, [], span: default, options.CodeBlockBraceOnNextLine, options.AttributeIndentStyle, options.InsertSpaces, options.TabSize, RazorCSharpSyntaxFormattingOptions.Default);
105+
}
106+
91107
private async Task<TextEdit[]?> GetFormattingEditsAsync(string contents, string htmlChangesFile)
92108
{
93109
var document = CreateProjectAndRazorDocument(contents);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<button @onclick="()=>{
2+
StateHasChanged();}">
3+
</button>

0 commit comments

Comments
 (0)