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
61 changes: 47 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"type": "object",
"title": "Chocolatey Configuration",
"properties": {
"chocolatey.templatePackages": {
"chocolatey.templates": {
"type": "object",
"default": {
"names": [
Expand All @@ -69,11 +69,44 @@
},
"description": "The Chocolatey Template Packages Configuration"
},
"chocolatey.new.properties": {
"chocolatey.commands": {
"properties": {
"type": "object",
"new": {
"type": "object",
"properties": {
"properties": {
"type": "object",
"description": "A key value pair of properties to pass to Chocolatey when creating a new package.",
"uniqueItems": true,
"title": "New package properties"
}
}
}
}
},
"chocolatey.language": {
"type": "object",
"description": "A key value pair of properties to pass to Chocolatey when creating a new package.",
"uniqueItems": true,
"title": "New package properties"
"properties": {
"allowSuppressionOfRequirements": {
"title": "Allow requirements suppression",
"type": "boolean",
"default": false,
"description": "Allow the suppression of rules that are marked as requirements"
},
"disableLanguageService": {
"title": "Disable language service",
"type": "boolean",
"default": false,
"description": "Prevent the Chocolatey Language Service from starting up"
},
"suppressedRules": {
"title": "Suppress Rules",
"type": "array",
"description": "The codes for the Chocolatey Language Service rules that should be suppressed",
"default": []
}
}
}
}
}
Expand Down Expand Up @@ -104,15 +137,15 @@
"title": "Chocolatey: Add API Key"
}
],
"menus": {
"explorer/context": [
{
"when": "explorerResourceIsFolder",
"command": "chocolatey.new",
"group": "2_workspace"
}
]
},
"menus": {
"explorer/context": [
{
"when": "explorerResourceIsFolder",
"command": "chocolatey.new",
"group": "2_workspace"
}
]
},
"snippets": [
{
"language": "powershell",
Expand Down
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
93 changes: 93 additions & 0 deletions src/Chocolatey.Language.Server/Engine/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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 = "chocolatey";

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

/// <summary>
/// The Chocolatey Command Configuration
/// </summary>
[JsonProperty("commands", ObjectCreationHandling = ObjectCreationHandling.Reuse)]
public CommandsConfiguration Commands { get ; } = new CommandsConfiguration();


[JsonProperty("templates", ObjectCreationHandling = ObjectCreationHandling.Reuse)]
public TemplatesConfiguration Templates { get; } = new TemplatesConfiguration();
}

public class TemplatesConfiguration
{
public TemplatesConfiguration()
{
}

[JsonProperty("names", ObjectCreationHandling = ObjectCreationHandling.Reuse)]
public HashSet<string> Names { get; } = new HashSet<string>();

[JsonProperty("source", ObjectCreationHandling = ObjectCreationHandling.Reuse)]
public string Source { get; } = "";
}
public class CommandsConfiguration
{
public CommandsConfiguration()
{
}

[JsonProperty("new", ObjectCreationHandling = ObjectCreationHandling.Reuse)]
public NewCommandConfiguration NewCommand { get; } = new NewCommandConfiguration();
}

public class NewCommandConfiguration
{
public NewCommandConfiguration()
{
}

[JsonProperty("properties", ObjectCreationHandling = ObjectCreationHandling.Reuse)]
public Dictionary<string, string> Properties { get; } = new Dictionary<string, string>();
}

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

/// <summary>
/// Allow suppressing of requirements
/// </summary>
[JsonProperty("allowSuppressionOfRequirements")]
public bool AllowSuppressionOfRequirements { get; set; } = false;

/// <summary>
/// Disable the language service?
/// </summary>
[JsonProperty("disableLanguageService")]
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);
}
}
}
}
Loading