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

Workaround Roslyn formatting bug #2051

Closed
wants to merge 2 commits into from
Closed
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 @@ -36,7 +36,7 @@ public static async Task<IEnumerable<LinePositionSpanTextChange>> GetFormattingC
Debug.Assert(targetLine.Text != null);
if (!string.IsNullOrWhiteSpace(targetLine.Text!.ToString(targetLine.Span)))
{
return await GetFormattingChanges(document, targetLine.Start, targetLine.End, omnisharpOptions, loggerFactory);
return await GetFormattingChanges(document, targetLine.Start, targetLine.End, omnisharpOptions, loggerFactory, trimExtendingChanges: true);
}
}
else if (character == '}' || character == ';')
Expand All @@ -47,7 +47,7 @@ public static async Task<IEnumerable<LinePositionSpanTextChange>> GetFormattingC
var node = FindFormatTarget(root!, position);
if (node != null)
{
return await GetFormattingChanges(document, node.FullSpan.Start, node.FullSpan.End, omnisharpOptions, loggerFactory);
return await GetFormattingChanges(document, node.FullSpan.Start, node.FullSpan.End, omnisharpOptions, loggerFactory, trimExtendingChanges: false);
}
}

Expand Down Expand Up @@ -91,10 +91,10 @@ public static async Task<IEnumerable<LinePositionSpanTextChange>> GetFormattingC
return null;
}

public static async Task<IEnumerable<LinePositionSpanTextChange>> GetFormattingChanges(Document document, int start, int end, OmniSharpOptions omnisharpOptions, ILoggerFactory loggerFactory)
public static async Task<IEnumerable<LinePositionSpanTextChange>> GetFormattingChanges(Document document, int start, int end, OmniSharpOptions omnisharpOptions, ILoggerFactory loggerFactory, bool trimExtendingChanges = false)
{
var newDocument = await FormatDocument(document, omnisharpOptions, loggerFactory, TextSpan.FromBounds(start, end));
return await TextChanges.GetAsync(newDocument, document);
return await TextChanges.GetAsync(newDocument, document, trimExtendingChanges ? end : (int?)null);
}

public static async Task<string> GetFormattedText(Document document, OmniSharpOptions omnisharpOptions, ILoggerFactory loggerFactory)
Expand Down
37 changes: 33 additions & 4 deletions src/OmniSharp.Roslyn/Utilities/TextChangeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ namespace OmniSharp.Roslyn.Utilities
{
internal static class TextChanges
{
public static async Task<IEnumerable<LinePositionSpanTextChange>> GetAsync(Document document, Document oldDocument)
public static async Task<IEnumerable<LinePositionSpanTextChange>> GetAsync(Document document, Document oldDocument, int? originalEnd = null)
{
var changes = await document.GetTextChangesAsync(oldDocument);
var oldText = await oldDocument.GetTextAsync();

return Convert(oldText, changes);
return Convert(oldText, changes, originalEnd);
}

public static IEnumerable<LinePositionSpanTextChange> Convert(SourceText oldText, params TextChange[] changes)
{
return Convert(oldText, (IEnumerable<TextChange>)changes);
}

public static IEnumerable<LinePositionSpanTextChange> Convert(SourceText oldText, IEnumerable<TextChange> changes)
public static IEnumerable<LinePositionSpanTextChange> Convert(SourceText oldText, IEnumerable<TextChange> changes, int? originalEnd = null)
{
return changes
.OrderByDescending(change => change.Span)
Expand All @@ -35,6 +35,35 @@ public static IEnumerable<LinePositionSpanTextChange> Convert(SourceText oldText

if (newText.Length > 0)
{
// Due to https://github.com/dotnet/roslyn/issues/50129, we might get a text change
// that extends further than the requested end bound. This doesn't matter for every
// scenario, but is annoying when hitting enter and having a trailing space on the previous
// line, as that will end up removing the newly-added indentation on the current line.
// For the cases that matters, originalEnd will be not null, and we reduce the end of the span
// to be that location.
if (originalEnd is int oEnd && span.Start < oEnd && span.End > oEnd)
{
// Since the new change is beyond the requested line, it's also going to end in either a
// \r\n or \n, which replaces the newline on the current line. To avoid that newline
// becoming an extra blank line, we extend the span an addition 2 or 1 characters to replace
// that point.
int newLength = span.Length - (span.End - oEnd);
if (newText[newText.Length - 1] == '\n')
{
// The new text ends with a newline. Check the original text to see what type of newline
// it used at the end location and increase by that amount
newLength += oldText[oEnd] switch
{
'\r' => 2,
'\n' => 1,
_ => 0
};
}

span = new TextSpan(span.Start, newLength);
}


// Roslyn computes text changes on character arrays. So it might happen that a
// change starts inbetween \r\n which is OK when you are offset-based but a problem
// when you are line,column-based. This code extends text edits which just overlap
Expand Down Expand Up @@ -67,7 +96,7 @@ public static IEnumerable<LinePositionSpanTextChange> Convert(SourceText oldText
EndLine = linePositionSpan.End.Line,
EndColumn = linePositionSpan.End.Character
};
});
}).ToList();
}
}
}
100 changes: 100 additions & 0 deletions tests/OmniSharp.Roslyn.CSharp.Tests/FormatAfterKeystrokeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,106 @@ public void M()
{
///
$$
}
}");
}

[Theory]
[InlineData("file.cs")]
[InlineData("file.csx")]
public async Task LeadingSpacesNotRemovedOnNewline_ExtraWhitespaceOnPreviousLine(string fileName)
{
await VerifyChange(fileName, "\n",
@"class C
{
public void M()
{
// There is a space after the ; here
var i = 1;
$$
}
}",
@"class C
{
public void M()
{
// There is a space after the ; here
var i = 1;

}
}");
}

[Theory]
[InlineData("file.cs")]
[InlineData("file.csx")]
public async Task LeadingSpacesNotRemovedOnNewline_FormattingChangesOnPreviousLine(string fileName)
{
await VerifyChange(fileName, "\n",
@"class C
{
public void M()
{
var i =1;
$$
}
}",
@"class C
{
public void M()
{
var i = 1;

}
}");
}

[Theory]
[InlineData("file.cs")]
[InlineData("file.csx")]
public async Task LeadingSpacesNotRemovedOnNewline_NewlinesWithSpaceAfter(string fileName)
{
await VerifyChange(fileName, "\n",
@"class C
{
public void M()
{
var i =1;
$$

}
}",
@"class C
{
public void M()
{
var i = 1;


}
}");
}

[Theory(Skip = "https://github.com/dotnet/roslyn/issues/50130")]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure there's a good way to work around this one until the upstream bug is fixed.

[InlineData("file.cs")]
[InlineData("file.csx")]
public async Task LeadingSpacesNotRemovedOnNewline_IncompleteDeclaration(string fileName)
{
await VerifyChange(fileName, "\n",
@"class C
{
public void M()
{
var i
$$
}
}",
@"class C
{
public void M()
{
var i

}
}");
}
Expand Down