Skip to content

Commit 74ab788

Browse files
committed
Nits and reverted some changes that werent supposed to make it
1 parent 00e9d32 commit 74ab788

File tree

4 files changed

+43
-83
lines changed

4 files changed

+43
-83
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToComponentCodeActionResolver.cs

Lines changed: 37 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,13 @@ internal sealed class ExtractToComponentCodeActionResolver(
115115
}.Uri;
116116

117117
var componentName = Path.GetFileNameWithoutExtension(componentPath);
118-
var newComponentResult = await GenerateNewComponentAsync(selectionAnalysis, codeDocument, actionParams.Uri, newComponentUri, documentContext, removeRange, cancellationToken).ConfigureAwait(false);
118+
var newComponentResult = await GenerateNewComponentAsync(selectionAnalysis, codeDocument, actionParams.Uri, newComponentUri, documentContext, cancellationToken).ConfigureAwait(false);
119119

120120
if (newComponentResult is null)
121121
{
122122
return null;
123123
}
124+
124125
var newComponentContent = newComponentResult.NewContents;
125126
var componentNameAndParams = GenerateComponentNameAndParameters(newComponentResult.Methods, componentName);
126127

@@ -156,37 +157,6 @@ internal sealed class ExtractToComponentCodeActionResolver(
156157
}
157158
};
158159

159-
//if (!_documentContextFactory.TryCreateForOpenDocument(newComponentUri, out var versionedDocumentContext))
160-
//{
161-
// throw new InvalidOperationException("Failed to create a versioned document context for the new component");
162-
//}
163-
164-
//var formattingOptions = new VisualStudio.LanguageServer.Protocol.FormattingOptions()
165-
//{
166-
// TabSize = _razorLSPOptionsMonitor.CurrentValue.TabSize,
167-
// InsertSpaces = _razorLSPOptionsMonitor.CurrentValue.InsertSpaces,
168-
// OtherOptions = new Dictionary<string, object>
169-
// {
170-
// { "trimTrailingWhitespace", true },
171-
// { "insertFinalNewline", true },
172-
// { "trimFinalNewlines", true },
173-
// },
174-
//};
175-
176-
//TextEdit[]? formattedEdits;
177-
//try
178-
//{
179-
// formattedEdits = await _razorFormattingService.FormatAsync(
180-
// documentContext,
181-
// range: removeRange,
182-
// formattingOptions,
183-
// cancellationToken: default).ConfigureAwait(false);
184-
//}
185-
//catch (Exception ex)
186-
//{
187-
// throw new InvalidOperationException("Failed to format the new component", ex);
188-
//}
189-
190160
return new WorkspaceEdit
191161
{
192162
DocumentChanges = documentChanges,
@@ -559,7 +529,6 @@ metadata.Value is not null &&
559529
Uri componentUri,
560530
Uri newComponentUri,
561531
DocumentContext documentContext,
562-
Range relevantRange,
563532
CancellationToken cancellationToken)
564533
{
565534
var contents = await documentContext.GetSourceTextAsync(cancellationToken).ConfigureAwait(false);
@@ -568,33 +537,13 @@ metadata.Value is not null &&
568537
return null;
569538
}
570539

571-
var dependencies = string.Join(Environment.NewLine, selectionAnalysis.ComponentDependencies);
540+
var dependencies = selectionAnalysis.ComponentDependencies is not null
541+
? string.Join(Environment.NewLine, selectionAnalysis.ComponentDependencies)
542+
: string.Empty;
543+
572544
var extractedContents = contents.GetSubTextString(new CodeAnalysis.Text.TextSpan(selectionAnalysis.ExtractStart, selectionAnalysis.ExtractEnd - selectionAnalysis.ExtractStart)).Trim();
573545
var newFileContent = $"{dependencies}{(dependencies.Length > 0 ? Environment.NewLine + Environment.NewLine : "")}{extractedContents}";
574546

575-
//var formattingParams = new FormatNewFileParams
576-
//{
577-
// Project = new TextDocumentIdentifier
578-
// {
579-
// Uri = new Uri(documentContext.Project.FilePath, UriKind.Absolute)
580-
// },
581-
// Document = new TextDocumentIdentifier
582-
// {
583-
// Uri = newComponentUri
584-
// },
585-
// Contents = newFileContent
586-
//};
587-
588-
//string fixedContent = string.Empty;
589-
//try
590-
//{
591-
// fixedContent = await _clientConnection.SendRequestAsync<FormatNewFileParams, string?>(CustomMessageNames.RazorFormatNewFileEndpointName, formattingParams, cancellationToken: default).ConfigureAwait(false);
592-
//}
593-
//catch (Exception ex)
594-
//{
595-
// throw new InvalidOperationException("Failed to send request to RazorFormatNewFileEndpoint", ex);
596-
//}
597-
598547
// Get CSharpStatements within component
599548
var syntaxTree = razorCodeDocument.GetSyntaxTree();
600549
var cSharpCodeBlocks = GetCSharpCodeBlocks(syntaxTree, selectionAnalysis.ExtractStart, selectionAnalysis.ExtractEnd);
@@ -652,14 +601,25 @@ metadata.Value is not null &&
652601
}
653602

654603
var codeBlockAtEnd = GetCodeBlockAtEnd(syntaxTree);
604+
if (codeBlockAtEnd is null)
605+
{
606+
return result;
607+
}
608+
655609
var identifiersInCodeBlock = GetIdentifiersInContext(codeBlockAtEnd, cSharpCodeBlocks);
656610

611+
if (componentInfo.Methods is null)
612+
{
613+
return result;
614+
}
615+
657616
var methodsInFile = componentInfo.Methods.Select(method => method.Name).ToHashSet();
658617
var methodStringsInContext = methodsInFile.Intersect(identifiersInCodeBlock);
659618
var methodsInContext = GetMethodsInContext(componentInfo, methodStringsInContext);
660-
661619
var promotedMethods = GeneratePromotedMethods(methodsInContext);
662-
var forwardedFields = GenerateForwardedConstantFields(codeBlockAtEnd, GetFieldsInContext(componentInfo, identifiersInCodeBlock));
620+
621+
var fieldsInContext = GetFieldsInContext(componentInfo, identifiersInCodeBlock);
622+
var forwardedFields = GenerateForwardedConstantFields(codeBlockAtEnd, fieldsInContext);
663623
var newFileCodeBlock = GenerateNewFileCodeBlock(promotedMethods, forwardedFields);
664624

665625
newFileContent = ReplaceMethodInvocations(newFileContent, methodsInContext);
@@ -730,16 +690,22 @@ private static HashSet<string> GetIdentifiersInContext(SyntaxNode codeBlockAtEnd
730690
return identifiersInLastCodeBlock;
731691
}
732692

733-
private static HashSet<MethodInsideRazorElementInfo>? GetMethodsInContext(RazorComponentInfo componentInfo, IEnumerable<string> methodStringsInContext)
693+
private static HashSet<MethodInsideRazorElementInfo> GetMethodsInContext(RazorComponentInfo componentInfo, IEnumerable<string> methodStringsInContext)
734694
{
735695
var methodsInContext = new HashSet<MethodInsideRazorElementInfo>();
696+
if (componentInfo.Methods is null)
697+
{
698+
return methodsInContext;
699+
}
700+
736701
foreach (var componentMethod in componentInfo.Methods)
737702
{
738703
if (methodStringsInContext.Contains(componentMethod.Name) && !methodsInContext.Any(method => method.Name == componentMethod.Name))
739704
{
740705
methodsInContext.Add(componentMethod);
741706
}
742707
}
708+
743709
return methodsInContext;
744710
}
745711

@@ -803,7 +769,8 @@ private static string GeneratePromotedMethods(HashSet<MethodInsideRazorElementIn
803769
builder.Append(method.ReturnType);
804770
}
805771

806-
builder.Append($"{(method.ReturnType == "void" ? (method.ParameterTypes.Count > 0 ? ">" : "") : ">")}? Parameter{(parameterCount > 0 ? parameterCount : "")} {{ get; set; }}");
772+
builder.Append($"{(method.ReturnType == "void" ? (method.ParameterTypes.Count > 0 ? ">" : "") : ">")}? " +
773+
$"Parameter{(parameterCount > 0 ? parameterCount : "")} {{ get; set; }}");
807774
if (parameterCount < totalMethods - 1)
808775
{
809776
builder.AppendLine();
@@ -812,6 +779,7 @@ private static string GeneratePromotedMethods(HashSet<MethodInsideRazorElementIn
812779

813780
parameterCount++;
814781
}
782+
815783
return builder.ToString();
816784
}
817785

@@ -834,8 +802,13 @@ private static string GenerateForwardedConstantFields(SyntaxNode codeBlockAtEnd,
834802
}
835803

836804
// GetFieldsInContext(componentInfo, identifiersInCodeBlock)
837-
private static HashSet<string>? GetFieldsInContext(RazorComponentInfo componentInfo, HashSet<string> identifiersInCodeBlock)
805+
private static HashSet<string> GetFieldsInContext(RazorComponentInfo componentInfo, HashSet<string> identifiersInCodeBlock)
838806
{
807+
if (componentInfo.Fields is null)
808+
{
809+
return [];
810+
}
811+
839812
var identifiersInFile = componentInfo.Fields.Select(field => field.Name).ToHashSet();
840813
return identifiersInFile.Intersect(identifiersInCodeBlock).ToHashSet();
841814
}
@@ -861,6 +834,7 @@ private static string ReplaceMethodInvocations(string newFileContent, HashSet<Me
861834
newFileContent = newFileContent.Replace(method.Name, $"Parameter{(parameterCount > 0 ? parameterCount : "")}");
862835
parameterCount++;
863836
}
837+
864838
return newFileContent;
865839
}
866840

@@ -882,6 +856,7 @@ private static string GenerateComponentNameAndParameters(HashSet<MethodInsideRaz
882856
builder.Append(" ");
883857
parameterCount++;
884858
}
859+
885860
return builder.ToString();
886861
}
887862

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Protocol/CodeActions/RazorComponentInfoParams.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
65
using System.Collections.Generic;
76
using System.Runtime.Serialization;
87
using System.Text.Json.Serialization;
9-
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
10-
using Microsoft.CodeAnalysis.Remote;
118
using Microsoft.VisualStudio.LanguageServer.Protocol;
129

1310
namespace Microsoft.CodeAnalysis.Razor.Protocol.CodeActions;
@@ -38,9 +35,8 @@ internal sealed record RazorComponentInfoParams
3835
// Not sure where to put these two records
3936
internal sealed record RazorComponentInfo
4037
{
41-
public required List<MethodInsideRazorElementInfo>? Methods { get; set; }
42-
public required List<SymbolInsideRazorElementInfo>? Fields { get; set; }
43-
38+
public required List<MethodInsideRazorElementInfo> Methods { get; set; }
39+
public required List<SymbolInsideRazorElementInfo> Fields { get; set; }
4440
}
4541

4642
internal sealed record MethodInsideRazorElementInfo
@@ -49,7 +45,7 @@ internal sealed record MethodInsideRazorElementInfo
4945

5046
public required string ReturnType { get; set; }
5147

52-
public required List<string>? ParameterTypes { get; set; }
48+
public required List<string> ParameterTypes { get; set; }
5349
}
5450

5551
internal sealed record SymbolInsideRazorElementInfo

src/Razor/src/Microsoft.VisualStudio.LanguageServer.ContainedLanguage/DefaultLSPRequestInvoker.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,9 @@ public async override Task<ReinvokeResponse<TOut>> ReinvokeRequestOnServerAsync<
5454
throw new ArgumentException("message", nameof(method));
5555
}
5656

57-
58-
TOut? response;
59-
try
60-
{
61-
response = await _languageServiceBroker.RequestAsync(
62-
new GeneralRequest<TIn, TOut> { LanguageServerName = languageServerName, Method = method, Request = parameters },
63-
cancellationToken);
64-
}
65-
catch (Exception ex)
66-
{
67-
throw new InvalidOperationException($"Failed to invoke language server '{languageServerName}' with method '{method}'.", ex);
68-
}
69-
57+
var response = await _languageServiceBroker.RequestAsync(
58+
new GeneralRequest<TIn, TOut> { LanguageServerName = languageServerName, Method = method, Request = parameters },
59+
cancellationToken);
7060

7161
// No callers actually use the languageClient when handling the response.
7262
var result = response is not null ? new ReinvokeResponse<TOut>(languageClient: null!, response) : default;

src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Endpoints/RazorCustomMessageTarget_RazorComponentInfo.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Microsoft.CodeAnalysis.Razor.Protocol;
88
using Microsoft.CodeAnalysis.Razor.Protocol.CodeActions;
99
using Microsoft.VisualStudio.LanguageServer.ContainedLanguage;
10-
using Microsoft.VisualStudio.LanguageServer.Protocol;
1110
using StreamJsonRpc;
1211

1312
namespace Microsoft.VisualStudio.Razor.LanguageClient.Endpoints;

0 commit comments

Comments
 (0)