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

Fix serialization of capabilities in onautoinsert #73867

Merged
merged 1 commit into from
Jun 6, 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 @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using Roslyn.LanguageServer.Protocol;
using Roslyn.Test.Utilities;
using Xunit.Abstractions;

namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests;
Expand Down Expand Up @@ -54,4 +55,15 @@ public async Task TestServerHandlesTextSyncRequestsAsync()
// These are notifications so we should get a null response (but no exceptions).
Assert.Null(response);
}

[Fact]
public async Task TestOnAutoInsertCapabilitiesSerializedCorrectly()
{
await using var server = await CreateLanguageServerAsync();

var capabilities = server.ServerCapabilities as VSInternalServerCapabilities;
AssertEx.NotNull(capabilities);
AssertEx.NotNull(capabilities.OnAutoInsertProvider);
Assert.NotEmpty(capabilities.OnAutoInsertProvider.TriggerCharacters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ protected sealed class TestLspServer : IAsyncDisposable
private readonly Task _languageServerHostCompletionTask;
private readonly JsonRpc _clientRpc;

private ServerCapabilities? _serverCapabilities;

internal static async Task<TestLspServer> CreateAsync(ClientCapabilities clientCapabilities, TestOutputLogger logger, bool includeDevKitComponents = true)
{
var exportProvider = await LanguageServerTestComposition.CreateExportProviderAsync(
logger.Factory, includeDevKitComponents, out var _, out var assemblyLoader);
var testLspServer = new TestLspServer(exportProvider, logger, assemblyLoader);
var initializeResponse = await testLspServer.ExecuteRequestAsync<InitializeParams, InitializeResult>(Methods.InitializeName, new InitializeParams { Capabilities = clientCapabilities }, CancellationToken.None);
Assert.NotNull(initializeResponse?.Capabilities);
testLspServer._serverCapabilities = initializeResponse!.Capabilities;

await testLspServer.ExecuteRequestAsync<InitializedParams, object>(Methods.InitializedName, new InitializedParams(), CancellationToken.None);

Expand All @@ -46,14 +49,16 @@ internal static async Task<TestLspServer> CreateAsync(ClientCapabilities clientC
internal LanguageServerHost LanguageServerHost { get; }
public ExportProvider ExportProvider { get; }

internal ServerCapabilities ServerCapabilities => _serverCapabilities ?? throw new InvalidOperationException("Initialize has not been called");

private TestLspServer(ExportProvider exportProvider, TestOutputLogger logger, IAssemblyLoader assemblyLoader)
{
var typeRefResolver = new ExtensionTypeRefResolver(assemblyLoader, logger.Factory);

var (clientStream, serverStream) = FullDuplexStream.CreatePair();
LanguageServerHost = new LanguageServerHost(serverStream, serverStream, exportProvider, logger, typeRefResolver);

var messageFormatter = LanguageServerHost.CreateJsonMessageFormatter();
var messageFormatter = RoslynLanguageServer.CreateJsonMessageFormatter();
_clientRpc = new JsonRpc(new HeaderDelimitedMessageHandler(clientStream, clientStream, messageFormatter))
{
AllowModificationWhileListening = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal sealed class LanguageServerHost

public LanguageServerHost(Stream inputStream, Stream outputStream, ExportProvider exportProvider, ILogger logger, AbstractTypeRefResolver typeRefResolver)
{
var messageFormatter = CreateJsonMessageFormatter();
var messageFormatter = RoslynLanguageServer.CreateJsonMessageFormatter();
Copy link
Member Author

@dibarbet dibarbet Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was the bug - the vscode serializer wasn't adding all of the vs extension converters. I deleted the vscode specific one so we always have consistent serialization.


var handler = new HeaderDelimitedMessageHandler(outputStream, inputStream, messageFormatter);

Expand All @@ -56,14 +56,6 @@ public LanguageServerHost(Stream inputStream, Stream outputStream, ExportProvide
typeRefResolver);
}

internal static SystemTextJsonFormatter CreateJsonMessageFormatter()
{
var messageFormatter = new SystemTextJsonFormatter();
messageFormatter.JsonSerializerOptions.AddVSCodeInternalExtensionConverters();
messageFormatter.JsonSerializerOptions.Converters.Add(new NaturalObjectConverter());
return messageFormatter;
}

public void Start()
{
_jsonRpc.StartListening();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.LanguageServer;
using Microsoft.CommonLanguageServerProtocol.Framework.Example;
using Microsoft.Extensions.DependencyInjection;
using Nerdbank.Streams;
Expand Down Expand Up @@ -105,7 +106,7 @@ internal async Task<int> WaitForExit()
private static SystemTextJsonFormatter CreateJsonMessageFormatter()
{
var messageFormatter = new SystemTextJsonFormatter();
messageFormatter.JsonSerializerOptions.AddVSCodeInternalExtensionConverters();
messageFormatter.JsonSerializerOptions.AddLspSerializerOptions();
return messageFormatter;
}

Expand Down

This file was deleted.

Loading