Skip to content
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

(GH-121) Implement ConfigurationHandler #135

Merged
merged 12 commits into from
Mar 3, 2019
2 changes: 1 addition & 1 deletion src/Chocolatey.Language.Server/.vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build",
"${workspaceFolder}/Chocolatey.Language.Server.csproj"
],
"problemMatcher": "$tsc"
"problemMatcher": "$msCompile"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<ItemGroup>
<PackageReference Include="GuiLabs.Language.Xml" Version="1.2.35" />
<PackageReference Include="OmniSharp.Extensions.LanguageServer" Version="0.11.3" />
<PackageReference Include="Serilog" Version="2.5.0" />
<PackageReference Include="Serilog.Enrichers.Demystify" Version="0.1.0-dev-00016" />
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2" />
<PackageReference Include="Serilog.Sinks.Seq" Version="3.3.3" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OmniSharp.Extensions.Embedded.MediatR;

namespace Chocolatey.Language.Server.CustomProtocol
{
/// <summary>
/// Custom handler for "workspace/didChangeConfiguration" with the configuration as a <see cref="JObject"/>.
/// </summary>
[Method("workspace/didChangeConfiguration")]
public interface IDidChangeConfigurationSettingsHandler
: IJsonRpcNotificationHandler<DidChangeConfigurationObjectParams>, IJsonRpcHandler, IRegistration<object>, ICapability<DidChangeConfigurationCapability>
{
}

/// <summary>
/// Notification parameters for "workspace/didChangeConfiguration".
/// </summary>
public class DidChangeConfigurationObjectParams : IRequest
{
/// <summary>
/// The current settings.
/// </summary>
[JsonProperty("settings")]
public JToken Settings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using Buffer = Microsoft.Language.Xml.Buffer;

namespace Chocolatey.Language.Server
namespace Chocolatey.Language.Server.Engine
{
public class BufferManager
{
Expand Down
45 changes: 45 additions & 0 deletions src/Chocolatey.Language.Server/Engine/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Chocolatey.Language.Server.Engine
{
public sealed class Configuration
{
/// <summary>
/// The name of the configuration section as passed in messages such as <see cref="CustomProtocol.DidChangeConfigurationObjectParams"/>.
/// </summary>
public static readonly string SectionName = "chocoLanguageServer";

/// <summary>
/// The MSBuild language service's main configuration.
/// </summary>
[JsonProperty("language", ObjectCreationHandling = ObjectCreationHandling.Reuse)]
public LanguageConfiguration Language { get; } = new LanguageConfiguration();
}

/// <summary>
/// The main settings for the MSBuild language service.
/// </summary>
public class LanguageConfiguration
{
/// <summary>
/// Create a new <see cref="LanguageConfiguration"/>.
/// </summary>
public LanguageConfiguration()
{
}

/// <summary>
/// Disable the language service?
/// </summary>
[JsonProperty("useClassicProvider")]
public bool DisableLanguageService { get; set; } = false;

/// <summary>
/// Types of object from the current project to include when offering completions.
/// </summary>
[JsonProperty("suppressedRules", ObjectCreationHandling = ObjectCreationHandling.Reuse)]
public HashSet<string> SuppressedRules { get; } = new HashSet<string>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Chocolatey.Language.Server.Engine
{
public interface IConfigurationProvider
{
Configuration Configuration { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using System.Collections.Generic;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;

namespace Chocolatey.Language.Server
namespace Chocolatey.Language.Server.Engine
{
/// <summary>
/// A quick-and-dirty calculator for text positions.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Linq;

namespace Chocolatey.Language.Server
namespace Chocolatey.Language.Server.Extensions
{
/// <summary>
/// Extensions for IEnumerable
Expand Down
117 changes: 117 additions & 0 deletions src/Chocolatey.Language.Server/Extensions/ProtocolExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using Chocolatey.Language.Server.Engine;
using Chocolatey.Language.Server.CustomProtocol;
using OmniSharp.Extensions.LanguageServer;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Server;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Linq;

namespace Chocolatey.Language.Server.Extensions
{
/// <summary>
/// Custom Language Server Protocol extensions.
/// </summary>
public static class ProtocolExtensions
{
/// <summary>
/// Update the configuration from the specified configuration-change notification.
/// </summary>
/// <param name="configuration">
/// The <see cref="Configuration"/> to update.
/// </param>
/// <param name="request">
/// The configuration-change notification.
/// </param>
public static void UpdateFrom(this Configuration configuration, DidChangeConfigurationObjectParams request)
{
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}

if (request == null)
{
throw new ArgumentNullException(nameof(request));
}

JObject json = request.Settings?.SelectToken(Configuration.SectionName) as JObject;

if (json == null)
{
return;
}

configuration.UpdateFrom(json);
}

/// <summary>
/// Update the configuration from the specified initialisation request.
/// </summary>
/// <param name="configuration">
/// The <see cref="Configuration"/> to update.
/// </param>
/// <param name="request">
/// The initialisation request.
/// </param>
public static void UpdateFrom(this Configuration configuration, InitializeParams request)
{
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}

if (request == null)
{
throw new ArgumentNullException(nameof(request));
}

JToken initializationParameters = request.InitializationOptions as JToken;

if (initializationParameters == null)
{
return;
}

JObject json = initializationParameters.SelectToken(Configuration.SectionName) as JObject;

if (json == null)
{
return;
}

configuration.UpdateFrom(json);
}

/// <summary>
/// Update the configuration from the specified JSON.
/// </summary>
/// <param name="configuration">
/// The <see cref="Configuration"/> to update.
/// </param>
/// <param name="settingsJson">
/// A <see cref="JObject"/> representing the flattened settings JSON from VS Code.
/// </param>
public static void UpdateFrom(this Configuration configuration, JObject settingsJson)
{
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}

if (settingsJson == null)
{
throw new ArgumentNullException(nameof(settingsJson));
}

// Temporary workaround - JsonSerializer.Populate reuses existing HashSet.
configuration.Language.SuppressedRules.Clear();

using (JsonReader reader = settingsJson.CreateReader())
{
new JsonSerializer().Populate(reader, configuration);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Net;

namespace Chocolatey.Language.Server
namespace Chocolatey.Language.Server.Extensions
{
/// <summary>
/// Extensions for Uri
Expand Down Expand Up @@ -36,7 +36,7 @@ public static bool IsValid(this Uri url, bool useGetMethod = false)
}

/// <summary>
/// Tries to validate if an URL is SSL capable.
/// Tries to validate if an URL is SSL capable.
/// HTTP: Will return true if the URL validates with SSL, otherwise false.
/// HTTPS: it returns false
/// </summary>
Expand All @@ -52,7 +52,7 @@ public static bool SslCapable(this Uri url)
// Handle http: override the scheme and use the default https port
uri.Scheme = Uri.UriSchemeHttps;
uri.Port = -1;
return uri.Uri.IsValid();
return uri.Uri.IsValid();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;

namespace Chocolatey.Language.Server
namespace Chocolatey.Language.Server.Handlers
{
public class CodeActionHandler : ICodeActionHandler
{
Expand Down
Loading