Skip to content

Commit b37e6d8

Browse files
committed
Updated functionality to match Roslyn endpoint response, finished attribute promotion functionality, Resolver now handles event handlers and data binding
1 parent 0f547f3 commit b37e6d8

File tree

5 files changed

+221
-312
lines changed

5 files changed

+221
-312
lines changed

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

+15-15
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
3838
}
3939

4040
var syntaxTree = context.CodeDocument.GetSyntaxTree();
41-
if (!IsSelectionValid(context, syntaxTree))
41+
if (!IsValidSelection(context, syntaxTree))
4242
{
4343
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
4444
}
@@ -80,7 +80,7 @@ private static bool IsValidContext(RazorCodeActionContext context)
8080
context.CodeDocument.GetSyntaxTree()?.Root is not null;
8181
}
8282

83-
private bool IsSelectionValid(RazorCodeActionContext context, RazorSyntaxTree syntaxTree)
83+
private bool IsValidSelection(RazorCodeActionContext context, RazorSyntaxTree syntaxTree)
8484
{
8585
var owner = syntaxTree.Root.FindInnermostNode(context.Location.AbsoluteIndex, includeWhitespace: true);
8686
if (owner is null)
@@ -90,33 +90,33 @@ private bool IsSelectionValid(RazorCodeActionContext context, RazorSyntaxTree sy
9090
}
9191

9292
var startElementNode = owner.FirstAncestorOrSelf<MarkupSyntaxNode>(node => node is MarkupElementSyntax or MarkupTagHelperElementSyntax);
93-
return startElementNode is not null && !HasDiagnosticErrors(startElementNode) && !IsInsideProperHtmlContent(context, owner);
93+
return startElementNode is not null && HasNoDiagnosticErrors(startElementNode) && IsInsideMarkupTag(context, owner);
9494
}
9595

96-
private static bool IsInsideProperHtmlContent(RazorCodeActionContext context, SyntaxNode owner)
96+
private static bool IsInsideMarkupTag(RazorCodeActionContext context, SyntaxNode owner)
9797
{
9898
// The selection could start either in a MarkupElement or MarkupTagHelperElement.
99-
// Both of these have the necessary properties to do this check, but not the base MarkupSyntaxNode.
100-
// The workaround for this is to try to cast to the specific types and then do the check.
99+
// Both of these have the necessary properties to do this check, but the base class MarkupSyntaxNode does not.
100+
// The workaround for this is to try to find the specific types as ancestors and then do the check.
101101

102102
var tryMakeMarkupElement = owner.FirstAncestorOrSelf<MarkupElementSyntax>();
103103
var tryMakeMarkupTagHelperElement = owner.FirstAncestorOrSelf<MarkupTagHelperElementSyntax>();
104104

105-
var isLocationInProperMarkupElement = tryMakeMarkupElement is not null &&
106-
context.Location.AbsoluteIndex > tryMakeMarkupElement.StartTag.Span.End &&
107-
context.Location.AbsoluteIndex < tryMakeMarkupElement.EndTag.SpanStart;
105+
var isLocationInElementTag = tryMakeMarkupElement is not null &&
106+
(tryMakeMarkupElement.StartTag.Span.Contains(context.Location.AbsoluteIndex) ||
107+
tryMakeMarkupElement.EndTag.Span.Contains(context.Location.AbsoluteIndex));
108108

109-
var isLocationInProperMarkupTagHelper = tryMakeMarkupTagHelperElement is not null &&
110-
context.Location.AbsoluteIndex > tryMakeMarkupTagHelperElement.StartTag.Span.End &&
111-
context.Location.AbsoluteIndex < tryMakeMarkupTagHelperElement.EndTag.SpanStart;
109+
var isLocationInTagHelperTag = tryMakeMarkupTagHelperElement is not null &&
110+
(tryMakeMarkupTagHelperElement.StartTag.Span.Contains(context.Location.AbsoluteIndex) ||
111+
tryMakeMarkupTagHelperElement.EndTag.Span.Contains(context.Location.AbsoluteIndex));
112112

113-
return isLocationInProperMarkupElement || isLocationInProperMarkupTagHelper;
113+
return isLocationInElementTag || isLocationInTagHelperTag;
114114
}
115115

116-
private static bool HasDiagnosticErrors(MarkupSyntaxNode markupElement)
116+
private static bool HasNoDiagnosticErrors(MarkupSyntaxNode markupElement)
117117
{
118118
var diagnostics = markupElement.GetDiagnostics();
119-
return diagnostics.Any(d => d.Severity == RazorDiagnosticSeverity.Error);
119+
return !diagnostics.Any(d => d.Severity == RazorDiagnosticSeverity.Error);
120120
}
121121

122122
private static bool TryGetNamespace(RazorCodeDocument codeDocument, [NotNullWhen(returnValue: true)] out string? @namespace)

0 commit comments

Comments
 (0)