Skip to content

Commit 0b7c1a8

Browse files
committed
Offer extract to code behind at the start of an @code block
Without this fix, depending on where in the line the cursor is, sometimes the first code action is "Extract to Code Behind", and sometimes its "Extract to New Component". The difference is pretty jarring. Before we had "Extract to New Component", there would simply be nothing offered sometimes, but now people see the lightbulb and muscle-memory their way to the wrong action.
1 parent 0029139 commit 0b7c1a8

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
5757
// When the caret is '@$$code' or '@c$$ode' or '@co$$de' or '@cod$$e' then tree is:
5858
// RazorDirective -> RazorDirectiveBody -> MetaCode
5959
RazorDirectiveBodySyntax { Parent: RazorDirectiveSyntax d } => d,
60+
// When the caret is '$$@code' then tree is:
61+
// RazorDirective
62+
RazorDirectiveSyntax d => d,
6063
_ => null
6164
};
6265
if (directiveNode is null)

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,42 @@ await VerifyCodeActionAsync(
2626
<div></div>
2727
2828
29+
""",
30+
codeActionName: LanguageServerConstants.CodeActions.ExtractToCodeBehindAction,
31+
additionalExpectedFiles: [
32+
(FileUri("File1.razor.cs"), $$"""
33+
namespace SomeProject
34+
{
35+
public partial class File1
36+
{
37+
private int x = 1;
38+
}
39+
}
40+
""")]);
41+
}
42+
43+
[Theory]
44+
[InlineData("[||]@code {")]
45+
[InlineData("@[||]code {")]
46+
[InlineData("@c[||]ode {")]
47+
[InlineData("@co[||]de {")]
48+
[InlineData("@cod[||]e {")]
49+
[InlineData("@code[||] {")]
50+
[InlineData("@code[||]{")]
51+
public async Task WorkAtAnyCursorPosition(string codeBlockStart)
52+
{
53+
await VerifyCodeActionAsync(
54+
input: $$"""
55+
<div></div>
56+
57+
{{codeBlockStart}}
58+
private int x = 1;
59+
}
60+
""",
61+
expected: """
62+
<div></div>
63+
64+
2965
""",
3066
codeActionName: LanguageServerConstants.CodeActions.ExtractToCodeBehindAction,
3167
additionalExpectedFiles: [

0 commit comments

Comments
 (0)