diff --git a/src/Microsoft.AspNetCore.Blazor/ITagHelper.cs b/src/Microsoft.AspNetCore.Blazor/ITagHelper.cs new file mode 100644 index 0000000000..28409cdf3b --- /dev/null +++ b/src/Microsoft.AspNetCore.Blazor/ITagHelper.cs @@ -0,0 +1,16 @@ +// 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 System.Collections.Generic; +using System.Text; + +namespace Microsoft.AspNetCore.Razor.TagHelpers +{ + // This is an unfortunate workaround due to https://github.com/aspnet/Razor/issues/2482 + // The Razor tooling looks for a type with exactly this name and will prevent tag helper + // discovery if it is not found. + internal class ITagHelper + { + } +} diff --git a/tooling/Microsoft.VisualStudio.LanguageServices.Blazor/RazorTextViewListener.cs b/tooling/Microsoft.VisualStudio.LanguageServices.Blazor/RazorTextViewListener.cs index 8126ef2e0a..a6344d515d 100644 --- a/tooling/Microsoft.VisualStudio.LanguageServices.Blazor/RazorTextViewListener.cs +++ b/tooling/Microsoft.VisualStudio.LanguageServices.Blazor/RazorTextViewListener.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -8,6 +8,7 @@ using System.ComponentModel.Composition; using System.Linq; using System.Reflection; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Host; using Microsoft.CodeAnalysis.Razor; using Microsoft.VisualStudio.Editor.Razor; using Microsoft.VisualStudio.Text; @@ -27,6 +28,7 @@ namespace Microsoft.VisualStudio.LanguageServices.Blazor private readonly HashSet _openViews; private Type _codeGeneratorType; + private Type _projectSnapshotManagerType; [ImportingConstructor] public BlazorOpenDocumentTracker( @@ -109,13 +111,49 @@ namespace Microsoft.VisualStudio.LanguageServices.Blazor break; } - OnDeclarationsChanged(); + OnDeclarationsChanged(e.NewSolution.GetProject(e.ProjectId)); break; } } - private void OnDeclarationsChanged() + private void OnDeclarationsChanged(Project project) { + // In 15.8 the Razor Language Services provides the actual Tag Helper discovery logic. + // We can interface with that if we're running in a 15.8 build. + if (_projectSnapshotManagerType == null && _codeGeneratorType == null) + { + try + { + var assembly = typeof(Microsoft.CodeAnalysis.Razor.IProjectEngineFactory).Assembly; + _projectSnapshotManagerType = assembly.GetType("Microsoft.CodeAnalysis.Razor.ProjectSystem.ProjectSnapshotManager"); + } + catch (Exception) + { + // If the above fails, try the 15.7 logic. + } + } + + if (_projectSnapshotManagerType != null) + { + try + { + var languageServices = _workspace.Services.GetLanguageServices(RazorLanguage.Name); + var manager = languageServices + .GetType() + .GetMethod(nameof(HostLanguageServices.GetService)) + .MakeGenericMethod(_projectSnapshotManagerType) + .Invoke(languageServices, null); + + manager.GetType().GetMethod("WorkspaceProjectChanged").Invoke(manager, new object[] { project, }); + return; + } + catch (Exception) + { + // If the above fails, try the 15.7 logic. + } + } + + // This is a design-time Razor file change.Go poke all of the open // Razor documents and tell them to update. var buffers = _openViews