Exemplo n.º 1
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            var configuration   = new ConfigurationBuilder(_environment).Build();
            var serviceProvider = CompositionHostBuilder.CreateDefaultServiceProvider(_environment, configuration, _eventEmitter, services,
                                                                                      configureLogging: builder =>
            {
                builder.AddConsole();

                var workspaceInformationServiceName = typeof(WorkspaceInformationService).FullName;
                var projectEventForwarder           = typeof(ProjectEventForwarder).FullName;
                var exceptionHandlerMiddlewareName  = typeof(ExceptionHandlerMiddleware).FullName;

                builder.AddFilter(
                    (category, logLevel) =>
                    category.Equals(exceptionHandlerMiddlewareName, StringComparison.OrdinalIgnoreCase) ||
                    (_environment.LogLevel <= logLevel &&
                     category.StartsWith("OmniSharp", StringComparison.OrdinalIgnoreCase) &&
                     !category.Equals(workspaceInformationServiceName, StringComparison.OrdinalIgnoreCase) &&
                     !category.Equals(projectEventForwarder, StringComparison.OrdinalIgnoreCase)));
            });

            var options = serviceProvider.GetRequiredService <IOptionsMonitor <OmniSharpOptions> >();
            var plugins = _commandLinePlugins.AssemblyNames.Concat(options.CurrentValue.Plugins.GetNormalizedLocationPaths(_environment));

            var loggerFactory  = serviceProvider.GetRequiredService <ILoggerFactory>();
            var logger         = loggerFactory.CreateLogger <Startup>();
            var assemblyLoader = serviceProvider.GetRequiredService <IAssemblyLoader>();

            _compositionHost = new CompositionHostBuilder(serviceProvider)
                               .WithOmniSharpAssemblies()
                               .WithAssemblies(assemblyLoader.LoadByAssemblyNameOrPath(logger, plugins).ToArray())
                               .Build();

            return(serviceProvider);
        }
Exemplo n.º 2
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            var configuration   = new ConfigurationBuilder(_environment).Build();
            var serviceProvider = CompositionHostBuilder.CreateDefaultServiceProvider(_environment, configuration, _eventEmitter, services,
                                                                                      configureLogging: builder =>
            {
                builder.AddConsole();

                var workspaceInformationServiceName = typeof(WorkspaceInformationService).FullName;
                var projectEventForwarder           = typeof(ProjectEventForwarder).FullName;
                var exceptionHandlerMiddlewareName  = typeof(ExceptionHandlerMiddleware).FullName;

                builder.AddFilter(
                    (category, logLevel) =>
                    category.Equals(exceptionHandlerMiddlewareName, StringComparison.OrdinalIgnoreCase) ||
                    (_environment.LogLevel <= logLevel &&
                     category.StartsWith("OmniSharp", StringComparison.OrdinalIgnoreCase) &&
                     !category.Equals(workspaceInformationServiceName, StringComparison.OrdinalIgnoreCase) &&
                     !category.Equals(projectEventForwarder, StringComparison.OrdinalIgnoreCase)));
            });

            _compositionHost = new CompositionHostBuilder(serviceProvider)
                               .WithOmniSharpAssemblies()
                               .Build();

            return(serviceProvider);
        }
Exemplo n.º 3
0
        static int Main(string[] args) => HostHelpers.Start(() =>
        {
            var application = new StdioCommandLineApplication();
            application.OnExecute(() =>
            {
                // If an encoding was specified, be sure to set the Console with it before we access the input/output streams.
                // Otherwise, the streams will be created with the default encoding.
                if (application.Encoding != null)
                {
                    var encoding           = Encoding.GetEncoding(application.Encoding);
                    Console.InputEncoding  = encoding;
                    Console.OutputEncoding = encoding;
                }

                var cancellation = new CancellationTokenSource();

                if (application.Lsp)
                {
                    Configuration.ZeroBasedIndices = true;
                    using (var host = new LanguageServerHost(
                               Console.OpenStandardInput(),
                               Console.OpenStandardOutput(),
                               application,
                               cancellation))
                    {
                        host.Start().Wait();
                        cancellation.Token.WaitHandle.WaitOne();
                    }
                }
                else
                {
                    var input  = Console.In;
                    var output = Console.Out;

                    var environment = application.CreateEnvironment();
                    Configuration.ZeroBasedIndices = application.ZeroBasedIndices;
                    var configuration   = new ConfigurationBuilder(environment).Build();
                    var writer          = new SharedTextWriter(output);
                    var serviceProvider = CompositionHostBuilder.CreateDefaultServiceProvider(environment, configuration, new StdioEventEmitter(writer));
                    var loggerFactory   = serviceProvider.GetRequiredService <ILoggerFactory>();
                    var plugins         = application.CreatePluginAssemblies();


                    var assemblyLoader         = serviceProvider.GetRequiredService <IAssemblyLoader>();
                    var compositionHostBuilder = new CompositionHostBuilder(serviceProvider)
                                                 .WithOmniSharpAssemblies()
                                                 .WithAssemblies(assemblyLoader.LoadByAssemblyNameOrPath(plugins.AssemblyNames).ToArray());

                    using (var host = new Host(input, writer, environment, configuration, serviceProvider, compositionHostBuilder, loggerFactory, cancellation))
                    {
                        host.Start();
                        cancellation.Token.WaitHandle.WaitOne();
                    }
                }

                return(0);
            });

            return(application.Execute(args));
        });
Exemplo n.º 4
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            var serviceProvider = CompositionHostBuilder.CreateDefaultServiceProvider(_configuration, services);

            _compositionHost = new CompositionHostBuilder(serviceProvider, _environment, _writer, _eventEmitter)
                               .WithOmniSharpAssemblies()
                               .Build();

            return(serviceProvider);
        }
Exemplo n.º 5
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            var configuration   = new ConfigurationBuilder(_environment).Build();
            var serviceProvider = CompositionHostBuilder.CreateDefaultServiceProvider(_environment, configuration, _eventEmitter, services,
                                                                                      configureLogging: builder =>
            {
                builder.AddConsole();
            });

            _compositionHost = new CompositionHostBuilder(serviceProvider)
                               .WithOmniSharpAssemblies()
                               .Build();

            return(serviceProvider);
        }
Exemplo n.º 6
0
        private static (IServiceProvider serviceProvider, CompositionHost compositionHost) CreateCompositionHost(
            ILanguageServer server,
            InitializeParams initializeParams,
            CommandLineApplication application,
            IServiceCollection services,
            Action <ILoggingBuilder> configureLogging)
        {
            var logLevel    = GetLogLevel(initializeParams.Trace);
            var environment = new OmniSharpEnvironment(
                // TODO: Support solution selection from the server side in the future
                // For now selection can be done by passing -s to the server
                !string.IsNullOrEmpty(application.ApplicationRoot) ? application.ApplicationRoot : Helpers.FromUri(initializeParams.RootUri),
                Convert.ToInt32(initializeParams.ProcessId ?? application.HostPid),
                application.LogLevel < logLevel ? application.LogLevel : logLevel,
                application.OtherArgs.ToArray());

            var configurationRoot = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
                                    .AddConfiguration(new ConfigurationBuilder(environment).Build())
                                    .AddConfiguration(server.Configuration.GetSection("csharp"))
                                    .AddConfiguration(server.Configuration.GetSection("omnisharp"))
                                    .Build()
            ;

            var eventEmitter = new LanguageServerEventEmitter(server);

            services.AddSingleton(server)
            .AddSingleton <ILanguageServerFacade>(server);

            var serviceProvider =
                CompositionHostBuilder.CreateDefaultServiceProvider(environment, configurationRoot, eventEmitter,
                                                                    services, GetLogBuilderAction(configureLogging, environment.LogLevel));

            var loggerFactory = serviceProvider.GetService <ILoggerFactory>();
            var logger        = loggerFactory.CreateLogger <LanguageServerHost>();

            var options = serviceProvider.GetRequiredService <IOptionsMonitor <OmniSharpOptions> >();
            var plugins = application.CreatePluginAssemblies(options.CurrentValue, environment);

            var assemblyLoader         = serviceProvider.GetRequiredService <IAssemblyLoader>();
            var compositionHostBuilder = new CompositionHostBuilder(serviceProvider)
                                         .WithOmniSharpAssemblies()
                                         .WithAssemblies(typeof(LanguageServerHost).Assembly)
                                         .WithAssemblies(assemblyLoader.LoadByAssemblyNameOrPath(logger, plugins.AssemblyNames).ToArray());

            return(serviceProvider, compositionHostBuilder.Build(environment.TargetDirectory));
        }
Exemplo n.º 7
0
        private Host BuildTestServerAndStart(TextReader reader, ISharedTextWriter writer, Action <Host> programDelegate = null)
        {
            var configuration          = new Microsoft.Extensions.Configuration.ConfigurationBuilder().Build();
            var environment            = new OmniSharpEnvironment();
            var serviceProvider        = CompositionHostBuilder.CreateDefaultServiceProvider(environment, configuration, NullEventEmitter.Instance);
            var cancelationTokenSource = new CancellationTokenSource();
            var host = new Host(reader, writer,
                                environment,
                                serviceProvider,
                                new CompositionHostBuilder(serviceProvider),
                                serviceProvider.GetRequiredService <ILoggerFactory>(),
                                cancelationTokenSource);

            programDelegate?.Invoke(host);

            host.Start();

            return(host);
        }
Exemplo n.º 8
0
        private static (IServiceProvider serviceProvider, CompositionHost compositionHost) CreateCompositionHost(
            ILanguageServer server,
            InitializeParams initializeParams,
            CommandLineApplication application,
            IServiceCollection services
            )
        {
            var logLevel    = GetLogLevel(initializeParams.Trace);
            var environment = new OmniSharpEnvironment(
                Helpers.FromUri(initializeParams.RootUri),
                Convert.ToInt32(initializeParams.ProcessId ?? -1L),
                application.LogLevel < logLevel ? application.LogLevel : logLevel,
                application.OtherArgs.ToArray());

            var configurationRoot = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
                                    .AddConfiguration(new ConfigurationBuilder(environment).Build())
                                    .AddConfiguration(server.Configuration.GetSection("csharp"))
                                    .AddConfiguration(server.Configuration.GetSection("omnisharp"))
                                    .Build()
            ;

            var eventEmitter = new LanguageServerEventEmitter(server);

            services.AddSingleton(server);
            var serviceProvider =
                CompositionHostBuilder.CreateDefaultServiceProvider(environment, configurationRoot, eventEmitter,
                                                                    services);

            var loggerFactory = serviceProvider.GetService <ILoggerFactory>();
            var logger        = loggerFactory.CreateLogger <LanguageServerHost>();

            var options = serviceProvider.GetRequiredService <IOptionsMonitor <OmniSharpOptions> >();
            var plugins = application.CreatePluginAssemblies(options.CurrentValue, environment);

            var assemblyLoader         = serviceProvider.GetRequiredService <IAssemblyLoader>();
            var compositionHostBuilder = new CompositionHostBuilder(serviceProvider)
                                         .WithOmniSharpAssemblies()
                                         .WithAssemblies(typeof(LanguageServerHost).Assembly)
                                         .WithAssemblies(assemblyLoader.LoadByAssemblyNameOrPath(logger, plugins.AssemblyNames).ToArray());

            return(serviceProvider, compositionHostBuilder.Build());
        }
Exemplo n.º 9
0
        private void CreateCompositionHost(InitializeParams initializeParams)
        {
            _environment = new OmniSharpEnvironment(
                Helpers.FromUri(initializeParams.RootUri),
                Convert.ToInt32(initializeParams.ProcessId ?? -1L),
                GetLogLevel(initializeParams.Trace),
                _application.OtherArgs.ToArray());

            // TODO: Make this work with logger factory differently
            // Maybe create a child logger factory?
            _loggerFactory.AddProvider(_server, _environment);
            _logger = _loggerFactory.CreateLogger <LanguageServerHost>();

            var configurationRoot = new ConfigurationBuilder(_environment).Build();
            var eventEmitter      = new LanguageServerEventEmitter(_server);

            _serviceProvider = CompositionHostBuilder.CreateDefaultServiceProvider(_environment, configurationRoot, eventEmitter, _services);

            var plugins = _application.CreatePluginAssemblies();

            var assemblyLoader         = _serviceProvider.GetRequiredService <IAssemblyLoader>();
            var compositionHostBuilder = new CompositionHostBuilder(_serviceProvider)
                                         .WithOmniSharpAssemblies()
                                         .WithAssemblies(typeof(LanguageServerHost).Assembly)
                                         .WithAssemblies(assemblyLoader.LoadByAssemblyNameOrPath(plugins.AssemblyNames).ToArray());

            _compositionHost = compositionHostBuilder.Build();

            var projectSystems = _compositionHost.GetExports <IProjectSystem>();

            var documentSelectors = projectSystems
                                    .GroupBy(x => x.Language)
                                    .Select(x => (
                                                language: x.Key,
                                                selector: new DocumentSelector(x
                                                                               .SelectMany(z => z.Extensions)
                                                                               .Distinct()
                                                                               .Select(z => new DocumentFilter()
            {
                Pattern = $"**/*{z}"
            }))
                                                ));

            _logger.LogTrace(
                "Configured Document Selectors {@DocumentSelectors}",
                documentSelectors.Select(x => new { x.language, x.selector })
                );

            // TODO: Get these with metadata so we can attach languages
            // This will thne let us build up a better document filter, and add handles foreach type of handler
            // This will mean that we will have a strategy to create handlers from the interface type
            _handlers = new RequestHandlers(
                _compositionHost.GetExports <Lazy <IRequestHandler, OmniSharpRequestHandlerMetadata> >(),
                documentSelectors
                );

            _logger.LogTrace("--- Handler Definitions ---");
            foreach (var handlerCollection in _handlers)
            {
                foreach (var handler in handlerCollection)
                {
                    _logger.LogTrace(
                        "Handler: {Language}:{DocumentSelector}:{Handler}",
                        handlerCollection.Language,
                        handlerCollection.DocumentSelector.ToString(),
                        handler.GetType().FullName
                        );
                }
            }
            _logger.LogTrace("--- Handler Definitions ---");
        }