Exemple #1
0
        public void Constructor_RegistrationExtensions_Populates()
        {
            // Arrange
            var registrationExtension1 = new TestRegistrationExtension("test1");
            var registrationExtension2 = new TestRegistrationExtension("test2");
            var registrations          = new IRegistrationExtension[] { registrationExtension1, registrationExtension2 };
            var baseCapability         = new ServerCapabilities();

            // Act
            var extendableCapabilities = new ExtendableServerCapabilities(baseCapability, registrations);

            // Assert
            Assert.Equal(new[] { "test1", "test2" }, extendableCapabilities.CapabilityExtensions.Keys.ToArray());
        }
Exemple #2
0
        public void CapabilityExtensions_RoundTripsCorrectly()
        {
            // Arrange
            var registrationExtension  = new TestRegistrationExtension("test1");
            var registrations          = new IRegistrationExtension[] { registrationExtension };
            var baseCapability         = new ServerCapabilities();
            var extendableCapabilities = new ExtendableServerCapabilities(baseCapability, registrations);

            // Act
            var serialized   = JsonConvert.SerializeObject(extendableCapabilities);
            var deserialized = JsonConvert.DeserializeObject <VSCapabilities>(serialized);

            // Assert
            Assert.True(deserialized.Test1);
        }
        public static Task <RazorLanguageServer> CreateAsync(Stream input, Stream output, Trace trace, Action <RazorLanguageServerBuilder> configure = null)
        {
            Serializer.Instance.JsonSerializer.Converters.RegisterRazorConverters();

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

            ILanguageServer server   = null;
            var             logLevel = RazorLSPOptions.GetLogLevelForTrace(trace);
            var             initializedCompletionSource = new TaskCompletionSource <bool>();

            server = OmniSharp.Extensions.LanguageServer.Server.LanguageServer.PreInit(options =>
                                                                                       options
                                                                                       .WithInput(input)
                                                                                       .WithOutput(output)
                                                                                       // StreamJsonRpc has both Serial and Parallel requests. With WithContentModifiedSupport(true) (which is default) when a Serial
                                                                                       // request is made any Parallel requests will be cancelled because the assumption is that Serial requests modify state, and that
                                                                                       // therefore any Parallel request is now invalid and should just try again. A specific instance of this can be seen when you
                                                                                       // hover over a TagHelper while the switch is set to true. Hover is parallel, and a lot of our endpoints like
                                                                                       // textDocument/_ms_onAutoInsert, and razor/languageQuery are Serial. I BELIEVE that specifically what happened is the serial
                                                                                       // languageQuery event gets fired by our semantic tokens endpoint (which fires constantly), cancelling the hover, which red-bars.
                                                                                       // We can prevent that behavior entirely by doing WithContentModifiedSupport, at the possible expense of some delays due doing all requests in serial.
                                                                                       //
                                                                                       // I recommend that we attempt to resolve this and switch back to WithContentModifiedSupport(true) in the future,
                                                                                       // I think that would mean either having 0 Serial Handlers in the whole LS, or making VSLanguageServerClient handle this more gracefully.
                                                                                       .WithContentModifiedSupport(false)
                                                                                       .WithSerializer(Serializer.Instance)
                                                                                       .OnInitialized(async(s, request, response, cancellationToken) =>
            {
                var handlersManager        = s.GetRequiredService <IHandlersManager>();
                var jsonRpcHandlers        = handlersManager.Descriptors.Select(d => d.Handler);
                var registrationExtensions = jsonRpcHandlers.OfType <IRegistrationExtension>().Distinct();
                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>();

                    // Explicitly not passing in the same CancellationToken as that might get cancelled before the update happens.
                    _ = Task.Delay(TimeSpan.FromSeconds(3))
                        .ContinueWith(async(_) => await optionsMonitor.UpdateAsync(), TaskScheduler.Default);
                }
            })
                                                                                       .WithHandler <RazorDocumentSynchronizationEndpoint>()
                                                                                       .WithHandler <RazorCompletionEndpoint>()
                                                                                       .WithHandler <RazorHoverEndpoint>()
                                                                                       .WithHandler <RazorLanguageEndpoint>()
                                                                                       .WithHandler <RazorDiagnosticsEndpoint>()
                                                                                       .WithHandler <RazorConfigurationEndpoint>()
                                                                                       .WithHandler <RazorFormattingEndpoint>()
                                                                                       .WithHandler <RazorSemanticTokensEndpoint>()
                                                                                       .AddHandlerLink(LanguageServerConstants.RazorSemanticTokensEditEndpoint, LanguageServerConstants.LegacyRazorSemanticTokensEditEndpoint)
                                                                                       .AddHandlerLink(LanguageServerConstants.RazorSemanticTokensEndpoint, LanguageServerConstants.LegacyRazorSemanticTokensEndpoint)
                                                                                       .WithHandler <RazorSemanticTokensLegendEndpoint>()
                                                                                       .WithHandler <OnAutoInsertEndpoint>()
                                                                                       .WithHandler <CodeActionEndpoint>()
                                                                                       .WithHandler <CodeActionResolutionEndpoint>()
                                                                                       .WithHandler <MonitorProjectConfigurationFilePathEndpoint>()
                                                                                       .WithHandler <RazorComponentRenameEndpoint>()
                                                                                       .WithHandler <RazorDefinitionEndpoint>()
                                                                                       .WithServices(services =>
            {
                services.AddLogging(builder => builder
                                    .SetMinimumLevel(logLevel)
                                    .AddLanguageProtocolLogging(logLevel));

                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>();

                services.AddSingleton <ProjectSnapshotChangeTrigger, RazorServerReadyPublisher>();

                services.AddSingleton <ClientNotifierServiceBase, DefaultClientNotifierService>();

                services.AddSingleton <IOnLanguageServerStarted, DefaultClientNotifierService>();

                // 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 <TagHelperDescriptionFactory, DefaultTagHelperDescriptionFactory>();

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

                // 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, HtmlFormattingPass>();
                services.AddSingleton <IFormattingPass, CSharpFormattingPass>();
                services.AddSingleton <IFormattingPass, CSharpOnTypeFormattingPass>();
                services.AddSingleton <IFormattingPass, FormattingDiagnosticValidationPass>();
                services.AddSingleton <IFormattingPass, FormattingContentValidationPass>();

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

                // CSharp Code actions
                services.AddSingleton <CSharpCodeActionProvider, TypeAccessibilityCodeActionProvider>();
                services.AddSingleton <CSharpCodeActionProvider, ImplementInterfaceAbstractClassCodeActionProvider>();
                services.AddSingleton <CSharpCodeActionProvider, DefaultCSharpCodeActionProvider>();
                services.AddSingleton <CSharpCodeActionResolver, DefaultCSharpCodeActionResolver>();
                services.AddSingleton <CSharpCodeActionResolver, AddUsingsCSharpCodeActionResolver>();

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

                if (configure != null)
                {
                    var builder = new RazorLanguageServerBuilder(services);
                    configure(builder);
                }

                // Defaults: For when the caller hasn't provided them through the `configure` action.
                services.TryAddSingleton <LanguageServerFeatureOptions, DefaultLanguageServerFeatureOptions>();
            }));

            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));
        }
        public static Task <RazorLanguageServer> CreateAsync(Stream input, Stream output, Trace trace)
        {
            Serializer.Instance.Settings.Converters.Add(SemanticTokensOrSemanticTokensEditsConverter.Instance);
            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 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>()
                                                                                       .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>();

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

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

                // 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));
        }