예제 #1
0
        public async Task StartServer()
        {
            log.Debug(Resources.LoggingMessages.server_starting);

            server = await LanguageServer.From(options =>
                                               options

                                               .WithInput(Console.OpenStandardInput())
                                               .WithOutput(Console.OpenStandardOutput())
                                               .ConfigureLogging(x => x
                                                                 .AddSerilog(log)
                                                                 .AddLanguageServer()
                                                                 )

                                               .WithServices(ConfigureServices)

                                               .WithHandler <TextDocumentSyncTaskHandler>()
                                               .WithHandler <DidChangeWatchedFilesHandler>()
                                               .WithHandler <CompletionTaskHandler>()
                                               .WithHandler <CompileHandler>()
                                               .WithHandler <CounterExampleHandler>()
                                               .WithHandler <CodeLensTaskHandler>()
                                               .WithHandler <DefinitionTaskHandler>()
                                               .WithHandler <RenameTaskHandler>()
                                               .WithHandler <HoverTaskHandler>()
                                               .WithHandler <ShutdownHandler>()
                                               );

            ExecutePostLaunchTasks();

            await RedirectStreamUntilServerExits();

            log.Debug(Resources.LoggingMessages.server_closed);
        }
        private RazorLanguageServer(ILanguageServer innerServer)
        {
            if (innerServer is null)
            {
                throw new ArgumentNullException(nameof(innerServer));
            }

            _innerServer = innerServer;
            _disposeLock = new object();
        }
예제 #3
0
        public static Task <ILanguageServer> CreateAsync(Stream input, Stream output, Trace trace)
        {
            Serializer.Instance.JsonSerializer.Converters.RegisterRazorConverters();

            ILanguageServer server = null;

            server = OmniSharp.Extensions.LanguageServer.Server.LanguageServer.PreInit(options =>
                                                                                       options
                                                                                       .WithInput(input)
                                                                                       .WithOutput(output)
                                                                                       .ConfigureLogging(builder => builder
                                                                                                         .AddLanguageServer()
                                                                                                         .SetMinimumLevel(RazorLSPOptions.GetLogLevelForTrace(trace)))
                                                                                       .OnInitialized(async(s, request, response) =>
            {
                var fileChangeDetectorManager = s.Services.GetRequiredService <RazorFileChangeDetectorManager>();
                await fileChangeDetectorManager.InitializedAsync(s);

                // Workaround for https://github.com/OmniSharp/csharp-language-server-protocol/issues/106
                var languageServer = (OmniSharp.Extensions.LanguageServer.Server.LanguageServer)server;
                if (request.Capabilities.Workspace.Configuration.IsSupported)
                {
                    // Initialize our options for the first time.
                    var optionsMonitor = languageServer.Services.GetRequiredService <RazorLSPOptionsMonitor>();
                    _ = Task.Delay(TimeSpan.FromSeconds(3)).ContinueWith(async(_) => await optionsMonitor.UpdateAsync());
                }
            })
                                                                                       .WithHandler <RazorDocumentSynchronizationEndpoint>()
                                                                                       .WithHandler <RazorCompletionEndpoint>()
                                                                                       .WithHandler <RazorHoverEndpoint>()
                                                                                       .WithHandler <RazorLanguageEndpoint>()
                                                                                       .WithHandler <RazorConfigurationEndpoint>()
                                                                                       .WithHandler <RazorFormattingEndpoint>()
                                                                                       .WithHandler <RazorOnTypeFormattingEndpoint>()
                                                                                       .WithHandler <RazorSemanticTokenEndpoint>()
                                                                                       .WithHandler <RazorSemanticTokenLegendEndpoint>()
                                                                                       .WithServices(services =>
            {
                var filePathNormalizer = new FilePathNormalizer();
                services.AddSingleton <FilePathNormalizer>(filePathNormalizer);

                var foregroundDispatcher = new DefaultForegroundDispatcher();
                services.AddSingleton <ForegroundDispatcher>(foregroundDispatcher);

                var generatedDocumentPublisher = new DefaultGeneratedDocumentPublisher(foregroundDispatcher, new Lazy <OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>(() => server));
                services.AddSingleton <ProjectSnapshotChangeTrigger>(generatedDocumentPublisher);
                services.AddSingleton <GeneratedDocumentPublisher>(generatedDocumentPublisher);

                var documentVersionCache = new DefaultDocumentVersionCache(foregroundDispatcher);
                services.AddSingleton <DocumentVersionCache>(documentVersionCache);
                services.AddSingleton <ProjectSnapshotChangeTrigger>(documentVersionCache);
                var containerStore = new DefaultGeneratedDocumentContainerStore(
                    foregroundDispatcher,
                    documentVersionCache,
                    generatedDocumentPublisher);
                services.AddSingleton <GeneratedDocumentContainerStore>(containerStore);
                services.AddSingleton <ProjectSnapshotChangeTrigger>(containerStore);

                services.AddSingleton <RemoteTextLoaderFactory, DefaultRemoteTextLoaderFactory>();
                services.AddSingleton <ProjectResolver, DefaultProjectResolver>();
                services.AddSingleton <DocumentResolver, DefaultDocumentResolver>();
                services.AddSingleton <RazorProjectService, DefaultRazorProjectService>();
                services.AddSingleton <ProjectSnapshotChangeTrigger, BackgroundDocumentGenerator>();
                services.AddSingleton <RazorDocumentMappingService, DefaultRazorDocumentMappingService>();
                services.AddSingleton <RazorFileChangeDetectorManager>();

                // Options
                services.AddSingleton <RazorConfigurationService, DefaultRazorConfigurationService>();
                services.AddSingleton <RazorLSPOptionsMonitor>();
                services.AddSingleton <IOptionsMonitor <RazorLSPOptions>, RazorLSPOptionsMonitor>();

                // File change listeners
                services.AddSingleton <IProjectConfigurationFileChangeListener, ProjectConfigurationStateSynchronizer>();
                services.AddSingleton <IProjectFileChangeListener, ProjectFileSynchronizer>();
                services.AddSingleton <IRazorFileChangeListener, RazorFileSynchronizer>();

                // File Change detectors
                services.AddSingleton <IFileChangeDetector, ProjectConfigurationFileChangeDetector>();
                services.AddSingleton <IFileChangeDetector, ProjectFileChangeDetector>();
                services.AddSingleton <IFileChangeDetector, RazorFileChangeDetector>();

                // Document processed listeners
                services.AddSingleton <DocumentProcessedListener, RazorDiagnosticsPublisher>();
                services.AddSingleton <DocumentProcessedListener, UnsynchronizableContentDocumentProcessedListener>();

                services.AddSingleton <HostDocumentFactory, DefaultHostDocumentFactory>();
                services.AddSingleton <ProjectSnapshotManagerAccessor, DefaultProjectSnapshotManagerAccessor>();
                services.AddSingleton <TagHelperFactsService, DefaultTagHelperFactsService>();
                services.AddSingleton <VisualStudio.Editor.Razor.TagHelperCompletionService, VisualStudio.Editor.Razor.DefaultTagHelperCompletionService>();
                services.AddSingleton <TagHelperDescriptionFactory, DefaultTagHelperDescriptionFactory>();

                // Completion
                services.AddSingleton <Completion.TagHelperCompletionService, Completion.DefaultTagHelperCompletionService>();
                services.AddSingleton <RazorCompletionItemProvider, DirectiveCompletionItemProvider>();
                services.AddSingleton <RazorCompletionItemProvider, DirectiveAttributeCompletionItemProvider>();
                services.AddSingleton <RazorCompletionItemProvider, DirectiveAttributeParameterCompletionItemProvider>();
                services.AddSingleton <RazorCompletionItemProvider, DirectiveAttributeTransitionCompletionItemProvider>();
                services.AddSingleton <RazorCompletionItemProvider, MarkupTransitionCompletionItemProvider>();

                // Formatting
                services.AddSingleton <RazorFormatOnTypeProvider, HtmlSmartIndentFormatOnTypeProvider>();
                services.AddSingleton <RazorFormatOnTypeProvider, CloseRazorCommentFormatOnTypeProvider>();
                services.AddSingleton <RazorFormatOnTypeProvider, CloseTextTagFormatOnTypeProvider>();
                services.AddSingleton <RazorFormattingService, DefaultRazorFormattingService>();

                services.AddSingleton <RazorCompletionFactsService, DefaultRazorCompletionFactsService>();
                services.AddSingleton <RazorSemanticTokenInfoService, DefaultRazorSemanticTokenInfoService>();
                services.AddSingleton <RazorHoverInfoService, DefaultRazorHoverInfoService>();
                services.AddSingleton <HtmlFactsService, DefaultHtmlFactsService>();
            }));

            server.OnShutdown(() =>
            {
                TempDirectory.Instance.Dispose();
                return(Unit.Task);
            });

            try
            {
                var factory = new LoggerFactory();
                var logger  = factory.CreateLogger <RazorLanguageServer>();
                var assemblyInformationAttribute = typeof(RazorLanguageServer).Assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>();
                logger.LogInformation("Razor Language Server version " + assemblyInformationAttribute.InformationalVersion);
            }
            catch
            {
                // Swallow exceptions from determining assembly information.
            }

            return(Task.FromResult(server));
        }
        public static Task <RazorLanguageServer> CreateAsync(Stream input, Stream output, Trace trace, Action <RazorLanguageServerBuilder> configure = null)
        {
            Serializer.Instance.Settings.Converters.Add(SemanticTokensOrSemanticTokensEditsConverter.Instance);
            Serializer.Instance.JsonSerializer.Converters.RegisterRazorConverters();

            // Custom ClientCapabilities deserializer to extract experimental capabilities
            Serializer.Instance.JsonSerializer.Converters.Add(ExtendableClientCapabilitiesJsonConverter.Instance);

            ILanguageServer server = null;

            server = OmniSharp.Extensions.LanguageServer.Server.LanguageServer.PreInit(options =>
                                                                                       options
                                                                                       .WithInput(input)
                                                                                       .WithOutput(output)
                                                                                       .ConfigureLogging(builder => builder
                                                                                                         .AddLanguageServer(RazorLSPOptions.GetLogLevelForTrace(trace))
                                                                                                         .SetMinimumLevel(LogLevel.Trace)) // We set the minimum level here to "Trace" to ensure that other providers still get the opportunity to act on logs if they prefer.
                                                                                       .OnInitialized(async(s, request, response) =>
            {
                var jsonRpcHandlers        = s.Services.GetServices <IJsonRpcHandler>();
                var registrationExtensions = jsonRpcHandlers.OfType <IRegistrationExtension>();
                if (registrationExtensions.Any())
                {
                    var capabilities      = new ExtendableServerCapabilities(response.Capabilities, registrationExtensions);
                    response.Capabilities = capabilities;
                }

                var fileChangeDetectorManager = s.Services.GetRequiredService <RazorFileChangeDetectorManager>();
                await fileChangeDetectorManager.InitializedAsync();

                // Workaround for https://github.com/OmniSharp/csharp-language-server-protocol/issues/106
                var languageServer = (OmniSharp.Extensions.LanguageServer.Server.LanguageServer)server;
                if (request.Capabilities.Workspace.Configuration.IsSupported)
                {
                    // Initialize our options for the first time.
                    var optionsMonitor = languageServer.Services.GetRequiredService <RazorLSPOptionsMonitor>();
                    _ = Task.Delay(TimeSpan.FromSeconds(3)).ContinueWith(async(_) => await optionsMonitor.UpdateAsync());
                }
            })
                                                                                       .WithHandler <RazorDocumentSynchronizationEndpoint>()
                                                                                       .WithHandler <RazorCompletionEndpoint>()
                                                                                       .WithHandler <RazorHoverEndpoint>()
                                                                                       .WithHandler <RazorLanguageEndpoint>()
                                                                                       .WithHandler <RazorConfigurationEndpoint>()
                                                                                       .WithHandler <RazorFormattingEndpoint>()
                                                                                       .WithHandler <RazorSemanticTokensEndpoint>()
                                                                                       .WithHandler <RazorSemanticTokensLegendEndpoint>()
                                                                                       .WithHandler <OnAutoInsertEndpoint>()
                                                                                       .WithHandler <CodeActionEndpoint>()
                                                                                       .WithHandler <CodeActionResolutionEndpoint>()
                                                                                       .WithHandler <MonitorProjectConfigurationFilePathEndpoint>()
                                                                                       .WithHandler <RazorComponentRenameEndpoint>()
                                                                                       .WithHandler <RazorDefinitionEndpoint>()
                                                                                       .WithServices(services =>
            {
                if (configure != null)
                {
                    var builder = new RazorLanguageServerBuilder(services);
                    configure(builder);
                }

                var filePathNormalizer = new FilePathNormalizer();
                services.AddSingleton <FilePathNormalizer>(filePathNormalizer);

                var foregroundDispatcher = new DefaultForegroundDispatcher();
                services.AddSingleton <ForegroundDispatcher>(foregroundDispatcher);

                var generatedDocumentPublisher = new DefaultGeneratedDocumentPublisher(foregroundDispatcher, new Lazy <OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>(() => server));
                services.AddSingleton <ProjectSnapshotChangeTrigger>(generatedDocumentPublisher);
                services.AddSingleton <GeneratedDocumentPublisher>(generatedDocumentPublisher);

                var documentVersionCache = new DefaultDocumentVersionCache(foregroundDispatcher);
                services.AddSingleton <DocumentVersionCache>(documentVersionCache);
                services.AddSingleton <ProjectSnapshotChangeTrigger>(documentVersionCache);
                var containerStore = new DefaultGeneratedDocumentContainerStore(
                    foregroundDispatcher,
                    documentVersionCache,
                    generatedDocumentPublisher);
                services.AddSingleton <GeneratedDocumentContainerStore>(containerStore);
                services.AddSingleton <ProjectSnapshotChangeTrigger>(containerStore);

                services.AddSingleton <RemoteTextLoaderFactory, DefaultRemoteTextLoaderFactory>();
                services.AddSingleton <ProjectResolver, DefaultProjectResolver>();
                services.AddSingleton <DocumentResolver, DefaultDocumentResolver>();
                services.AddSingleton <RazorProjectService, DefaultRazorProjectService>();
                services.AddSingleton <ProjectSnapshotChangeTrigger, BackgroundDocumentGenerator>();
                services.AddSingleton <RazorDocumentMappingService, DefaultRazorDocumentMappingService>();
                services.AddSingleton <RazorFileChangeDetectorManager>();

                // Options
                services.AddSingleton <RazorConfigurationService, DefaultRazorConfigurationService>();
                services.AddSingleton <RazorLSPOptionsMonitor>();
                services.AddSingleton <IOptionsMonitor <RazorLSPOptions>, RazorLSPOptionsMonitor>();

                // File change listeners
                services.AddSingleton <IProjectConfigurationFileChangeListener, ProjectConfigurationStateSynchronizer>();
                services.AddSingleton <IProjectFileChangeListener, ProjectFileSynchronizer>();
                services.AddSingleton <IRazorFileChangeListener, RazorFileSynchronizer>();

                // File Change detectors
                services.AddSingleton <IFileChangeDetector, ProjectConfigurationFileChangeDetector>();
                services.AddSingleton <IFileChangeDetector, ProjectFileChangeDetector>();
                services.AddSingleton <IFileChangeDetector, RazorFileChangeDetector>();

                // Document processed listeners
                services.AddSingleton <DocumentProcessedListener, RazorDiagnosticsPublisher>();
                services.AddSingleton <DocumentProcessedListener, UnsynchronizableContentDocumentProcessedListener>();

                services.AddSingleton <HostDocumentFactory, DefaultHostDocumentFactory>();
                services.AddSingleton <ProjectSnapshotManagerAccessor, DefaultProjectSnapshotManagerAccessor>();
                services.AddSingleton <TagHelperFactsService, DefaultTagHelperFactsService>();
                services.AddSingleton <VisualStudio.Editor.Razor.TagHelperCompletionService, VisualStudio.Editor.Razor.DefaultTagHelperCompletionService>();
                services.AddSingleton <TagHelperDescriptionFactory, DefaultTagHelperDescriptionFactory>();

                // Completion
                services.AddSingleton <Completion.TagHelperCompletionService, Completion.DefaultTagHelperCompletionService>();
                services.AddSingleton <RazorCompletionItemProvider, DirectiveCompletionItemProvider>();
                services.AddSingleton <RazorCompletionItemProvider, DirectiveAttributeCompletionItemProvider>();
                services.AddSingleton <RazorCompletionItemProvider, DirectiveAttributeParameterCompletionItemProvider>();
                services.AddSingleton <RazorCompletionItemProvider, DirectiveAttributeTransitionCompletionItemProvider>();
                services.AddSingleton <RazorCompletionItemProvider, MarkupTransitionCompletionItemProvider>();

                // Auto insert
                services.AddSingleton <RazorOnAutoInsertProvider, HtmlSmartIndentOnAutoInsertProvider>();
                services.AddSingleton <RazorOnAutoInsertProvider, CloseRazorCommentOnAutoInsertProvider>();
                services.AddSingleton <RazorOnAutoInsertProvider, CloseTextTagOnAutoInsertProvider>();
                services.AddSingleton <RazorOnAutoInsertProvider, AttributeSnippetOnAutoInsertProvider>();

                // Formatting
                services.AddSingleton <RazorFormattingService, DefaultRazorFormattingService>();

                // Formatting Passes
                services.AddSingleton <IFormattingPass, CodeBlockDirectiveFormattingPass>();
                services.AddSingleton <IFormattingPass, CSharpOnTypeFormattingPass>();
                services.AddSingleton <IFormattingPass, FormattingStructureValidationPass>();
                services.AddSingleton <IFormattingPass, FormattingContentValidationPass>();

                // Code actions
                services.AddSingleton <RazorCodeActionProvider, ExtractToCodeBehindCodeActionProvider>();
                services.AddSingleton <RazorCodeActionResolver, ExtractToCodeBehindCodeActionResolver>();
                services.AddSingleton <RazorCodeActionProvider, ComponentAccessibilityCodeActionProvider>();
                services.AddSingleton <RazorCodeActionResolver, CreateComponentCodeActionResolver>();
                services.AddSingleton <RazorCodeActionResolver, AddUsingsCodeActionResolver>();

                // Other
                services.AddSingleton <RazorCompletionFactsService, DefaultRazorCompletionFactsService>();
                services.AddSingleton <RazorSemanticTokensInfoService, DefaultRazorSemanticTokensInfoService>();
                services.AddSingleton <RazorHoverInfoService, DefaultRazorHoverInfoService>();
                services.AddSingleton <HtmlFactsService, DefaultHtmlFactsService>();
                services.AddSingleton <WorkspaceDirectoryPathResolver, DefaultWorkspaceDirectoryPathResolver>();
                services.AddSingleton <RazorComponentSearchEngine, DefaultRazorComponentSearchEngine>();
            }));

            try
            {
                var factory = new LoggerFactory();
                var logger  = factory.CreateLogger <RazorLanguageServer>();
                var assemblyInformationAttribute = typeof(RazorLanguageServer).Assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>();
                logger.LogInformation("Razor Language Server version " + assemblyInformationAttribute.InformationalVersion);
                factory.Dispose();
            }
            catch
            {
                // Swallow exceptions from determining assembly information.
            }

            var razorLanguageServer = new RazorLanguageServer(server);

            IDisposable exitSubscription = null;

            exitSubscription = server.Exit.Subscribe((_) =>
            {
                exitSubscription.Dispose();
                razorLanguageServer.Dispose();
            });

            return(Task.FromResult(razorLanguageServer));
        }