Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 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 @@ -7,6 +7,7 @@
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Microsoft.AspNetCore.Razor.LanguageServer;
using Microsoft.AspNetCore.Razor.LanguageServer.CodeActions;
using Microsoft.AspNetCore.Razor.LanguageServer.EndpointContracts;
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
Expand All @@ -15,7 +16,6 @@
using Microsoft.CodeAnalysis.Razor.Protocol.CodeActions;
using Microsoft.CodeAnalysis.Razor.Telemetry;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CommonLanguageServerProtocol.Framework;

namespace Microsoft.AspNetCore.Razor.Microbenchmarks.LanguageServer;

Expand Down Expand Up @@ -75,7 +75,11 @@ public async Task SetupAsync()

var documentContext = new DocumentContext(DocumentUri, DocumentSnapshot, projectContext: null);

RazorRequestContext = new RazorRequestContext(documentContext, RazorLanguageServerHost.GetRequiredService<ILspServices>(), "lsp/method", uri: null);
RazorRequestContext = new RazorRequestContext(
documentContext,
RazorLanguageServerHost.GetRequiredService<LspServices>(),
"lsp/method",
uri: null);
}

private string GetFileContents(FileTypes fileType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Telemetry;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CommonLanguageServerProtocol.Framework;

namespace Microsoft.AspNetCore.Razor.Microbenchmarks.LanguageServer;

Expand All @@ -37,12 +36,11 @@ public class RazorCompletionBenchmark : RazorLanguageServerBenchmarkBase
public async Task SetupAsync()
{
var razorCompletionListProvider = RazorLanguageServerHost.GetRequiredService<RazorCompletionListProvider>();
var lspServices = RazorLanguageServerHost.GetRequiredService<ILspServices>();
var documentMappingService = lspServices.GetRequiredService<IDocumentMappingService>();
var clientConnection = lspServices.GetRequiredService<IClientConnection>();
var completionListCache = lspServices.GetRequiredService<CompletionListCache>();
var triggerAndCommitCharacters = lspServices.GetRequiredService<CompletionTriggerAndCommitCharacters>();
var loggerFactory = lspServices.GetRequiredService<ILoggerFactory>();
var documentMappingService = RazorLanguageServerHost.GetRequiredService<IDocumentMappingService>();
var clientConnection = RazorLanguageServerHost.GetRequiredService<IClientConnection>();
var completionListCache = RazorLanguageServerHost.GetRequiredService<CompletionListCache>();
var triggerAndCommitCharacters = RazorLanguageServerHost.GetRequiredService<CompletionTriggerAndCommitCharacters>();
var loggerFactory = RazorLanguageServerHost.GetRequiredService<ILoggerFactory>();

var delegatedCompletionListProvider = new TestDelegatedCompletionListProvider(documentMappingService, clientConnection, completionListCache, triggerAndCommitCharacters);
var completionListProvider = new CompletionListProvider(razorCompletionListProvider, delegatedCompletionListProvider, triggerAndCommitCharacters);
Expand Down Expand Up @@ -80,7 +78,11 @@ public async Task SetupAsync()
RazorPosition = DocumentText.GetPosition(razorCodeActionIndex);

var documentContext = new DocumentContext(DocumentUri, DocumentSnapshot, projectContext: null);
RazorRequestContext = new RazorRequestContext(documentContext, RazorLanguageServerHost.GetRequiredService<ILspServices>(), "lsp/method", uri: null);
RazorRequestContext = new RazorRequestContext(
documentContext,
RazorLanguageServerHost.GetRequiredService<LspServices>(),
"lsp/method",
uri: null);
}

private static string GetFileContents()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using Microsoft.CodeAnalysis.Razor.Telemetry;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CommonLanguageServerProtocol.Framework;
using Microsoft.Extensions.DependencyInjection;
using Nerdbank.Streams;

Expand Down Expand Up @@ -83,7 +82,7 @@ await projectManager.UpdateAsync(

private sealed class NoOpClientNotifierService : IClientConnection, IOnInitialized
{
public Task OnInitializedAsync(ILspServices services, CancellationToken cancellationToken)
public Task OnInitializedAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using Microsoft.CodeAnalysis.Razor.SemanticTokens;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CommonLanguageServerProtocol.Framework;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.Razor.Microbenchmarks.LanguageServer;
Expand Down Expand Up @@ -76,7 +75,11 @@ public async Task InitializeRazorSemanticAsync()
start: (0, 0),
end: (text.Lines.Count - 1, text.Lines[^1].Span.Length - 1));

RequestContext = new RazorRequestContext(DocumentContext, RazorLanguageServerHost.GetRequiredService<ILspServices>(), "lsp/method", uri: null);
RequestContext = new RazorRequestContext(
DocumentContext,
RazorLanguageServerHost.GetRequiredService<LspServices>(),
"lsp/method",
uri: null);

var random = new Random();
var codeDocument = await DocumentContext.GetCodeDocumentAsync(CancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.DiaSymReader" />

<PackageReference Include="Microsoft.CodeAnalysis.LanguageServer.Protocol" Aliases="RLSP" />
</ItemGroup>

<Import Project="..\..\..\Shared\Microsoft.AspNetCore.Razor.Serialization.Json\Microsoft.AspNetCore.Razor.Serialization.Json.projitems" Label="Shared" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Reflection;
using Microsoft.CommonLanguageServerProtocol.Framework;

namespace Microsoft.AspNetCore.Razor.LanguageServer;

/// <summary>
/// Useful helpers for accessing CLaSP types and performing reflection.
/// These are intended to only be used for testing to avoid type ambiguities
/// that occur because CLaSP is both compiled into the Razor language server
/// and is referenced as metadata through Microsoft.CodeAnalysis.LanguageServer.Protocol.
/// </summary>
internal static class CLaSPTypeHelpers
{
public static Type IMethodHandlerType = typeof(IMethodHandler);

public static LanguageServerEndpointAttribute? GetLanguageServerEnpointAttribute(Type type)
=> type.GetCustomAttribute<LanguageServerEndpointAttribute>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer;

internal sealed class CapabilitiesManager : IInitializeManager<InitializeParams, InitializeResult>, IClientCapabilitiesService, IWorkspaceRootPathProvider
{
private readonly ILspServices _lspServices;
private readonly LspServices _lspServices;
private readonly TaskCompletionSource<InitializeParams> _initializeParamsTaskSource;
private readonly VisualStudio.Threading.AsyncLazy<string> _lazyRootPath;

Expand All @@ -26,7 +26,7 @@ internal sealed class CapabilitiesManager : IInitializeManager<InitializeParams,

public VSInternalClientCapabilities ClientCapabilities => GetInitializeParams().Capabilities.ToVSInternalClientCapabilities();

public CapabilitiesManager(ILspServices lspServices)
public CapabilitiesManager(LspServices lspServices)
{
_lspServices = lspServices;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
using Microsoft.CommonLanguageServerProtocol.Framework;
using StreamJsonRpc;

namespace Microsoft.AspNetCore.Razor.LanguageServer;
Expand Down Expand Up @@ -47,7 +46,7 @@ public async Task SendNotificationAsync(string method, CancellationToken cancell
/// <summary>
/// Fires when the language server is set to "Started".
/// </summary>
public Task OnInitializedAsync(ILspServices services, CancellationToken cancellationToken)
public Task OnInitializedAsync(CancellationToken cancellationToken)
{
_initializedCompletionSource.TrySetResult(true);
return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

extern alias RLSP;

using System;
using System.Linq;
using System.Threading;
Expand All @@ -17,11 +15,11 @@
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using DefinitionResult = RLSP::Roslyn.LanguageServer.Protocol.SumType<
RLSP::Roslyn.LanguageServer.Protocol.Location,
RLSP::Roslyn.LanguageServer.Protocol.VSInternalLocation,
RLSP::Roslyn.LanguageServer.Protocol.VSInternalLocation[],
RLSP::Roslyn.LanguageServer.Protocol.DocumentLink[]>;
using DefinitionResult = Roslyn.LanguageServer.Protocol.SumType<
Roslyn.LanguageServer.Protocol.Location,
Roslyn.LanguageServer.Protocol.VSInternalLocation,
Roslyn.LanguageServer.Protocol.VSInternalLocation[],
Roslyn.LanguageServer.Protocol.DocumentLink[]>;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Definition;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

extern alias RLSP;

using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -20,7 +18,7 @@
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using RLSP::Roslyn.Text.Adornments;
using Roslyn.Text.Adornments;

namespace Microsoft.AspNetCore.Razor.LanguageServer.FindAllReferences;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@

// The <Using> item doesn't support aliases so we need to define aliased global usings in a .cs file not in the .csproj
// https://github.com/dotnet/sdk/issues/37814
extern alias RLSP;
global using RLSP::Roslyn.LanguageServer.Protocol;

// Avoid extern alias in every file that needs to disambiguate common LSP type names
global using LspColorPresentation = RLSP::Roslyn.LanguageServer.Protocol.ColorPresentation;
global using LspDiagnostic = RLSP::Roslyn.LanguageServer.Protocol.Diagnostic;
global using LspDiagnosticSeverity = RLSP::Roslyn.LanguageServer.Protocol.DiagnosticSeverity;
global using LspDocumentHighlight = RLSP::Roslyn.LanguageServer.Protocol.DocumentHighlight;
global using LspHover = RLSP::Roslyn.LanguageServer.Protocol.Hover;
global using LspLocation = RLSP::Roslyn.LanguageServer.Protocol.Location;
global using LspRange = RLSP::Roslyn.LanguageServer.Protocol.Range;
global using LspSignatureHelp = RLSP::Roslyn.LanguageServer.Protocol.SignatureHelp;
global using LspColorPresentation = Roslyn.LanguageServer.Protocol.ColorPresentation;
global using LspDiagnostic = Roslyn.LanguageServer.Protocol.Diagnostic;
global using LspDiagnosticSeverity = Roslyn.LanguageServer.Protocol.DiagnosticSeverity;
global using LspDocumentHighlight = Roslyn.LanguageServer.Protocol.DocumentHighlight;
global using LspHover = Roslyn.LanguageServer.Protocol.Hover;
global using LspLocation = Roslyn.LanguageServer.Protocol.Location;
global using LspRange = Roslyn.LanguageServer.Protocol.Range;
global using LspSignatureHelp = Roslyn.LanguageServer.Protocol.SignatureHelp;

// Avoid ambiguity errors because of our global using above
global using Range = System.Range;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

using System.Threading;
using System.Threading.Tasks;
using Microsoft.CommonLanguageServerProtocol.Framework;

namespace Microsoft.AspNetCore.Razor.LanguageServer;

internal interface IOnInitialized
{
Task OnInitializedAsync(ILspServices services, CancellationToken cancellationToken);
Task OnInitializedAsync(CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

extern alias RLSP;

using System;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -13,9 +11,9 @@
using Microsoft.CodeAnalysis.Razor.Logging;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using ImplementationResult = RLSP::Roslyn.LanguageServer.Protocol.SumType<
RLSP::Roslyn.LanguageServer.Protocol.Location[],
RLSP::Roslyn.LanguageServer.Protocol.VSInternalReferenceItem[]>;
using ImplementationResult = Roslyn.LanguageServer.Protocol.SumType<
Roslyn.LanguageServer.Protocol.Location[],
Roslyn.LanguageServer.Protocol.VSInternalReferenceItem[]>;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Implementation;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,55 @@

namespace Microsoft.AspNetCore.Razor.LanguageServer;

internal class LspServices : ILspServices
internal sealed class LspServices : ILspServices
{
private sealed class EmptyServiceProvider : IServiceProvider
{
public object? GetService(Type serviceType) => null;
}

public static LspServices Empty { get; } = new(new EmptyServiceProvider());

private readonly IServiceProvider _serviceProvider;
public bool IsDisposed = false;

private readonly object _disposeLock = new();
private bool _isDisposed = false;

public bool IsDisposed => _isDisposed;

public LspServices(IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton<ILspServices>(this);
serviceCollection.AddSingleton<LspServices>(this);

_serviceProvider = serviceCollection.BuildServiceProvider();
// Create all startup services

// By requesting the startup services, we ensure that they are created.
// This gives them an opportunity to set up any necessary state or perform.
_serviceProvider.GetServices<IRazorStartupService>();
}

public ImmutableArray<Type> GetRegisteredServices()
private LspServices(IServiceProvider serviceProvider)
{
throw new NotImplementedException();
_serviceProvider = serviceProvider;
}

public void Dispose()
{
lock (_disposeLock)
{
if (_isDisposed)
{
return;
}

_isDisposed = true;
}

if (_serviceProvider is IDisposable disposable)
{
disposable.Dispose();
}
}

public T GetRequiredService<T>() where T : notnull
Expand All @@ -45,20 +78,6 @@ public IEnumerable<T> GetRequiredServices<T>()
return services;
}

public bool SupportsGetRegisteredServices()
{
return false;
}

public void Dispose()
{
if (_serviceProvider is IDisposable disposable)
{
disposable.Dispose();
IsDisposed = true;
}
}

public T? GetService<T>() where T : notnull
=> _serviceProvider.GetService<T>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
<Description>Razor is a markup syntax for adding server-side logic to web pages. This package contains the language server library assets.</Description>
<EnableApiCheck>false</EnableApiCheck>
<IsShippingPackage>false</IsShippingPackage>

<!-- CLaSP is a source package, and has a more lenient approach to using statements than us-->
<NoWarn>IDE0005</NoWarn>
<NoWarn>$(NoWarn);IDE0005</NoWarn>

<!-- We are compiling CLaSP as a source package and referencing Microsoft.CodeAnalysis.LanguageServer.Protocol,
which includes the same CLaSP types. This results in C# compiler warnings when it prefers types from source
over types from metadata to avoid ambiguities. These warnings can be surpressed. -->
<NoWarn>$(NoWarn);0436</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand All @@ -33,14 +39,7 @@
Now we aren't sure why this exposes a "flaky" issue; however, to workaround the break we pin the following packages to workaround the issue.
-->
<PackageReference Include="Microsoft.VisualStudio.RpcContracts" />

<!--
Razor and Roslyn both use CLaSP as the basis for their language servers, but CLaSP is a source package
which means when we reference Roslyn's server to get the LSP protocol types (which we have a restricted
IVT to) we get ambiguous type errors for everything in CLaSP. To fix this we reference Roslyn with an
alias, and then have a global using for the LSP protocol types only.
-->
<PackageReference Include="Microsoft.CodeAnalysis.LanguageServer.Protocol" Aliases="RLSP" />
<PackageReference Include="Microsoft.CodeAnalysis.LanguageServer.Protocol" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading