Skip to content

Fix language server not working with the latest beta #153

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

Merged
merged 2 commits into from
Jul 18, 2019
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
3 changes: 3 additions & 0 deletions sample/SampleServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using OmniSharp.Extensions.LanguageServer;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using OmniSharp.Extensions.LanguageServer.Server;

namespace SampleServer
Expand All @@ -29,6 +30,8 @@ static async Task MainAsync(string[] args)
.AddDefaultLoggingProvider()
.WithMinimumLogLevel(LogLevel.Trace)
.WithHandler<TextDocumentHandler>()
.WithHandler<DidChangeWatchedFilesHandler>()
.WithHandler<FoldingRangeHandler>()
);

await server.WaitForExit;
Expand Down
67 changes: 55 additions & 12 deletions sample/SampleServer/TextDocumentHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class TextDocumentHandler : ITextDocumentSyncHandler
private readonly OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer _router;

private readonly DocumentSelector _documentSelector = new DocumentSelector(
new DocumentFilter()
{
new DocumentFilter() {
Pattern = "**/*.cs"
}
);
Expand All @@ -35,8 +34,7 @@ public TextDocumentHandler(OmniSharp.Extensions.LanguageServer.Protocol.Server.I

public Task<Unit> Handle(DidChangeTextDocumentParams notification, CancellationToken token)
{
_router.Window.LogMessage(new LogMessageParams()
{
_router.Window.LogMessage(new LogMessageParams() {
Type = MessageType.Log,
Message = "Hello World!!!!"
});
Expand All @@ -45,8 +43,7 @@ public Task<Unit> Handle(DidChangeTextDocumentParams notification, CancellationT

TextDocumentChangeRegistrationOptions IRegistration<TextDocumentChangeRegistrationOptions>.GetRegistrationOptions()
{
return new TextDocumentChangeRegistrationOptions()
{
return new TextDocumentChangeRegistrationOptions() {
DocumentSelector = _documentSelector,
SyncKind = Change
};
Expand All @@ -60,8 +57,7 @@ public void SetCapability(SynchronizationCapability capability)
public async Task<Unit> Handle(DidOpenTextDocumentParams notification, CancellationToken token)
{
await Task.Yield();
_router.Window.LogMessage(new LogMessageParams()
{
_router.Window.LogMessage(new LogMessageParams() {
Type = MessageType.Log,
Message = "Hello World!!!!"
});
Expand All @@ -70,8 +66,7 @@ public async Task<Unit> Handle(DidOpenTextDocumentParams notification, Cancellat

TextDocumentRegistrationOptions IRegistration<TextDocumentRegistrationOptions>.GetRegistrationOptions()
{
return new TextDocumentRegistrationOptions()
{
return new TextDocumentRegistrationOptions() {
DocumentSelector = _documentSelector,
};
}
Expand All @@ -88,8 +83,7 @@ public Task<Unit> Handle(DidSaveTextDocumentParams notification, CancellationTok

TextDocumentSaveRegistrationOptions IRegistration<TextDocumentSaveRegistrationOptions>.GetRegistrationOptions()
{
return new TextDocumentSaveRegistrationOptions()
{
return new TextDocumentSaveRegistrationOptions() {
DocumentSelector = _documentSelector,
IncludeText = true
};
Expand All @@ -99,4 +93,53 @@ public TextDocumentAttributes GetTextDocumentAttributes(Uri uri)
return new TextDocumentAttributes(uri, "csharp");
}
}

class FoldingRangeHandler : IFoldingRangeHandler
{
private FoldingRangeCapability _capability;

public TextDocumentRegistrationOptions GetRegistrationOptions()
{
return new TextDocumentRegistrationOptions() {
DocumentSelector = DocumentSelector.ForLanguage("csharp")
};
}

public Task<Container<FoldingRange>> Handle(FoldingRangeRequestParam request,
CancellationToken cancellationToken)
{
return Task.FromResult(new Container<FoldingRange>(new FoldingRange() {
StartLine = 10,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the folding range in the gif attached.

EndLine = 20,
Kind = FoldingRangeKind.Region,
EndCharacter = 0,
StartCharacter = 0
}));
}

public void SetCapability(FoldingRangeCapability capability)
{
_capability = capability;
}
}

class DidChangeWatchedFilesHandler : IDidChangeWatchedFilesHandler
{
private DidChangeWatchedFilesCapability _capability;

public object GetRegistrationOptions()
{
return new object();
}

public Task<Unit> Handle(DidChangeWatchedFilesParams request, CancellationToken cancellationToken)
{
return Unit.Task;
}

public void SetCapability(DidChangeWatchedFilesCapability capability)
{
_capability = capability;
}
}
}
3 changes: 3 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Lsp.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100391db875e68eb4bfef49ce14313b9e13f2cd3cc89eb273bbe6c11a55044c7d4f566cf092e1c77ef9e7c75b1496ae7f95d925938f5a01793dd8d9f99ae0a7595779b71b971287d7d7b5960d052078d14f5ce1a85ea5c9fb2f59ac735ff7bc215cab469b7c3486006860bad6f4c3b5204ea2f28dd4e1d05e2cca462cfd593b9f9f</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project>
6 changes: 3 additions & 3 deletions src/JsonRpc/JsonRpc.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -13,7 +13,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
<PackageReference Include="Newtonsoft.Json" />
<Compile Include="../../submodules/MediatR/src/MediatR/**/*.cs" Exclude="**/AssemblyInfo.cs" />
<Compile Include="../../submodules/MediatR.Extensions.Microsoft.DependencyInjection/src/MediatR.Extensions.Microsoft.DependencyInjection/**/*.cs" Exclude="**/AssemblyInfo.cs" />
<Compile Include="../../submodules/MediatR/src/MediatR/**/*.cs" Exclude="**/AssemblyInfo.cs" Visible="false" />
<Compile Include="../../submodules/MediatR.Extensions.Microsoft.DependencyInjection/src/MediatR.Extensions.Microsoft.DependencyInjection/**/*.cs" Exclude="**/AssemblyInfo.cs" Visible="false" />
</ItemGroup>
</Project>
13 changes: 12 additions & 1 deletion src/Protocol/Document/ITextDocumentIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,15 @@ public interface ITextDocumentIdentifier
/// <returns></returns>
TextDocumentAttributes GetTextDocumentAttributes(Uri uri);
}
}

public abstract class TextDocumentIdentifierBase : ITextDocumentIdentifier
{
public TextDocumentAttributes GetTextDocumentAttributes(Uri uri)
{
var (languageId, schema) = GetAttributes(uri);
return new TextDocumentAttributes(uri, languageId, schema);
}

protected abstract (string languageId, string schema) GetAttributes(Uri uri);
}
}
6 changes: 5 additions & 1 deletion src/Protocol/ILanguageServerRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
// ReSharper disable once CheckNamespace
namespace OmniSharp.Extensions.LanguageServer.Protocol.Server
{
public interface ILanguageServerRegistry : IJsonRpcHandlerRegistry { }
public interface ILanguageServerRegistry : IJsonRpcHandlerRegistry
{
IDisposable AddTextDocumentIdentifier(params ITextDocumentIdentifier[] handlers);
IDisposable AddTextDocumentIdentifier<T>() where T : ITextDocumentIdentifier;
}
}
3 changes: 1 addition & 2 deletions src/Server/Abstractions/IHandlerCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace OmniSharp.Extensions.LanguageServer.Server.Abstractions
{
public interface IHandlerCollection : IEnumerable<ILspHandlerDescriptor>
internal interface IHandlerCollection : IEnumerable<ILspHandlerDescriptor>
{
LspHandlerDescriptorDisposable Add(params IJsonRpcHandler[] handlers);
LspHandlerDescriptorDisposable Add(params Type[] handlerTypes);
Expand All @@ -15,6 +15,5 @@ public interface IHandlerCollection : IEnumerable<ILspHandlerDescriptor>
LspHandlerDescriptorDisposable Add(string method, Type handlerType);
bool ContainsHandler(Type type);
bool ContainsHandler(TypeInfo typeInfo);
IEnumerable<ITextDocumentIdentifier> TextDocumentIdentifiers();
}
}
81 changes: 42 additions & 39 deletions src/Server/HandlerCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,23 @@ class HandlerCollection : IHandlerCollection
.GetTypeInfo()
.GetMethod(nameof(GetRegistration), BindingFlags.NonPublic | BindingFlags.Static);
private readonly ISupportedCapabilities _supportedCapabilities;
private readonly HashSet<HandlerDescriptor> _textDocumentIdentifiers = new HashSet<HandlerDescriptor>();
private readonly TextDocumentIdentifiers _textDocumentIdentifiers;
internal readonly HashSet<HandlerDescriptor> _handlers = new HashSet<HandlerDescriptor>();
private IServiceProvider _serviceProvider;


public HandlerCollection(ISupportedCapabilities supportedCapabilities)
public HandlerCollection(ISupportedCapabilities supportedCapabilities,
TextDocumentIdentifiers textDocumentIdentifiers)
{
_supportedCapabilities = supportedCapabilities;
_textDocumentIdentifiers = textDocumentIdentifiers;
}

public void SetServiceProvider(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

public IEnumerable<ITextDocumentIdentifier> TextDocumentIdentifiers()
{
return _textDocumentIdentifiers
.Select(descriptor => descriptor.Handler)
.OfType<ITextDocumentIdentifier>();
}

public IEnumerator<ILspHandlerDescriptor> GetEnumerator()
{
return _handlers.GetEnumerator();
Expand All @@ -53,74 +48,85 @@ public LspHandlerDescriptorDisposable Add(string method, IJsonRpcHandler handler
{
var descriptor = GetDescriptor(method, handler.GetType(), handler);
_handlers.Add(descriptor);
return new LspHandlerDescriptorDisposable(descriptor);
var cd = new CompositeDisposable();
if (descriptor.Handler is ITextDocumentIdentifier textDocumentIdentifier)
{
cd.Add(_textDocumentIdentifiers.Add(textDocumentIdentifier));
}
return new LspHandlerDescriptorDisposable(new[] { descriptor }, cd);
}

public LspHandlerDescriptorDisposable Add(string method, Func<IServiceProvider, IJsonRpcHandler> handlerFunc)
{
var handler = handlerFunc(_serviceProvider);
var descriptor = GetDescriptor(method, handler.GetType(), handler);
_handlers.Add(descriptor);
return new LspHandlerDescriptorDisposable(descriptor);
var cd = new CompositeDisposable();
if (descriptor.Handler is ITextDocumentIdentifier textDocumentIdentifier)
{
_textDocumentIdentifiers.Add(textDocumentIdentifier);
}
return new LspHandlerDescriptorDisposable(new[] { descriptor }, cd);
}

public LspHandlerDescriptorDisposable Add(string method, Type handlerType)
{
var descriptor = GetDescriptor(method, handlerType, _serviceProvider);
_handlers.Add(descriptor);
return new LspHandlerDescriptorDisposable(descriptor);
var cd = new CompositeDisposable();
if (descriptor.Handler is ITextDocumentIdentifier textDocumentIdentifier)
{
cd.Add(_textDocumentIdentifiers.Add(textDocumentIdentifier));
}
return new LspHandlerDescriptorDisposable(new [] {descriptor }, cd);
}

public LspHandlerDescriptorDisposable Add(params Type[] handlerTypes)
{
var descriptors = new HashSet<HandlerDescriptor>();
var cd = new CompositeDisposable();
foreach (var handlerType in handlerTypes)
{
foreach (var (method, implementedInterface) in handlerType.GetTypeInfo()
.ImplementedInterfaces
.Select(x => (method: LspHelper.GetMethodName(x), implementedInterface: x))
.Where(x => !string.IsNullOrWhiteSpace(x.method)))
{
descriptors.Add(GetDescriptor(method, implementedInterface, _serviceProvider));
}
}

foreach (var descriptor in descriptors)
{
_handlers.Add(descriptor);
if (typeof(ITextDocumentIdentifier).IsAssignableFrom(descriptor.ImplementationType))
{
_textDocumentIdentifiers.Add(descriptor);
var descriptor = GetDescriptor(method, implementedInterface, _serviceProvider);
descriptors.Add(descriptor);
_handlers.Add(descriptor);
if (descriptor.Handler is ITextDocumentIdentifier textDocumentIdentifier)
{
cd.Add(_textDocumentIdentifiers.Add(textDocumentIdentifier));
}
}
}

return new LspHandlerDescriptorDisposable(descriptors);
return new LspHandlerDescriptorDisposable(descriptors, cd);
}

public LspHandlerDescriptorDisposable Add(params IJsonRpcHandler[] handlers)
{
var descriptors = new HashSet<HandlerDescriptor>();
var cd = new CompositeDisposable();
foreach (var handler in handlers)
{
foreach (var (method, implementedInterface) in handler.GetType().GetTypeInfo()
.ImplementedInterfaces
.Select(x => (method: LspHelper.GetMethodName(x), implementedInterface: x))
.Where(x => !string.IsNullOrWhiteSpace(x.method)))
{
descriptors.Add(GetDescriptor(method, implementedInterface, handler));
}
}

foreach (var descriptor in descriptors)
{
_handlers.Add(descriptor);
if (descriptor.Handler is ITextDocumentIdentifier)
{
_textDocumentIdentifiers.Add(descriptor);
var descriptor = GetDescriptor(method, implementedInterface, handler);
descriptors.Add(descriptor);
_handlers.Add(descriptor);
if (descriptor.Handler is ITextDocumentIdentifier textDocumentIdentifier)
{
cd.Add(_textDocumentIdentifiers.Add(textDocumentIdentifier));
}
}
}

return new LspHandlerDescriptorDisposable(descriptors);
return new LspHandlerDescriptorDisposable(descriptors, cd);
}

private HandlerDescriptor GetDescriptor(string method, Type handlerType, IServiceProvider serviceProvider)
Expand Down Expand Up @@ -153,8 +159,7 @@ private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRp

if (_supportedCapabilities.AllowsDynamicRegistration(capabilityType))
{
registration = new Registration()
{
registration = new Registration() {
Id = Guid.NewGuid().ToString(),
Method = method,
RegisterOptions = registrationOptions
Expand Down Expand Up @@ -190,10 +195,8 @@ private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRp
registrationType,
registration,
capabilityType,
() =>
{
() => {
_handlers.RemoveWhere(d => d.Handler == handler);
_textDocumentIdentifiers.RemoveWhere(d => d.Handler == handler);
});

return descriptor;
Expand Down
2 changes: 1 addition & 1 deletion src/Server/HandlerCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace OmniSharp.Extensions.LanguageServer.Server
{
public static class HandlerCollectionExtensions
static class HandlerCollectionExtensions
{
public static LspHandlerDescriptorDisposable Add(this IHandlerCollection collection, IEnumerable<IJsonRpcHandler> handlers)
{
Expand Down
Loading