Пример #1
0
        public EndpointMiddleware(RequestDelegate next, CompositionHost host, ILoggerFactory loggerFactory)
        {
            _next           = next;
            _host           = host;
            _projectSystems = host.GetExports <IProjectSystem>();
            _logger         = loggerFactory.CreateLogger <EndpointMiddleware>();
            var endpoints = _host.GetExports <Lazy <IRequest, EndpointDescriptor> >()
                            .Select(x => x.Metadata);

            var handlers = _host.GetExports <Lazy <IRequestHandler, OmniSharpLanguage> >();

            _endpoints = new HashSet <string>(
                endpoints
                .Select(x => x.EndpointName)
                .Distinct(),
                StringComparer.OrdinalIgnoreCase
                );

            var updateBufferEndpointHandler   = new Lazy <EndpointHandler <UpdateBufferRequest, object> >(() => (EndpointHandler <UpdateBufferRequest, object>)_endpointHandlers[OmnisharpEndpoints.UpdateBuffer].Value);
            var languagePredicateHandler      = new LanguagePredicateHandler(_projectSystems);
            var projectSystemPredicateHandler = new StaticLanguagePredicateHandler("Projects");
            var nugetPredicateHandler         = new StaticLanguagePredicateHandler("NuGet");
            var endpointHandlers = endpoints.ToDictionary(
                x => x.EndpointName,
                endpoint => new Lazy <EndpointHandler>(() =>
            {
                IPredicateHandler handler;

                // Projects are a special case, this allows us to select the correct "Projects" language for them
                if (endpoint.EndpointName == OmnisharpEndpoints.ProjectInformation || endpoint.EndpointName == OmnisharpEndpoints.WorkspaceInformation)
                {
                    handler = projectSystemPredicateHandler;
                }
                else if (endpoint.EndpointName == OmnisharpEndpoints.PackageSearch || endpoint.EndpointName == OmnisharpEndpoints.PackageSource || endpoint.EndpointName == OmnisharpEndpoints.PackageVersion)
                {
                    handler = nugetPredicateHandler;
                }
                else
                {
                    handler = languagePredicateHandler;
                }

                // This lets any endpoint, that contains a Request object, invoke update buffer.
                // The language will be same language as the caller, this means any language service
                // must implement update buffer.
                var updateEndpointHandler = updateBufferEndpointHandler;
                if (endpoint.EndpointName == OmnisharpEndpoints.UpdateBuffer)
                {
                    // We don't want to call update buffer on update buffer.
                    updateEndpointHandler = new Lazy <EndpointHandler <UpdateBufferRequest, object> >(() => null);
                }

                return(EndpointHandler.Factory(handler, _host, _logger, endpoint, handlers, updateEndpointHandler, Enumerable.Empty <Plugin>()));
            }),
                StringComparer.OrdinalIgnoreCase
                );

            _endpointHandlers = new ReadOnlyDictionary <string, Lazy <EndpointHandler> >(endpointHandlers);
        }
Пример #2
0
        private IDictionary <string, Lazy <EndpointHandler> > Initialize()
        {
            var workspace         = _compositionHost.GetExport <OmniSharpWorkspace>();
            var projectSystems    = _compositionHost.GetExports <IProjectSystem>();
            var endpointMetadatas = _compositionHost.GetExports <Lazy <IRequest, OmniSharpEndpointMetadata> >()
                                    .Select(x => x.Metadata)
                                    .ToArray();

            var handlers = _compositionHost.GetExports <Lazy <IRequestHandler, OmniSharpRequestHandlerMetadata> >();

            var updateBufferEndpointHandler = new Lazy <EndpointHandler <UpdateBufferRequest, object> >(
                () => (EndpointHandler <UpdateBufferRequest, object>)_endpointHandlers[OmniSharpEndpoints.UpdateBuffer].Value);
            var languagePredicateHandler      = new LanguagePredicateHandler(projectSystems);
            var projectSystemPredicateHandler = new StaticLanguagePredicateHandler("Projects");
            var nugetPredicateHandler         = new StaticLanguagePredicateHandler("NuGet");
            var endpointHandlers = endpointMetadatas.ToDictionary(
                x => x.EndpointName,
                endpoint => new Lazy <EndpointHandler>(() =>
            {
                IPredicateHandler handler;

                // Projects are a special case, this allows us to select the correct "Projects" language for them
                if (endpoint.EndpointName == OmniSharpEndpoints.ProjectInformation || endpoint.EndpointName == OmniSharpEndpoints.WorkspaceInformation)
                {
                    handler = projectSystemPredicateHandler;
                }
                else if (endpoint.EndpointName == OmniSharpEndpoints.PackageSearch || endpoint.EndpointName == OmniSharpEndpoints.PackageSource || endpoint.EndpointName == OmniSharpEndpoints.PackageVersion)
                {
                    handler = nugetPredicateHandler;
                }
                else
                {
                    handler = languagePredicateHandler;
                }

                // This lets any endpoint, that contains a Request object, invoke update buffer.
                // The language will be same language as the caller, this means any language service
                // must implement update buffer.
                var updateEndpointHandler = updateBufferEndpointHandler;
                if (endpoint.EndpointName == OmniSharpEndpoints.UpdateBuffer)
                {
                    // We don't want to call update buffer on update buffer.
                    updateEndpointHandler = new Lazy <EndpointHandler <UpdateBufferRequest, object> >(() => null);
                }

                return(EndpointHandler.Factory(handler, _compositionHost, _logger, endpoint, handlers, updateEndpointHandler, Enumerable.Empty <Plugin>()));
            }),
                StringComparer.OrdinalIgnoreCase
                );


            // Handled as alternative middleware in http
            endpointHandlers.Add(
                OmniSharpEndpoints.CheckAliveStatus,
                new Lazy <EndpointHandler>(
                    () => new GenericEndpointHandler(x => Task.FromResult <object>(true)))
                );
            endpointHandlers.Add(
                OmniSharpEndpoints.CheckReadyStatus,
                new Lazy <EndpointHandler>(
                    () => new GenericEndpointHandler(x => Task.FromResult <object>(workspace.Initialized)))
                );
            endpointHandlers.Add(
                OmniSharpEndpoints.StopServer,
                new Lazy <EndpointHandler>(
                    () => new GenericEndpointHandler(x =>
            {
                _cancellationTokenSource.Cancel();
                return(Task.FromResult <object>(null));
            }))
                );

            return(endpointHandlers);
        }