Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Lsp/Capabilities/Client/Supports.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Newtonsoft.Json;
using OmniSharp.Extensions.LanguageServer.Converters;

Expand Down Expand Up @@ -38,8 +38,9 @@ public static implicit operator Supports<T>(T value)
public static class Supports
{
public static Supports<T> OfValue<T>(T value)
where T : class
{
return new Supports<T>(true, value);
return new Supports<T>(value != null, value);
}

public static Supports<T> OfBoolean<T>(bool isSupported)
Expand Down
48 changes: 47 additions & 1 deletion src/Lsp/Capabilities/Client/TextDocumentClientCapabilities.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System.ComponentModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace OmniSharp.Extensions.LanguageServer.Capabilities.Client
Expand All @@ -8,74 +9,119 @@ public class TextDocumentClientCapabilities
{
public Supports<SynchronizationCapability> Synchronization { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeSynchronization() => Synchronization.IsSupported;

/// <summary>
/// Capabilities specific to the `textDocument/completion`
/// </summary>
public Supports<CompletionCapability> Completion { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeCompletion() => Completion.IsSupported;

/// <summary>
/// Capabilities specific to the `textDocument/hover`
/// </summary>
public Supports<HoverCapability> Hover { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeHover() => Hover.IsSupported;

/// <summary>
/// Capabilities specific to the `textDocument/signatureHelp`
/// </summary>
public Supports<SignatureHelpCapability> SignatureHelp { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeSignatureHelp() => SignatureHelp.IsSupported;

/// <summary>
/// Capabilities specific to the `textDocument/references`
/// </summary>
public Supports<ReferencesCapability> References { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeReferences() => References.IsSupported;

/// <summary>
/// Capabilities specific to the `textDocument/documentHighlight`
/// </summary>
public Supports<DocumentHighlightCapability> DocumentHighlight { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeDocumentHighlight() => DocumentHighlight.IsSupported;

/// <summary>
/// Capabilities specific to the `textDocument/documentSymbol`
/// </summary>
public Supports<DocumentSymbolCapability> DocumentSymbol { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeDocumentSymbol() => DocumentSymbol.IsSupported;

/// <summary>
/// Capabilities specific to the `textDocument/formatting`
/// </summary>
public Supports<DocumentFormattingCapability> Formatting { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeFormatting() => Formatting.IsSupported;

/// <summary>
/// Capabilities specific to the `textDocument/rangeFormatting`
/// </summary>
public Supports<DocumentRangeFormattingCapability> RangeFormatting { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeRangeFormatting() => RangeFormatting.IsSupported;

/// <summary>
/// Capabilities specific to the `textDocument/onTypeFormatting`
/// </summary>
public Supports<DocumentOnTypeFormattingCapability> OnTypeFormatting { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeOnTypeFormatting() => OnTypeFormatting.IsSupported;

/// <summary>
/// Capabilities specific to the `textDocument/definition`
/// </summary>
public Supports<DefinitionCapability> Definition { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeDefinition() => Definition.IsSupported;

/// <summary>
/// Capabilities specific to the `textDocument/codeAction`
/// </summary>
public Supports<CodeActionCapability> CodeAction { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeCodeAction() => CodeAction.IsSupported;

/// <summary>
/// Capabilities specific to the `textDocument/codeLens`
/// </summary>
public Supports<CodeLensCapability> CodeLens { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeCodeLens() => CodeLens.IsSupported;

/// <summary>
/// Capabilities specific to the `textDocument/documentLink`
/// </summary>
public Supports<DocumentLinkCapability> DocumentLink { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeDocumentLink() => DocumentLink.IsSupported;

/// <summary>
/// Capabilities specific to the `textDocument/rename`
/// </summary>
public Supports<RenameCapability> Rename { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeRename() => Rename.IsSupported;
}
}
23 changes: 21 additions & 2 deletions src/Lsp/Capabilities/Client/WorkspaceClientCapabilites.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System.ComponentModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace OmniSharp.Extensions.LanguageServer.Capabilities.Client
Expand All @@ -10,28 +11,46 @@ public class WorkspaceClientCapabilites
/// The client supports applying batch edits
/// to the workspace.
/// </summary>
public bool ApplyEdit { get; set; }
public Supports<bool> ApplyEdit { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeApplyEdit() => ApplyEdit.IsSupported;

public Supports<WorkspaceEditCapability> WorkspaceEdit { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeWorkspaceEdit() => WorkspaceEdit.IsSupported;

/// <summary>
/// Capabilities specific to the `workspace/didChangeConfiguration` notification.
/// </summary>
public Supports<DidChangeConfigurationCapability> DidChangeConfiguration { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeDidChangeConfiguration() => DidChangeConfiguration.IsSupported;

/// <summary>
/// Capabilities specific to the `workspace/didChangeWatchedFiles` notification.
/// </summary>
public Supports<DidChangeWatchedFilesCapability> DidChangeWatchedFiles { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeDidChangeWatchedFiles() => DidChangeWatchedFiles.IsSupported;

/// <summary>
/// Capabilities specific to the `workspace/symbol` request.
/// </summary>
public Supports<WorkspaceSymbolCapability> Symbol { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeSymbol() => Symbol.IsSupported;

/// <summary>
/// Capabilities specific to the `workspace/executeCommand` request.
/// </summary>
public Supports<ExecuteCommandCapability> ExecuteCommand { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeExecuteCommand() => ExecuteCommand.IsSupported;
}
}
10 changes: 7 additions & 3 deletions src/Lsp/Converters/SupportsConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Reflection;
using Newtonsoft.Json;
using OmniSharp.Extensions.LanguageServer.Capabilities.Client;
Expand Down Expand Up @@ -35,7 +35,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
}
else
{
serializer.Serialize(writer, false);
serializer.Serialize(writer, null);
}
}

Expand All @@ -44,6 +44,10 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
var targetType = objectType.GetTypeInfo().GetGenericArguments()[0];
if (reader.TokenType == JsonToken.Boolean)
{
if (targetType == typeof(bool))
{
return new Supports<bool>(true, (bool)reader.Value);
}
return OfBooleanMethod
.MakeGenericMethod(targetType)
.Invoke(null, new [] { reader.Value });
Expand All @@ -60,4 +64,4 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist

public override bool CanConvert(Type objectType) => objectType.GetGenericTypeDefinition() == typeof(Supports<>);
}
}
}
17 changes: 14 additions & 3 deletions src/Lsp/HandlerCollection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -48,9 +48,11 @@ public IDisposable Add(params IJsonRpcHandler[] handlers)
}

var key = "default";
if (handler is IRegistration<TextDocumentRegistrationOptions> textDocumentRegistration)
if (handler is IRegistration<TextDocumentRegistrationOptions>)
{
var options = textDocumentRegistration.GetRegistrationOptions();
var options = GetTextDocumentRegistrationOptionsMethod
.MakeGenericMethod(registration)
.Invoke(handler, new object[] { handler }) as TextDocumentRegistrationOptions;
key = options.DocumentSelector;
}

Expand All @@ -76,6 +78,15 @@ public IDisposable Add(params IJsonRpcHandler[] handlers)
return new ImmutableDisposable(descriptors);
}

private static readonly MethodInfo GetTextDocumentRegistrationOptionsMethod = typeof(HandlerCollection).GetTypeInfo()
.GetMethod(nameof(GetTextDocumentRegistrationOptions), BindingFlags.Static | BindingFlags.NonPublic);

private static TextDocumentRegistrationOptions GetTextDocumentRegistrationOptions<T>(IRegistration<T> instance)
where T : TextDocumentRegistrationOptions
{
return instance.GetRegistrationOptions();
}

private Type UnwrapGenericType(Type genericType, Type type)
{
return type?.GetTypeInfo()
Expand Down
7 changes: 7 additions & 0 deletions src/Lsp/InitializeDelegate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System.Threading.Tasks;
using OmniSharp.Extensions.LanguageServer.Models;

namespace OmniSharp.Extensions.LanguageServer
{
public delegate Task InitializeDelegate(InitializeParams request);
}
2 changes: 0 additions & 2 deletions src/Lsp/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

namespace OmniSharp.Extensions.LanguageServer
{
public delegate Task InitializeDelegate(InitializeParams request);

public class LanguageServer : ILanguageServer, IInitializeHandler, IInitializedHandler, IDisposable, IAwaitableTermination
{
private readonly Connection _connection;
Expand Down
27 changes: 18 additions & 9 deletions src/Lsp/LspRequestRouter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
Expand All @@ -18,7 +19,7 @@ namespace OmniSharp.Extensions.LanguageServer
class LspRequestRouter : IRequestRouter
{
private readonly IHandlerCollection _collection;
private ITextDocumentSyncHandler _textDocumentSyncHandler;
private ITextDocumentSyncHandler[] _textDocumentSyncHandlers;
private readonly ConcurrentDictionary<string, CancellationTokenSource> _requests = new ConcurrentDictionary<string, CancellationTokenSource>();

public LspRequestRouter(IHandlerCollection collection)
Expand All @@ -43,22 +44,23 @@ private string GetId(object id)

private ILspHandlerDescriptor FindDescriptor(string method, JToken @params)
{
var descriptor = _collection.FirstOrDefault(x => x.Method == method);
var descriptor = _collection.FirstOrDefault(x => x.Method.Equals(method, StringComparison.OrdinalIgnoreCase));
if (descriptor is null) return null;

if (_textDocumentSyncHandler == null)
if (_textDocumentSyncHandlers == null)
{
_textDocumentSyncHandler = _collection
_textDocumentSyncHandlers = _collection
.Select(x => x.Handler is ITextDocumentSyncHandler r ? r : null)
.FirstOrDefault(x => x != null);
.Where(x => x != null)
.ToArray();
}

if (_textDocumentSyncHandler is null) return descriptor;

if (typeof(ITextDocumentIdentifierParams).GetTypeInfo().IsAssignableFrom(descriptor.Params))
{
var textDocumentIdentifierParams = @params.ToObject(descriptor.Params) as ITextDocumentIdentifierParams;
var attributes = _textDocumentSyncHandler.GetTextDocumentAttributes(textDocumentIdentifierParams.TextDocument.Uri);
var attributes = _textDocumentSyncHandlers
.Select(x => x.GetTextDocumentAttributes(textDocumentIdentifierParams.TextDocument.Uri))
.Where(x => x != null);

return GetHandler(method, attributes);
}
Expand All @@ -75,6 +77,13 @@ private ILspHandlerDescriptor FindDescriptor(string method, JToken @params)
return descriptor;
}

private ILspHandlerDescriptor GetHandler(string method, IEnumerable<TextDocumentAttributes> attributes)
{
return attributes
.Select(x => GetHandler(method, x))
.FirstOrDefault(x => x != null);
}

private ILspHandlerDescriptor GetHandler(string method, TextDocumentAttributes attributes)
{
foreach (var handler in _collection.Where(x => x.Method == method))
Expand Down
7 changes: 1 addition & 6 deletions src/Lsp/Models/CodeActionParams.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using Newtonsoft.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace OmniSharp.Extensions.LanguageServer.Models
{
public interface ITextDocumentIdentifierParams
{
TextDocumentIdentifier TextDocument { get; }
}

/// <summary>
/// Params for the CodeActionRequest
/// </summary>
Expand Down
18 changes: 17 additions & 1 deletion src/Lsp/Models/DocumentFilter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Minimatch;
using Newtonsoft.Json;
Expand Down Expand Up @@ -108,5 +109,20 @@ public bool IsMatch(TextDocumentAttributes attributes)

return false;
}

public static DocumentFilter ForPattern(string wildcard)
{
return new DocumentFilter() { Pattern = wildcard };
}

public static DocumentFilter ForLanguage(string language)
{
return new DocumentFilter() { Language = language };
}

public static DocumentFilter ForScheme(string scheme)
{
return new DocumentFilter() { Scheme = scheme };
}
}
}
Loading