modern-monaco 0.1.8 → 0.2.0

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.
@@ -94,11 +94,11 @@ function isSameImports(a, b) {
94
94
  }
95
95
 
96
96
  // src/lsp/typescript/setup.ts
97
- import { cache } from "../../cache.js";
98
- import { ErrorNotFound } from "../../workspace.js";
99
- import * as ls from "../language-service.js";
97
+ import { cache } from "../../cache.mjs";
98
+ import { ErrorNotFound } from "../../workspace.mjs";
99
+ import * as ls from "../language-service.mjs";
100
100
  var worker = null;
101
- async function setup(monaco, languageId, workspace, languageSettings, formattingOptions) {
101
+ async function setup(monaco, languageId, languageSettings, formattingOptions, workspace) {
102
102
  if (!worker) {
103
103
  worker = createWorker(monaco, workspace, languageSettings, formattingOptions);
104
104
  }
@@ -110,12 +110,6 @@ async function setup(monaco, languageId, workspace, languageSettings, formatting
110
110
  ls.registerSignatureHelp(languageId, worker, ["(", ","]);
111
111
  ls.registerCodeAction(languageId, worker);
112
112
  }
113
- function getWorkerUrl() {
114
- const i = () => import("./worker.js");
115
- const m = getWorkerUrl.toString().match(/import\(['"](.+?)['"]\)/);
116
- if (!m) throw new Error("worker url not found", { cause: i });
117
- return new URL(m[1], import.meta.url);
118
- }
119
113
  async function createWorker(monaco, workspace, languageSettings, formattingOptions) {
120
114
  const fs = workspace?.fs;
121
115
  const defaultCompilerOptions = {
@@ -173,13 +167,12 @@ async function createWorker(monaco, workspace, languageSettings, formattingOptio
173
167
  insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: insertSpaces
174
168
  },
175
169
  importMap,
176
- types: typesStore.types
170
+ types: typesStore.types,
171
+ workspace: !!workspace
177
172
  };
178
173
  const worker2 = monaco.editor.createWebWorker({
179
- moduleId: "lsp/typescript/worker",
180
- label: "typescript",
174
+ worker: getWorker(createData),
181
175
  keepIdleModels: true,
182
- createData,
183
176
  host: {
184
177
  openModel: async (uri) => {
185
178
  if (!workspace) {
@@ -284,6 +277,21 @@ async function createWorker(monaco, workspace, languageSettings, formattingOptio
284
277
  });
285
278
  return worker2;
286
279
  }
280
+ function createWebWorker() {
281
+ const workerUrl = new URL("./worker.mjs", import.meta.url);
282
+ if (workerUrl.origin !== location.origin) {
283
+ return new Worker(
284
+ URL.createObjectURL(new Blob([`import "${workerUrl.href}"`], { type: "application/javascript" })),
285
+ { type: "module" }
286
+ );
287
+ }
288
+ return new Worker(new URL("./worker.mjs", import.meta.url), { type: "module" });
289
+ }
290
+ function getWorker(createData) {
291
+ const worker2 = createWebWorker();
292
+ worker2.postMessage(createData);
293
+ return worker2;
294
+ }
287
295
  var TypesSet = class {
288
296
  _types = {};
289
297
  _removedtypes = {};
@@ -418,7 +426,6 @@ function parseJsonc(text) {
418
426
  }
419
427
  }
420
428
  export {
421
- getWorkerUrl,
422
429
  loadImportMap,
423
430
  setup
424
431
  };
@@ -1466,14 +1466,15 @@ function getWellformedEdit(textEdit) {
1466
1466
 
1467
1467
  // src/lsp/worker-base.ts
1468
1468
  var WorkerBase = class {
1469
- constructor(_ctx, _createLanguageDocument) {
1469
+ constructor(_ctx, _createData, _createLanguageDocument) {
1470
1470
  this._ctx = _ctx;
1471
+ this._createData = _createData;
1471
1472
  this._createLanguageDocument = _createLanguageDocument;
1472
1473
  }
1473
- _documentCache = /* @__PURE__ */ new Map();
1474
- _fsEntries;
1474
+ #documentCache = /* @__PURE__ */ new Map();
1475
+ #fs;
1475
1476
  get hasFileSystemProvider() {
1476
- return !!this._fsEntries;
1477
+ return !!this._createData.workspace;
1477
1478
  }
1478
1479
  get host() {
1479
1480
  return this._ctx.host;
@@ -1506,17 +1507,17 @@ var WorkerBase = class {
1506
1507
  if (!model) {
1507
1508
  return null;
1508
1509
  }
1509
- const cached = this._documentCache.get(uri);
1510
+ const cached = this.#documentCache.get(uri);
1510
1511
  if (cached && cached[0] === model.version) {
1511
1512
  return cached[1];
1512
1513
  }
1513
1514
  const document2 = TextDocument2.create(uri, "-", model.version, model.getValue());
1514
- this._documentCache.set(uri, [model.version, document2, void 0]);
1515
+ this.#documentCache.set(uri, [model.version, document2, void 0]);
1515
1516
  return document2;
1516
1517
  }
1517
1518
  getLanguageDocument(document2) {
1518
1519
  const { uri, version } = document2;
1519
- const cached = this._documentCache.get(uri);
1520
+ const cached = this.#documentCache.get(uri);
1520
1521
  if (cached && cached[0] === version && cached[2]) {
1521
1522
  return cached[2];
1522
1523
  }
@@ -1524,14 +1525,13 @@ var WorkerBase = class {
1524
1525
  throw new Error("createLanguageDocument is not provided");
1525
1526
  }
1526
1527
  const languageDocument = this._createLanguageDocument(document2);
1527
- this._documentCache.set(uri, [version, document2, languageDocument]);
1528
+ this.#documentCache.set(uri, [version, document2, languageDocument]);
1528
1529
  return languageDocument;
1529
1530
  }
1530
1531
  readDir(uri, extensions) {
1531
1532
  const entries = [];
1532
- const fsTree = this._fsEntries;
1533
- if (fsTree) {
1534
- for (const [path, type] of fsTree) {
1533
+ if (this.#fs) {
1534
+ for (const [path, type] of this.#fs) {
1535
1535
  if (path.startsWith(uri)) {
1536
1536
  const name = path.slice(uri.length);
1537
1537
  if (!name.includes("/")) {
@@ -1567,30 +1567,25 @@ var WorkerBase = class {
1567
1567
  resolveReference(ref, baseUrl) {
1568
1568
  const url = new URL(ref, baseUrl);
1569
1569
  const href = url.href;
1570
- if (url.protocol === "file:" && this._fsEntries) {
1571
- if (!this._fsEntries.has(href)) {
1572
- return void 0;
1573
- }
1570
+ if (url.protocol === "file:" && url.pathname !== "/" && this.#fs && !this.#fs.has(href.endsWith("/") ? href.slice(0, -1) : href)) {
1571
+ return void 0;
1574
1572
  }
1575
1573
  return href;
1576
1574
  }
1577
1575
  // #region methods used by the host
1578
1576
  async removeDocumentCache(uri) {
1579
- this._documentCache.delete(uri);
1580
- }
1581
- async syncFSEntries(entries) {
1582
- this._fsEntries = new Map(entries);
1577
+ this.#documentCache.delete(uri);
1583
1578
  }
1584
1579
  async fsNotify(kind, path, type) {
1585
1580
  const url = "file://" + path;
1586
- const entries = this._fsEntries ?? (this._fsEntries = /* @__PURE__ */ new Map());
1581
+ const entries = this.#fs ?? (this.#fs = /* @__PURE__ */ new Map());
1587
1582
  if (kind === "create") {
1588
1583
  if (type) {
1589
1584
  entries.set(url, type);
1590
1585
  }
1591
1586
  } else if (kind === "remove") {
1592
1587
  if (entries.get(url) === 1 /* File */) {
1593
- this._documentCache.delete(url);
1588
+ this.#documentCache.delete(url);
1594
1589
  }
1595
1590
  entries.delete(url);
1596
1591
  }
@@ -1599,17 +1594,17 @@ var WorkerBase = class {
1599
1594
  };
1600
1595
 
1601
1596
  // src/lsp/typescript/worker.ts
1602
- import libs from "./libs.js";
1603
- import { cache } from "../../cache.js";
1604
- import { initializeWorker } from "../../editor-worker.js";
1597
+ import libs from "./libs.mjs";
1598
+ import { cache } from "../../cache.mjs";
1599
+ import { initializeWorker } from "../../editor-worker.mjs";
1605
1600
  var TypeScriptWorker = class extends WorkerBase {
1606
1601
  #compilerOptions;
1602
+ #languageService;
1607
1603
  #formatOptions;
1608
1604
  #importMap;
1609
1605
  #importMapVersion;
1610
1606
  #isBlankImportMap;
1611
1607
  #types;
1612
- #languageService = ts.createLanguageService(this);
1613
1608
  #urlMappings = /* @__PURE__ */ new Map();
1614
1609
  #typesMappings = /* @__PURE__ */ new Map();
1615
1610
  #httpLibs = /* @__PURE__ */ new Map();
@@ -1622,8 +1617,9 @@ var TypeScriptWorker = class extends WorkerBase {
1622
1617
  #fetchPromises = /* @__PURE__ */ new Map();
1623
1618
  #httpDocumentCache = /* @__PURE__ */ new Map();
1624
1619
  constructor(ctx, createData) {
1625
- super(ctx);
1620
+ super(ctx, createData);
1626
1621
  this.#compilerOptions = ts.convertCompilerOptionsFromJson(createData.compilerOptions, ".").options;
1622
+ this.#languageService = ts.createLanguageService(this);
1627
1623
  this.#importMap = createData.importMap;
1628
1624
  this.#importMapVersion = 0;
1629
1625
  this.#isBlankImportMap = isBlankImportMap(createData.importMap);