@marko/language-server 0.12.4 → 0.12.5

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/dist/index.js CHANGED
@@ -21,6 +21,7 @@ var import_vscode_languageserver_textdocument3 = require("vscode-languageserver-
21
21
  var import_util2 = require("util");
22
22
 
23
23
  // src/utils/compiler.ts
24
+ var import_vscode_uri2 = require("vscode-uri");
24
25
  var import_resolve_from = __toESM(require("resolve-from"));
25
26
  var import_lasso_package_root = __toESM(require("lasso-package-root"));
26
27
  var builtinCompiler = __toESM(require("@marko/compiler"));
@@ -30,7 +31,8 @@ var builtinTranslator = __toESM(require("@marko/translator-default"));
30
31
  var import_path = __toESM(require("path"));
31
32
  var import_vscode_uri = require("vscode-uri");
32
33
  function getDocDir(doc) {
33
- return import_path.default.dirname(getDocFile(doc));
34
+ const filename = getDocFile(doc);
35
+ return filename ? import_path.default.dirname(filename) : void 0;
34
36
  }
35
37
  function getDocFile(doc) {
36
38
  return import_vscode_uri.URI.parse(doc.uri).fsPath;
@@ -533,6 +535,12 @@ function isTransparentTag(node) {
533
535
  // src/utils/compiler.ts
534
536
  var lookupKey = Symbol("lookup");
535
537
  var compilerInfoByDir = /* @__PURE__ */ new Map();
538
+ var builtinInfo = {
539
+ cache: /* @__PURE__ */ new Map(),
540
+ lookup: builtinCompiler.taglib.buildLookup(__dirname, builtinTranslator),
541
+ compiler: builtinCompiler,
542
+ translator: builtinTranslator
543
+ };
536
544
  builtinCompiler.configure({ translator: builtinTranslator });
537
545
  function parse2(doc) {
538
546
  const compilerInfo = getCompilerInfo(doc);
@@ -545,6 +553,8 @@ function parse2(doc) {
545
553
  }
546
554
  function getCompilerInfo(doc) {
547
555
  const dir = getDocDir(doc);
556
+ if (!dir)
557
+ return builtinInfo;
548
558
  let info = compilerInfoByDir.get(dir);
549
559
  if (!info) {
550
560
  info = loadCompilerInfo(dir);
@@ -557,15 +567,19 @@ function setup(connection3, documents2) {
557
567
  clearAllCaches();
558
568
  });
559
569
  documents2.onDidChangeContent(({ document }) => {
560
- var _a;
561
570
  if (document.version > 1) {
562
- if (document.uri.endsWith(".marko")) {
563
- (_a = getCompilerInfo(document)) == null ? void 0 : _a.cache.delete(document);
571
+ if (document.languageId === "marko") {
572
+ getCompilerInfo(document).cache.delete(document);
564
573
  } else if (/[./\\]marko(?:-tag)?\.json$/.test(document.uri)) {
565
574
  clearAllCaches();
566
575
  }
567
576
  }
568
577
  });
578
+ documents2.onDidClose(({ document }) => {
579
+ if (document.languageId === "marko" && import_vscode_uri2.URI.parse(document.uri).scheme !== "file") {
580
+ getCompilerInfo(document).cache.delete(document);
581
+ }
582
+ });
569
583
  }
570
584
  function clearAllCaches() {
571
585
  for (const [, info] of compilerInfoByDir) {
@@ -601,7 +615,7 @@ function loadCompilerInfo(dir) {
601
615
  try {
602
616
  lookup = compiler.taglib.buildLookup(dir, translator);
603
617
  } catch {
604
- lookup = null;
618
+ lookup = builtinInfo.lookup;
605
619
  }
606
620
  cache2.set(lookupKey, lookup);
607
621
  }
@@ -674,7 +688,7 @@ ${closingTagStr}`
674
688
 
675
689
  // src/service/marko/complete/OpenTagName.ts
676
690
  var import_path2 = __toESM(require("path"));
677
- var import_vscode_uri2 = require("vscode-uri");
691
+ var import_vscode_uri3 = require("vscode-uri");
678
692
  var import_vscode_languageserver2 = require("vscode-languageserver");
679
693
  function OpenTagName({
680
694
  document,
@@ -682,8 +696,6 @@ function OpenTagName({
682
696
  parsed,
683
697
  node
684
698
  }) {
685
- if (!lookup)
686
- return;
687
699
  const currentTemplateFilePath = getDocFile(document);
688
700
  const tag = node.parent;
689
701
  const tagNameLocation = parsed.locationAt(node);
@@ -700,7 +712,7 @@ function OpenTagName({
700
712
  return tags.filter((it) => !it.deprecated).filter((it) => it.name !== "*").filter((it) => /^[^_]/.test(it.name) || !/\/node_modules\//.test(it.filePath)).map((it) => {
701
713
  let label = it.isNestedTag ? `@${it.name}` : it.name;
702
714
  const fileForTag = it.template || it.renderer || it.filePath;
703
- const fileURIForTag = import_vscode_uri2.URI.file(fileForTag).toString();
715
+ const fileURIForTag = import_vscode_uri3.URI.file(fileForTag).toString();
704
716
  const nodeModuleMatch = /\/node_modules\/((?:@[^/]+\/)?[^/]+)/.exec(fileForTag);
705
717
  const nodeModuleName = nodeModuleMatch && nodeModuleMatch[1];
706
718
  const isCoreTag = nodeModuleName === "marko";
@@ -708,7 +720,7 @@ function OpenTagName({
708
720
  kind: import_vscode_languageserver2.MarkupKind.Markdown,
709
721
  value: it.html ? `Built in [<${it.name}>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/${it.name}) HTML tag.` : nodeModuleName ? isCoreTag ? `Core Marko [<${it.name}>](${fileURIForTag}) tag.` : `Custom Marko tag discovered from the ["${nodeModuleName}"](${fileURIForTag}) npm package.` : `Custom Marko tag discovered from:
710
722
 
711
- [${import_path2.default.relative(currentTemplateFilePath, fileForTag)}](${fileURIForTag})`
723
+ [${currentTemplateFilePath ? import_path2.default.relative(currentTemplateFilePath, fileForTag) : currentTemplateFilePath}](${fileURIForTag})`
712
724
  };
713
725
  if (it.description) {
714
726
  documentation.value += `
@@ -772,8 +784,6 @@ function AttrName({
772
784
  name = name.slice(0, modifierIndex);
773
785
  }
774
786
  }
775
- if (!lookup)
776
- return;
777
787
  const completions = [];
778
788
  const attrNameLoc = parsed.locationAt(hasModifier ? {
779
789
  start: node.start,
@@ -871,31 +881,26 @@ var doComplete = async (doc, params) => {
871
881
  };
872
882
 
873
883
  // src/service/marko/validate.ts
874
- var import_vscode_uri3 = require("vscode-uri");
875
884
  var import_vscode_languageserver5 = require("vscode-languageserver");
876
885
  var markoErrorRegExp = /^(.+?)(?:\((\d+)(?:\s*,\s*(\d+))?\))?: (.*)$/gm;
877
886
  var doValidate = (doc) => {
878
- const { fsPath, scheme } = import_vscode_uri3.URI.parse(doc.uri);
887
+ const fsPath = getDocFile(doc);
879
888
  const diagnostics = [];
880
- if (scheme === "file") {
881
- const { compiler, translator, cache: cache2, lookup } = getCompilerInfo(doc);
882
- if (lookup) {
883
- try {
884
- compiler.compileSync(doc.getText(), fsPath, {
885
- cache: cache2,
886
- output: "source",
887
- code: false,
888
- translator
889
- });
890
- } catch (e) {
891
- let match;
892
- while (match = markoErrorRegExp.exec(e.message)) {
893
- const [, fileName, rawLine, rawCol, msg] = match;
894
- const line = (parseInt(rawLine, 10) || 1) - 1;
895
- const col = (parseInt(rawCol, 10) || 1) - 1;
896
- diagnostics.push(import_vscode_languageserver5.Diagnostic.create(import_vscode_languageserver5.Range.create(line, col, line, col), msg, import_vscode_languageserver5.DiagnosticSeverity.Error, void 0, fileName));
897
- }
898
- }
889
+ const { compiler, translator, cache: cache2 } = getCompilerInfo(doc);
890
+ try {
891
+ compiler.compileSync(doc.getText(), fsPath || "untitled.marko", {
892
+ cache: cache2,
893
+ output: "source",
894
+ code: false,
895
+ translator
896
+ });
897
+ } catch (e) {
898
+ let match;
899
+ while (match = markoErrorRegExp.exec(e.message)) {
900
+ const [, fileName, rawLine, rawCol, msg] = match;
901
+ const line = (parseInt(rawLine, 10) || 1) - 1;
902
+ const col = (parseInt(rawCol, 10) || 1) - 1;
903
+ diagnostics.push(import_vscode_languageserver5.Diagnostic.create(import_vscode_languageserver5.Range.create(line, col, line, col), msg, import_vscode_languageserver5.DiagnosticSeverity.Error, void 0, fileName));
899
904
  }
900
905
  }
901
906
  return diagnostics;
@@ -948,8 +953,6 @@ function OpenTagName2({
948
953
  parsed,
949
954
  node
950
955
  }) {
951
- if (!lookup)
952
- return;
953
956
  const tag = node.parent;
954
957
  let tagDef;
955
958
  let range = START_OF_FILE;
@@ -988,8 +991,6 @@ function AttrName2({
988
991
  parsed,
989
992
  node
990
993
  }) {
991
- if (!lookup)
992
- return;
993
994
  const tagName = node.parent.parent.nameText;
994
995
  const attrName = parsed.read(node);
995
996
  if (attrName[0] === "{")
@@ -1255,7 +1256,7 @@ var stylesheet_default = {
1255
1256
  if (generatedOffset === void 0)
1256
1257
  continue;
1257
1258
  const { service: service2, virtualDoc } = info;
1258
- const result = service2.doComplete(virtualDoc, virtualDoc.positionAt(generatedOffset), service2.parseStylesheet(virtualDoc));
1259
+ const result = service2.doComplete(virtualDoc, virtualDoc.positionAt(generatedOffset), info.parsed);
1259
1260
  for (const item of result.items) {
1260
1261
  if (item.additionalTextEdits) {
1261
1262
  for (const edit of item.additionalTextEdits) {
@@ -1294,13 +1295,28 @@ var stylesheet_default = {
1294
1295
  if (generatedOffset === void 0)
1295
1296
  continue;
1296
1297
  const { service: service2, virtualDoc } = info;
1297
- const result = service2.findDefinition(virtualDoc, virtualDoc.positionAt(generatedOffset), service2.parseStylesheet(virtualDoc));
1298
+ const result = service2.findDefinition(virtualDoc, virtualDoc.positionAt(generatedOffset), info.parsed);
1298
1299
  if (result && updateRange(doc, info, result.range)) {
1299
1300
  return result;
1300
1301
  }
1301
1302
  break;
1302
1303
  }
1303
1304
  },
1305
+ async doHover(doc, params) {
1306
+ const infoByExt = getStyleSheetInfo(doc);
1307
+ const sourceOffset = doc.offsetAt(params.position);
1308
+ for (const ext in infoByExt) {
1309
+ const info = infoByExt[ext];
1310
+ const generatedOffset = info.generatedOffsetAt(sourceOffset);
1311
+ if (generatedOffset === void 0)
1312
+ continue;
1313
+ const { service: service2, virtualDoc } = info;
1314
+ const result = service2.doHover(virtualDoc, virtualDoc.positionAt(generatedOffset), service2.parseStylesheet(virtualDoc));
1315
+ if (result && (!result.range || updateRange(doc, info, result.range))) {
1316
+ return result;
1317
+ }
1318
+ }
1319
+ },
1304
1320
  async doValidate(doc) {
1305
1321
  const infoByExt = getStyleSheetInfo(doc);
1306
1322
  const result = [];
@@ -1406,6 +1422,20 @@ var service = {
1406
1422
  }
1407
1423
  return result;
1408
1424
  },
1425
+ async doHover(doc, params, cancel) {
1426
+ var _a;
1427
+ try {
1428
+ for (const plugin of plugins) {
1429
+ const result = await ((_a = plugin.doHover) == null ? void 0 : _a.call(plugin, doc, params, cancel));
1430
+ if (cancel.isCancellationRequested)
1431
+ return;
1432
+ if (result)
1433
+ return result;
1434
+ }
1435
+ } catch (err) {
1436
+ displayError(err);
1437
+ }
1438
+ },
1409
1439
  async doValidate(doc) {
1410
1440
  const result = [];
1411
1441
  try {
@@ -1450,6 +1480,7 @@ connection2.onInitialize(() => {
1450
1480
  textDocumentSync: import_node.TextDocumentSyncKind.Incremental,
1451
1481
  documentFormattingProvider: true,
1452
1482
  definitionProvider: true,
1483
+ hoverProvider: true,
1453
1484
  completionProvider: {
1454
1485
  triggerCharacters: [
1455
1486
  ".",
@@ -1488,6 +1519,9 @@ connection2.onCompletion(async (params, cancel) => {
1488
1519
  connection2.onDefinition(async (params, cancel) => {
1489
1520
  return await service.findDefinition(documents.get(params.textDocument.uri), params, cancel) || null;
1490
1521
  });
1522
+ connection2.onHover(async (params, cancel) => {
1523
+ return await service.doHover(documents.get(params.textDocument.uri), params, cancel) || null;
1524
+ });
1491
1525
  connection2.onDocumentFormatting(async (params, cancel) => {
1492
1526
  return await service.format(documents.get(params.textDocument.uri), params, cancel) || null;
1493
1527
  });