@@ -115,12 +115,13 @@ internal sealed class ExtractToComponentCodeActionResolver(
115
115
} . Uri ;
116
116
117
117
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 ) ;
119
119
120
120
if ( newComponentResult is null )
121
121
{
122
122
return null ;
123
123
}
124
+
124
125
var newComponentContent = newComponentResult . NewContents ;
125
126
var componentNameAndParams = GenerateComponentNameAndParameters ( newComponentResult . Methods , componentName ) ;
126
127
@@ -156,37 +157,6 @@ internal sealed class ExtractToComponentCodeActionResolver(
156
157
}
157
158
} ;
158
159
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
-
190
160
return new WorkspaceEdit
191
161
{
192
162
DocumentChanges = documentChanges ,
@@ -559,7 +529,6 @@ metadata.Value is not null &&
559
529
Uri componentUri ,
560
530
Uri newComponentUri ,
561
531
DocumentContext documentContext ,
562
- Range relevantRange ,
563
532
CancellationToken cancellationToken )
564
533
{
565
534
var contents = await documentContext . GetSourceTextAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
@@ -568,33 +537,13 @@ metadata.Value is not null &&
568
537
return null ;
569
538
}
570
539
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
+
572
544
var extractedContents = contents . GetSubTextString ( new CodeAnalysis . Text . TextSpan ( selectionAnalysis . ExtractStart , selectionAnalysis . ExtractEnd - selectionAnalysis . ExtractStart ) ) . Trim ( ) ;
573
545
var newFileContent = $ "{ dependencies } { ( dependencies . Length > 0 ? Environment . NewLine + Environment . NewLine : "" ) } { extractedContents } ";
574
546
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
-
598
547
// Get CSharpStatements within component
599
548
var syntaxTree = razorCodeDocument . GetSyntaxTree ( ) ;
600
549
var cSharpCodeBlocks = GetCSharpCodeBlocks ( syntaxTree , selectionAnalysis . ExtractStart , selectionAnalysis . ExtractEnd ) ;
@@ -652,14 +601,25 @@ metadata.Value is not null &&
652
601
}
653
602
654
603
var codeBlockAtEnd = GetCodeBlockAtEnd ( syntaxTree ) ;
604
+ if ( codeBlockAtEnd is null )
605
+ {
606
+ return result ;
607
+ }
608
+
655
609
var identifiersInCodeBlock = GetIdentifiersInContext ( codeBlockAtEnd , cSharpCodeBlocks ) ;
656
610
611
+ if ( componentInfo . Methods is null )
612
+ {
613
+ return result ;
614
+ }
615
+
657
616
var methodsInFile = componentInfo . Methods . Select ( method => method . Name ) . ToHashSet ( ) ;
658
617
var methodStringsInContext = methodsInFile . Intersect ( identifiersInCodeBlock ) ;
659
618
var methodsInContext = GetMethodsInContext ( componentInfo , methodStringsInContext ) ;
660
-
661
619
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 ) ;
663
623
var newFileCodeBlock = GenerateNewFileCodeBlock ( promotedMethods , forwardedFields ) ;
664
624
665
625
newFileContent = ReplaceMethodInvocations ( newFileContent , methodsInContext ) ;
@@ -730,16 +690,22 @@ private static HashSet<string> GetIdentifiersInContext(SyntaxNode codeBlockAtEnd
730
690
return identifiersInLastCodeBlock ;
731
691
}
732
692
733
- private static HashSet < MethodInsideRazorElementInfo > ? GetMethodsInContext ( RazorComponentInfo componentInfo , IEnumerable < string > methodStringsInContext )
693
+ private static HashSet < MethodInsideRazorElementInfo > GetMethodsInContext ( RazorComponentInfo componentInfo , IEnumerable < string > methodStringsInContext )
734
694
{
735
695
var methodsInContext = new HashSet < MethodInsideRazorElementInfo > ( ) ;
696
+ if ( componentInfo . Methods is null )
697
+ {
698
+ return methodsInContext ;
699
+ }
700
+
736
701
foreach ( var componentMethod in componentInfo . Methods )
737
702
{
738
703
if ( methodStringsInContext . Contains ( componentMethod . Name ) && ! methodsInContext . Any ( method => method . Name == componentMethod . Name ) )
739
704
{
740
705
methodsInContext . Add ( componentMethod ) ;
741
706
}
742
707
}
708
+
743
709
return methodsInContext ;
744
710
}
745
711
@@ -803,7 +769,8 @@ private static string GeneratePromotedMethods(HashSet<MethodInsideRazorElementIn
803
769
builder . Append ( method . ReturnType ) ;
804
770
}
805
771
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; }}") ;
807
774
if ( parameterCount < totalMethods - 1 )
808
775
{
809
776
builder . AppendLine ( ) ;
@@ -812,6 +779,7 @@ private static string GeneratePromotedMethods(HashSet<MethodInsideRazorElementIn
812
779
813
780
parameterCount ++ ;
814
781
}
782
+
815
783
return builder . ToString ( ) ;
816
784
}
817
785
@@ -834,8 +802,13 @@ private static string GenerateForwardedConstantFields(SyntaxNode codeBlockAtEnd,
834
802
}
835
803
836
804
// 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 )
838
806
{
807
+ if ( componentInfo . Fields is null )
808
+ {
809
+ return [ ] ;
810
+ }
811
+
839
812
var identifiersInFile = componentInfo . Fields . Select ( field => field . Name ) . ToHashSet ( ) ;
840
813
return identifiersInFile . Intersect ( identifiersInCodeBlock ) . ToHashSet ( ) ;
841
814
}
@@ -861,6 +834,7 @@ private static string ReplaceMethodInvocations(string newFileContent, HashSet<Me
861
834
newFileContent = newFileContent . Replace ( method . Name , $ "Parameter{ ( parameterCount > 0 ? parameterCount : "" ) } ") ;
862
835
parameterCount ++ ;
863
836
}
837
+
864
838
return newFileContent ;
865
839
}
866
840
@@ -882,6 +856,7 @@ private static string GenerateComponentNameAndParameters(HashSet<MethodInsideRaz
882
856
builder . Append ( " " ) ;
883
857
parameterCount ++ ;
884
858
}
859
+
885
860
return builder . ToString ( ) ;
886
861
}
887
862
0 commit comments