File tree Expand file tree Collapse file tree 4 files changed +49
-5
lines changed
src/Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions
test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/CodeActions Expand file tree Collapse file tree 4 files changed +49
-5
lines changed Original file line number Diff line number Diff line change 66using System . Threading ;
77using System . Threading . Tasks ;
88using Microsoft . AspNetCore . Razor . Language ;
9+ using Microsoft . AspNetCore . Razor . Language . Components ;
910using Microsoft . AspNetCore . Razor . Language . Syntax ;
1011using Microsoft . AspNetCore . Razor . Threading ;
1112using Microsoft . CodeAnalysis . Razor . CodeActions . Models ;
@@ -26,6 +27,14 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
2627 return SpecializedTasks . EmptyImmutableArray < RazorVSInternalCodeAction > ( ) ;
2728 }
2829
30+ if ( context . ContainsDiagnostic ( ComponentDiagnosticFactory . UnexpectedMarkupElement . Id ) &&
31+ ! context . HasSelection )
32+ {
33+ // If we are telling the user that a component doesn't exist, and they just have their cursor in the tag, they
34+ // won't get any benefit from extracting a non-existing component to a new component.
35+ return SpecializedTasks . EmptyImmutableArray < RazorVSInternalCodeAction > ( ) ;
36+ }
37+
2938 if ( ! FileKinds . IsComponent ( context . CodeDocument . FileKind ) )
3039 {
3140 return SpecializedTasks . EmptyImmutableArray < RazorVSInternalCodeAction > ( ) ;
Original file line number Diff line number Diff line change 22// Licensed under the MIT license. See License.txt in the project root for license information.
33
44using System ;
5- using System . Collections . Generic ;
65using System . Collections . Immutable ;
76using System . Diagnostics . CodeAnalysis ;
8- using System . Linq ;
97using System . Threading ;
108using System . Threading . Tasks ;
119using Microsoft . AspNetCore . Razor ;
1412using Microsoft . AspNetCore . Razor . Language . Intermediate ;
1513using Microsoft . AspNetCore . Razor . Language . Syntax ;
1614using Microsoft . AspNetCore . Razor . Threading ;
17- using Microsoft . CodeAnalysis ;
1815using Microsoft . CodeAnalysis . Razor . CodeActions . Models ;
1916using SyntaxFacts = Microsoft . CodeAnalysis . CSharp . SyntaxFacts ;
2017
@@ -26,8 +23,7 @@ internal class GenerateMethodCodeActionProvider : IRazorCodeActionProvider
2623{
2724 public Task < ImmutableArray < RazorVSInternalCodeAction > > ProvideAsync ( RazorCodeActionContext context , CancellationToken cancellationToken )
2825 {
29- var nameNotExistDiagnostics = context . Request . Context . Diagnostics . Any ( d => d . Code == "CS0103" ) ;
30- if ( ! nameNotExistDiagnostics )
26+ if ( ! context . ContainsDiagnostic ( "CS0103" ) )
3127 {
3228 return SpecializedTasks . EmptyImmutableArray < RazorVSInternalCodeAction > ( ) ;
3329 }
Original file line number Diff line number Diff line change @@ -22,4 +22,24 @@ internal sealed record class RazorCodeActionContext(
2222 bool SupportsCodeActionResolve )
2323{
2424 public bool HasSelection => StartAbsoluteIndex != EndAbsoluteIndex ;
25+
26+ public bool ContainsDiagnostic ( string code )
27+ {
28+ if ( Request . Context . Diagnostics is null )
29+ {
30+ return false ;
31+ }
32+
33+ foreach ( var diagnostic in Request . Context . Diagnostics )
34+ {
35+ if ( diagnostic . Code is { } codeSumType &&
36+ codeSumType . TryGetSecond ( out var codeString ) &&
37+ codeString == code )
38+ {
39+ return true ;
40+ }
41+ }
42+
43+ return false ;
44+ }
2545}
Original file line number Diff line number Diff line change @@ -38,4 +38,23 @@ Hello World
3838 </div>
3939 """ ) ] ) ;
4040 }
41+
42+ [ Fact ]
43+ public async Task DontOfferOnNonExistentComponent ( )
44+ {
45+ await VerifyCodeActionAsync (
46+ input : """
47+ <div></div>
48+
49+ <div>
50+ Hello World
51+ </div>
52+
53+ <{|RZ10012:Not$$AComponent|} />
54+
55+ <div></div>
56+ """ ,
57+ expected : null ,
58+ codeActionName : WorkspacesSR . ExtractTo_Component_Title ) ;
59+ }
4160}
You can’t perform that action at this time.
0 commit comments