Skip to content

Commit 06ef015

Browse files
committed
PR Feedback
1 parent 4c2e17e commit 06ef015

File tree

12 files changed

+130
-55
lines changed

12 files changed

+130
-55
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
113113
var resolutionParams = new RazorCodeActionResolutionParams()
114114
{
115115
TextDocument = context.Request.TextDocument,
116-
Action = LanguageServerConstants.CodeActions.ExtractToCodeBehindAction,
116+
Action = LanguageServerConstants.CodeActions.ExtractToCodeBehind,
117117
Language = RazorLanguageKind.Razor,
118118
DelegatedDocumentUri = context.DelegatedDocumentUri,
119119
Data = actionParams,

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal class ExtractToCodeBehindCodeActionResolver(
2727
private readonly LanguageServerFeatureOptions _languageServerFeatureOptions = languageServerFeatureOptions;
2828
private readonly IRoslynCodeActionHelpers _roslynCodeActionHelpers = roslynCodeActionHelpers;
2929

30-
public string Action => LanguageServerConstants.CodeActions.ExtractToCodeBehindAction;
30+
public string Action => LanguageServerConstants.CodeActions.ExtractToCodeBehind;
3131

3232
public async Task<WorkspaceEdit?> ResolveAsync(DocumentContext documentContext, JsonElement data, RazorFormattingOptions options, CancellationToken cancellationToken)
3333
{
@@ -37,19 +37,14 @@ internal class ExtractToCodeBehindCodeActionResolver(
3737
return null;
3838
}
3939

40-
if (!documentContext.FileKind.IsComponent())
41-
{
42-
return null;
43-
}
44-
4540
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
4641

4742
var path = FilePathNormalizer.Normalize(documentContext.Uri.GetAbsoluteOrUNCPath());
4843
var codeBehindPath = FileUtilities.GenerateUniquePath(path, $"{Path.GetExtension(path)}.cs");
4944

5045
// VS Code in Windows expects path to start with '/'
5146
var updatedCodeBehindPath = _languageServerFeatureOptions.ReturnCodeActionAndRenamePathsWithPrefixedSlash && !codeBehindPath.StartsWith("/")
52-
? '/' + codeBehindPath
47+
? $"/{codeBehindPath}"
5348
: codeBehindPath;
5449

5550
var codeBehindUri = LspFactory.CreateFilePathUri(updatedCodeBehindPath);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
7474
var resolutionParams = new RazorCodeActionResolutionParams()
7575
{
7676
TextDocument = context.Request.TextDocument,
77-
Action = LanguageServerConstants.CodeActions.ExtractToNewComponentAction,
77+
Action = LanguageServerConstants.CodeActions.ExtractToNewComponent,
7878
Language = RazorLanguageKind.Razor,
7979
DelegatedDocumentUri = context.DelegatedDocumentUri,
8080
Data = actionParams,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal class ExtractToComponentCodeActionResolver(
2727
{
2828
private readonly LanguageServerFeatureOptions _languageServerFeatureOptions = languageServerFeatureOptions;
2929

30-
public string Action => LanguageServerConstants.CodeActions.ExtractToNewComponentAction;
30+
public string Action => LanguageServerConstants.CodeActions.ExtractToNewComponent;
3131

3232
public async Task<WorkspaceEdit?> ResolveAsync(DocumentContext documentContext, JsonElement data, RazorFormattingOptions options, CancellationToken cancellationToken)
3333
{
@@ -53,7 +53,7 @@ internal class ExtractToComponentCodeActionResolver(
5353

5454
// VS Code in Windows expects path to start with '/'
5555
componentPath = _languageServerFeatureOptions.ReturnCodeActionAndRenamePathsWithPrefixedSlash && !componentPath.StartsWith('/')
56-
? '/' + componentPath
56+
? $"/{componentPath}"
5757
: componentPath;
5858

5959
var newComponentUri = new DocumentUri(LspFactory.CreateFilePathUri(componentPath));

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
4242
var owner = root.FindInnermostNode(context.StartAbsoluteIndex);
4343
if (owner is null)
4444
{
45-
_logger.LogWarning($"Owner should never be null.");
45+
_logger.LogWarning("Owner should never be null.");
4646
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
4747
}
4848

@@ -60,11 +60,17 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
6060

6161
// If there is any C# or Razor in the style tag, we can't offer, so it has to be one big text literal.
6262
if (owner.Parent is not MarkupElementSyntax { Body: [MarkupTextLiteralSyntax textLiteral] } markupElement ||
63-
textLiteral.ChildNodes().Count() > 0)
63+
textLiteral.ChildNodes().Any())
6464
{
6565
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
6666
}
6767

68+
if (textLiteral.LiteralTokens.All(t => t.IsWhitespace()))
69+
{
70+
// If the text literal is all whitespace, we don't want to offer the action.
71+
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
72+
}
73+
6874
// If there are diagnostics, we can't trust the tree to be what we expect.
6975
if (markupElement.GetDiagnostics().Any(d => d.Severity == RazorDiagnosticSeverity.Error))
7076
{
@@ -82,14 +88,13 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
8288
var resolutionParams = new RazorCodeActionResolutionParams()
8389
{
8490
TextDocument = context.Request.TextDocument,
85-
Action = LanguageServerConstants.CodeActions.ExtractToCssAction,
91+
Action = LanguageServerConstants.CodeActions.ExtractToCss,
8692
Language = RazorLanguageKind.Razor,
8793
DelegatedDocumentUri = context.DelegatedDocumentUri,
8894
Data = actionParams,
8995
};
9096

9197
var razorFileName = Path.GetFileName(context.Request.TextDocument.DocumentUri.GetAbsoluteOrUNCPath());
92-
9398
var codeAction = RazorCodeActionFactory.CreateExtractToCss(razorFileName, resolutionParams);
9499
return Task.FromResult<ImmutableArray<RazorVSInternalCodeAction>>([codeAction]);
95100
}

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Text.Json;
88
using System.Threading;
99
using System.Threading.Tasks;
10-
using Microsoft.AspNetCore.Razor.Language;
1110
using Microsoft.AspNetCore.Razor.PooledObjects;
1211
using Microsoft.CodeAnalysis.Razor.CodeActions.Models;
1312
using Microsoft.CodeAnalysis.Razor.Formatting;
@@ -26,7 +25,7 @@ internal class ExtractToCssCodeActionResolver(
2625
private readonly LanguageServerFeatureOptions _languageServerFeatureOptions = languageServerFeatureOptions;
2726
private readonly IFileSystem _fileSystem = fileSystem;
2827

29-
public string Action => LanguageServerConstants.CodeActions.ExtractToCssAction;
28+
public string Action => LanguageServerConstants.CodeActions.ExtractToCss;
3029

3130
public async Task<WorkspaceEdit?> ResolveAsync(DocumentContext documentContext, JsonElement data, RazorFormattingOptions options, CancellationToken cancellationToken)
3231
{
@@ -36,18 +35,13 @@ internal class ExtractToCssCodeActionResolver(
3635
return null;
3736
}
3837

39-
if (!documentContext.FileKind.IsComponent())
40-
{
41-
return null;
42-
}
43-
4438
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
4539

4640
var cssFilePath = $"{FilePathNormalizer.Normalize(documentContext.Uri.GetAbsoluteOrUNCPath())}.css";
4741

4842
// VS Code in Windows expects path to start with '/'
4943
cssFilePath = _languageServerFeatureOptions.ReturnCodeActionAndRenamePathsWithPrefixedSlash && !cssFilePath.StartsWith("/")
50-
? '/' + cssFilePath
44+
? $"/{cssFilePath}"
5145
: cssFilePath;
5246

5347
var cssFileUri = LspFactory.CreateFilePathUri(cssFilePath);
@@ -79,7 +73,7 @@ internal class ExtractToCssCodeActionResolver(
7973
TextDocument = cssDocumentIdentifier,
8074
Edits = [LspFactory.CreateTextEdit(
8175
position: (lastLineNumber, lastLineLength),
82-
newText: lastLineNumber == 0
76+
newText: lastLineNumber == 0 && lastLineLength == 0
8377
? cssContent
8478
: Environment.NewLine + Environment.NewLine + cssContent)]
8579
});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static RazorVSInternalCodeAction CreateExtractToCss(string razorFileName,
9292
Title = SR.FormatExtractTo_Css_Title(razorFileName),
9393
Data = data,
9494
TelemetryId = s_createExtractToCssTelemetryId,
95-
Name = LanguageServerConstants.CodeActions.ExtractToCssAction,
95+
Name = LanguageServerConstants.CodeActions.ExtractToCss,
9696
};
9797
return codeAction;
9898
}
@@ -105,7 +105,7 @@ public static RazorVSInternalCodeAction CreateExtractToCodeBehind(RazorCodeActio
105105
Title = SR.ExtractTo_CodeBehind_Title,
106106
Data = data,
107107
TelemetryId = s_createExtractToCodeBehindTelemetryId,
108-
Name = LanguageServerConstants.CodeActions.ExtractToCodeBehindAction,
108+
Name = LanguageServerConstants.CodeActions.ExtractToCodeBehind,
109109
};
110110
return codeAction;
111111
}
@@ -118,7 +118,7 @@ public static RazorVSInternalCodeAction CreateExtractToComponent(RazorCodeAction
118118
Title = SR.ExtractTo_Component_Title,
119119
Data = data,
120120
TelemetryId = s_createExtractToComponentTelemetryId,
121-
Name = LanguageServerConstants.CodeActions.ExtractToNewComponentAction,
121+
Name = LanguageServerConstants.CodeActions.ExtractToNewComponent,
122122
// Since Extract to Component is offered basically everywhere, always offer it last
123123
Order = 9999
124124
};

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,38 +33,38 @@ internal static class LanguageServerConstants
3333

3434
public static class CodeActions
3535
{
36-
public const string GenerateEventHandler = "GenerateEventHandler";
36+
public const string GenerateEventHandler = nameof(GenerateEventHandler);
3737

38-
public const string GenerateAsyncEventHandler = "GenerateAsyncEventHandler";
38+
public const string GenerateAsyncEventHandler = nameof(GenerateAsyncEventHandler);
3939

40-
public const string EditBasedCodeActionCommand = "EditBasedCodeActionCommand";
40+
public const string EditBasedCodeActionCommand = nameof(EditBasedCodeActionCommand);
4141

42-
public const string ExtractToCodeBehindAction = "ExtractToCodeBehind";
42+
public const string ExtractToCodeBehind = nameof(ExtractToCodeBehind);
4343

44-
public const string ExtractToCssAction = "ExtractToCss";
44+
public const string ExtractToCss = nameof(ExtractToCss);
4545

46-
public const string ExtractToNewComponentAction = "ExtractToNewComponent";
46+
public const string ExtractToNewComponent = nameof(ExtractToNewComponent);
4747

48-
public const string CreateComponentFromTag = "CreateComponentFromTag";
48+
public const string CreateComponentFromTag = nameof(CreateComponentFromTag);
4949

50-
public const string AddUsing = "AddUsing";
50+
public const string AddUsing = nameof(AddUsing);
5151

52-
public const string FullyQualify = "FullyQualify";
52+
public const string FullyQualify = nameof(FullyQualify);
5353

54-
public const string PromoteUsingDirective = "PromoteUsingDirective";
54+
public const string PromoteUsingDirective = nameof(PromoteUsingDirective);
5555

56-
public const string CodeActionFromVSCode = "CodeActionFromVSCode";
56+
public const string CodeActionFromVSCode = nameof(CodeActionFromVSCode);
5757

58-
public const string WrapAttributes = "WrapAttributes";
58+
public const string WrapAttributes = nameof(WrapAttributes);
5959

6060
/// <summary>
6161
/// Remaps without formatting the resolved code action edit
6262
/// </summary>
63-
public const string UnformattedRemap = "UnformattedRemap";
63+
public const string UnformattedRemap = nameof(UnformattedRemap);
6464

6565
/// <summary>
6666
/// Remaps and formats the resolved code action edit
6767
/// </summary>
68-
public const string Default = "Default";
68+
public const string Default = nameof(Default);
6969
}
7070
}

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ExtractToComponentCodeActionResolverTest.NetFx.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ private async Task TestAsync(
304304
null);
305305

306306
Assert.NotEmpty(result);
307-
var codeActionToRun = GetCodeActionToRun(LanguageServerConstants.CodeActions.ExtractToNewComponentAction, 0, result);
307+
var codeActionToRun = GetCodeActionToRun(LanguageServerConstants.CodeActions.ExtractToNewComponent, 0, result);
308308

309309
if (expectedNewComponent is null)
310310
{

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
@@ -27,7 +27,7 @@ await VerifyCodeActionAsync(
2727
2828
2929
""",
30-
codeActionName: LanguageServerConstants.CodeActions.ExtractToCodeBehindAction,
30+
codeActionName: LanguageServerConstants.CodeActions.ExtractToCodeBehind,
3131
additionalExpectedFiles: [
3232
(FileUri("File1.razor.cs"), $$"""
3333
namespace SomeProject
@@ -63,7 +63,7 @@ await VerifyCodeActionAsync(
6363
6464
6565
""",
66-
codeActionName: LanguageServerConstants.CodeActions.ExtractToCodeBehindAction,
66+
codeActionName: LanguageServerConstants.CodeActions.ExtractToCodeBehind,
6767
additionalExpectedFiles: [
6868
(FileUri("File1.razor.cs"), $$"""
6969
namespace SomeProject

0 commit comments

Comments
 (0)