-
Couldn't load subscription status.
- Fork 214
Sync client settings with LSP server #1549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
acee1f3
da179f9
95f057b
86660d7
76ee76d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.Logging; | ||
| using OmniSharp.Extensions.LanguageServer.Protocol.Models; | ||
| using OmniSharp.Extensions.LanguageServer.Protocol.Server; | ||
|
|
||
| namespace Microsoft.AspNetCore.Razor.LanguageServer | ||
| { | ||
| internal class DefaultRazorConfigurationService : RazorConfigurationService | ||
| { | ||
| private readonly ILanguageServer _server; | ||
| private readonly ILogger _logger; | ||
|
|
||
| public DefaultRazorConfigurationService(ILanguageServer languageServer, ILoggerFactory loggerFactory) | ||
| { | ||
| if (languageServer is null) | ||
| { | ||
| throw new ArgumentNullException(nameof(languageServer)); | ||
| } | ||
|
|
||
| if (loggerFactory is null) | ||
| { | ||
| throw new ArgumentNullException(nameof(loggerFactory)); | ||
| } | ||
|
|
||
| _server = languageServer; | ||
| _logger = loggerFactory.CreateLogger<DefaultRazorConfigurationService>(); | ||
| } | ||
|
|
||
| public async override Task<RazorLSPOptions> GetLatestOptionsAsync() | ||
| { | ||
| try | ||
| { | ||
| var request = new ConfigurationParams() | ||
| { | ||
| Items = new[] | ||
| { | ||
| new ConfigurationItem() | ||
| { | ||
| Section = "razor" | ||
| }, | ||
| } | ||
| }; | ||
|
|
||
| var result = await _server.Client.SendRequest<ConfigurationParams, object[]>("workspace/configuration", request); | ||
| if (result == null || result.Length < 1 || result[0] == null) | ||
| { | ||
| _logger.LogWarning("Client failed to provide the expected configuration."); | ||
| return null; | ||
| } | ||
|
|
||
| var jsonString = result[0].ToString(); | ||
| var builder = new ConfigurationBuilder(); | ||
| using var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); | ||
| builder.AddJsonStream(stream); | ||
| var config = builder.Build(); | ||
|
|
||
| var instance = BuildOptions(config); | ||
| return instance; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.LogWarning($"Failed to sync client configuration on the server: {ex}"); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private RazorLSPOptions BuildOptions(IConfiguration config) | ||
| { | ||
| var instance = RazorLSPOptions.Default; | ||
|
|
||
| Enum.TryParse(config["trace"], out Trace trace); | ||
|
|
||
| var enableFormatting = instance.EnableFormatting; | ||
| if (bool.TryParse(config["format:enable"], out var parsedEnableFormatting)) | ||
| { | ||
| enableFormatting = parsedEnableFormatting; | ||
| } | ||
|
|
||
| return new RazorLSPOptions(trace, enableFormatting); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using MediatR; | ||
| using Microsoft.Extensions.Logging; | ||
| using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities; | ||
| using OmniSharp.Extensions.LanguageServer.Protocol.Models; | ||
| using OmniSharp.Extensions.LanguageServer.Protocol.Server; | ||
|
|
||
| namespace Microsoft.AspNetCore.Razor.LanguageServer | ||
| { | ||
| internal class RazorConfigurationEndpoint : IDidChangeConfigurationHandler | ||
| { | ||
| private readonly RazorLSPOptionsMonitor _optionsMonitor; | ||
| private readonly ILogger _logger; | ||
| private DidChangeConfigurationCapability _capability; | ||
|
|
||
| public RazorConfigurationEndpoint(RazorLSPOptionsMonitor optionsMonitor, ILoggerFactory loggerFactory) | ||
| { | ||
| if (optionsMonitor is null) | ||
| { | ||
| throw new ArgumentNullException(nameof(optionsMonitor)); | ||
| } | ||
|
|
||
| if (loggerFactory is null) | ||
| { | ||
| throw new ArgumentNullException(nameof(loggerFactory)); | ||
| } | ||
|
|
||
| _optionsMonitor = optionsMonitor; | ||
| _logger = loggerFactory.CreateLogger<RazorConfigurationEndpoint>(); | ||
| } | ||
|
|
||
| public object GetRegistrationOptions() | ||
| { | ||
| return new TextDocumentRegistrationOptions() | ||
| { | ||
| DocumentSelector = RazorDefaults.Selector | ||
| }; | ||
| } | ||
|
|
||
| public async Task<Unit> Handle(DidChangeConfigurationParams request, CancellationToken cancellationToken) | ||
| { | ||
| _logger.LogTrace("Settings changed. Updating the server."); | ||
|
|
||
| await _optionsMonitor.UpdateAsync(); | ||
|
|
||
| return new Unit(); | ||
| } | ||
|
|
||
| public void SetCapability(DidChangeConfigurationCapability capability) | ||
| { | ||
| _capability = capability; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Microsoft.AspNetCore.Razor.LanguageServer | ||
| { | ||
| internal abstract class RazorConfigurationService | ||
| { | ||
| public abstract Task<RazorLSPOptions> GetLatestOptionsAsync(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.Extensions.Internal; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Microsoft.AspNetCore.Razor.LanguageServer | ||
| { | ||
| internal class RazorLSPOptions : IEquatable<RazorLSPOptions> | ||
| { | ||
| public RazorLSPOptions(Trace trace, bool enableFormatting) | ||
| { | ||
| Trace = trace; | ||
| EnableFormatting = enableFormatting; | ||
| } | ||
|
|
||
| public static RazorLSPOptions Default => new RazorLSPOptions(trace: default, enableFormatting: true); | ||
|
|
||
| public Trace Trace { get; } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm should this class be immutable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can be but we will no longer be able to do things like services.Configure<RazorLSPOptions>(opt => opt.EnableFormatting = false);There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, our |
||
| public LogLevel MinLogLevel => GetLogLevelForTrace(Trace); | ||
|
|
||
| public bool EnableFormatting { get; } | ||
|
|
||
| public static LogLevel GetLogLevelForTrace(Trace trace) | ||
| { | ||
| return trace switch | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pranav points!!!!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a juicy syntax. |
||
| { | ||
| Trace.Off => LogLevel.None, | ||
| Trace.Messages => LogLevel.Information, | ||
| Trace.Verbose => LogLevel.Trace, | ||
| _ => LogLevel.None, | ||
| }; | ||
| } | ||
|
|
||
| public bool Equals([AllowNull] RazorLSPOptions other) | ||
| { | ||
| return | ||
| other != null && | ||
| Trace == other.Trace && | ||
| EnableFormatting == other.EnableFormatting; | ||
| } | ||
|
|
||
| public override bool Equals(object obj) | ||
| { | ||
| return Equals(obj as RazorLSPOptions); | ||
| } | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| var hash = new HashCodeCombiner(); | ||
| hash.Add(Trace); | ||
| hash.Add(EnableFormatting); | ||
| return hash; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.