Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added LSP support for document color requests #74933

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -10,6 +11,7 @@ namespace Microsoft.CodeAnalysis.ExternalAccess.Xaml;
/// <summary>
/// Represents a service to be exported via MEF for language server initialization.
/// </summary>
[Obsolete("Use IOnInitializedService instead.")]
internal interface IInitializationService
{
/// <summary>
Expand Down
20 changes: 20 additions & 0 deletions src/Tools/ExternalAccess/Xaml/External/IOnInitializedService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Threading;
using System.Threading.Tasks;
using LSP = Roslyn.LanguageServer.Protocol;

namespace Microsoft.CodeAnalysis.ExternalAccess.Xaml;

/// <summary>
/// Represents a service to be exported via MEF for language server initialization.
/// </summary>
internal interface IOnInitializedService
{
/// <summary>
/// Called when the language server is being initialized.
/// </summary>
Task OnInitializedAsync(IClientRequestManager clientRequestManager, LSP.ClientCapabilities clientCapabilities, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ private XamlRequestContext(RequestContext context)
_context = context;
}

internal readonly LSP.ClientCapabilities ClientCapabilities => _context.GetRequiredClientCapabilities();
public readonly LSP.ClientCapabilities ClientCapabilities => _context.GetRequiredClientCapabilities();

public readonly TextDocument? TextDocument => _context.TextDocument;

[Obsolete("Use ClientCapabilities instead.")]
public readonly IClientCapabilityProvider ClientCapabilityProvider => new ClientCapabilityProvider(_context.GetRequiredClientCapabilities());

public object ToCachedResolveData(object data, Uri uri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public bool IsDynamicRegistrationSupported(string methodName)
return internalTextDocumentClientCapabilities.OnAutoInsert?.DynamicRegistration == true;
}
return false;
case LSP.Methods.TextDocumentDocumentColorName:
return _clientCapabilities?.TextDocument?.ColorProvider?.DynamicRegistration == true;
default:
throw new InvalidOperationException($"Unsupported dynamic registration method: {methodName}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,56 @@ namespace Microsoft.CodeAnalysis.ExternalAccess.Xaml;
[ExportCSharpVisualBasicLspServiceFactory(typeof(OnInitializedService)), Shared]
internal sealed class OnInitializedServiceFactory : ILspServiceFactory
{
#pragma warning disable CS0618 // Type or member is obsolete
private readonly IInitializationService? _initializationService;
#pragma warning restore CS0618 // Type or member is obsolete
private readonly IOnInitializedService? _onInitializedService;

[ImportingConstructor]
[Obsolete(StringConstants.ImportingConstructorMessage, error: true)]
public OnInitializedServiceFactory([Import(AllowDefault = true)] IInitializationService? initializationService)
public OnInitializedServiceFactory(
[Import(AllowDefault = true)] IInitializationService? initializationService,
[Import(AllowDefault = true)] IOnInitializedService? onInitializedService)
{
_initializationService = initializationService;
_onInitializedService = onInitializedService;
}

public ILspService CreateILspService(LspServices lspServices, WellKnownLspServerKinds serverKind)
{
var clientLanguageServerManager = lspServices.GetRequiredService<IClientLanguageServerManager>();

return new OnInitializedService(_initializationService, clientLanguageServerManager);
return new OnInitializedService(_initializationService, _onInitializedService, clientLanguageServerManager);
}

private class OnInitializedService : ILspService, IOnInitialized
{
#pragma warning disable CS0618 // Type or member is obsolete
private readonly IInitializationService? _initializationService;
#pragma warning restore CS0618 // Type or member is obsolete
private readonly IOnInitializedService? _onInitializedService;
private readonly IClientLanguageServerManager _clientLanguageServerManager;

public OnInitializedService(IInitializationService? initializationService, IClientLanguageServerManager clientLanguageServerManager)
#pragma warning disable CS0618 // Type or member is obsolete
public OnInitializedService(IInitializationService? initializationService, IOnInitializedService? onInitializedService, IClientLanguageServerManager clientLanguageServerManager)
#pragma warning restore CS0618 // Type or member is obsolete
{
_initializationService = initializationService;
_onInitializedService = onInitializedService;
_clientLanguageServerManager = clientLanguageServerManager;
}

public async Task OnInitializedAsync(LSP.ClientCapabilities clientCapabilities, RequestContext context, CancellationToken cancellationToken)
{
if (_initializationService is null)
if (_initializationService is not null)
{
return;
await _initializationService.OnInitializedAsync(new ClientRequestManager(_clientLanguageServerManager), new ClientCapabilityProvider(clientCapabilities), cancellationToken).ConfigureAwait(false);
}

await _initializationService.OnInitializedAsync(new ClientRequestManager(_clientLanguageServerManager), new ClientCapabilityProvider(clientCapabilities), cancellationToken).ConfigureAwait(false);
if (_onInitializedService is not null)
{
await _onInitializedService.OnInitializedAsync(new ClientRequestManager(_clientLanguageServerManager), clientCapabilities, cancellationToken).ConfigureAwait(false);
}
}

private class ClientRequestManager : IClientRequestManager
Expand Down
4 changes: 3 additions & 1 deletion src/Tools/ExternalAccess/Xaml/InternalAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Microsoft.CodeAnalysis.ExternalAccess.Xaml.IInitializationService.OnInitializedA
Microsoft.CodeAnalysis.ExternalAccess.Xaml.ILocationService
Microsoft.CodeAnalysis.ExternalAccess.Xaml.ILocationService.GetLocationAsync(Microsoft.CodeAnalysis.TextDocument! document, Microsoft.CodeAnalysis.Text.TextSpan textSpan, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.CodeAnalysis.FileLinePositionSpan?>!
Microsoft.CodeAnalysis.ExternalAccess.Xaml.ILocationService.GetSymbolLocationsAsync(Microsoft.CodeAnalysis.ISymbol! symbol, Microsoft.CodeAnalysis.Project! project, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.CodeAnalysis.FileLinePositionSpan[]!>!
Microsoft.CodeAnalysis.ExternalAccess.Xaml.IOnInitializedService
Microsoft.CodeAnalysis.ExternalAccess.Xaml.IOnInitializedService.OnInitializedAsync(Microsoft.CodeAnalysis.ExternalAccess.Xaml.IClientRequestManager! clientRequestManager, Roslyn.LanguageServer.Protocol.ClientCapabilities! clientCapabilities, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!
Microsoft.CodeAnalysis.ExternalAccess.Xaml.IResolveCachedDataService
Microsoft.CodeAnalysis.ExternalAccess.Xaml.IResolveCachedDataService.FromResolveData(object? resolveData) -> (object? data, System.Uri? uri)
Microsoft.CodeAnalysis.ExternalAccess.Xaml.IResolveCachedDataService.ToResolveData(object! data, System.Uri! uri) -> object!
Expand All @@ -51,7 +53,7 @@ Microsoft.CodeAnalysis.ExternalAccess.Xaml.LocationService.GetSymbolLocationsAsy
Microsoft.CodeAnalysis.ExternalAccess.Xaml.LocationService.LocationService(Microsoft.CodeAnalysis.MetadataAsSource.IMetadataAsSourceFileService! metadataAsSourceFileService, Microsoft.CodeAnalysis.Options.IGlobalOptionService! globalOptions) -> void
Microsoft.CodeAnalysis.ExternalAccess.Xaml.OnInitializedServiceFactory
Microsoft.CodeAnalysis.ExternalAccess.Xaml.OnInitializedServiceFactory.CreateILspService(Microsoft.CodeAnalysis.LanguageServer.LspServices! lspServices, Microsoft.CodeAnalysis.LanguageServer.WellKnownLspServerKinds serverKind) -> Microsoft.CodeAnalysis.LanguageServer.ILspService!
Microsoft.CodeAnalysis.ExternalAccess.Xaml.OnInitializedServiceFactory.OnInitializedServiceFactory(Microsoft.CodeAnalysis.ExternalAccess.Xaml.IInitializationService? initializationService) -> void
Microsoft.CodeAnalysis.ExternalAccess.Xaml.OnInitializedServiceFactory.OnInitializedServiceFactory(Microsoft.CodeAnalysis.ExternalAccess.Xaml.IInitializationService? initializationService, Microsoft.CodeAnalysis.ExternalAccess.Xaml.IOnInitializedService? onInitializedService) -> void
Microsoft.CodeAnalysis.ExternalAccess.Xaml.ResolveDataConversions
Microsoft.CodeAnalysis.ExternalAccess.Xaml.StringConstants
Microsoft.CodeAnalysis.ExternalAccess.Xaml.XamlCommandAttribute
Expand Down
Loading