Skip to content

Commit

Permalink
(GH-121) Refactoring of ConfigurationHandler
Browse files Browse the repository at this point in the history
- Based on the work in this repository:
https://github.com/tintoy/msbuild-project-tools-server
- Needed updated due to changes in most recent version of Omnisharp
  • Loading branch information
gep13 committed Mar 1, 2019
1 parent 7dc427c commit 064b563
Show file tree
Hide file tree
Showing 10 changed files with 398 additions and 35 deletions.
26 changes: 26 additions & 0 deletions src/Chocolatey.Language.Server/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.1/Chocolatey.Language.Server.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
15 changes: 15 additions & 0 deletions src/Chocolatey.Language.Server/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Chocolatey.Language.Server.csproj"
],
"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>
45 changes: 45 additions & 0 deletions src/Chocolatey.Language.Server/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
{
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>();
}
}
32 changes: 0 additions & 32 deletions src/Chocolatey.Language.Server/ConfigurationHandler.cs

This file was deleted.

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;
}
}
115 changes: 115 additions & 0 deletions src/Chocolatey.Language.Server/CustomProtocol/ProtocolExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
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.CustomProtocol
{
/// <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);
}
}
}
}
Loading

0 comments on commit 064b563

Please sign in to comment.