-
Notifications
You must be signed in to change notification settings - Fork 212
Implement "Extract to Foo.razor.css" code action #11989
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
Conversation
...azor/src/Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/RazorCodeActionFactory.cs
Show resolved
Hide resolved
src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Protocol/LanguageServerConstants.cs
Outdated
Show resolved
Hide resolved
.../Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCssCodeActionProvider.cs
Outdated
Show resolved
Hide resolved
.../Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCssCodeActionProvider.cs
Outdated
Show resolved
Hide resolved
.../Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCssCodeActionProvider.cs
Show resolved
Hide resolved
.../Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCssCodeActionResolver.cs
Outdated
Show resolved
Hide resolved
.../Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCssCodeActionResolver.cs
Outdated
Show resolved
Hide resolved
.../Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCssCodeActionResolver.cs
Show resolved
Hide resolved
position: (lastLineNumber, lastLineLength), | ||
newText: lastLineNumber == 0 | ||
? cssContent | ||
: Environment.NewLine + Environment.NewLine + cssContent)] |
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.
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.
Should make no difference. My understanding is the concat will lower to a string.Concat
call, which will pre-allocate a string of the right length, and copy everything in. So there is no intermediate allocation for each piece.
I'm sure Todd or Dustin will correct me if I'm wrong :)
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.
No, the compiler doesn't optimize this, since NewLine
can be different at runtime.
It's correct that this'll just be a string.Contact
call. it'll allocate a string of size Environment.NewLine.Length + Environment.NewLine.Length + cssContent.Length
and perform three copies into the new string to bring the data over. Creating a static with Environment.NewLine + Environment.NewLine
to use instead wouldn't be noticeable from a CPU perspective, unless this were some fabulously tight loop executing millions of times. Even then, it probably wouldn't bubble up.
.../Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCssCodeActionResolver.cs
Outdated
Show resolved
Hide resolved
GetLastLineNumberAndLength(stream, bufferSize: 4096, out lastLineNumber, out lastLineLength); | ||
} | ||
|
||
private static void GetLastLineNumberAndLength(Stream stream, int bufferSize, out int lastLineNumber, out int lastLineLength) |
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.
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.
It could, but also feels pretty niche to me so I'm not sure
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.
[JsonPropertyName("extractStart")] | ||
public int ExtractStart { get; set; } | ||
|
||
[JsonPropertyName("extractEnd")] | ||
public int ExtractEnd { get; set; } | ||
|
||
[JsonPropertyName("removeStart")] | ||
public int RemoveStart { get; set; } | ||
|
||
[JsonPropertyName("removeEnd")] | ||
public int RemoveEnd { get; set; } |
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.
💭 Broader question: Should we just be using TextSpan
for "start/end" pairs of properties like these? After all, we generally assign them with TextSpan.Start
and TextSpan.End
. If you agree, that'd probably be better in a separate change since here are several "params" classes with a similar shape.
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.
These need to be JSON serializable so we'd have to use Range, which means it's probably a wash to me, but I don't mind it. One day I'll beat these provider and resolver classes into a shape I like, and have things spread across less files, so maybe then? :)
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.
Just wanted to ask the question. I'd forgotten that [DataMember]
isn't supported by System.Text.Json, but I'm glad you're keeping track of those details. 😄
It does seem like it'd be pretty easy to add RazorTextSpan
that is JSON-serializable and implicitly convertible to/from TextSpan
, if you fancy that idea.
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 forgot about that type!
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.
Oh, I didn't realize we already had. FWIW, it looks like that type should probably be a record struct rather than a sealed record to avoid heap allocations. Then, it's just a matter of making it implicitly convertible to/from TextSpan it should be super easy to use.
...oft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCodeBehindCodeActionResolver.cs
Outdated
Show resolved
Hide resolved
.../Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCssCodeActionProvider.cs
Outdated
Show resolved
Hide resolved
.../Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCssCodeActionProvider.cs
Outdated
Show resolved
Hide resolved
.../Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCssCodeActionProvider.cs
Show resolved
Hide resolved
position: (lastLineNumber, lastLineLength), | ||
newText: lastLineNumber == 0 | ||
? cssContent | ||
: Environment.NewLine + Environment.NewLine + cssContent)] |
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.
No, the compiler doesn't optimize this, since NewLine
can be different at runtime.
It's correct that this'll just be a string.Contact
call. it'll allocate a string of size Environment.NewLine.Length + Environment.NewLine.Length + cssContent.Length
and perform three copies into the new string to bring the data over. Creating a static with Environment.NewLine + Environment.NewLine
to use instead wouldn't be noticeable from a CPU perspective, unless this were some fabulously tight loop executing millions of times. Even then, it probably wouldn't bubble up.
.../Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCssCodeActionResolver.cs
Outdated
Show resolved
Hide resolved
.../Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCssCodeActionResolver.cs
Outdated
Show resolved
Hide resolved
.../Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCssCodeActionResolver.cs
Outdated
Show resolved
Hide resolved
src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/FileSystem.cs
Outdated
Show resolved
Hide resolved
# Conflicts: # src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Protocol/LanguageServerConstants.cs # src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Resources/SR.resx
Just a little quality-of-life feature I felt I was missing