@codemirror/language 6.3.1 → 6.4.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## 6.4.0 (2023-01-12)
2
+
3
+ ### New features
4
+
5
+ The `bracketMatchingHandle` node prop can now be used to limit bracket matching behavior for larger nodes to a single subnode (for example the tag name of an HTML tag).
6
+
7
+ ## 6.3.2 (2022-12-16)
8
+
9
+ ### Bug fixes
10
+
11
+ Fix a bug that caused `ensureSyntaxTree` to return incomplete trees when using a viewport-aware parser like `StreamLanguage`.
12
+
1
13
  ## 6.3.1 (2022-11-14)
2
14
 
3
15
  ### Bug fixes
package/dist/index.cjs CHANGED
@@ -183,7 +183,13 @@ up to that point if the tree isn't already available.
183
183
  function ensureSyntaxTree(state, upto, timeout = 50) {
184
184
  var _a;
185
185
  let parse = (_a = state.field(Language.state, false)) === null || _a === void 0 ? void 0 : _a.context;
186
- return !parse ? null : parse.isDone(upto) || parse.work(timeout, upto) ? parse.tree : null;
186
+ if (!parse)
187
+ return null;
188
+ let oldVieport = parse.viewport;
189
+ parse.updateViewport({ from: 0, to: upto });
190
+ let result = parse.isDone(upto) || parse.work(timeout, upto) ? parse.tree : null;
191
+ parse.updateViewport(oldVieport);
192
+ return result;
187
193
  }
188
194
  /**
189
195
  Queries whether there is a full syntax tree available up to the
@@ -1682,7 +1688,7 @@ A default highlight style (works well with light themes).
1682
1688
  */
1683
1689
  const defaultHighlightStyle = HighlightStyle.define([
1684
1690
  { tag: highlight.tags.meta,
1685
- color: "#7a757a" },
1691
+ color: "#404740" },
1686
1692
  { tag: highlight.tags.link,
1687
1693
  textDecoration: "underline" },
1688
1694
  { tag: highlight.tags.heading,
@@ -1781,6 +1787,15 @@ highlighting style is used to indicate this.
1781
1787
  function bracketMatching(config = {}) {
1782
1788
  return [bracketMatchingConfig.of(config), bracketMatchingUnique];
1783
1789
  }
1790
+ /**
1791
+ When larger syntax nodes, such as HTML tags, are marked as
1792
+ opening/closing, it can be a bit messy to treat the whole node as
1793
+ a matchable bracket. This node prop allows you to define, for such
1794
+ a node, a ‘handle’—the part of the node that is highlighted, and
1795
+ that the cursor must be on to activate highlighting in the first
1796
+ place.
1797
+ */
1798
+ const bracketMatchingHandle = new common.NodeProp();
1784
1799
  function matchingNodes(node, dir, brackets) {
1785
1800
  let byProp = node.prop(dir < 0 ? common.NodeProp.openedBy : common.NodeProp.closedBy);
1786
1801
  if (byProp)
@@ -1792,6 +1807,10 @@ function matchingNodes(node, dir, brackets) {
1792
1807
  }
1793
1808
  return null;
1794
1809
  }
1810
+ function findHandle(node) {
1811
+ let hasHandle = node.type.prop(bracketMatchingHandle);
1812
+ return hasHandle ? hasHandle(node.node) : node;
1813
+ }
1795
1814
  /**
1796
1815
  Find the matching bracket for the token at `pos`, scanning
1797
1816
  direction `dir`. Only the `brackets` and `maxScanDistance`
@@ -1803,30 +1822,36 @@ function matchBrackets(state, pos, dir, config = {}) {
1803
1822
  let tree = syntaxTree(state), node = tree.resolveInner(pos, dir);
1804
1823
  for (let cur = node; cur; cur = cur.parent) {
1805
1824
  let matches = matchingNodes(cur.type, dir, brackets);
1806
- if (matches && cur.from < cur.to)
1807
- return matchMarkedBrackets(state, pos, dir, cur, matches, brackets);
1825
+ if (matches && cur.from < cur.to) {
1826
+ let handle = findHandle(cur);
1827
+ if (handle && (dir > 0 ? pos >= handle.from && pos < handle.to : pos > handle.from && pos <= handle.to))
1828
+ return matchMarkedBrackets(state, pos, dir, cur, handle, matches, brackets);
1829
+ }
1808
1830
  }
1809
1831
  return matchPlainBrackets(state, pos, dir, tree, node.type, maxScanDistance, brackets);
1810
1832
  }
1811
- function matchMarkedBrackets(_state, _pos, dir, token, matching, brackets) {
1812
- let parent = token.parent, firstToken = { from: token.from, to: token.to };
1833
+ function matchMarkedBrackets(_state, _pos, dir, token, handle, matching, brackets) {
1834
+ let parent = token.parent, firstToken = { from: handle.from, to: handle.to };
1813
1835
  let depth = 0, cursor = parent === null || parent === void 0 ? void 0 : parent.cursor();
1814
1836
  if (cursor && (dir < 0 ? cursor.childBefore(token.from) : cursor.childAfter(token.to)))
1815
1837
  do {
1816
1838
  if (dir < 0 ? cursor.to <= token.from : cursor.from >= token.to) {
1817
1839
  if (depth == 0 && matching.indexOf(cursor.type.name) > -1 && cursor.from < cursor.to) {
1818
- return { start: firstToken, end: { from: cursor.from, to: cursor.to }, matched: true };
1840
+ let endHandle = findHandle(cursor);
1841
+ return { start: firstToken, end: endHandle ? { from: endHandle.from, to: endHandle.to } : undefined, matched: true };
1819
1842
  }
1820
1843
  else if (matchingNodes(cursor.type, dir, brackets)) {
1821
1844
  depth++;
1822
1845
  }
1823
1846
  else if (matchingNodes(cursor.type, -dir, brackets)) {
1824
- if (depth == 0)
1847
+ if (depth == 0) {
1848
+ let endHandle = findHandle(cursor);
1825
1849
  return {
1826
1850
  start: firstToken,
1827
- end: cursor.from == cursor.to ? undefined : { from: cursor.from, to: cursor.to },
1851
+ end: endHandle && endHandle.from < endHandle.to ? { from: endHandle.from, to: endHandle.to } : undefined,
1828
1852
  matched: false
1829
1853
  };
1854
+ }
1830
1855
  depth--;
1831
1856
  }
1832
1857
  }
@@ -2398,6 +2423,7 @@ exports.StreamLanguage = StreamLanguage;
2398
2423
  exports.StringStream = StringStream;
2399
2424
  exports.TreeIndentContext = TreeIndentContext;
2400
2425
  exports.bracketMatching = bracketMatching;
2426
+ exports.bracketMatchingHandle = bracketMatchingHandle;
2401
2427
  exports.codeFolding = codeFolding;
2402
2428
  exports.continuedIndent = continuedIndent;
2403
2429
  exports.defaultHighlightStyle = defaultHighlightStyle;
package/dist/index.d.ts CHANGED
@@ -889,6 +889,15 @@ highlighting style is used to indicate this.
889
889
  */
890
890
  declare function bracketMatching(config?: Config): Extension;
891
891
  /**
892
+ When larger syntax nodes, such as HTML tags, are marked as
893
+ opening/closing, it can be a bit messy to treat the whole node as
894
+ a matchable bracket. This node prop allows you to define, for such
895
+ a node, a ‘handle’—the part of the node that is highlighted, and
896
+ that the cursor must be on to activate highlighting in the first
897
+ place.
898
+ */
899
+ declare const bracketMatchingHandle: NodeProp<(node: SyntaxNode) => SyntaxNode | null>;
900
+ /**
892
901
  The result returned from `matchBrackets`.
893
902
  */
894
903
  interface MatchResult {
@@ -1103,4 +1112,4 @@ declare class StreamLanguage<State> extends Language {
1103
1112
  get allowsNesting(): boolean;
1104
1113
  }
1105
1114
 
1106
- export { Config, HighlightStyle, IndentContext, LRLanguage, Language, LanguageDescription, LanguageSupport, MatchResult, ParseContext, StreamLanguage, StreamParser, StringStream, TagStyle, TreeIndentContext, bracketMatching, codeFolding, continuedIndent, defaultHighlightStyle, defineLanguageFacet, delimitedIndent, ensureSyntaxTree, flatIndent, foldAll, foldCode, foldEffect, foldGutter, foldInside, foldKeymap, foldNodeProp, foldService, foldState, foldable, foldedRanges, forceParsing, getIndentUnit, getIndentation, highlightingFor, indentNodeProp, indentOnInput, indentRange, indentService, indentString, indentUnit, language, languageDataProp, matchBrackets, syntaxHighlighting, syntaxParserRunning, syntaxTree, syntaxTreeAvailable, unfoldAll, unfoldCode, unfoldEffect };
1115
+ export { Config, HighlightStyle, IndentContext, LRLanguage, Language, LanguageDescription, LanguageSupport, MatchResult, ParseContext, StreamLanguage, StreamParser, StringStream, TagStyle, TreeIndentContext, bracketMatching, bracketMatchingHandle, codeFolding, continuedIndent, defaultHighlightStyle, defineLanguageFacet, delimitedIndent, ensureSyntaxTree, flatIndent, foldAll, foldCode, foldEffect, foldGutter, foldInside, foldKeymap, foldNodeProp, foldService, foldState, foldable, foldedRanges, forceParsing, getIndentUnit, getIndentation, highlightingFor, indentNodeProp, indentOnInput, indentRange, indentService, indentString, indentUnit, language, languageDataProp, matchBrackets, syntaxHighlighting, syntaxParserRunning, syntaxTree, syntaxTreeAvailable, unfoldAll, unfoldCode, unfoldEffect };
package/dist/index.js CHANGED
@@ -179,7 +179,13 @@ up to that point if the tree isn't already available.
179
179
  function ensureSyntaxTree(state, upto, timeout = 50) {
180
180
  var _a;
181
181
  let parse = (_a = state.field(Language.state, false)) === null || _a === void 0 ? void 0 : _a.context;
182
- return !parse ? null : parse.isDone(upto) || parse.work(timeout, upto) ? parse.tree : null;
182
+ if (!parse)
183
+ return null;
184
+ let oldVieport = parse.viewport;
185
+ parse.updateViewport({ from: 0, to: upto });
186
+ let result = parse.isDone(upto) || parse.work(timeout, upto) ? parse.tree : null;
187
+ parse.updateViewport(oldVieport);
188
+ return result;
183
189
  }
184
190
  /**
185
191
  Queries whether there is a full syntax tree available up to the
@@ -1678,7 +1684,7 @@ A default highlight style (works well with light themes).
1678
1684
  */
1679
1685
  const defaultHighlightStyle = /*@__PURE__*/HighlightStyle.define([
1680
1686
  { tag: tags.meta,
1681
- color: "#7a757a" },
1687
+ color: "#404740" },
1682
1688
  { tag: tags.link,
1683
1689
  textDecoration: "underline" },
1684
1690
  { tag: tags.heading,
@@ -1777,6 +1783,15 @@ highlighting style is used to indicate this.
1777
1783
  function bracketMatching(config = {}) {
1778
1784
  return [bracketMatchingConfig.of(config), bracketMatchingUnique];
1779
1785
  }
1786
+ /**
1787
+ When larger syntax nodes, such as HTML tags, are marked as
1788
+ opening/closing, it can be a bit messy to treat the whole node as
1789
+ a matchable bracket. This node prop allows you to define, for such
1790
+ a node, a ‘handle’—the part of the node that is highlighted, and
1791
+ that the cursor must be on to activate highlighting in the first
1792
+ place.
1793
+ */
1794
+ const bracketMatchingHandle = /*@__PURE__*/new NodeProp();
1780
1795
  function matchingNodes(node, dir, brackets) {
1781
1796
  let byProp = node.prop(dir < 0 ? NodeProp.openedBy : NodeProp.closedBy);
1782
1797
  if (byProp)
@@ -1788,6 +1803,10 @@ function matchingNodes(node, dir, brackets) {
1788
1803
  }
1789
1804
  return null;
1790
1805
  }
1806
+ function findHandle(node) {
1807
+ let hasHandle = node.type.prop(bracketMatchingHandle);
1808
+ return hasHandle ? hasHandle(node.node) : node;
1809
+ }
1791
1810
  /**
1792
1811
  Find the matching bracket for the token at `pos`, scanning
1793
1812
  direction `dir`. Only the `brackets` and `maxScanDistance`
@@ -1799,30 +1818,36 @@ function matchBrackets(state, pos, dir, config = {}) {
1799
1818
  let tree = syntaxTree(state), node = tree.resolveInner(pos, dir);
1800
1819
  for (let cur = node; cur; cur = cur.parent) {
1801
1820
  let matches = matchingNodes(cur.type, dir, brackets);
1802
- if (matches && cur.from < cur.to)
1803
- return matchMarkedBrackets(state, pos, dir, cur, matches, brackets);
1821
+ if (matches && cur.from < cur.to) {
1822
+ let handle = findHandle(cur);
1823
+ if (handle && (dir > 0 ? pos >= handle.from && pos < handle.to : pos > handle.from && pos <= handle.to))
1824
+ return matchMarkedBrackets(state, pos, dir, cur, handle, matches, brackets);
1825
+ }
1804
1826
  }
1805
1827
  return matchPlainBrackets(state, pos, dir, tree, node.type, maxScanDistance, brackets);
1806
1828
  }
1807
- function matchMarkedBrackets(_state, _pos, dir, token, matching, brackets) {
1808
- let parent = token.parent, firstToken = { from: token.from, to: token.to };
1829
+ function matchMarkedBrackets(_state, _pos, dir, token, handle, matching, brackets) {
1830
+ let parent = token.parent, firstToken = { from: handle.from, to: handle.to };
1809
1831
  let depth = 0, cursor = parent === null || parent === void 0 ? void 0 : parent.cursor();
1810
1832
  if (cursor && (dir < 0 ? cursor.childBefore(token.from) : cursor.childAfter(token.to)))
1811
1833
  do {
1812
1834
  if (dir < 0 ? cursor.to <= token.from : cursor.from >= token.to) {
1813
1835
  if (depth == 0 && matching.indexOf(cursor.type.name) > -1 && cursor.from < cursor.to) {
1814
- return { start: firstToken, end: { from: cursor.from, to: cursor.to }, matched: true };
1836
+ let endHandle = findHandle(cursor);
1837
+ return { start: firstToken, end: endHandle ? { from: endHandle.from, to: endHandle.to } : undefined, matched: true };
1815
1838
  }
1816
1839
  else if (matchingNodes(cursor.type, dir, brackets)) {
1817
1840
  depth++;
1818
1841
  }
1819
1842
  else if (matchingNodes(cursor.type, -dir, brackets)) {
1820
- if (depth == 0)
1843
+ if (depth == 0) {
1844
+ let endHandle = findHandle(cursor);
1821
1845
  return {
1822
1846
  start: firstToken,
1823
- end: cursor.from == cursor.to ? undefined : { from: cursor.from, to: cursor.to },
1847
+ end: endHandle && endHandle.from < endHandle.to ? { from: endHandle.from, to: endHandle.to } : undefined,
1824
1848
  matched: false
1825
1849
  };
1850
+ }
1826
1851
  depth--;
1827
1852
  }
1828
1853
  }
@@ -2383,4 +2408,4 @@ function docID(data) {
2383
2408
  return type;
2384
2409
  }
2385
2410
 
2386
- export { HighlightStyle, IndentContext, LRLanguage, Language, LanguageDescription, LanguageSupport, ParseContext, StreamLanguage, StringStream, TreeIndentContext, bracketMatching, codeFolding, continuedIndent, defaultHighlightStyle, defineLanguageFacet, delimitedIndent, ensureSyntaxTree, flatIndent, foldAll, foldCode, foldEffect, foldGutter, foldInside, foldKeymap, foldNodeProp, foldService, foldState, foldable, foldedRanges, forceParsing, getIndentUnit, getIndentation, highlightingFor, indentNodeProp, indentOnInput, indentRange, indentService, indentString, indentUnit, language, languageDataProp, matchBrackets, syntaxHighlighting, syntaxParserRunning, syntaxTree, syntaxTreeAvailable, unfoldAll, unfoldCode, unfoldEffect };
2411
+ export { HighlightStyle, IndentContext, LRLanguage, Language, LanguageDescription, LanguageSupport, ParseContext, StreamLanguage, StringStream, TreeIndentContext, bracketMatching, bracketMatchingHandle, codeFolding, continuedIndent, defaultHighlightStyle, defineLanguageFacet, delimitedIndent, ensureSyntaxTree, flatIndent, foldAll, foldCode, foldEffect, foldGutter, foldInside, foldKeymap, foldNodeProp, foldService, foldState, foldable, foldedRanges, forceParsing, getIndentUnit, getIndentation, highlightingFor, indentNodeProp, indentOnInput, indentRange, indentService, indentString, indentUnit, language, languageDataProp, matchBrackets, syntaxHighlighting, syntaxParserRunning, syntaxTree, syntaxTreeAvailable, unfoldAll, unfoldCode, unfoldEffect };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codemirror/language",
3
- "version": "6.3.1",
3
+ "version": "6.4.0",
4
4
  "description": "Language support infrastructure for the CodeMirror code editor",
5
5
  "scripts": {
6
6
  "test": "cm-runtests",