-
Notifications
You must be signed in to change notification settings - Fork 196
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
Better support for mixed formatting #2526
Conversation
8521542
to
b296c4f
Compare
|
||
// Add a marker at the position where we need the indentation. | ||
var changedText = context.CSharpSourceText; | ||
var marker = $"{context.NewLineString}#line default{context.NewLineString}#line hidden{context.NewLineString}"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried a bunch of different things to use as markers. But this is the only one I found to work in all cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you go a little bit into why the other ones didn't work? Do they just appear in other scenarios? How did you determine that this whole string wouldn't appear otherwise?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a hell of a marker lol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you go a little bit into why the other ones didn't work?
Not entirely sure. Roslyn's IIndentationService
doesn't like it when I provide markers like /**/
or empty lines etc. The API returns wacky results that don't make any sense. I plan to follow up with them to understand this further.
@@ -110,14 +162,14 @@ public async Task<FormattingContext> WithTextAsync(SourceText changedText) | |||
|
|||
var codeDocument = engine.ProcessDesignTime(changedSourceDocument, OriginalSnapshot.FileKind, importSources, OriginalSnapshot.Project.TagHelpers); | |||
|
|||
var newContext = Create(Uri, OriginalSnapshot, codeDocument, Options, Range); | |||
var newContext = Create(Uri, OriginalSnapshot, codeDocument, Options, Range, IsFormatOnType); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was a mistake from before.
@@ -48,7 +48,10 @@ public override void VisitRazorCommentBlock(RazorCommentBlockSyntax node) | |||
// We need to generate a formatting span at this position. So insert a marker in its place. | |||
comment = (SyntaxToken)SyntaxFactory.Token(SyntaxKind.Marker, string.Empty).Green.CreateRed(razorCommentSyntax, razorCommentSyntax.StartCommentStar.EndPosition); | |||
} | |||
|
|||
_currentIndentationLevel++; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes it so that,
@*
some comment
*@
becomes.
@*
some comment
*@
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing structural, just had a couple questions.
@@ -11,6 +11,8 @@ internal static class RazorCodeDocumentExtensions | |||
{ | |||
private static readonly object UnsupportedKey = new object(); | |||
private static readonly object SourceTextKey = new object(); | |||
private static readonly object CSharpSourceTextKey = new object(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird that this should be a "key" when it's just an empty object. Makes sense, it's just not what I would have expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup. This is what we use for storing arbitrary things in the CodeDocument. The other option is to use a string but using an object makes it so that no one else can retrieve it directly without using the methods defined here.
src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormatter.cs
Outdated
Show resolved
Hide resolved
Range range, | ||
DocumentUri uri, | ||
FormattingOptions options, | ||
FormattingContext context, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏 love me some context.
|
||
// Add a marker at the position where we need the indentation. | ||
var changedText = context.CSharpSourceText; | ||
var marker = $"{context.NewLineString}#line default{context.NewLineString}#line hidden{context.NewLineString}"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you go a little bit into why the other ones didn't work? Do they just appear in other scenarios? How did you determine that this whole string wouldn't appear otherwise?
var csharpSourceText = context.CodeDocument.GetCSharpSourceText(); | ||
var spanToFormat = projectedRange.AsTextSpan(csharpSourceText); | ||
var root = await context.CSharpWorkspaceDocument.GetSyntaxRootAsync(cancellationToken); | ||
var workspace = context.CSharpWorkspace; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much cleaner now, good refactor.
@@ -58,7 +58,7 @@ public override async Task<TextEdit[]> FormatAsync(DocumentUri uri, DocumentSnap | |||
} | |||
|
|||
var codeDocument = await documentSnapshot.GetGeneratedOutputAsync(); | |||
var context = FormattingContext.Create(uri, documentSnapshot, codeDocument, options, range); | |||
using var context = FormattingContext.Create(uri, documentSnapshot, codeDocument, options, range); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉
src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormatter.cs
Outdated
Show resolved
Hide resolved
csharpEdits.AddRange(edits.Where(e => range.Contains(e.Range))); | ||
} | ||
|
||
return csharpEdits; | ||
} | ||
|
||
private List<TextChange> AdjustIndentation(FormattingContext context, CancellationToken cancellationToken, Range range = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would eventually move into FormattingPassBase
once I get onTypeFormatting to support mixed cases as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Killer job commenting all of this. It reads really easy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a timeline/issue for moving from reflection to external access? Concerned with perf/maintainability/regressions from non-strongly typed dependencies from this reflection.
src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/FormattingContext.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks incredible! Now all that being said It would be nice to incorporate possibly a few of WTE's edge-case test cases here to ensure that things work as expected for them.
Really impressed, super clear, joy to review. 👏
src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormatter.cs
Outdated
Show resolved
Hide resolved
src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormatter.cs
Outdated
Show resolved
Hide resolved
src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormatter.cs
Outdated
Show resolved
Hide resolved
|
||
// Add a marker at the position where we need the indentation. | ||
var changedText = context.CSharpSourceText; | ||
var marker = $"{context.NewLineString}#line default{context.NewLineString}#line hidden{context.NewLineString}"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a hell of a marker lol
src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormattingPass.cs
Show resolved
Hide resolved
src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormattingPass.cs
Outdated
Show resolved
Hide resolved
csharpEdits.AddRange(edits.Where(e => range.Contains(e.Range))); | ||
} | ||
|
||
return csharpEdits; | ||
} | ||
|
||
private List<TextChange> AdjustIndentation(FormattingContext context, CancellationToken cancellationToken, Range range = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Killer job commenting all of this. It reads really easy
🆙 📅
|
I did incorporate some of them in my tests. That is how I found the |
src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormatter.cs
Outdated
Show resolved
Hide resolved
8a8c89b
to
e059ff1
Compare
Hello @ajaybhargavb! Because this pull request has the p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (
|
Part of https://github.com/dotnet/aspnetcore/issues/14271
Part of https://github.com/dotnet/aspnetcore/issues/23530
Part of https://github.com/dotnet/aspnetcore/issues/25475
Fixes https://github.com/dotnet/aspnetcore/issues/25470