Skip to content

Commit

Permalink
Move TagHelperTooltipFactories to the Workspaces layer (#10980)
Browse files Browse the repository at this point in the history
This change decouples the two TagHelperTooltipFactories from the Razor
language server and moves them to the Workspaces layer for use in
co-hosting. Ultimately, co-hosting will be able to do a better job
producing hover and completion item tooltips, but that can come later.
  • Loading branch information
DustinCampbell authored Oct 8, 2024
2 parents 82f7008 + 29055de commit ef8467d
Show file tree
Hide file tree
Showing 55 changed files with 1,845 additions and 1,821 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,19 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.LanguageServer.Tooltip;
using Microsoft.AspNetCore.Razor.LanguageServer.ProjectSystem;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Razor.Completion;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Tooltip;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Microsoft.VisualStudio.Text.Adornments;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Completion;

internal class RazorCompletionItemResolver : CompletionItemResolver
internal class RazorCompletionItemResolver(IProjectSnapshotManager projectManager) : CompletionItemResolver
{
private readonly LSPTagHelperTooltipFactory _lspTagHelperTooltipFactory;
private readonly VSLSPTagHelperTooltipFactory _vsLspTagHelperTooltipFactory;

public RazorCompletionItemResolver(
LSPTagHelperTooltipFactory lspTagHelperTooltipFactory,
VSLSPTagHelperTooltipFactory vsLspTagHelperTooltipFactory)
{
_lspTagHelperTooltipFactory = lspTagHelperTooltipFactory;
_vsLspTagHelperTooltipFactory = vsLspTagHelperTooltipFactory;
}
private readonly IProjectSnapshotManager _projectManager = projectManager;

public override async Task<VSInternalCompletionItem?> ResolveAsync(
VSInternalCompletionItem completionItem,
Expand Down Expand Up @@ -109,11 +102,11 @@ public RazorCompletionItemResolver(

if (useDescriptionProperty)
{
_vsLspTagHelperTooltipFactory.TryCreateTooltip(descriptionInfo, out tagHelperClassifiedTextTooltip);
ClassifiedTagHelperTooltipFactory.TryCreateTooltip(descriptionInfo, out tagHelperClassifiedTextTooltip);
}
else
{
_lspTagHelperTooltipFactory.TryCreateTooltip(descriptionInfo, documentationKind, out tagHelperMarkupTooltip);
MarkupTagHelperTooltipFactory.TryCreateTooltip(descriptionInfo, documentationKind, out tagHelperMarkupTooltip);
}

break;
Expand All @@ -128,11 +121,15 @@ public RazorCompletionItemResolver(

if (useDescriptionProperty)
{
tagHelperClassifiedTextTooltip = await _vsLspTagHelperTooltipFactory.TryCreateTooltipAsync(razorCompletionResolveContext.FilePath, descriptionInfo, cancellationToken).ConfigureAwait(false);
tagHelperClassifiedTextTooltip = await ClassifiedTagHelperTooltipFactory
.TryCreateTooltipAsync(razorCompletionResolveContext.FilePath, descriptionInfo, _projectManager.GetQueryOperations(), cancellationToken)
.ConfigureAwait(false);
}
else
{
tagHelperMarkupTooltip = await _lspTagHelperTooltipFactory.TryCreateTooltipAsync(razorCompletionResolveContext.FilePath, descriptionInfo, documentationKind, cancellationToken).ConfigureAwait(false);
tagHelperMarkupTooltip = await MarkupTagHelperTooltipFactory
.TryCreateTooltipAsync(razorCompletionResolveContext.FilePath, descriptionInfo, _projectManager.GetQueryOperations(), documentationKind, cancellationToken)
.ConfigureAwait(false);
}

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using Microsoft.AspNetCore.Razor.LanguageServer.ProjectSystem;
using Microsoft.AspNetCore.Razor.LanguageServer.Semantic;
using Microsoft.AspNetCore.Razor.LanguageServer.SpellCheck;
using Microsoft.AspNetCore.Razor.LanguageServer.Tooltip;
using Microsoft.AspNetCore.Razor.ProjectEngineHost;
using Microsoft.CodeAnalysis.Razor.Completion;
using Microsoft.CodeAnalysis.Razor.Diagnostics;
Expand Down Expand Up @@ -221,9 +220,6 @@ public static void AddDocumentManagementServices(this IServiceCollection service
services.AddSingleton<IDocumentProcessedListener, GeneratedDocumentSynchronizer>();
services.AddSingleton<IDocumentProcessedListener, CodeDocumentReferenceHolder>();

services.AddSingleton<LSPTagHelperTooltipFactory, DefaultLSPTagHelperTooltipFactory>();
services.AddSingleton<VSLSPTagHelperTooltipFactory, DefaultVSLSPTagHelperTooltipFactory>();

// Add project snapshot manager
services.AddSingleton<IProjectEngineFactoryProvider, LspProjectEngineFactoryProvider>();
services.AddSingleton<IProjectSnapshotManager, LspProjectSnapshotManager>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Legacy;
using Microsoft.AspNetCore.Razor.Language.Syntax;
using Microsoft.AspNetCore.Razor.LanguageServer.Tooltip;
using Microsoft.AspNetCore.Razor.LanguageServer.ProjectSystem;
using Microsoft.CodeAnalysis.Razor;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
Expand All @@ -26,13 +26,11 @@
namespace Microsoft.AspNetCore.Razor.LanguageServer.Hover;

internal sealed partial class HoverService(
LSPTagHelperTooltipFactory lspTagHelperTooltipFactory,
VSLSPTagHelperTooltipFactory vsLspTagHelperTooltipFactory,
IProjectSnapshotManager projectManager,
IDocumentMappingService documentMappingService,
IClientCapabilitiesService clientCapabilitiesService) : IHoverService
{
private readonly LSPTagHelperTooltipFactory _lspTagHelperTooltipFactory = lspTagHelperTooltipFactory;
private readonly VSLSPTagHelperTooltipFactory _vsLspTagHelperTooltipFactory = vsLspTagHelperTooltipFactory;
private readonly IProjectSnapshotManager _projectManager = projectManager;
private readonly IDocumentMappingService _documentMappingService = documentMappingService;
private readonly IClientCapabilitiesService _clientCapabilitiesService = clientCapabilitiesService;

Expand Down Expand Up @@ -261,7 +259,7 @@ internal sealed partial class HoverService(
var attrDescriptionInfo = new AggregateBoundAttributeDescription(descriptionInfos);

var isVSClient = clientCapabilities.SupportsVisualStudioExtensions;
if (isVSClient && _vsLspTagHelperTooltipFactory.TryCreateTooltip(attrDescriptionInfo, out ContainerElement? classifiedTextElement))
if (isVSClient && ClassifiedTagHelperTooltipFactory.TryCreateTooltip(attrDescriptionInfo, out ContainerElement? classifiedTextElement))
{
var vsHover = new VSInternalHover
{
Expand All @@ -276,7 +274,7 @@ internal sealed partial class HoverService(
{
var hoverContentFormat = GetHoverContentFormat(clientCapabilities);

if (!_lspTagHelperTooltipFactory.TryCreateTooltip(attrDescriptionInfo, hoverContentFormat, out var vsMarkupContent))
if (!MarkupTagHelperTooltipFactory.TryCreateTooltip(attrDescriptionInfo, hoverContentFormat, out var vsMarkupContent))
{
return null;
}
Expand Down Expand Up @@ -305,7 +303,10 @@ internal sealed partial class HoverService(
var isVSClient = clientCapabilities.SupportsVisualStudioExtensions;
if (isVSClient)
{
var classifiedTextElement = await _vsLspTagHelperTooltipFactory.TryCreateTooltipContainerAsync(documentFilePath, elementDescriptionInfo, cancellationToken).ConfigureAwait(false);
var classifiedTextElement = await ClassifiedTagHelperTooltipFactory
.TryCreateTooltipContainerAsync(documentFilePath, elementDescriptionInfo, _projectManager.GetQueryOperations(), cancellationToken)
.ConfigureAwait(false);

if (classifiedTextElement is not null)
{
var vsHover = new VSInternalHover
Expand All @@ -321,7 +322,10 @@ internal sealed partial class HoverService(

var hoverContentFormat = GetHoverContentFormat(clientCapabilities);

var vsMarkupContent = await _lspTagHelperTooltipFactory.TryCreateTooltipAsync(documentFilePath, elementDescriptionInfo, hoverContentFormat, cancellationToken).ConfigureAwait(false);
var vsMarkupContent = await MarkupTagHelperTooltipFactory
.TryCreateTooltipAsync(documentFilePath, elementDescriptionInfo, _projectManager.GetQueryOperations(), hoverContentFormat, cancellationToken)
.ConfigureAwait(false);

if (vsMarkupContent is null)
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.AspNetCore.Razor.Utilities;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;

namespace Microsoft.AspNetCore.Razor.LanguageServer.ProjectSystem;
Expand All @@ -19,19 +20,25 @@ public IEnumerable<IProjectSnapshot> GetProjects()
return _projectManager.GetProjects();
}

public ImmutableArray<IProjectSnapshot> FindProjects(string documentFilePath)
public ImmutableArray<IProjectSnapshot> GetProjectsContainingDocument(string documentFilePath)
{
using var results = new PooledArrayBuilder<IProjectSnapshot>();
using var projects = new PooledArrayBuilder<IProjectSnapshot>();

foreach (var project in _projectManager.GetProjects())
{
if (!project.TryGetDocument(documentFilePath, out _))
// Always exclude the miscellaneous project.
if (project.Key == MiscFilesHostProject.Instance.Key)
{
results.Add(project);
continue;
}

if (project.ContainsDocument(documentFilePath))
{
projects.Add(project);
}
}

return results.DrainToImmutable();
return projects.DrainToImmutable();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,9 @@
<data name="Generate_Event_Handler_Title" xml:space="preserve">
<value>Generate Event Handler '{0}'</value>
</data>
<data name="Not_Available_In" xml:space="preserve">
<value>Not available in</value>
</data>
<data name="ReTrigger_Completions_Title" xml:space="preserve">
<value>"Re-trigger completions..."</value>
</data>
<data name="TagHelper_Attribute_Glyph" xml:space="preserve">
<value>Razor TagHelper Attribute Glyph</value>
</data>
<data name="TagHelper_Element_Glyph" xml:space="preserve">
<value>Razor TagHelper Element Glyph</value>
</data>
<data name="Unknown_ProjectChangeKind" xml:space="preserve">
<value>Unknown ProjectChangeKind {0}</value>
</data>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ef8467d

Please sign in to comment.