@difizen/libro-language-client 0.1.18

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.
Files changed (298) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +0 -0
  3. package/es/common/api.d.ts +38 -0
  4. package/es/common/api.d.ts.map +1 -0
  5. package/es/common/api.js +9 -0
  6. package/es/common/callHierarchy.d.ts +39 -0
  7. package/es/common/callHierarchy.d.ts.map +1 -0
  8. package/es/common/callHierarchy.js +139 -0
  9. package/es/common/client.d.ts +482 -0
  10. package/es/common/client.d.ts.map +1 -0
  11. package/es/common/client.js +2731 -0
  12. package/es/common/codeAction.d.ts +22 -0
  13. package/es/common/codeAction.d.ts.map +1 -0
  14. package/es/common/codeAction.js +149 -0
  15. package/es/common/codeConverter.d.ts +81 -0
  16. package/es/common/codeConverter.d.ts.map +1 -0
  17. package/es/common/codeConverter.js +1040 -0
  18. package/es/common/codeLens.d.ts +26 -0
  19. package/es/common/codeLens.d.ts.map +1 -0
  20. package/es/common/codeLens.js +125 -0
  21. package/es/common/colorProvider.d.ts +27 -0
  22. package/es/common/colorProvider.d.ts.map +1 -0
  23. package/es/common/colorProvider.js +104 -0
  24. package/es/common/completion.d.ts +22 -0
  25. package/es/common/completion.d.ts.map +1 -0
  26. package/es/common/completion.js +130 -0
  27. package/es/common/configuration.d.ts +71 -0
  28. package/es/common/configuration.d.ts.map +1 -0
  29. package/es/common/configuration.js +292 -0
  30. package/es/common/declaration.d.ts +18 -0
  31. package/es/common/declaration.d.ts.map +1 -0
  32. package/es/common/declaration.js +88 -0
  33. package/es/common/definition.d.ts +18 -0
  34. package/es/common/definition.d.ts.map +1 -0
  35. package/es/common/definition.js +80 -0
  36. package/es/common/diagnostic.d.ts +125 -0
  37. package/es/common/diagnostic.d.ts.map +1 -0
  38. package/es/common/diagnostic.js +1442 -0
  39. package/es/common/documentHighlight.d.ts +17 -0
  40. package/es/common/documentHighlight.d.ts.map +1 -0
  41. package/es/common/documentHighlight.js +73 -0
  42. package/es/common/documentLink.d.ts +21 -0
  43. package/es/common/documentLink.d.ts.map +1 -0
  44. package/es/common/documentLink.js +90 -0
  45. package/es/common/documentSymbol.d.ts +20 -0
  46. package/es/common/documentSymbol.d.ts.map +1 -0
  47. package/es/common/documentSymbol.js +134 -0
  48. package/es/common/executeCommand.d.ts +22 -0
  49. package/es/common/executeCommand.d.ts.map +1 -0
  50. package/es/common/executeCommand.js +117 -0
  51. package/es/common/features.d.ts +421 -0
  52. package/es/common/features.d.ts.map +1 -0
  53. package/es/common/features.js +576 -0
  54. package/es/common/fileOperations.d.ts +118 -0
  55. package/es/common/fileOperations.d.ts.map +1 -0
  56. package/es/common/fileOperations.js +705 -0
  57. package/es/common/fileSystemWatcher.d.ts +19 -0
  58. package/es/common/fileSystemWatcher.d.ts.map +1 -0
  59. package/es/common/fileSystemWatcher.js +173 -0
  60. package/es/common/foldingRange.d.ts +22 -0
  61. package/es/common/foldingRange.d.ts.map +1 -0
  62. package/es/common/foldingRange.js +127 -0
  63. package/es/common/formatting.d.ts +41 -0
  64. package/es/common/formatting.d.ts.map +1 -0
  65. package/es/common/formatting.js +233 -0
  66. package/es/common/hover.d.ts +18 -0
  67. package/es/common/hover.d.ts.map +1 -0
  68. package/es/common/hover.js +80 -0
  69. package/es/common/implementation.d.ts +18 -0
  70. package/es/common/implementation.d.ts.map +1 -0
  71. package/es/common/implementation.js +88 -0
  72. package/es/common/inlayHint.d.ts +23 -0
  73. package/es/common/inlayHint.d.ts.map +1 -0
  74. package/es/common/inlayHint.js +187 -0
  75. package/es/common/inlineCompletion.d.ts +20 -0
  76. package/es/common/inlineCompletion.d.ts.map +1 -0
  77. package/es/common/inlineCompletion.js +74 -0
  78. package/es/common/inlineValue.d.ts +21 -0
  79. package/es/common/inlineValue.d.ts.map +1 -0
  80. package/es/common/inlineValue.js +124 -0
  81. package/es/common/linkedEditingRange.d.ts +23 -0
  82. package/es/common/linkedEditingRange.d.ts.map +1 -0
  83. package/es/common/linkedEditingRange.js +94 -0
  84. package/es/common/notebook.d.ts +97 -0
  85. package/es/common/notebook.d.ts.map +1 -0
  86. package/es/common/notebook.js +1444 -0
  87. package/es/common/progress.d.ts +12 -0
  88. package/es/common/progress.d.ts.map +1 -0
  89. package/es/common/progress.js +75 -0
  90. package/es/common/progressPart.d.ts +25 -0
  91. package/es/common/progressPart.d.ts.map +1 -0
  92. package/es/common/progressPart.js +147 -0
  93. package/es/common/protocolCallHierarchyItem.d.ts +9 -0
  94. package/es/common/protocolCallHierarchyItem.d.ts.map +1 -0
  95. package/es/common/protocolCallHierarchyItem.js +34 -0
  96. package/es/common/protocolCodeAction.d.ts +7 -0
  97. package/es/common/protocolCodeAction.d.ts.map +1 -0
  98. package/es/common/protocolCodeAction.js +32 -0
  99. package/es/common/protocolCodeLens.d.ts +7 -0
  100. package/es/common/protocolCodeLens.d.ts.map +1 -0
  101. package/es/common/protocolCodeLens.js +29 -0
  102. package/es/common/protocolCompletionItem.d.ts +13 -0
  103. package/es/common/protocolCompletionItem.d.ts.map +1 -0
  104. package/es/common/protocolCompletionItem.js +29 -0
  105. package/es/common/protocolConverter.d.ts +174 -0
  106. package/es/common/protocolConverter.d.ts.map +1 -0
  107. package/es/common/protocolConverter.js +1982 -0
  108. package/es/common/protocolDiagnostic.d.ts +20 -0
  109. package/es/common/protocolDiagnostic.d.ts.map +1 -0
  110. package/es/common/protocolDiagnostic.js +46 -0
  111. package/es/common/protocolDocumentLink.d.ts +8 -0
  112. package/es/common/protocolDocumentLink.d.ts.map +1 -0
  113. package/es/common/protocolDocumentLink.js +29 -0
  114. package/es/common/protocolInlayHint.d.ts +8 -0
  115. package/es/common/protocolInlayHint.d.ts.map +1 -0
  116. package/es/common/protocolInlayHint.js +29 -0
  117. package/es/common/protocolTypeHierarchyItem.d.ts +9 -0
  118. package/es/common/protocolTypeHierarchyItem.d.ts.map +1 -0
  119. package/es/common/protocolTypeHierarchyItem.js +34 -0
  120. package/es/common/protocolWorkspaceSymbol.d.ts +9 -0
  121. package/es/common/protocolWorkspaceSymbol.d.ts.map +1 -0
  122. package/es/common/protocolWorkspaceSymbol.js +36 -0
  123. package/es/common/reference.d.ts +22 -0
  124. package/es/common/reference.d.ts.map +1 -0
  125. package/es/common/reference.js +78 -0
  126. package/es/common/rename.d.ts +29 -0
  127. package/es/common/rename.d.ts.map +1 -0
  128. package/es/common/rename.js +132 -0
  129. package/es/common/selectionRange.d.ts +18 -0
  130. package/es/common/selectionRange.d.ts.map +1 -0
  131. package/es/common/selectionRange.js +108 -0
  132. package/es/common/semanticTokens.d.ts +36 -0
  133. package/es/common/semanticTokens.d.ts.map +1 -0
  134. package/es/common/semanticTokens.js +226 -0
  135. package/es/common/signatureHelp.d.ts +18 -0
  136. package/es/common/signatureHelp.d.ts.map +1 -0
  137. package/es/common/signatureHelp.js +103 -0
  138. package/es/common/textSynchronization.d.ts +104 -0
  139. package/es/common/textSynchronization.d.ts.map +1 -0
  140. package/es/common/textSynchronization.js +771 -0
  141. package/es/common/typeDefinition.d.ts +18 -0
  142. package/es/common/typeDefinition.d.ts.map +1 -0
  143. package/es/common/typeDefinition.js +89 -0
  144. package/es/common/typeHierarchy.d.ts +33 -0
  145. package/es/common/typeHierarchy.d.ts.map +1 -0
  146. package/es/common/typeHierarchy.js +138 -0
  147. package/es/common/utils/async.d.ts +42 -0
  148. package/es/common/utils/async.d.ts.map +1 -0
  149. package/es/common/utils/async.js +441 -0
  150. package/es/common/utils/is.d.ts +13 -0
  151. package/es/common/utils/is.d.ts.map +1 -0
  152. package/es/common/utils/is.js +52 -0
  153. package/es/common/utils/uuid.d.ts +23 -0
  154. package/es/common/utils/uuid.d.ts.map +1 -0
  155. package/es/common/utils/uuid.js +85 -0
  156. package/es/common/vscodeAdaptor/convertor.d.ts +7 -0
  157. package/es/common/vscodeAdaptor/convertor.d.ts.map +1 -0
  158. package/es/common/vscodeAdaptor/convertor.js +66 -0
  159. package/es/common/vscodeAdaptor/diagnosticCollection.d.ts +33 -0
  160. package/es/common/vscodeAdaptor/diagnosticCollection.d.ts.map +1 -0
  161. package/es/common/vscodeAdaptor/diagnosticCollection.js +310 -0
  162. package/es/common/vscodeAdaptor/extHostTypes.d.ts +1496 -0
  163. package/es/common/vscodeAdaptor/extHostTypes.d.ts.map +1 -0
  164. package/es/common/vscodeAdaptor/extHostTypes.js +3825 -0
  165. package/es/common/vscodeAdaptor/fileWatcher.d.ts +19 -0
  166. package/es/common/vscodeAdaptor/fileWatcher.d.ts.map +1 -0
  167. package/es/common/vscodeAdaptor/fileWatcher.js +45 -0
  168. package/es/common/vscodeAdaptor/hostTypeUtil.d.ts +192 -0
  169. package/es/common/vscodeAdaptor/hostTypeUtil.d.ts.map +1 -0
  170. package/es/common/vscodeAdaptor/hostTypeUtil.js +566 -0
  171. package/es/common/vscodeAdaptor/libro-fs.d.ts +21 -0
  172. package/es/common/vscodeAdaptor/libro-fs.d.ts.map +1 -0
  173. package/es/common/vscodeAdaptor/libro-fs.js +64 -0
  174. package/es/common/vscodeAdaptor/libroWindow.d.ts +21 -0
  175. package/es/common/vscodeAdaptor/libroWindow.d.ts.map +1 -0
  176. package/es/common/vscodeAdaptor/libroWindow.js +75 -0
  177. package/es/common/vscodeAdaptor/libroWorkspace.d.ts +33 -0
  178. package/es/common/vscodeAdaptor/libroWorkspace.d.ts.map +1 -0
  179. package/es/common/vscodeAdaptor/libroWorkspace.js +250 -0
  180. package/es/common/vscodeAdaptor/lspEnv.d.ts +8 -0
  181. package/es/common/vscodeAdaptor/lspEnv.d.ts.map +1 -0
  182. package/es/common/vscodeAdaptor/lspEnv.js +31 -0
  183. package/es/common/vscodeAdaptor/monaco-converter.d.ts +229 -0
  184. package/es/common/vscodeAdaptor/monaco-converter.d.ts.map +1 -0
  185. package/es/common/vscodeAdaptor/monaco-converter.js +1613 -0
  186. package/es/common/vscodeAdaptor/monacoLanguages.d.ts +48 -0
  187. package/es/common/vscodeAdaptor/monacoLanguages.d.ts.map +1 -0
  188. package/es/common/vscodeAdaptor/monacoLanguages.js +484 -0
  189. package/es/common/vscodeAdaptor/services.d.ts +85 -0
  190. package/es/common/vscodeAdaptor/services.d.ts.map +1 -0
  191. package/es/common/vscodeAdaptor/services.js +3 -0
  192. package/es/common/vscodeAdaptor/typings.d.ts +10 -0
  193. package/es/common/vscodeAdaptor/util.d.ts +3 -0
  194. package/es/common/vscodeAdaptor/util.d.ts.map +1 -0
  195. package/es/common/vscodeAdaptor/util.js +6 -0
  196. package/es/common/vscodeAdaptor/vscodeAdaptor.d.ts +77 -0
  197. package/es/common/vscodeAdaptor/vscodeAdaptor.d.ts.map +1 -0
  198. package/es/common/vscodeAdaptor/vscodeAdaptor.js +124 -0
  199. package/es/common/workspaceFolder.d.ts +32 -0
  200. package/es/common/workspaceFolder.d.ts.map +1 -0
  201. package/es/common/workspaceFolder.js +204 -0
  202. package/es/common/workspaceSymbol.d.ts +21 -0
  203. package/es/common/workspaceSymbol.d.ts.map +1 -0
  204. package/es/common/workspaceSymbol.js +101 -0
  205. package/es/constants.d.ts +2 -0
  206. package/es/constants.d.ts.map +1 -0
  207. package/es/constants.js +1 -0
  208. package/es/index.d.ts +6 -0
  209. package/es/index.d.ts.map +1 -0
  210. package/es/index.js +5 -0
  211. package/es/libro-language-client-contribution.d.ts +10 -0
  212. package/es/libro-language-client-contribution.d.ts.map +1 -0
  213. package/es/libro-language-client-contribution.js +143 -0
  214. package/es/libro-language-client-manager.d.ts +34 -0
  215. package/es/libro-language-client-manager.d.ts.map +1 -0
  216. package/es/libro-language-client-manager.js +277 -0
  217. package/es/libro-language-client.d.ts +27 -0
  218. package/es/libro-language-client.d.ts.map +1 -0
  219. package/es/libro-language-client.js +141 -0
  220. package/es/module.d.ts +3 -0
  221. package/es/module.d.ts.map +1 -0
  222. package/es/module.js +13 -0
  223. package/package.json +69 -0
  224. package/src/common/api.ts +155 -0
  225. package/src/common/callHierarchy.ts +269 -0
  226. package/src/common/client.ts +3192 -0
  227. package/src/common/codeAction.ts +237 -0
  228. package/src/common/codeConverter.ts +1409 -0
  229. package/src/common/codeLens.ts +188 -0
  230. package/src/common/colorProvider.ts +192 -0
  231. package/src/common/completion.ts +281 -0
  232. package/src/common/configuration.ts +338 -0
  233. package/src/common/declaration.ts +140 -0
  234. package/src/common/definition.ts +138 -0
  235. package/src/common/diagnostic.ts +1408 -0
  236. package/src/common/documentHighlight.ts +140 -0
  237. package/src/common/documentLink.ts +180 -0
  238. package/src/common/documentSymbol.ts +186 -0
  239. package/src/common/executeCommand.ts +129 -0
  240. package/src/common/features.ts +1157 -0
  241. package/src/common/fileOperations.ts +635 -0
  242. package/src/common/fileSystemWatcher.ts +184 -0
  243. package/src/common/foldingRange.ts +160 -0
  244. package/src/common/formatting.ts +465 -0
  245. package/src/common/hover.ts +133 -0
  246. package/src/common/implementation.ts +142 -0
  247. package/src/common/inlayHint.ts +201 -0
  248. package/src/common/inlineCompletion.ts +160 -0
  249. package/src/common/inlineValue.ts +158 -0
  250. package/src/common/linkedEditingRange.ts +141 -0
  251. package/src/common/notebook.ts +1443 -0
  252. package/src/common/progress.ts +61 -0
  253. package/src/common/progressPart.ts +151 -0
  254. package/src/common/protocolCallHierarchyItem.ts +29 -0
  255. package/src/common/protocolCodeAction.ts +17 -0
  256. package/src/common/protocolCodeLens.ts +15 -0
  257. package/src/common/protocolCompletionItem.ts +22 -0
  258. package/src/common/protocolConverter.ts +2627 -0
  259. package/src/common/protocolDiagnostic.ts +47 -0
  260. package/src/common/protocolDocumentLink.ts +17 -0
  261. package/src/common/protocolInlayHint.ts +21 -0
  262. package/src/common/protocolTypeHierarchyItem.ts +29 -0
  263. package/src/common/protocolWorkspaceSymbol.ts +39 -0
  264. package/src/common/reference.ts +144 -0
  265. package/src/common/rename.ts +230 -0
  266. package/src/common/selectionRange.ts +136 -0
  267. package/src/common/semanticTokens.ts +383 -0
  268. package/src/common/signatureHelp.ts +170 -0
  269. package/src/common/textSynchronization.ts +819 -0
  270. package/src/common/typeDefinition.ts +146 -0
  271. package/src/common/typeHierarchy.ts +248 -0
  272. package/src/common/utils/async.ts +354 -0
  273. package/src/common/utils/is.ts +63 -0
  274. package/src/common/utils/uuid.ts +136 -0
  275. package/src/common/vscodeAdaptor/convertor.ts +73 -0
  276. package/src/common/vscodeAdaptor/diagnosticCollection.ts +238 -0
  277. package/src/common/vscodeAdaptor/extHostTypes.ts +4498 -0
  278. package/src/common/vscodeAdaptor/fileWatcher.ts +36 -0
  279. package/src/common/vscodeAdaptor/hostTypeUtil.ts +539 -0
  280. package/src/common/vscodeAdaptor/libro-fs.ts +51 -0
  281. package/src/common/vscodeAdaptor/libroWindow.ts +85 -0
  282. package/src/common/vscodeAdaptor/libroWorkspace.ts +261 -0
  283. package/src/common/vscodeAdaptor/lspEnv.ts +16 -0
  284. package/src/common/vscodeAdaptor/monaco-converter.ts +1800 -0
  285. package/src/common/vscodeAdaptor/monacoLanguages.ts +511 -0
  286. package/src/common/vscodeAdaptor/services.ts +278 -0
  287. package/src/common/vscodeAdaptor/typings.d.ts +10 -0
  288. package/src/common/vscodeAdaptor/util.ts +7 -0
  289. package/src/common/vscodeAdaptor/vscodeAdaptor.ts +122 -0
  290. package/src/common/workspaceFolder.ts +236 -0
  291. package/src/common/workspaceSymbol.ts +166 -0
  292. package/src/constants.ts +1 -0
  293. package/src/index.spec.ts +7 -0
  294. package/src/index.ts +5 -0
  295. package/src/libro-language-client-contribution.ts +49 -0
  296. package/src/libro-language-client-manager.ts +131 -0
  297. package/src/libro-language-client.ts +100 -0
  298. package/src/module.ts +19 -0
@@ -0,0 +1,354 @@
1
+ /* eslint-disable promise/always-return */
2
+ /* eslint-disable promise/catch-or-return */
3
+ /* --------------------------------------------------------------------------------------------
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ * Licensed under the MIT License. See License.txt in the project root for license information.
6
+ * ------------------------------------------------------------------------------------------ */
7
+
8
+ import type { Disposable } from '@difizen/vscode-languageserver-protocol';
9
+ import { RAL } from '@difizen/vscode-languageserver-protocol';
10
+ import type { CancellationToken } from 'vscode';
11
+
12
+ export type ITask<T> = () => T;
13
+
14
+ export class Delayer<T> {
15
+ public defaultDelay: number;
16
+ private timeout: Disposable | undefined;
17
+ private completionPromise: Promise<T> | undefined;
18
+ private onSuccess: ((value: T | Promise<T> | undefined) => void) | undefined;
19
+ private task: ITask<T> | undefined;
20
+
21
+ constructor(defaultDelay: number) {
22
+ this.defaultDelay = defaultDelay;
23
+ this.timeout = undefined;
24
+ this.completionPromise = undefined;
25
+ this.onSuccess = undefined;
26
+ this.task = undefined;
27
+ }
28
+
29
+ public trigger(task: ITask<T>, delay: number = this.defaultDelay): Promise<T> {
30
+ this.task = task;
31
+ if (delay >= 0) {
32
+ this.cancelTimeout();
33
+ }
34
+
35
+ if (!this.completionPromise) {
36
+ this.completionPromise = new Promise<T | undefined>((resolve) => {
37
+ this.onSuccess = resolve;
38
+ }).then(() => {
39
+ this.completionPromise = undefined;
40
+ this.onSuccess = undefined;
41
+ const result = this.task!();
42
+ this.task = undefined;
43
+ return result;
44
+ });
45
+ }
46
+
47
+ if (delay >= 0 || this.timeout === void 0) {
48
+ this.timeout = RAL().timer.setTimeout(
49
+ () => {
50
+ this.timeout = undefined;
51
+ this.onSuccess!(undefined);
52
+ },
53
+ delay >= 0 ? delay : this.defaultDelay,
54
+ );
55
+ }
56
+
57
+ return this.completionPromise;
58
+ }
59
+
60
+ public forceDelivery(): T | undefined {
61
+ if (!this.completionPromise) {
62
+ return undefined;
63
+ }
64
+ this.cancelTimeout();
65
+ const result: T = this.task!();
66
+ this.completionPromise = undefined;
67
+ this.onSuccess = undefined;
68
+ this.task = undefined;
69
+ return result;
70
+ }
71
+
72
+ public isTriggered(): boolean {
73
+ return this.timeout !== undefined;
74
+ }
75
+
76
+ public cancel(): void {
77
+ this.cancelTimeout();
78
+ this.completionPromise = undefined;
79
+ }
80
+
81
+ private cancelTimeout(): void {
82
+ if (this.timeout !== undefined) {
83
+ this.timeout.dispose();
84
+ this.timeout = undefined;
85
+ }
86
+ }
87
+ }
88
+
89
+ type Thunk<T> = () => T;
90
+
91
+ type Waiting<T> = {
92
+ thunk: Thunk<T | PromiseLike<T>>;
93
+ resolve: (value: T | PromiseLike<T>) => void;
94
+ reject: (reason?: any) => void;
95
+ };
96
+
97
+ export class Semaphore<T = void> {
98
+ private _capacity: number;
99
+ private _active: number;
100
+ private _waiting: Waiting<T>[];
101
+
102
+ public constructor(capacity = 1) {
103
+ if (capacity <= 0) {
104
+ throw new Error('Capacity must be greater than 0');
105
+ }
106
+ this._capacity = capacity;
107
+ this._active = 0;
108
+ this._waiting = [];
109
+ }
110
+
111
+ public lock(thunk: () => T | PromiseLike<T>): Promise<T> {
112
+ return new Promise((resolve, reject) => {
113
+ this._waiting.push({ thunk, resolve, reject });
114
+ this.runNext();
115
+ });
116
+ }
117
+
118
+ public get active(): number {
119
+ return this._active;
120
+ }
121
+
122
+ private runNext(): void {
123
+ if (this._waiting.length === 0 || this._active === this._capacity) {
124
+ return;
125
+ }
126
+ RAL().timer.setImmediate(() => this.doRunNext());
127
+ }
128
+
129
+ private doRunNext(): void {
130
+ if (this._waiting.length === 0 || this._active === this._capacity) {
131
+ return;
132
+ }
133
+ const next = this._waiting.shift()!;
134
+ this._active++;
135
+ if (this._active > this._capacity) {
136
+ throw new Error(`To many thunks active`);
137
+ }
138
+ try {
139
+ const result = next.thunk();
140
+ if (result instanceof Promise) {
141
+ result.then(
142
+ (value) => {
143
+ this._active--;
144
+ next.resolve(value);
145
+ this.runNext();
146
+ },
147
+ (err) => {
148
+ this._active--;
149
+ next.reject(err);
150
+ this.runNext();
151
+ },
152
+ );
153
+ } else {
154
+ this._active--;
155
+ next.resolve(result);
156
+ this.runNext();
157
+ }
158
+ } catch (err) {
159
+ this._active--;
160
+ next.reject(err);
161
+ this.runNext();
162
+ }
163
+ }
164
+ }
165
+
166
+ let $test = false;
167
+ export function setTestMode() {
168
+ $test = true;
169
+ }
170
+ export function clearTestMode() {
171
+ $test = false;
172
+ }
173
+
174
+ const defaultYieldTimeout = 15; /*ms*/
175
+
176
+ declare const console: any;
177
+
178
+ class Timer {
179
+ private readonly yieldAfter: number;
180
+ private startTime: number;
181
+ private counter: number;
182
+ private total: number;
183
+ private counterInterval: number;
184
+
185
+ constructor(yieldAfter: number = defaultYieldTimeout) {
186
+ this.yieldAfter =
187
+ $test === true
188
+ ? Math.max(yieldAfter, 2)
189
+ : Math.max(yieldAfter, defaultYieldTimeout);
190
+ this.startTime = Date.now();
191
+ this.counter = 0;
192
+ this.total = 0;
193
+ // start with a counter interval of 1.
194
+ this.counterInterval = 1;
195
+ }
196
+ public start() {
197
+ this.counter = 0;
198
+ this.total = 0;
199
+ this.counterInterval = 1;
200
+ this.startTime = Date.now();
201
+ }
202
+ public shouldYield(): boolean {
203
+ if (++this.counter >= this.counterInterval) {
204
+ const timeTaken = Date.now() - this.startTime;
205
+ const timeLeft = Math.max(0, this.yieldAfter - timeTaken);
206
+ this.total += this.counter;
207
+ this.counter = 0;
208
+ if (timeTaken >= this.yieldAfter || timeLeft <= 1) {
209
+ // Yield also if time left <= 1 since we compute the counter
210
+ // for max < 2 ms.
211
+
212
+ // Start with interval 1 again. We could do some calculation
213
+ // with using 80% of the last counter however other things (GC)
214
+ // affect the timing heavily since we have small timings (1 - 15ms).
215
+ this.counterInterval = 1;
216
+ this.total = 0;
217
+ return true;
218
+ } else {
219
+ // Only increase the counter until we have spent <= 2 ms. Increasing
220
+ // the counter further is very fragile since timing is influenced
221
+ // by other things and can increase the counter too much. This will result
222
+ // that we yield in average after [14 - 16]ms.
223
+ switch (timeTaken) {
224
+ case 0:
225
+ case 1:
226
+ this.counterInterval = this.total * 2;
227
+ break;
228
+ }
229
+ }
230
+ }
231
+ return false;
232
+ }
233
+ }
234
+
235
+ export type YieldOptions = {
236
+ /**
237
+ * The time in ms after which the function should yield.
238
+ * The minimum yield time is 15ms
239
+ */
240
+ yieldAfter?: number /* ms */;
241
+
242
+ /**
243
+ * An optional callback that is invoke when the code yields.
244
+ */
245
+ yieldCallback?: () => void;
246
+ };
247
+
248
+ export async function map<P, C>(
249
+ items: ReadonlyArray<P>,
250
+ func: (item: P) => C,
251
+ token?: CancellationToken,
252
+ options?: YieldOptions,
253
+ ): Promise<C[]> {
254
+ if (items.length === 0) {
255
+ return [];
256
+ }
257
+ const result: C[] = new Array(items.length);
258
+ const timer = new Timer(options?.yieldAfter);
259
+ function convertBatch(start: number): number {
260
+ timer.start();
261
+ for (let i = start; i < items.length; i++) {
262
+ result[i] = func(items[i]);
263
+ if (timer.shouldYield()) {
264
+ options?.yieldCallback && options.yieldCallback();
265
+ return i + 1;
266
+ }
267
+ }
268
+ return -1;
269
+ }
270
+ // Convert the first batch sync on the same frame.
271
+ let index = convertBatch(0);
272
+ while (index !== -1) {
273
+ if (token !== undefined && token.isCancellationRequested) {
274
+ break;
275
+ }
276
+ index = await new Promise((resolve) => {
277
+ RAL().timer.setImmediate(() => {
278
+ resolve(convertBatch(index));
279
+ });
280
+ });
281
+ }
282
+ return result;
283
+ }
284
+
285
+ export async function mapAsync<P, C>(
286
+ items: ReadonlyArray<P>,
287
+ func: (item: P, token?: CancellationToken) => Promise<C>,
288
+ token?: CancellationToken,
289
+ options?: YieldOptions,
290
+ ): Promise<C[]> {
291
+ if (items.length === 0) {
292
+ return [];
293
+ }
294
+ const result: C[] = new Array(items.length);
295
+ const timer = new Timer(options?.yieldAfter);
296
+ async function convertBatch(start: number): Promise<number> {
297
+ timer.start();
298
+ for (let i = start; i < items.length; i++) {
299
+ result[i] = await func(items[i], token);
300
+ if (timer.shouldYield()) {
301
+ options?.yieldCallback && options.yieldCallback();
302
+ return i + 1;
303
+ }
304
+ }
305
+ return -1;
306
+ }
307
+ let index = await convertBatch(0);
308
+ while (index !== -1) {
309
+ if (token !== undefined && token.isCancellationRequested) {
310
+ break;
311
+ }
312
+ index = await new Promise((resolve) => {
313
+ RAL().timer.setImmediate(() => {
314
+ resolve(convertBatch(index));
315
+ });
316
+ });
317
+ }
318
+ return result;
319
+ }
320
+
321
+ export async function forEach<P>(
322
+ items: ReadonlyArray<P>,
323
+ func: (item: P) => void,
324
+ token?: CancellationToken,
325
+ options?: YieldOptions,
326
+ ): Promise<void> {
327
+ if (items.length === 0) {
328
+ return;
329
+ }
330
+ const timer = new Timer(options?.yieldAfter);
331
+ function runBatch(start: number): number {
332
+ timer.start();
333
+ for (let i = start; i < items.length; i++) {
334
+ func(items[i]);
335
+ if (timer.shouldYield()) {
336
+ options?.yieldCallback && options.yieldCallback();
337
+ return i + 1;
338
+ }
339
+ }
340
+ return -1;
341
+ }
342
+ // Convert the first batch sync on the same frame.
343
+ let index = runBatch(0);
344
+ while (index !== -1) {
345
+ if (token !== undefined && token.isCancellationRequested) {
346
+ break;
347
+ }
348
+ index = await new Promise((resolve) => {
349
+ RAL().timer.setImmediate(() => {
350
+ resolve(runBatch(index));
351
+ });
352
+ });
353
+ }
354
+ }
@@ -0,0 +1,63 @@
1
+ /* --------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ * ------------------------------------------------------------------------------------------ */
5
+
6
+ export function boolean(value: any): value is boolean {
7
+ return value === true || value === false;
8
+ }
9
+
10
+ export function string(value: any): value is string {
11
+ return typeof value === 'string' || value instanceof String;
12
+ }
13
+
14
+ export function number(value: any): value is number {
15
+ return typeof value === 'number' || value instanceof Number;
16
+ }
17
+
18
+ export function error(value: any): value is Error {
19
+ return value instanceof Error;
20
+ }
21
+
22
+ // eslint-disable-next-line @typescript-eslint/ban-types
23
+ export function func(value: any): value is Function {
24
+ return typeof value === 'function';
25
+ }
26
+
27
+ export function array<T>(value: any): value is T[] {
28
+ return Array.isArray(value);
29
+ }
30
+
31
+ export function stringArray(value: any): value is string[] {
32
+ return array(value) && (<any[]>value).every((elem) => string(elem));
33
+ }
34
+
35
+ export function typedArray<T>(
36
+ value: any,
37
+ check: (value: any) => boolean,
38
+ ): value is T[] {
39
+ return Array.isArray(value) && (<any[]>value).every(check);
40
+ }
41
+
42
+ export function thenable<T>(value: any): value is Thenable<T> {
43
+ return value && func(value.then);
44
+ }
45
+
46
+ export function asPromise<T>(value: Promise<T>): Promise<T>;
47
+ export function asPromise<T>(value: Thenable<T>): Promise<T>;
48
+ export function asPromise<T>(value: T): Promise<T>;
49
+ export function asPromise(value: any): Promise<any> {
50
+ if (value instanceof Promise) {
51
+ return value;
52
+ } else if (thenable(value)) {
53
+ return new Promise((resolve, reject) => {
54
+ // eslint-disable-next-line promise/catch-or-return
55
+ value.then(
56
+ (resolved) => resolve(resolved),
57
+ (error) => reject(error),
58
+ );
59
+ });
60
+ } else {
61
+ return Promise.resolve(value);
62
+ }
63
+ }
@@ -0,0 +1,136 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+
6
+ /**
7
+ * Represents a UUID as defined by rfc4122.
8
+ */
9
+ export interface UUID {
10
+ /**
11
+ * @returns the canonical representation in sets of hexadecimal numbers separated by dashes.
12
+ */
13
+ asHex(): string;
14
+
15
+ equals(other: UUID): boolean;
16
+ }
17
+
18
+ class ValueUUID implements UUID {
19
+ constructor(public _value: string) {
20
+ // empty
21
+ }
22
+
23
+ public asHex(): string {
24
+ return this._value;
25
+ }
26
+
27
+ public equals(other: UUID): boolean {
28
+ return this.asHex() === other.asHex();
29
+ }
30
+ }
31
+
32
+ class V4UUID extends ValueUUID {
33
+ private static _chars = [
34
+ '0',
35
+ '1',
36
+ '2',
37
+ '3',
38
+ '4',
39
+ '5',
40
+ '6',
41
+ '6',
42
+ '7',
43
+ '8',
44
+ '9',
45
+ 'a',
46
+ 'b',
47
+ 'c',
48
+ 'd',
49
+ 'e',
50
+ 'f',
51
+ ];
52
+
53
+ private static _timeHighBits = ['8', '9', 'a', 'b'];
54
+
55
+ private static _oneOf(array: string[]): string {
56
+ return array[Math.floor(array.length * Math.random())];
57
+ }
58
+
59
+ private static _randomHex(): string {
60
+ return V4UUID._oneOf(V4UUID._chars);
61
+ }
62
+
63
+ constructor() {
64
+ super(
65
+ [
66
+ V4UUID._randomHex(),
67
+ V4UUID._randomHex(),
68
+ V4UUID._randomHex(),
69
+ V4UUID._randomHex(),
70
+ V4UUID._randomHex(),
71
+ V4UUID._randomHex(),
72
+ V4UUID._randomHex(),
73
+ V4UUID._randomHex(),
74
+ '-',
75
+ V4UUID._randomHex(),
76
+ V4UUID._randomHex(),
77
+ V4UUID._randomHex(),
78
+ V4UUID._randomHex(),
79
+ '-',
80
+ '4',
81
+ V4UUID._randomHex(),
82
+ V4UUID._randomHex(),
83
+ V4UUID._randomHex(),
84
+ '-',
85
+ V4UUID._oneOf(V4UUID._timeHighBits),
86
+ V4UUID._randomHex(),
87
+ V4UUID._randomHex(),
88
+ V4UUID._randomHex(),
89
+ '-',
90
+ V4UUID._randomHex(),
91
+ V4UUID._randomHex(),
92
+ V4UUID._randomHex(),
93
+ V4UUID._randomHex(),
94
+ V4UUID._randomHex(),
95
+ V4UUID._randomHex(),
96
+ V4UUID._randomHex(),
97
+ V4UUID._randomHex(),
98
+ V4UUID._randomHex(),
99
+ V4UUID._randomHex(),
100
+ V4UUID._randomHex(),
101
+ V4UUID._randomHex(),
102
+ ].join(''),
103
+ );
104
+ }
105
+ }
106
+
107
+ /**
108
+ * An empty UUID that contains only zeros.
109
+ */
110
+ export const empty: UUID = new ValueUUID('00000000-0000-0000-0000-000000000000');
111
+
112
+ export function v4(): UUID {
113
+ return new V4UUID();
114
+ }
115
+
116
+ const _UUIDPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
117
+
118
+ export function isUUID(value: string): boolean {
119
+ return _UUIDPattern.test(value);
120
+ }
121
+
122
+ /**
123
+ * Parses a UUID that is of the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
124
+ * @param value A uuid string.
125
+ */
126
+ export function parse(value: string): UUID {
127
+ if (!isUUID(value)) {
128
+ throw new Error('invalid uuid');
129
+ }
130
+
131
+ return new ValueUUID(value);
132
+ }
133
+
134
+ export function generateUuid(): string {
135
+ return v4().asHex();
136
+ }
@@ -0,0 +1,73 @@
1
+ import type { CellView, LibroView } from '@difizen/libro-core';
2
+ import type { NotebookCell, NotebookDocument, NotebookRange } from 'vscode';
3
+
4
+ import { NotebookDocumentSyncFeature } from '../notebook.js';
5
+
6
+ import { unsupported } from './util.js';
7
+ import { EndOfLine, NotebookCellKind, Uri } from './vscodeAdaptor.js';
8
+
9
+ export const l2c = {
10
+ asNotebookDocument(libroView: LibroView): NotebookDocument {
11
+ const model = libroView.model as any;
12
+ if (model.filePath === undefined) {
13
+ throw new Error('no filePath: invalid libro jupyter model');
14
+ }
15
+ const filePath = model.filePath as string;
16
+ return {
17
+ uri: Uri.parse(filePath),
18
+ notebookType: 'jupyter',
19
+ version: 0,
20
+ isDirty: libroView.model.dirty,
21
+ isUntitled: false,
22
+ isClosed: false,
23
+ metadata: libroView.model.metadata,
24
+ cellCount: libroView.model.executeCount,
25
+ cellAt: unsupported,
26
+ getCells(range?: NotebookRange): NotebookCell[] {
27
+ return libroView.model.cells.map(l2c.asNotebookCell);
28
+ },
29
+ save: unsupported,
30
+ };
31
+ },
32
+
33
+ asNotebookCell(cell: CellView): NotebookCell {
34
+ const model = cell.parent.model as any;
35
+ if (model.filePath === undefined) {
36
+ throw new Error('no filePath: invalid libro jupyter model');
37
+ }
38
+ const filePath = model.filePath as string;
39
+ return {
40
+ index: cell.parent.findCellIndex(cell),
41
+ notebook: l2c.asNotebookDocument(cell.parent),
42
+ kind:
43
+ cell.model.type === 'code' ? NotebookCellKind.Code : NotebookCellKind.Markup,
44
+ document: {
45
+ uri: Uri.parse(filePath).with({
46
+ scheme: NotebookDocumentSyncFeature.CellScheme,
47
+ query: `cellid=${cell.model.id}`,
48
+ }),
49
+ fileName: filePath,
50
+ isUntitled: false,
51
+ languageId: 'python',
52
+ version: 0,
53
+ isDirty: false,
54
+ isClosed: false,
55
+ save: unsupported,
56
+ eol: EndOfLine.LF,
57
+ lineCount: 0,
58
+ lineAt: unsupported,
59
+ offsetAt: unsupported,
60
+ positionAt: unsupported,
61
+ getText: () => {
62
+ return cell.model.value;
63
+ },
64
+ getWordRangeAtPosition: unsupported,
65
+ validateRange: unsupported,
66
+ validatePosition: unsupported,
67
+ },
68
+ metadata: cell.model.metadata,
69
+ outputs: [],
70
+ executionSummary: undefined,
71
+ };
72
+ },
73
+ };