Skip to content

Commit

Permalink
Merge pull request #42 from OmniSharp/serially-parallel
Browse files Browse the repository at this point in the history
Serially parallel
  • Loading branch information
david-driscoll authored Oct 30, 2017
2 parents 55725f4 + 8768555 commit 80e1b38
Show file tree
Hide file tree
Showing 73 changed files with 552 additions and 321 deletions.
102 changes: 0 additions & 102 deletions sample/SampleServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using OmniSharp.Extensions.LanguageServer;
using OmniSharp.Extensions.LanguageServer.Abstractions;
using OmniSharp.Extensions.LanguageServer.Capabilities.Client;
using OmniSharp.Extensions.LanguageServer.Capabilities.Server;
using OmniSharp.Extensions.LanguageServer.Models;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Document;

namespace SampleServer
{
Expand All @@ -34,100 +28,4 @@ static async Task MainAsync(string[] args)
await server.WasShutDown;
}
}

class TextDocumentHandler : ITextDocumentSyncHandler
{
private readonly ILanguageServer _router;

private readonly DocumentSelector _documentSelector = new DocumentSelector(
new DocumentFilter()
{
Pattern = "**/*.csproj",
Language = "xml"
}
);

private SynchronizationCapability _capability;

public TextDocumentHandler(ILanguageServer router)
{
_router = router;
}

public TextDocumentSyncOptions Options { get; } = new TextDocumentSyncOptions()
{
WillSaveWaitUntil = false,
WillSave = true,
Change = TextDocumentSyncKind.Full,
Save = new SaveOptions()
{
IncludeText = true
},
OpenClose = true
};

public Task Handle(DidChangeTextDocumentParams notification)
{
_router.LogMessage(new LogMessageParams()
{
Type = MessageType.Log,
Message = "Hello World!!!!"
});
return Task.CompletedTask;
}

TextDocumentChangeRegistrationOptions IRegistration<TextDocumentChangeRegistrationOptions>.GetRegistrationOptions()
{
return new TextDocumentChangeRegistrationOptions()
{
DocumentSelector = _documentSelector,
SyncKind = Options.Change
};
}

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

public async Task Handle(DidOpenTextDocumentParams notification)
{
_router.LogMessage(new LogMessageParams()
{
Type = MessageType.Log,
Message = "Hello World!!!!"
});
}

TextDocumentRegistrationOptions IRegistration<TextDocumentRegistrationOptions>.GetRegistrationOptions()
{
return new TextDocumentRegistrationOptions()
{
DocumentSelector = _documentSelector,
};
}

public Task Handle(DidCloseTextDocumentParams notification)
{
return Task.CompletedTask;
}

public Task Handle(DidSaveTextDocumentParams notification)
{
return Task.CompletedTask;
}

TextDocumentSaveRegistrationOptions IRegistration<TextDocumentSaveRegistrationOptions>.GetRegistrationOptions()
{
return new TextDocumentSaveRegistrationOptions()
{
DocumentSelector = _documentSelector,
IncludeText = Options.Save.IncludeText
};
}
public TextDocumentAttributes GetTextDocumentAttributes(Uri uri)
{
return new TextDocumentAttributes(uri, "csharp");
}
}
}
108 changes: 108 additions & 0 deletions sample/SampleServer/TextDocumentHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Threading.Tasks;
using OmniSharp.Extensions.LanguageServer;
using OmniSharp.Extensions.LanguageServer.Abstractions;
using OmniSharp.Extensions.LanguageServer.Capabilities.Client;
using OmniSharp.Extensions.LanguageServer.Capabilities.Server;
using OmniSharp.Extensions.LanguageServer.Models;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Document;

namespace SampleServer
{
class TextDocumentHandler : ITextDocumentSyncHandler
{
private readonly ILanguageServer _router;

private readonly DocumentSelector _documentSelector = new DocumentSelector(
new DocumentFilter()
{
Pattern = "**/*.csproj",
Language = "xml"
}
);

private SynchronizationCapability _capability;

public TextDocumentHandler(ILanguageServer router)
{
_router = router;
}

public TextDocumentSyncOptions Options { get; } = new TextDocumentSyncOptions()
{
WillSaveWaitUntil = false,
WillSave = true,
Change = TextDocumentSyncKind.Full,
Save = new SaveOptions()
{
IncludeText = true
},
OpenClose = true
};

public Task Handle(DidChangeTextDocumentParams notification)
{
_router.LogMessage(new LogMessageParams()
{
Type = MessageType.Log,
Message = "Hello World!!!!"
});
return Task.CompletedTask;
}

TextDocumentChangeRegistrationOptions IRegistration<TextDocumentChangeRegistrationOptions>.GetRegistrationOptions()
{
return new TextDocumentChangeRegistrationOptions()
{
DocumentSelector = _documentSelector,
SyncKind = Options.Change
};
}

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

public async Task Handle(DidOpenTextDocumentParams notification)
{
_router.LogMessage(new LogMessageParams()
{
Type = MessageType.Log,
Message = "Hello World!!!!"
});
}

TextDocumentRegistrationOptions IRegistration<TextDocumentRegistrationOptions>.GetRegistrationOptions()
{
return new TextDocumentRegistrationOptions()
{
DocumentSelector = _documentSelector,
};
}

public Task Handle(DidCloseTextDocumentParams notification)
{
return Task.CompletedTask;
}

public Task Handle(DidSaveTextDocumentParams notification)
{
return Task.CompletedTask;
}

TextDocumentSaveRegistrationOptions IRegistration<TextDocumentSaveRegistrationOptions>.GetRegistrationOptions()
{
return new TextDocumentSaveRegistrationOptions()
{
DocumentSelector = _documentSelector,
IncludeText = Options.Save.IncludeText
};
}
public TextDocumentAttributes GetTextDocumentAttributes(Uri uri)
{
return new TextDocumentAttributes(uri, "csharp");
}
}
}
11 changes: 11 additions & 0 deletions src/JsonRpc/Events.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.Extensions.Logging;

namespace OmniSharp.Extensions.JsonRpc
{
public static class Events
{
public static EventId UnhandledException = new EventId(1337_100);
public static EventId UnhandledRequest = new EventId(1337_101);
public static EventId UnhandledNotification = new EventId(1337_102);
}
}
6 changes: 3 additions & 3 deletions src/JsonRpc/HandlerCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

namespace OmniSharp.Extensions.JsonRpc
{
class HandlerCollection : IEnumerable<IHandlerInstance>
class HandlerCollection : IEnumerable<IHandlerDescriptor>
{
internal readonly List<HandlerInstance> _handlers = new List<HandlerInstance>();

internal class HandlerInstance : IHandlerInstance, IDisposable
internal class HandlerInstance : IHandlerDescriptor, IDisposable
{
private readonly Action _disposeAction;

Expand All @@ -34,7 +34,7 @@ public void Dispose()
}
}

public IEnumerator<IHandlerInstance> GetEnumerator()
public IEnumerator<IHandlerDescriptor> GetEnumerator()
{
return _handlers.GetEnumerator();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace OmniSharp.Extensions.JsonRpc
{
public interface IHandlerInstance
public interface IHandlerDescriptor
{
string Method { get; }
IJsonRpcHandler Handler { get; }
Type HandlerType { get; }
Type Params { get; }
}
}
}
4 changes: 2 additions & 2 deletions src/JsonRpc/IRequestProcessIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace OmniSharp.Extensions.JsonRpc
{
public interface IRequestProcessIdentifier
{
RequestProcessType Identify(Renor renor);
RequestProcessType Identify(IHandlerDescriptor descriptor);
}
}
}
5 changes: 5 additions & 0 deletions src/JsonRpc/IRequestRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ namespace OmniSharp.Extensions.JsonRpc
{
public interface IRequestRouter
{
IHandlerDescriptor GetDescriptor(Notification notification);
IHandlerDescriptor GetDescriptor(Request request);

Task RouteNotification(Notification notification);
Task RouteNotification(IHandlerDescriptor descriptor, Notification notification);
Task<ErrorResponse> RouteRequest(Request request);
Task<ErrorResponse> RouteRequest(IHandlerDescriptor descriptor, Request request);
}
}
12 changes: 9 additions & 3 deletions src/JsonRpc/InputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,20 @@ private void HandleRequest(string request)
return;
}

foreach (var (type, item) in requests.Select(x => (type: _requestProcessIdentifier.Identify(x), item: x)))
foreach (var item in requests)
{
if (item.IsRequest)
{
var descriptor = _requestRouter.GetDescriptor(item.Request);
if (descriptor is null) continue;
var type = _requestProcessIdentifier.Identify(descriptor);
_scheduler.Add(
type,
item.Request.Method,
async () => {
try
{
var result = await _requestRouter.RouteRequest(item.Request);
var result = await _requestRouter.RouteRequest(descriptor, item.Request);
_outputHandler.Send(result.Value);
}
catch (Exception e)
Expand All @@ -187,13 +190,16 @@ private void HandleRequest(string request)
}
else if (item.IsNotification)
{
var descriptor = _requestRouter.GetDescriptor(item.Notification);
if (descriptor is null) continue;
var type = _requestProcessIdentifier.Identify(descriptor);
_scheduler.Add(
type,
item.Notification.Method,
async () => {
try
{
await _requestRouter.RouteNotification(item.Notification);
await _requestRouter.RouteNotification(descriptor, item.Notification);
}
catch (Exception e)
{
Expand Down
4 changes: 2 additions & 2 deletions src/JsonRpc/MethodAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace OmniSharp.Extensions.JsonRpc
{
Expand All @@ -12,4 +12,4 @@ public MethodAttribute(string method)
Method = method;
}
}
}
}
7 changes: 7 additions & 0 deletions src/JsonRpc/ParallelAttrbute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OmniSharp.Extensions.JsonRpc
{
public sealed class ParallelAttribute : ProcessAttribute
{
public ParallelAttribute() : base(RequestProcessType.Parallel) { }
}
}
4 changes: 2 additions & 2 deletions src/JsonRpc/ParallelRequestProcessIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace OmniSharp.Extensions.JsonRpc
{
public class ParallelRequestProcessIdentifier : IRequestProcessIdentifier
{
public RequestProcessType Identify(Renor renor)
public RequestProcessType Identify(IHandlerDescriptor descriptor)
{
return RequestProcessType.Parallel;
}
}
}
}
15 changes: 15 additions & 0 deletions src/JsonRpc/ProcessAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace OmniSharp.Extensions.JsonRpc
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
public class ProcessAttribute : Attribute
{
public ProcessAttribute(RequestProcessType type)
{
Type = type;
}

public RequestProcessType Type { get; }
}
}
Loading

0 comments on commit 80e1b38

Please sign in to comment.