forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 1
/
connector.ts
106 lines (94 loc) · 2.92 KB
/
connector.ts
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { DataConnector } from '@jupyterlab/coreutils';
import { KernelConnector } from './kernelconnector';
import { ContextConnector } from './contextconnector';
import { CompletionHandler } from './handler';
/**
* A context+kernel connector for completion handlers.
*/
export class CompletionConnector extends DataConnector<
CompletionHandler.IReply,
void,
CompletionHandler.IRequest
> {
/**
* Create a new connector for completion requests.
*
* @param options - The instatiation options for the connector.
*/
constructor(options: CompletionConnector.IOptions) {
super();
this._kernel = new KernelConnector(options);
this._context = new ContextConnector(options);
}
/**
* Fetch completion requests.
*
* @param request - The completion request text and details.
*/
fetch(
request: CompletionHandler.IRequest
): Promise<CompletionHandler.IReply> {
return Promise.all([
this._kernel.fetch(request),
this._context.fetch(request)
]).then(([kernel, context]) => Private.mergeReplies(kernel, context));
}
private _kernel: KernelConnector;
private _context: ContextConnector;
}
/**
* A namespace for completion connector statics.
*/
export namespace CompletionConnector {
/**
* The instantiation options for cell completion handlers.
*/
export type IOptions = KernelConnector.IOptions & ContextConnector.IOptions;
}
/**
* A namespace for private functionality.
*/
namespace Private {
/**
* Merge results from kernel and context completions.
*
* @param kernel - The kernel reply being merged.
*
* @param context - The context reply being merged.
*
* @returns A reply with a superset of kernel and context matches.
*
* #### Notes
* The kernel and context matches are merged with a preference for kernel
* results. Both lists are known to contain unique, non-repeating items;
* so this function returns a non-repeating superset by filtering out
* duplicates from the context list that appear in the kernel list.
*/
export function mergeReplies(
kernel: CompletionHandler.IReply,
context: CompletionHandler.IReply
): CompletionHandler.IReply {
// If one is empty, return the other.
if (kernel.matches.length === 0) {
return context;
} else if (context.matches.length === 0) {
return kernel;
}
// Populate the result with a copy of the kernel matches.
const matches = kernel.matches.slice();
// Cache all the kernel matches in a memo.
const memo = matches.reduce((acc, val) => {
acc[val] = null;
return acc;
}, {} as { [key: string]: string });
// Add each context match that is not in the memo to the result.
context.matches.forEach(match => {
if (!(match in memo)) {
matches.push(match);
}
});
return { ...kernel, matches };
}
}