public static async Task MainAsync(string[] args) { var logLevel = LogLevel.Information; for (var i = 0; i < args.Length; i++) { if (args[i].IndexOf("debug", StringComparison.OrdinalIgnoreCase) >= 0) { while (!Debugger.IsAttached) { Thread.Sleep(1000); } Debugger.Break(); continue; } if (args[i] == "--logLevel" && i + 1 < args.Length) { var logLevelString = args[++i]; if (!Enum.TryParse(logLevelString, out logLevel)) { logLevel = LogLevel.Information; Console.WriteLine($"Invalid log level '{logLevelString}'. Defaulting to {logLevel.ToString()}."); } } } Serializer.Instance.JsonSerializer.Converters.RegisterRazorConverters(); var factory = new LoggerFactory(); ILanguageServer server = null; server = await OmniSharp.Extensions.LanguageServer.Server.LanguageServer.From(options => options .WithInput(Console.OpenStandardInput()) .WithOutput(Console.OpenStandardOutput()) .WithLoggerFactory(factory) .AddDefaultLoggingProvider() .WithMinimumLogLevel(logLevel) .WithHandler <RazorDocumentSynchronizationEndpoint>() .WithHandler <RazorCompletionEndpoint>() .WithHandler <RazorHoverEndpoint>() .WithHandler <RazorLanguageEndpoint>() .WithHandler <RazorProjectEndpoint>() .WithServices(services => { services.AddSingleton <RemoteTextLoaderFactory, DefaultRemoteTextLoaderFactory>(); services.AddSingleton <ProjectResolver, DefaultProjectResolver>(); services.AddSingleton <DocumentResolver, DefaultDocumentResolver>(); services.AddSingleton <FilePathNormalizer>(); services.AddSingleton <RazorProjectService, DefaultRazorProjectService>(); services.AddSingleton <ProjectSnapshotChangeTrigger, BackgroundDocumentGenerator>(); // 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>(); var foregroundDispatcher = new VSCodeForegroundDispatcher(); services.AddSingleton <ForegroundDispatcher>(foregroundDispatcher); var csharpPublisher = new DefaultCSharpPublisher(foregroundDispatcher, new Lazy <OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer>(() => server)); services.AddSingleton <ProjectSnapshotChangeTrigger>(csharpPublisher); services.AddSingleton <CSharpPublisher>(csharpPublisher); services.AddSingleton <RazorCompletionFactsService, DefaultRazorCompletionFactsService>(); services.AddSingleton <RazorHoverInfoService, DefaultRazorHoverInfoService>(); services.AddSingleton <HtmlFactsService, DefaultHtmlFactsService>(); var documentVersionCache = new DefaultDocumentVersionCache(foregroundDispatcher); services.AddSingleton <DocumentVersionCache>(documentVersionCache); services.AddSingleton <ProjectSnapshotChangeTrigger>(documentVersionCache); var containerStore = new DefaultGeneratedCodeContainerStore( foregroundDispatcher, documentVersionCache, csharpPublisher); services.AddSingleton <GeneratedCodeContainerStore>(containerStore); services.AddSingleton <ProjectSnapshotChangeTrigger>(containerStore); })); // Workaround for https://github.com/OmniSharp/csharp-language-server-protocol/issues/106 var languageServer = (OmniSharp.Extensions.LanguageServer.Server.LanguageServer)server; languageServer.MinimumLogLevel = logLevel; try { var logger = factory.CreateLogger <Program>(); var assemblyInformationAttribute = typeof(Program).Assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>(); logger.LogInformation("Razor Language Server version " + assemblyInformationAttribute.InformationalVersion); } catch { // Swallow exceptions from determining assembly information. } await server.WaitForExit; TempDirectory.Instance.Dispose(); }
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 <RazorSemanticTokenEndpoint>() .WithHandler <RazorSemanticTokenLegendEndpoint>() .WithServices(services => { services.AddSingleton <FilePathNormalizer>(); 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>(); var foregroundDispatcher = new VSCodeForegroundDispatcher(); 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); // Formatting services.AddSingleton <RazorFormattingService, DefaultRazorFormattingService>(); services.AddSingleton <RazorCompletionFactsService, DefaultRazorCompletionFactsService>(); services.AddSingleton <RazorSemanticTokenInfoService, DefaultRazorSemanticTokenInfoService>(); services.AddSingleton <RazorHoverInfoService, DefaultRazorHoverInfoService>(); services.AddSingleton <HtmlFactsService, DefaultHtmlFactsService>(); 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); })); 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)); }