diff --git a/src/Microsoft.VisualStudio.Editor.Razor/RazorSyntaxFactsServiceExtensions.cs b/src/Microsoft.VisualStudio.Editor.Razor/RazorSyntaxFactsServiceExtensions.cs new file mode 100644 index 0000000000..c64a0210d1 --- /dev/null +++ b/src/Microsoft.VisualStudio.Editor.Razor/RazorSyntaxFactsServiceExtensions.cs @@ -0,0 +1,37 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNetCore.Razor.Language; + +namespace Microsoft.VisualStudio.Editor.Razor +{ + public static class RazorSyntaxFactsServiceExtensions + { + public static bool IsTagHelperSpan(this RazorSyntaxFactsService syntaxFactsService, RazorSyntaxTree syntaxTree, SourceSpan span) + { + if (syntaxFactsService == null) + { + throw new ArgumentNullException(nameof(syntaxFactsService)); + } + + if (syntaxTree == null) + { + // Extra hardening for the case that tooling hasn't retrieved a SyntaxTree yet. + return false; + } + + var tagHelperSpans = syntaxFactsService.GetTagHelperSpans(syntaxTree); + for (var i = 0; i < tagHelperSpans.Count; i++) + { + var tagHelperSpan = tagHelperSpans[i].Span; + if (tagHelperSpan.AbsoluteIndex == span.AbsoluteIndex && tagHelperSpan.Length == span.Length) + { + return true; + } + } + + return false; + } + } +} diff --git a/test/Microsoft.VisualStudio.Editor.Razor.Test/RazorSyntaxFactsServiceExtensionsTest.cs b/test/Microsoft.VisualStudio.Editor.Razor.Test/RazorSyntaxFactsServiceExtensionsTest.cs new file mode 100644 index 0000000000..96e4b7b2ec --- /dev/null +++ b/test/Microsoft.VisualStudio.Editor.Razor.Test/RazorSyntaxFactsServiceExtensionsTest.cs @@ -0,0 +1,82 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNetCore.Razor.Language; +using Xunit; + +namespace Microsoft.VisualStudio.Editor.Razor +{ + public class RazorSyntaxFactsServiceExtensionsTest + { + [Fact] + public void IsTagHelperSpan_ReturnsTrue() + { + // Arrange + var syntaxTree = GetSyntaxTree( +@"