forked from dotnet/interactive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompleteRequestHandler.cs
71 lines (61 loc) · 2.9 KB
/
CompleteRequestHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Linq;
using System.Reactive.Concurrency;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Jupyter.Protocol;
using Microsoft.DotNet.Interactive.Utility;
namespace Microsoft.DotNet.Interactive.Jupyter
{
public class CompleteRequestHandler : RequestHandlerBase<CompleteRequest>
{
public CompleteRequestHandler(Kernel kernel, IScheduler scheduler = null)
: base(kernel, scheduler ?? CurrentThreadScheduler.Instance)
{
}
public async Task Handle(JupyterRequestContext context)
{
var completeRequest = GetJupyterRequest(context);
var targetKernelName = context.GetLanguage();
var position = SourceUtilities.GetPositionFromCursorOffset(completeRequest.Code, completeRequest.CursorPosition);
var command = new RequestCompletions(completeRequest.Code, position, targetKernelName);
await SendAsync(context, command);
}
protected override void OnKernelEventReceived(
KernelEvent @event,
JupyterRequestContext context)
{
switch (@event)
{
case CompletionsProduced completionRequestCompleted:
OnCompletionRequestCompleted(
completionRequestCompleted,
context.JupyterMessageSender);
break;
}
}
private static void OnCompletionRequestCompleted(CompletionsProduced completionsProduced, IJupyterMessageSender jupyterMessageSender)
{
var startPosition = 0;
var endPosition = 0;
if (completionsProduced.Command is RequestCompletions command)
{
if (completionsProduced.LinePositionSpan is not null)
{
startPosition = SourceUtilities.GetCursorOffsetFromPosition(command.Code, completionsProduced.LinePositionSpan.Start);
endPosition = SourceUtilities.GetCursorOffsetFromPosition(command.Code, completionsProduced.LinePositionSpan.End);
}
else
{
var cursorOffset = SourceUtilities.GetCursorOffsetFromPosition(command.Code, command.LinePosition);
startPosition = SourceUtilities.ComputeReplacementStartPosition(command.Code, cursorOffset);
endPosition = cursorOffset;
}
}
var reply = new CompleteReply(startPosition, endPosition, matches: completionsProduced.Completions.Select(e => e.InsertText ?? e.DisplayText).ToList());
jupyterMessageSender.Send(reply);
}
}
}