public void Publish_SecondTime_PublishesSourceTextDifferences() { // Arrange var csharpPublisher = new DefaultCSharpPublisher(Dispatcher, new Lazy <ILanguageServer>(() => Server)); var initialSourceText = SourceText.From("// Initial content"); csharpPublisher.Publish("/path/to/file.razor", initialSourceText, 123); var change = new TextChange( new TextSpan(initialSourceText.Length, 0), "!!"); var changedSourceText = initialSourceText.WithChanges(change); // Act csharpPublisher.Publish("/path/to/file.razor", changedSourceText, 124); // Assert Assert.Equal(2, Server.UpdateRequests.Count); var updateRequest = Server.UpdateRequests.Last(); Assert.Equal("/path/to/file.razor", updateRequest.HostDocumentFilePath); var textChange = Assert.Single(updateRequest.Changes); Assert.Equal(change, textChange); Assert.Equal(124, updateRequest.HostDocumentVersion); }
public void ProjectSnapshotManager_DocumentRemoved_RepublishesTextChanges() { // Arrange var csharpPublisher = new DefaultCSharpPublisher(Dispatcher, new Lazy <ILanguageServer>(() => Server)); csharpPublisher.Initialize(ProjectManager); var sourceTextContent = "// The content"; var initialSourceText = SourceText.From(sourceTextContent); csharpPublisher.Publish(HostDocument.FilePath, initialSourceText, 123); // Act ProjectManager.DocumentRemoved(HostProject, HostDocument); csharpPublisher.Publish(HostDocument.FilePath, initialSourceText, 123); // Assert Assert.Equal(2, Server.UpdateRequests.Count); var updateRequest = Server.UpdateRequests.Last(); Assert.Equal(HostDocument.FilePath, updateRequest.HostDocumentFilePath); var textChange = Assert.Single(updateRequest.Changes); Assert.Equal(sourceTextContent, textChange.NewText); Assert.Equal(123, updateRequest.HostDocumentVersion); }
public void Publish_FirstTime_PublishesEntireSourceText() { // Arrange var csharpPublisher = new DefaultCSharpPublisher(Dispatcher, new Lazy <ILanguageServer>(() => Server)); var content = "// C# content"; var sourceText = SourceText.From(content); // Act csharpPublisher.Publish("/path/to/file.razor", sourceText, 123); // Assert var updateRequest = Assert.Single(Server.UpdateRequests); Assert.Equal("/path/to/file.razor", updateRequest.HostDocumentFilePath); var textChange = Assert.Single(updateRequest.Changes); Assert.Equal(content, textChange.NewText); Assert.Equal(123, updateRequest.HostDocumentVersion); }
public void Publish_SecondTime_IdenticalContent_NoTextChanges() { // Arrange var csharpPublisher = new DefaultCSharpPublisher(Dispatcher, new Lazy <ILanguageServer>(() => Server)); var sourceTextContent = "// The content"; var initialSourceText = SourceText.From(sourceTextContent); csharpPublisher.Publish("/path/to/file.razor", initialSourceText, 123); var identicalSourceText = SourceText.From(sourceTextContent); // Act csharpPublisher.Publish("/path/to/file.razor", identicalSourceText, 124); // Assert Assert.Equal(2, Server.UpdateRequests.Count); var updateRequest = Server.UpdateRequests.Last(); Assert.Equal("/path/to/file.razor", updateRequest.HostDocumentFilePath); Assert.Empty(updateRequest.Changes); Assert.Equal(124, updateRequest.HostDocumentVersion); }
public void Publish_DifferentFileSameContent_PublishesEverything() { // Arrange var csharpPublisher = new DefaultCSharpPublisher(Dispatcher, new Lazy <ILanguageServer>(() => Server)); var sourceTextContent = "// The content"; var initialSourceText = SourceText.From(sourceTextContent); csharpPublisher.Publish("/path/to/file1.razor", initialSourceText, 123); var identicalSourceText = SourceText.From(sourceTextContent); // Act csharpPublisher.Publish("/path/to/file2.razor", identicalSourceText, 123); // Assert Assert.Equal(2, Server.UpdateRequests.Count); var updateRequest = Server.UpdateRequests.Last(); Assert.Equal("/path/to/file2.razor", updateRequest.HostDocumentFilePath); var textChange = Assert.Single(updateRequest.Changes); Assert.Equal(sourceTextContent, textChange.NewText); Assert.Equal(123, updateRequest.HostDocumentVersion); }
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(); }