Skip to content

Commit 1628829

Browse files
committed
Add a test, and fix the code that was previously untested :)
1 parent 184300a commit 1628829

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCssCodeActionProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
4646
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
4747
}
4848

49-
if (owner is MarkupTextLiteralSyntax { Parent: MarkupStartTagSyntax })
49+
// If we're inside an element, move to the start tag so the following checks work as expected
50+
if (owner is MarkupTextLiteralSyntax { Parent: MarkupElementSyntax { StartTag: { } startTag } })
5051
{
51-
owner = owner.Parent;
52+
owner = startTag;
5253
}
5354

5455
// We have to be in a style tag (or inside it, but we'll have moved to the parent if so, above)

src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/CodeActions/ExtractToCssTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,43 @@ await VerifyCodeActionAsync(
262262
""")]);
263263
}
264264

265+
[Fact]
266+
public async Task ExtractToCss_FromInside()
267+
{
268+
await VerifyCodeActionAsync(
269+
input: """
270+
<div></div>
271+
272+
<style>
273+
body {
274+
back[||]ground-color: red;
275+
}
276+
</style>
277+
278+
@code
279+
{
280+
private int x = 1;
281+
}
282+
""",
283+
expected: """
284+
<div></div>
285+
286+
287+
288+
@code
289+
{
290+
private int x = 1;
291+
}
292+
""",
293+
codeActionName: LanguageServerConstants.CodeActions.ExtractToCssAction,
294+
additionalExpectedFiles: [
295+
(FileUri("File1.razor.css"), $$$"""
296+
body {
297+
background-color: red;
298+
}
299+
""")]);
300+
}
301+
265302
[Fact]
266303
public void GetLastLineNumberAndLength()
267304
{

0 commit comments

Comments
 (0)