Skip to content

Commit 54a350d

Browse files
committed
Always specify a name for code actions, and verify the same
1 parent 909f4d7 commit 54a350d

File tree

7 files changed

+34
-25
lines changed

7 files changed

+34
-25
lines changed

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public static RazorVSInternalCodeAction CreateAddComponentUsing(string @namespac
4848
Title = newTagName is null ? title : $"{newTagName} - {title}",
4949
Data = data,
5050
TelemetryId = s_addComponentUsingTelemetryId,
51-
Priority = VSInternalPriorityLevel.High
51+
Priority = VSInternalPriorityLevel.High,
52+
Name = LanguageServerConstants.CodeActions.AddUsing,
5253
};
5354
return codeAction;
5455
}
@@ -60,46 +61,47 @@ public static RazorVSInternalCodeAction CreateFullyQualifyComponent(string fully
6061
Title = fullyQualifiedName,
6162
Edit = workspaceEdit,
6263
TelemetryId = s_fullyQualifyComponentTelemetryId,
63-
Priority = VSInternalPriorityLevel.High
64+
Priority = VSInternalPriorityLevel.High,
65+
Name = LanguageServerConstants.CodeActions.FullyQualify,
6466
};
6567
return codeAction;
6668
}
6769

6870
public static RazorVSInternalCodeAction CreateComponentFromTag(RazorCodeActionResolutionParams resolutionParams)
6971
{
70-
var title = SR.Create_Component_FromTag_Title;
7172
var data = JsonSerializer.SerializeToElement(resolutionParams);
7273
var codeAction = new RazorVSInternalCodeAction()
7374
{
74-
Title = title,
75+
Title = SR.Create_Component_FromTag_Title,
7576
Data = data,
7677
TelemetryId = s_createComponentFromTagTelemetryId,
78+
Name = LanguageServerConstants.CodeActions.CreateComponentFromTag,
7779
};
7880
return codeAction;
7981
}
8082

8183
public static RazorVSInternalCodeAction CreateExtractToCodeBehind(RazorCodeActionResolutionParams resolutionParams)
8284
{
83-
var title = SR.ExtractTo_CodeBehind_Title;
8485
var data = JsonSerializer.SerializeToElement(resolutionParams);
8586
var codeAction = new RazorVSInternalCodeAction()
8687
{
87-
Title = title,
88+
Title = SR.ExtractTo_CodeBehind_Title,
8889
Data = data,
8990
TelemetryId = s_createExtractToCodeBehindTelemetryId,
91+
Name = LanguageServerConstants.CodeActions.ExtractToCodeBehindAction,
9092
};
9193
return codeAction;
9294
}
9395

9496
public static RazorVSInternalCodeAction CreateExtractToComponent(RazorCodeActionResolutionParams resolutionParams)
9597
{
96-
var title = SR.ExtractTo_Component_Title;
9798
var data = JsonSerializer.SerializeToElement(resolutionParams);
9899
var codeAction = new RazorVSInternalCodeAction()
99100
{
100-
Title = title,
101+
Title = SR.ExtractTo_Component_Title,
101102
Data = data,
102103
TelemetryId = s_createExtractToComponentTelemetryId,
104+
Name = LanguageServerConstants.CodeActions.ExtractToNewComponentAction,
103105
};
104106
return codeAction;
105107
}
@@ -127,7 +129,8 @@ public static RazorVSInternalCodeAction CreateGenerateMethod(VSTextDocumentIdent
127129
{
128130
Title = title,
129131
Data = data,
130-
TelemetryId = s_generateMethodTelemetryId
132+
TelemetryId = s_generateMethodTelemetryId,
133+
Name = LanguageServerConstants.CodeActions.GenerateEventHandler,
131134
};
132135
return codeAction;
133136
}
@@ -155,7 +158,8 @@ public static RazorVSInternalCodeAction CreateAsyncGenerateMethod(VSTextDocument
155158
{
156159
Title = title,
157160
Data = data,
158-
TelemetryId = s_generateAsyncMethodTelemetryId
161+
TelemetryId = s_generateAsyncMethodTelemetryId,
162+
Name = LanguageServerConstants.CodeActions.GenerateAsyncEventHandler,
159163
};
160164
return codeAction;
161165
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Protocol/LanguageServerConstants.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public static class CodeActions
3535
{
3636
public const string GenerateEventHandler = "GenerateEventHandler";
3737

38+
public const string GenerateAsyncEventHandler = "GenerateAsyncEventHandler";
39+
3840
public const string EditBasedCodeActionCommand = "EditBasedCodeActionCommand";
3941

4042
public const string ExtractToCodeBehindAction = "ExtractToCodeBehind";
@@ -45,6 +47,8 @@ public static class CodeActions
4547

4648
public const string AddUsing = "AddUsing";
4749

50+
public const string FullyQualify = "FullyQualify";
51+
4852
public const string PromoteUsingDirective = "PromoteUsingDirective";
4953

5054
public const string CodeActionFromVSCode = "CodeActionFromVSCode";

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private protected async Task VerifyCodeActionAsync(TestCode input, string? expec
116116
Assert.NotNull(result);
117117
Assert.NotEmpty(result);
118118

119-
var codeActionToRun = (VSInternalCodeAction?)result.SingleOrDefault(e => ((RazorVSInternalCodeAction)e.Value!).Name == codeActionName || ((RazorVSInternalCodeAction)e.Value!).Title == codeActionName).Value;
119+
var codeActionToRun = (VSInternalCodeAction?)result.SingleOrDefault(e => ((RazorVSInternalCodeAction)e.Value!).Name == codeActionName).Value;
120120

121121
if (!expectOffer)
122122
{
@@ -125,10 +125,10 @@ private protected async Task VerifyCodeActionAsync(TestCode input, string? expec
125125
}
126126

127127
AssertEx.NotNull(codeActionToRun, $"""
128-
Could not find code action with name or title '{codeActionName}'.
128+
Could not find code action with name '{codeActionName}'.
129129
130130
Available:
131-
{string.Join(Environment.NewLine + " ", result.Select(e => $"{((RazorVSInternalCodeAction)e.Value!).Name} or {((RazorVSInternalCodeAction)e.Value!).Title}"))}
131+
{string.Join(Environment.NewLine + " ", result.Select(e => ((RazorVSInternalCodeAction)e.Value!).Name))}
132132
""");
133133

134134
if (codeActionToRun.Children?.Length > 0)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

44
using System.Threading.Tasks;
5+
using Microsoft.CodeAnalysis.Razor.Protocol;
56
using Xunit;
67
using Xunit.Abstractions;
7-
using WorkspacesSR = Microsoft.CodeAnalysis.Razor.Workspaces.Resources.SR;
88

99
namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost.CodeActions;
1010

@@ -24,7 +24,7 @@ await VerifyCodeActionAsync(
2424
2525
<Hello><Hello>
2626
""",
27-
codeActionName: WorkspacesSR.Create_Component_FromTag_Title,
27+
codeActionName: LanguageServerConstants.CodeActions.CreateComponentFromTag,
2828
additionalExpectedFiles: [
2929
(FileUri("Hello.razor"), "")]);
3030
}
@@ -43,7 +43,7 @@ await VerifyCodeActionAsync(
4343
4444
<Hello><Hello>
4545
""",
46-
codeActionName: WorkspacesSR.Create_Component_FromTag_Title,
46+
codeActionName: LanguageServerConstants.CodeActions.CreateComponentFromTag,
4747
additionalExpectedFiles: [
4848
(FileUri("Hello.razor"), "")]);
4949
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

44
using System.Threading.Tasks;
5+
using Microsoft.CodeAnalysis.Razor.Protocol;
56
using Xunit;
67
using Xunit.Abstractions;
7-
using WorkspacesSR = Microsoft.CodeAnalysis.Razor.Workspaces.Resources.SR;
88

99
namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost.CodeActions;
1010

@@ -27,7 +27,7 @@ await VerifyCodeActionAsync(
2727
2828
2929
""",
30-
codeActionName: WorkspacesSR.ExtractTo_CodeBehind_Title,
30+
codeActionName: LanguageServerConstants.CodeActions.ExtractToCodeBehindAction,
3131
additionalExpectedFiles: [
3232
(FileUri("File1.razor.cs"), $$"""
3333
namespace SomeProject

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

44
using System.Threading.Tasks;
5+
using Microsoft.CodeAnalysis.Razor.Protocol;
56
using Xunit;
67
using Xunit.Abstractions;
7-
using WorkspacesSR = Microsoft.CodeAnalysis.Razor.Workspaces.Resources.SR;
88

99
namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost.CodeActions;
1010

@@ -30,7 +30,7 @@ Hello World
3030
3131
<div></div>
3232
""",
33-
codeActionName: WorkspacesSR.ExtractTo_Component_Title,
33+
codeActionName: LanguageServerConstants.CodeActions.ExtractToNewComponentAction,
3434
additionalExpectedFiles: [
3535
(FileUri("Component.razor"), """
3636
<div>
@@ -55,6 +55,6 @@ Hello World
5555
<div></div>
5656
""",
5757
expected: null,
58-
codeActionName: WorkspacesSR.ExtractTo_Component_Title);
58+
codeActionName: LanguageServerConstants.CodeActions.ExtractToNewComponentAction);
5959
}
6060
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

44
using System.Threading.Tasks;
5+
using Microsoft.CodeAnalysis.Razor.Protocol;
56
using Xunit;
67
using Xunit.Abstractions;
78

@@ -23,7 +24,7 @@ await VerifyCodeActionAsync(
2324
2425
<EditForm></EditForm>
2526
""",
26-
codeActionName: "EditForm");
27+
codeActionName: LanguageServerConstants.CodeActions.FullyQualify);
2728
}
2829

2930
[Fact]
@@ -40,7 +41,7 @@ await VerifyCodeActionAsync(
4041
4142
<Microsoft.AspNetCore.Components.Sections.SectionOutlet></Microsoft.AspNetCore.Components.Sections.SectionOutlet>
4243
""",
43-
codeActionName: "Microsoft.AspNetCore.Components.Sections.SectionOutlet");
44+
codeActionName: LanguageServerConstants.CodeActions.FullyQualify);
4445
}
4546

4647
[Fact]
@@ -58,7 +59,7 @@ @using Microsoft.AspNetCore.Components.Sections
5859
5960
<SectionOutlet></SectionOutlet>
6061
""",
61-
codeActionName: "@using Microsoft.AspNetCore.Components.Sections");
62+
codeActionName: LanguageServerConstants.CodeActions.AddUsing);
6263
}
6364

6465
[Fact]
@@ -76,6 +77,6 @@ @using Microsoft.AspNetCore.Components.Sections
7677
7778
<SectionOutlet></SectionOutlet>
7879
""",
79-
codeActionName: "SectionOutlet - @using Microsoft.AspNetCore.Components.Sections");
80+
codeActionName: LanguageServerConstants.CodeActions.AddUsing);
8081
}
8182
}

0 commit comments

Comments
 (0)