@volar/monaco 1.3.0-alpha.3-patch.2 → 1.4.0-alpha.1

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/out/worker.d.ts CHANGED
@@ -20,7 +20,7 @@ declare class CdnDtsHost {
20
20
  constructor(cdn: string, versions?: Record<string, string>, onFetch?: ((fileName: string, text: string) => void) | undefined);
21
21
  getVersion(): Promise<number>;
22
22
  readFile(fileName: string): string | Promise<string | undefined> | undefined;
23
- fetch(fileName: string): Promise<string | undefined>;
23
+ fetchFile(fileName: string): Promise<string | undefined>;
24
24
  resolveRequestFileName(fileName: string): string;
25
25
  /**
26
26
  * save / load with json
package/out/worker.js CHANGED
@@ -1,23 +1,12 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  Object.defineProperty(exports, "__esModule", { value: true });
11
2
  exports.createDtsHost = exports.createLanguageService = void 0;
12
3
  const language_service_1 = require("@volar/language-service");
13
4
  const vscode_uri_1 = require("vscode-uri");
14
- const axios_1 = require("axios");
15
5
  function createLanguageService(options) {
16
- var _a, _b, _c;
17
- const dtsClient = options.dtsHost ? createDtsClient(options.dtsHost) : undefined;
6
+ const dtsFiles = new Map();
18
7
  const ts = options.typescript ? options.typescript.module : undefined;
19
- const config = (_a = options.config) !== null && _a !== void 0 ? _a : {};
20
- const compilerOptions = (_c = (_b = options.typescript) === null || _b === void 0 ? void 0 : _b.compilerOptions) !== null && _c !== void 0 ? _c : {};
8
+ const config = options.config ?? {};
9
+ const compilerOptions = options.typescript?.compilerOptions ?? {};
21
10
  let host = createLanguageServiceHost();
22
11
  let languageService = (0, language_service_1.createLanguageService)({
23
12
  host,
@@ -37,13 +26,13 @@ function createLanguageService(options) {
37
26
  InnocentRabbit.prototype[api] = () => languageService[api];
38
27
  continue;
39
28
  }
40
- InnocentRabbit.prototype[api] = (...args) => __awaiter(this, void 0, void 0, function* () {
41
- if (!dtsClient) {
29
+ InnocentRabbit.prototype[api] = async (...args) => {
30
+ if (!options.dtsHost) {
42
31
  return languageService[api](...args);
43
32
  }
44
- let oldVersion = yield dtsClient.getVersion();
45
- let result = yield languageService[api](...args);
46
- let newVersion = yield dtsClient.getVersion();
33
+ let oldVersion = await options.dtsHost.getVersion();
34
+ let result = await languageService[api](...args);
35
+ let newVersion = await options.dtsHost.getVersion();
47
36
  while (newVersion !== oldVersion) {
48
37
  oldVersion = newVersion;
49
38
  if (newVersion !== dtsVersion) {
@@ -57,17 +46,17 @@ function createLanguageService(options) {
57
46
  fileNameToUri: (fileName) => vscode_uri_1.URI.file(fileName).toString(),
58
47
  });
59
48
  }
60
- result = yield languageService[api](...args);
61
- newVersion = yield dtsClient.getVersion();
49
+ result = await languageService[api](...args);
50
+ newVersion = await options.dtsHost.getVersion();
62
51
  }
63
52
  return result;
64
- });
53
+ };
65
54
  }
66
55
  return new InnocentRabbit();
67
56
  function createLanguageServiceHost() {
68
57
  let projectVersion = 0;
69
58
  const modelSnapshot = new WeakMap();
70
- const webFileSnapshot = new Map();
59
+ const dtsFileSnapshot = new Map();
71
60
  const modelVersions = new Map();
72
61
  const host = {
73
62
  getProjectVersion() {
@@ -93,16 +82,13 @@ function createLanguageService(options) {
93
82
  if (model) {
94
83
  return model.version.toString();
95
84
  }
96
- if (dtsClient) {
97
- const dts = dtsClient.readFile(fileName);
98
- if (dts) {
99
- return dts.length.toString();
100
- }
85
+ const dts = readDtsFile(fileName);
86
+ if (dts) {
87
+ return dts.length.toString();
101
88
  }
102
89
  return '';
103
90
  },
104
91
  getScriptSnapshot(fileName) {
105
- var _a;
106
92
  const model = options.workerContext.getMirrorModels().find(model => model.uri.fsPath === fileName);
107
93
  if (model) {
108
94
  const cache = modelSnapshot.get(model);
@@ -115,21 +101,19 @@ function createLanguageService(options) {
115
101
  getLength: () => text.length,
116
102
  getChangeRange: () => undefined,
117
103
  }]);
118
- return (_a = modelSnapshot.get(model)) === null || _a === void 0 ? void 0 : _a[1];
104
+ return modelSnapshot.get(model)?.[1];
119
105
  }
120
- if (webFileSnapshot.has(fileName)) {
121
- return webFileSnapshot.get(fileName);
106
+ if (dtsFileSnapshot.has(fileName)) {
107
+ return dtsFileSnapshot.get(fileName);
122
108
  }
123
- if (dtsClient) {
124
- const webFileText = dtsClient.readFile(fileName);
125
- if (webFileText !== undefined) {
126
- webFileSnapshot.set(fileName, {
127
- getText: (start, end) => webFileText.substring(start, end),
128
- getLength: () => webFileText.length,
129
- getChangeRange: () => undefined,
130
- });
131
- return webFileSnapshot.get(fileName);
132
- }
109
+ const dtsFileText = readDtsFile(fileName);
110
+ if (dtsFileText !== undefined) {
111
+ dtsFileSnapshot.set(fileName, {
112
+ getText: (start, end) => dtsFileText.substring(start, end),
113
+ getLength: () => dtsFileText.length,
114
+ getChangeRange: () => undefined,
115
+ });
116
+ return dtsFileSnapshot.get(fileName);
133
117
  }
134
118
  },
135
119
  getCompilationSettings() {
@@ -149,24 +133,30 @@ function createLanguageService(options) {
149
133
  if (model) {
150
134
  return model.getValue();
151
135
  }
152
- if (dtsClient) {
153
- return dtsClient.readFile(fileName);
154
- }
136
+ return readDtsFile(fileName);
155
137
  },
156
138
  fileExists(fileName) {
157
139
  const model = options.workerContext.getMirrorModels().find(model => model.uri.fsPath === fileName);
158
140
  if (model) {
159
141
  return true;
160
142
  }
161
- if (dtsClient) {
162
- return dtsClient.readFile(fileName) !== undefined;
163
- }
164
- return false;
143
+ return readDtsFile(fileName) !== undefined;
165
144
  },
166
145
  getTypeScriptModule: ts ? (() => ts) : undefined,
167
146
  };
168
147
  return host;
169
148
  }
149
+ function readDtsFile(fileName) {
150
+ if (!dtsFiles.has(fileName) && options.dtsHost) {
151
+ dtsFiles.set(fileName, undefined);
152
+ readDtsFileAsync(fileName);
153
+ }
154
+ return dtsFiles.get(fileName);
155
+ }
156
+ async function readDtsFileAsync(fileName) {
157
+ const text = await options.dtsHost?.readFile(fileName);
158
+ dtsFiles.set(fileName, text);
159
+ }
170
160
  }
171
161
  exports.createLanguageService = createLanguageService;
172
162
  function createDtsHost(cdn, versions = {}, onFetch) {
@@ -181,14 +171,12 @@ class CdnDtsHost {
181
171
  this.files = new Map();
182
172
  this.lastUpdateFilesSize = 0;
183
173
  }
184
- getVersion() {
185
- return __awaiter(this, void 0, void 0, function* () {
186
- while (this.files.size !== this.lastUpdateFilesSize) {
187
- this.lastUpdateFilesSize = this.files.size;
188
- yield Promise.all(this.files.values());
189
- }
190
- return this.files.size;
191
- });
174
+ async getVersion() {
175
+ while (this.files.size !== this.lastUpdateFilesSize) {
176
+ this.lastUpdateFilesSize = this.files.size;
177
+ await Promise.all(this.files.values());
178
+ }
179
+ return this.files.size;
192
180
  }
193
181
  readFile(fileName) {
194
182
  if (fileName.startsWith('/node_modules/')
@@ -196,31 +184,23 @@ class CdnDtsHost {
196
184
  && (fileName.endsWith('.d.ts') || fileName.endsWith('/package.json'))) {
197
185
  if (!this.files.has(fileName)) {
198
186
  this.files.set(fileName, undefined);
199
- this.files.set(fileName, this.fetch(fileName));
187
+ this.files.set(fileName, this.fetchFile(fileName));
200
188
  }
201
189
  return this.files.get(fileName);
202
190
  }
203
191
  return undefined;
204
192
  }
205
- fetch(fileName) {
206
- var _a, _b;
207
- return __awaiter(this, void 0, void 0, function* () {
208
- const requestFileName = this.resolveRequestFileName(fileName);
209
- const url = this.cdn + requestFileName.slice('/node_modules/'.length);
210
- try {
211
- const text = (_a = (yield axios_1.default.get(url, {
212
- transformResponse: (res) => {
213
- // avoid parse to json object
214
- return res;
215
- },
216
- })).data) !== null && _a !== void 0 ? _a : undefined;
217
- (_b = this.onFetch) === null || _b === void 0 ? void 0 : _b.call(this, fileName, text);
218
- return text;
219
- }
220
- catch (_c) {
221
- // ignore
222
- }
223
- });
193
+ async fetchFile(fileName) {
194
+ const requestFileName = this.resolveRequestFileName(fileName);
195
+ const url = this.cdn + requestFileName.slice('/node_modules/'.length);
196
+ try {
197
+ const text = await (await fetch(url)).text();
198
+ this.onFetch?.(fileName, text);
199
+ return text;
200
+ }
201
+ catch {
202
+ // ignore
203
+ }
224
204
  }
225
205
  resolveRequestFileName(fileName) {
226
206
  for (const [key, version] of Object.entries(this.versions)) {
@@ -234,42 +214,17 @@ class CdnDtsHost {
234
214
  /**
235
215
  * save / load with json
236
216
  */
237
- toJson() {
238
- var _a;
239
- return __awaiter(this, void 0, void 0, function* () {
240
- const json = {};
241
- for (const [fileName, file] of this.files) {
242
- json[fileName] = (_a = (yield file)) !== null && _a !== void 0 ? _a : null;
243
- }
244
- return json;
245
- });
217
+ async toJson() {
218
+ const json = {};
219
+ for (const [fileName, file] of this.files) {
220
+ json[fileName] = (await file) ?? null;
221
+ }
222
+ return json;
246
223
  }
247
224
  fromJson(json) {
248
225
  for (const [fileName, file] of Object.entries(json)) {
249
- this.files.set(fileName, file !== null && file !== void 0 ? file : undefined);
226
+ this.files.set(fileName, file ?? undefined);
250
227
  }
251
228
  }
252
229
  }
253
- function createDtsClient(server) {
254
- const fetchTasks = [];
255
- const files = new Map();
256
- return {
257
- readFile,
258
- getVersion: () => server.getVersion(),
259
- readFileAsync,
260
- };
261
- function readFile(fileName) {
262
- if (!files.has(fileName)) {
263
- files.set(fileName, undefined);
264
- fetchTasks.push([fileName, readFileAsync(fileName)]);
265
- }
266
- return files.get(fileName);
267
- }
268
- function readFileAsync(fileName) {
269
- return __awaiter(this, void 0, void 0, function* () {
270
- const text = yield server.readFile(fileName);
271
- files.set(fileName, text);
272
- });
273
- }
274
- }
275
230
  //# sourceMappingURL=worker.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volar/monaco",
3
- "version": "1.3.0-alpha.3-patch.2",
3
+ "version": "1.4.0-alpha.1",
4
4
  "main": "out/index.js",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -15,10 +15,10 @@
15
15
  "directory": "packages/monaco"
16
16
  },
17
17
  "dependencies": {
18
- "@volar/language-service": "1.3.0-alpha.3",
19
- "axios": "^1.3.4",
18
+ "@volar/language-service": "1.4.0-alpha.1",
20
19
  "monaco-editor-core": "^0.36.0",
21
20
  "vscode-languageserver-protocol": "^3.17.3",
22
21
  "vscode-uri": "^3.0.7"
23
- }
22
+ },
23
+ "gitHead": "41ea915b137aea284e2bf5edd98a788ce21b779c"
24
24
  }