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 Intellisense and other features of Bicep in Visual Studio that were broken by VS 17.10 #14532

Merged
merged 9 commits into from
Jul 17, 2024
Prev Previous commit
Next Next commit
CR: Improve DI initizliation
  • Loading branch information
StephenWeatherford committed Jul 16, 2024
commit 5df10849ca3924af598f5b4905348b5cc7016a92
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.IO.Pipelines;
using System.Security.Cryptography.Xml;
using Bicep.Core.Tracing;
using Bicep.Core.UnitTests;
using Bicep.Core.UnitTests.FileSystem;
using Bicep.Core.UnitTests.Utils;
using Bicep.LangServer.IntegrationTests.Helpers;
using Bicep.LanguageServer;
using Bicep.LanguageServer.Options;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OmniSharp.Extensions.LanguageServer.Client;
Expand Down Expand Up @@ -39,6 +41,7 @@ public static async Task<LanguageServerHelper> StartServer(TestContext testConte
var serverPipe = new Pipe();

var server = new Server(
new BicepLangServerOptions(),
options => options
.WithInput(serverPipe.Reader)
.WithOutput(clientPipe.Writer)
Expand Down
7 changes: 5 additions & 2 deletions src/Bicep.LangServer/IServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public static IServiceCollection AddBicepCore(this IServiceCollection services)
public static IServiceCollection AddBicepDecompiler(this IServiceCollection services) => services
.AddSingleton<BicepDecompiler>();

public static IServiceCollection AddServerDependencies(this IServiceCollection services) => services
public static IServiceCollection AddServerDependencies(
this IServiceCollection services,
IBicepLangServerOptions bicepLangServerOptions
) => services
.AddBicepCore()
.AddBicepDecompiler()
.AddSingleton<IWorkspace, Workspace>()
Expand All @@ -77,6 +80,6 @@ public static IServiceCollection AddServerDependencies(this IServiceCollection s
.AddSingleton<IDeploymentHelper, DeploymentHelper>()
.AddSingleton<ISettingsProvider, SettingsProvider>()
.AddSingleton<IAzureContainerRegistriesProvider, AzureContainerRegistriesProvider>()
.AddSingleton<IBicepLangServerOptions, BicepLangServerOptions>()
.AddSingleton(bicepLangServerOptions)
.AddSingleton<DocumentSelectorFactory>();
}
8 changes: 6 additions & 2 deletions src/Bicep.LangServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ private static async Task RunServer(CommandLineOptions options, CancellationToke
}

Server server;

var bicepLangServerOptions = new BicepLangServerOptions() { VsCompatibilityMode = options.VsCompatibilityMode };

if (options.Pipe is { } pipeName)
{
if (pipeName.StartsWith(@"\\.\pipe\"))
Expand All @@ -79,6 +82,7 @@ private static async Task RunServer(CommandLineOptions options, CancellationToke
await clientPipe.ConnectAsync(cancellationToken);

server = new(
bicepLangServerOptions,
options => options
.WithInput(clientPipe)
.WithOutput(clientPipe)
Expand All @@ -92,6 +96,7 @@ private static async Task RunServer(CommandLineOptions options, CancellationToke
var tcpStream = tcpClient.GetStream();

server = new(
bicepLangServerOptions,
options => options
.WithInput(tcpStream)
.WithOutput(tcpStream)
Expand All @@ -100,13 +105,12 @@ private static async Task RunServer(CommandLineOptions options, CancellationToke
else
{
server = new(
bicepLangServerOptions,
options => options
.WithInput(Console.OpenStandardInput())
.WithOutput(Console.OpenStandardOutput()));
}

server.GetRequiredService<IBicepLangServerOptions>().VsCompatibilityMode = options.VsCompatibilityMode;

await server.RunAsync(cancellationToken);
}

Expand Down
14 changes: 3 additions & 11 deletions src/Bicep.LangServer/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Bicep.Core.Registry.PublicRegistry;
using Bicep.Core.Tracing;
using Bicep.LanguageServer.Handlers;
using Bicep.LanguageServer.Options;
using Bicep.LanguageServer.Providers;
using Bicep.LanguageServer.Registry;
using Bicep.LanguageServer.Settings;
Expand All @@ -23,11 +24,7 @@ namespace Bicep.LanguageServer
public class Server : IDisposable
{
private readonly OmnisharpLanguageServer server;

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

public Server(Action<LanguageServerOptions> onOptionsFunc)
public Server(IBicepLangServerOptions bicepLangServerOptions, Action<LanguageServerOptions> onOptionsFunc)
{
server = OmnisharpLanguageServer.PreInit(options =>
{
Expand Down Expand Up @@ -72,7 +69,7 @@ public Server(Action<LanguageServerOptions> onOptionsFunc)
.WithHandler<InsertResourceHandler>()
.WithHandler<ConfigurationSettingsHandler>()
.WithHandler<LocalDeployHandler>()
.WithServices(RegisterServices);
.WithServices(services => services.AddServerDependencies(bicepLangServerOptions));

onOptionsFunc(options);
});
Expand Down Expand Up @@ -103,11 +100,6 @@ public async Task RunAsync(CancellationToken cancellationToken)
moduleMetadataProvider.StartUpdateCache();
}

private static void RegisterServices(IServiceCollection services)
{
services.AddServerDependencies();
}

public void Dispose()
{
server.Dispose();
Expand Down
15 changes: 9 additions & 6 deletions src/Bicep.Tools.Benchmark/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Bicep.Core.UnitTests.Utils;
using Bicep.LangServer.IntegrationTests;
using Bicep.LanguageServer;
using Bicep.LanguageServer.Options;
using Microsoft.Extensions.DependencyInjection;
using OmniSharp.Extensions.LanguageServer.Client;
using OmniSharp.Extensions.LanguageServer.Protocol;
Expand Down Expand Up @@ -38,12 +39,14 @@ private static async Task<ILanguageClient> StartServer(IFileSystem fileSystem, M
var clientPipe = new Pipe();
var serverPipe = new Pipe();

var server = new Server(options => options
.WithInput(serverPipe.Reader)
.WithOutput(clientPipe.Writer)
.WithServices(services => services
.AddSingleton<IScheduler>(ImmediateScheduler.Instance) // force work to run on a single thread to make snapshot profiling simpler
.AddSingleton(fileSystem)));
var server = new Server(
new BicepLangServerOptions(),
options => options
.WithInput(serverPipe.Reader)
.WithOutput(clientPipe.Writer)
.WithServices(services => services
.AddSingleton<IScheduler>(ImmediateScheduler.Instance) // force work to run on a single thread to make snapshot profiling simpler
.AddSingleton(fileSystem)));

var _ = server.RunAsync(CancellationToken.None); // do not wait on this async method, or you'll be waiting a long time!

Expand Down
Loading