@jupyterlab/lsp 4.0.0-alpha.19 → 4.0.0-alpha.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.ts ADDED
@@ -0,0 +1,106 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { ReadonlyJSONObject, ReadonlyJSONValue } from '@lumino/coreutils';
5
+ import mergeWith from 'lodash.mergewith';
6
+
7
+ /**
8
+ * Helper to wait for timeout.
9
+ *
10
+ * @param timeout - time out in ms
11
+ */
12
+ export async function sleep(timeout: number): Promise<void> {
13
+ return new Promise<void>(resolve => {
14
+ setTimeout(() => {
15
+ resolve();
16
+ }, timeout);
17
+ });
18
+ }
19
+
20
+ /**
21
+ * Wait for an event by pooling the `isReady` function.
22
+ */
23
+ export function untilReady(
24
+ isReady: CallableFunction,
25
+ maxRetrials: number = 35,
26
+ interval = 50,
27
+ intervalModifier = (i: number) => i
28
+ ): Promise<CallableFunction> {
29
+ return (async () => {
30
+ let i = 0;
31
+ while (isReady() !== true) {
32
+ i += 1;
33
+ if (maxRetrials !== -1 && i > maxRetrials) {
34
+ throw Error('Too many retrials');
35
+ }
36
+ interval = intervalModifier(interval);
37
+ await sleep(interval);
38
+ }
39
+ return isReady;
40
+ })();
41
+ }
42
+
43
+ /**
44
+ * Convert dotted path into dictionary.
45
+ */
46
+ export function expandDottedPaths(obj: ReadonlyJSONObject): ReadonlyJSONObject {
47
+ const settings: any = [];
48
+ for (let key in obj) {
49
+ const parsed = expandPath(key.split('.'), obj[key]);
50
+ settings.push(parsed);
51
+ }
52
+ return mergeWith({}, ...settings);
53
+ }
54
+
55
+ /**
56
+ * The docs for many language servers show settings in the
57
+ * VSCode format, e.g.: "pyls.plugins.pyflakes.enabled"
58
+ *
59
+ * VSCode converts that dot notation to JSON behind the scenes,
60
+ * as the language servers themselves don't accept that syntax.
61
+ */
62
+ export const expandPath = (
63
+ path: string[],
64
+ value: ReadonlyJSONValue
65
+ ): ReadonlyJSONObject => {
66
+ const obj: any = Object.create(null);
67
+
68
+ let curr = obj;
69
+ path.forEach((prop: string, i: any) => {
70
+ curr[prop] = Object.create(null);
71
+
72
+ if (i === path.length - 1) {
73
+ curr[prop] = value;
74
+ } else {
75
+ curr = curr[prop];
76
+ }
77
+ });
78
+
79
+ return obj;
80
+ };
81
+
82
+ /**
83
+ * An extended map which will create value for key on the fly.
84
+ */
85
+ export class DefaultMap<K, V> extends Map<K, V> {
86
+ constructor(
87
+ private defaultFactory: (...args: any[]) => V,
88
+ entries?: ReadonlyArray<readonly [K, V]> | null
89
+ ) {
90
+ super(entries);
91
+ }
92
+
93
+ get(k: K): V {
94
+ return this.getOrCreate(k);
95
+ }
96
+
97
+ getOrCreate(k: K, ...args: any[]): V {
98
+ if (this.has(k)) {
99
+ return super.get(k)!;
100
+ } else {
101
+ let v = this.defaultFactory(k, ...args);
102
+ this.set(k, v);
103
+ return v;
104
+ }
105
+ }
106
+ }