@tsrx/language-server 0.6.2 → 0.6.3

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.
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- const require_server = require('./server-y1HvqjeS.js');
2
+ const require_server = require('./server-CpRcmosU.js');
3
3
 
4
4
  //#region src/language-server.js
5
5
  require_server.createTsrxLanguageServer();
@@ -22685,6 +22685,7 @@ function typescript_notice(found, workspace_dirs) {
22685
22685
 
22686
22686
  //#endregion
22687
22687
  //#region src/autoInsertPlugin.js
22688
+ /** @import * as AST from 'estree' */
22688
22689
  /** @import { LanguageServicePlugin } from '@volar/language-server' */
22689
22690
  const { log: log$8 } = createLogging("[TSRX Auto-Insert Plugin]");
22690
22691
  /**
@@ -22710,6 +22711,12 @@ const VOID_ELEMENTS = /* @__PURE__ */ new Set([
22710
22711
  "wbr"
22711
22712
  ]);
22712
22713
  /**
22714
+ * Elements whose body is raw text, each with a pattern for its opening tag. The
22715
+ * body ends at the next `</style>` or `<\/script>`, wherever it is later in the
22716
+ * file, so that closing tag can belong to a later element (see `isUnclosed`).
22717
+ */
22718
+ const RAW_TEXT_OPENING_TAGS = /* @__PURE__ */ new Map([["style", /<style[\s/>]/], ["script", /<script[\s/>]/]]);
22719
+ /**
22713
22720
  * Auto-insert plugin for TSRX.
22714
22721
  * Handles auto-closing tags when typing '>' after a tag name. The VS Code extension
22715
22722
  * asks for them with Volar's `volar/client/autoInsert` request; other editors get
@@ -22786,7 +22793,8 @@ async provideAutoInsertSnippet(document, position, lastChange, _token) {
22786
22793
  return null;
22787
22794
  }
22788
22795
  const closingTag = `</${tagName}>`;
22789
- if (sourceCode.startsWith(closingTag, sourceOffset + 1)) {
22796
+ const element = virtualCode.isDotCompletionMode ? null : findElement(virtualCode.sourceAst, sourceOffset + 1);
22797
+ if (element ? !isUnclosed(element, sourceCode) : sourceCode.startsWith(closingTag, sourceOffset + 1)) {
22790
22798
  log$8("Closing tag already exists, skipping");
22791
22799
  return null;
22792
22800
  }
@@ -22797,6 +22805,73 @@ async provideAutoInsertSnippet(document, position, lastChange, _token) {
22797
22805
  };
22798
22806
  }
22799
22807
  /**
22808
+ * An element of the parsed source and, when the element is one of its children,
22809
+ * the element that holds it.
22810
+ * @typedef {{ element: AST.TSRXJSXElement; parent: ElementPath | null }} ElementPath
22811
+ */
22812
+ /**
22813
+ * Find the element whose opening tag ends at `end` in the parsed source.
22814
+ * @param {unknown} node
22815
+ * @param {number} end - The offset right after the opening tag's `>`
22816
+ * @param {ElementPath | null} [parent] - The element whose `children` hold `node`
22817
+ * @returns {ElementPath | null}
22818
+ */
22819
+ function findElement(node, end, parent = null) {
22820
+ if (Array.isArray(node)) {
22821
+ for (const child of node) {
22822
+ const found = findElement(child, end, parent);
22823
+ if (found) return found;
22824
+ }
22825
+ return null;
22826
+ }
22827
+ if (!node || typeof node !== "object") return null;
22828
+ const element = node;
22829
+ const path = element.openingElement ? {
22830
+ element,
22831
+ parent
22832
+ } : null;
22833
+ if (path?.element.openingElement.end === end) return path;
22834
+ for (const [key, value] of Object.entries(node)) {
22835
+ if (key === "loc" || key === "metadata") continue;
22836
+ const found = findElement(value, end, key === "children" ? path : null);
22837
+ if (found) return found;
22838
+ }
22839
+ return null;
22840
+ }
22841
+ /**
22842
+ * Whether the element still needs its closing tag, decided like TypeScript's
22843
+ * `isUnclosedTag`: the element is unclosed, or it took the closing tag of a parent
22844
+ * with the same name (`<div>` typed as the first child of a `<div>`), which is then
22845
+ * the unclosed one. Loose-mode recovery marks an element without its closing tag
22846
+ * `unclosed`.
22847
+ *
22848
+ * A `<style>` or `<script>` body ends at the next closing tag with its name, which
22849
+ * can belong to a later element: a new `<style>` typed in one component takes the
22850
+ * `</style>` of the style block in the next component. That element's opening tag
22851
+ * is then in the body, so the new element still needs its own closing tag.
22852
+ * @param {ElementPath} path
22853
+ * @param {string} sourceCode
22854
+ * @returns {boolean}
22855
+ */
22856
+ function isUnclosed(path, sourceCode) {
22857
+ /** @param {AST.TSRXJSXElement} element */
22858
+ const name = (element) => {
22859
+ const { start, end } = element.openingElement.name;
22860
+ return sourceCode.slice(start, end);
22861
+ };
22862
+ const { element } = path;
22863
+ const rawTextOpeningTag = RAW_TEXT_OPENING_TAGS.get(name(element));
22864
+ if (rawTextOpeningTag) {
22865
+ if (element.unclosed || !element.closingElement) return true;
22866
+ const bodyEnd = element.closingElement.start;
22867
+ return rawTextOpeningTag.test(sourceCode.slice(element.openingElement.end, bodyEnd));
22868
+ }
22869
+ for (let current = path;; current = current.parent) {
22870
+ if (current.element.unclosed) return true;
22871
+ if (!current.parent || name(current.parent.element) !== name(current.element)) return false;
22872
+ }
22873
+ }
22874
+ /**
22800
22875
  * Escape `text` so that a snippet inserts it as written. `\`, `$` and `}` are snippet
22801
22876
  * syntax: unescaped, the `$Foo` in `</$Foo>` is a variable, which VS Code replaces
22802
22877
  * with nothing.
@@ -25427,4 +25502,4 @@ Object.defineProperty(exports, 'createTsrxLanguageServer', {
25427
25502
  return createTsrxLanguageServer;
25428
25503
  }
25429
25504
  });
25430
- //# sourceMappingURL=server-y1HvqjeS.js.map
25505
+ //# sourceMappingURL=server-CpRcmosU.js.map