@codemirror/language 6.9.3 → 6.10.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/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## 6.10.1 (2024-02-02)
2
+
3
+ ### Bug fixes
4
+
5
+ Fix an issue where, when a lot of code is visible in the initial editor, the bottom bit of code is shown without highlighting for one frame.
6
+
7
+ ## 6.10.0 (2023-12-28)
8
+
9
+ ### New features
10
+
11
+ The new `bidiIsolates` extension can be used to wrap syntactic elements where this is appropriate in an element that isolates their text direction, avoiding weird ordering of neutral characters on direction boundaries.
12
+
1
13
  ## 6.9.3 (2023-11-27)
2
14
 
3
15
  ### Bug fixes
package/dist/index.cjs CHANGED
@@ -1751,16 +1751,20 @@ class TreeHighlighter {
1751
1751
  this.markCache = Object.create(null);
1752
1752
  this.tree = syntaxTree(view.state);
1753
1753
  this.decorations = this.buildDeco(view, getHighlighters(view.state));
1754
+ this.decoratedTo = view.viewport.to;
1754
1755
  }
1755
1756
  update(update) {
1756
1757
  let tree = syntaxTree(update.state), highlighters = getHighlighters(update.state);
1757
1758
  let styleChange = highlighters != getHighlighters(update.startState);
1758
- if (tree.length < update.view.viewport.to && !styleChange && tree.type == this.tree.type) {
1759
+ let { viewport } = update.view, decoratedToMapped = update.changes.mapPos(this.decoratedTo, 1);
1760
+ if (tree.length < viewport.to && !styleChange && tree.type == this.tree.type && decoratedToMapped >= viewport.to) {
1759
1761
  this.decorations = this.decorations.map(update.changes);
1762
+ this.decoratedTo = decoratedToMapped;
1760
1763
  }
1761
1764
  else if (tree != this.tree || update.viewportChanged || styleChange) {
1762
1765
  this.tree = tree;
1763
1766
  this.decorations = this.buildDeco(update.view, highlighters);
1767
+ this.decoratedTo = viewport.to;
1764
1768
  }
1765
1769
  }
1766
1770
  buildDeco(view$1, highlighters) {
@@ -2531,6 +2535,117 @@ function docID(data) {
2531
2535
  return type;
2532
2536
  }
2533
2537
 
2538
+ function buildForLine(line) {
2539
+ return line.length <= 4096 && /[\u0590-\u05f4\u0600-\u06ff\u0700-\u08ac\ufb50-\ufdff]/.test(line);
2540
+ }
2541
+ function textHasRTL(text) {
2542
+ for (let i = text.iter(); !i.next().done;)
2543
+ if (buildForLine(i.value))
2544
+ return true;
2545
+ return false;
2546
+ }
2547
+ function changeAddsRTL(change) {
2548
+ let added = false;
2549
+ change.iterChanges((fA, tA, fB, tB, ins) => {
2550
+ if (!added && textHasRTL(ins))
2551
+ added = true;
2552
+ });
2553
+ return added;
2554
+ }
2555
+ const alwaysIsolate = state.Facet.define({ combine: values => values.some(x => x) });
2556
+ /**
2557
+ Make sure nodes
2558
+ [marked](https://lezer.codemirror.net/docs/ref/#common.NodeProp^isolate)
2559
+ as isolating for bidirectional text are rendered in a way that
2560
+ isolates them from the surrounding text.
2561
+ */
2562
+ function bidiIsolates(options = {}) {
2563
+ let extensions = [isolateMarks];
2564
+ if (options.alwaysIsolate)
2565
+ extensions.push(alwaysIsolate.of(true));
2566
+ return extensions;
2567
+ }
2568
+ const isolateMarks = view.ViewPlugin.fromClass(class {
2569
+ constructor(view$1) {
2570
+ this.always = view$1.state.facet(alwaysIsolate) ||
2571
+ view$1.textDirection != view.Direction.LTR ||
2572
+ view$1.state.facet(view.EditorView.perLineTextDirection);
2573
+ this.hasRTL = !this.always && textHasRTL(view$1.state.doc);
2574
+ this.tree = syntaxTree(view$1.state);
2575
+ this.decorations = this.always || this.hasRTL ? buildDeco(view$1, this.tree, this.always) : view.Decoration.none;
2576
+ }
2577
+ update(update) {
2578
+ let always = update.state.facet(alwaysIsolate) ||
2579
+ update.view.textDirection != view.Direction.LTR ||
2580
+ update.state.facet(view.EditorView.perLineTextDirection);
2581
+ if (!always && !this.hasRTL && changeAddsRTL(update.changes))
2582
+ this.hasRTL = true;
2583
+ if (!always && !this.hasRTL)
2584
+ return;
2585
+ let tree = syntaxTree(update.state);
2586
+ if (always != this.always || tree != this.tree || update.docChanged || update.viewportChanged) {
2587
+ this.tree = tree;
2588
+ this.always = always;
2589
+ this.decorations = buildDeco(update.view, tree, always);
2590
+ }
2591
+ }
2592
+ }, {
2593
+ provide: plugin => {
2594
+ function access(view$1) {
2595
+ var _a, _b;
2596
+ return (_b = (_a = view$1.plugin(plugin)) === null || _a === void 0 ? void 0 : _a.decorations) !== null && _b !== void 0 ? _b : view.Decoration.none;
2597
+ }
2598
+ return [view.EditorView.outerDecorations.of(access),
2599
+ state.Prec.lowest(view.EditorView.bidiIsolatedRanges.of(access))];
2600
+ }
2601
+ });
2602
+ function buildDeco(view, tree, always) {
2603
+ let deco = new state.RangeSetBuilder();
2604
+ let ranges = view.visibleRanges;
2605
+ if (!always)
2606
+ ranges = clipRTLLines(ranges, view.state.doc);
2607
+ for (let { from, to } of ranges) {
2608
+ tree.iterate({
2609
+ enter: node => {
2610
+ let iso = node.type.prop(common.NodeProp.isolate);
2611
+ if (iso)
2612
+ deco.add(node.from, node.to, marks[iso]);
2613
+ },
2614
+ from, to
2615
+ });
2616
+ }
2617
+ return deco.finish();
2618
+ }
2619
+ function clipRTLLines(ranges, doc) {
2620
+ let cur = doc.iter(), pos = 0, result = [], last = null;
2621
+ for (let { from, to } of ranges) {
2622
+ if (from != pos) {
2623
+ if (pos < from)
2624
+ cur.next(from - pos);
2625
+ pos = from;
2626
+ }
2627
+ for (;;) {
2628
+ let start = pos, end = pos + cur.value.length;
2629
+ if (!cur.lineBreak && buildForLine(cur.value)) {
2630
+ if (last && last.to > start - 10)
2631
+ last.to = Math.min(to, end);
2632
+ else
2633
+ result.push(last = { from: start, to: Math.min(to, end) });
2634
+ }
2635
+ if (pos >= to)
2636
+ break;
2637
+ pos = end;
2638
+ cur.next();
2639
+ }
2640
+ }
2641
+ return result;
2642
+ }
2643
+ const marks = {
2644
+ rtl: view.Decoration.mark({ class: "cm-iso", inclusive: true, attributes: { dir: "rtl" }, bidiIsolate: view.Direction.RTL }),
2645
+ ltr: view.Decoration.mark({ class: "cm-iso", inclusive: true, attributes: { dir: "ltr" }, bidiIsolate: view.Direction.LTR }),
2646
+ auto: view.Decoration.mark({ class: "cm-iso", inclusive: true, attributes: { dir: "auto" }, bidiIsolate: null })
2647
+ };
2648
+
2534
2649
  exports.DocInput = DocInput;
2535
2650
  exports.HighlightStyle = HighlightStyle;
2536
2651
  exports.IndentContext = IndentContext;
@@ -2542,6 +2657,7 @@ exports.ParseContext = ParseContext;
2542
2657
  exports.StreamLanguage = StreamLanguage;
2543
2658
  exports.StringStream = StringStream;
2544
2659
  exports.TreeIndentContext = TreeIndentContext;
2660
+ exports.bidiIsolates = bidiIsolates;
2545
2661
  exports.bracketMatching = bracketMatching;
2546
2662
  exports.bracketMatchingHandle = bracketMatchingHandle;
2547
2663
  exports.codeFolding = codeFolding;
package/dist/index.d.cts CHANGED
@@ -1197,4 +1197,20 @@ declare class StreamLanguage<State> extends Language {
1197
1197
  get allowsNesting(): boolean;
1198
1198
  }
1199
1199
 
1200
- export { Config, DocInput, HighlightStyle, IndentContext, LRLanguage, Language, LanguageDescription, LanguageSupport, MatchResult, ParseContext, StreamLanguage, StreamParser, StringStream, Sublanguage, 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, sublanguageProp, syntaxHighlighting, syntaxParserRunning, syntaxTree, syntaxTreeAvailable, toggleFold, unfoldAll, unfoldCode, unfoldEffect };
1200
+ /**
1201
+ Make sure nodes
1202
+ [marked](https://lezer.codemirror.net/docs/ref/#common.NodeProp^isolate)
1203
+ as isolating for bidirectional text are rendered in a way that
1204
+ isolates them from the surrounding text.
1205
+ */
1206
+ declare function bidiIsolates(options?: {
1207
+ /**
1208
+ By default, isolating elements are only added when the editor
1209
+ direction isn't uniformly left-to-right, or if it is, on lines
1210
+ that contain right-to-left character. When true, disable this
1211
+ optimization and add them everywhere.
1212
+ */
1213
+ alwaysIsolate?: boolean;
1214
+ }): Extension;
1215
+
1216
+ export { type Config, DocInput, HighlightStyle, IndentContext, LRLanguage, Language, LanguageDescription, LanguageSupport, type MatchResult, ParseContext, StreamLanguage, type StreamParser, StringStream, type Sublanguage, type TagStyle, TreeIndentContext, bidiIsolates, 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, sublanguageProp, syntaxHighlighting, syntaxParserRunning, syntaxTree, syntaxTreeAvailable, toggleFold, unfoldAll, unfoldCode, unfoldEffect };
package/dist/index.d.ts CHANGED
@@ -1197,4 +1197,20 @@ declare class StreamLanguage<State> extends Language {
1197
1197
  get allowsNesting(): boolean;
1198
1198
  }
1199
1199
 
1200
- export { Config, DocInput, HighlightStyle, IndentContext, LRLanguage, Language, LanguageDescription, LanguageSupport, MatchResult, ParseContext, StreamLanguage, StreamParser, StringStream, Sublanguage, 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, sublanguageProp, syntaxHighlighting, syntaxParserRunning, syntaxTree, syntaxTreeAvailable, toggleFold, unfoldAll, unfoldCode, unfoldEffect };
1200
+ /**
1201
+ Make sure nodes
1202
+ [marked](https://lezer.codemirror.net/docs/ref/#common.NodeProp^isolate)
1203
+ as isolating for bidirectional text are rendered in a way that
1204
+ isolates them from the surrounding text.
1205
+ */
1206
+ declare function bidiIsolates(options?: {
1207
+ /**
1208
+ By default, isolating elements are only added when the editor
1209
+ direction isn't uniformly left-to-right, or if it is, on lines
1210
+ that contain right-to-left character. When true, disable this
1211
+ optimization and add them everywhere.
1212
+ */
1213
+ alwaysIsolate?: boolean;
1214
+ }): Extension;
1215
+
1216
+ export { type Config, DocInput, HighlightStyle, IndentContext, LRLanguage, Language, LanguageDescription, LanguageSupport, type MatchResult, ParseContext, StreamLanguage, type StreamParser, StringStream, type Sublanguage, type TagStyle, TreeIndentContext, bidiIsolates, 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, sublanguageProp, syntaxHighlighting, syntaxParserRunning, syntaxTree, syntaxTreeAvailable, toggleFold, unfoldAll, unfoldCode, unfoldEffect };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { NodeProp, IterMode, Tree, TreeFragment, Parser, NodeType, NodeSet } from '@lezer/common';
2
2
  import { StateEffect, StateField, Facet, EditorState, countColumn, combineConfig, RangeSet, RangeSetBuilder, Prec } from '@codemirror/state';
3
- import { ViewPlugin, logException, EditorView, Decoration, WidgetType, gutter, GutterMarker } from '@codemirror/view';
3
+ import { ViewPlugin, logException, EditorView, Decoration, WidgetType, gutter, GutterMarker, Direction } from '@codemirror/view';
4
4
  import { tags, tagHighlighter, highlightTree, styleTags } from '@lezer/highlight';
5
5
  import { StyleModule } from 'style-mod';
6
6
 
@@ -1749,16 +1749,20 @@ class TreeHighlighter {
1749
1749
  this.markCache = Object.create(null);
1750
1750
  this.tree = syntaxTree(view.state);
1751
1751
  this.decorations = this.buildDeco(view, getHighlighters(view.state));
1752
+ this.decoratedTo = view.viewport.to;
1752
1753
  }
1753
1754
  update(update) {
1754
1755
  let tree = syntaxTree(update.state), highlighters = getHighlighters(update.state);
1755
1756
  let styleChange = highlighters != getHighlighters(update.startState);
1756
- if (tree.length < update.view.viewport.to && !styleChange && tree.type == this.tree.type) {
1757
+ let { viewport } = update.view, decoratedToMapped = update.changes.mapPos(this.decoratedTo, 1);
1758
+ if (tree.length < viewport.to && !styleChange && tree.type == this.tree.type && decoratedToMapped >= viewport.to) {
1757
1759
  this.decorations = this.decorations.map(update.changes);
1760
+ this.decoratedTo = decoratedToMapped;
1758
1761
  }
1759
1762
  else if (tree != this.tree || update.viewportChanged || styleChange) {
1760
1763
  this.tree = tree;
1761
1764
  this.decorations = this.buildDeco(update.view, highlighters);
1765
+ this.decoratedTo = viewport.to;
1762
1766
  }
1763
1767
  }
1764
1768
  buildDeco(view, highlighters) {
@@ -2529,4 +2533,115 @@ function docID(data) {
2529
2533
  return type;
2530
2534
  }
2531
2535
 
2532
- export { DocInput, 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, sublanguageProp, syntaxHighlighting, syntaxParserRunning, syntaxTree, syntaxTreeAvailable, toggleFold, unfoldAll, unfoldCode, unfoldEffect };
2536
+ function buildForLine(line) {
2537
+ return line.length <= 4096 && /[\u0590-\u05f4\u0600-\u06ff\u0700-\u08ac\ufb50-\ufdff]/.test(line);
2538
+ }
2539
+ function textHasRTL(text) {
2540
+ for (let i = text.iter(); !i.next().done;)
2541
+ if (buildForLine(i.value))
2542
+ return true;
2543
+ return false;
2544
+ }
2545
+ function changeAddsRTL(change) {
2546
+ let added = false;
2547
+ change.iterChanges((fA, tA, fB, tB, ins) => {
2548
+ if (!added && textHasRTL(ins))
2549
+ added = true;
2550
+ });
2551
+ return added;
2552
+ }
2553
+ const alwaysIsolate = /*@__PURE__*/Facet.define({ combine: values => values.some(x => x) });
2554
+ /**
2555
+ Make sure nodes
2556
+ [marked](https://lezer.codemirror.net/docs/ref/#common.NodeProp^isolate)
2557
+ as isolating for bidirectional text are rendered in a way that
2558
+ isolates them from the surrounding text.
2559
+ */
2560
+ function bidiIsolates(options = {}) {
2561
+ let extensions = [isolateMarks];
2562
+ if (options.alwaysIsolate)
2563
+ extensions.push(alwaysIsolate.of(true));
2564
+ return extensions;
2565
+ }
2566
+ const isolateMarks = /*@__PURE__*/ViewPlugin.fromClass(class {
2567
+ constructor(view) {
2568
+ this.always = view.state.facet(alwaysIsolate) ||
2569
+ view.textDirection != Direction.LTR ||
2570
+ view.state.facet(EditorView.perLineTextDirection);
2571
+ this.hasRTL = !this.always && textHasRTL(view.state.doc);
2572
+ this.tree = syntaxTree(view.state);
2573
+ this.decorations = this.always || this.hasRTL ? buildDeco(view, this.tree, this.always) : Decoration.none;
2574
+ }
2575
+ update(update) {
2576
+ let always = update.state.facet(alwaysIsolate) ||
2577
+ update.view.textDirection != Direction.LTR ||
2578
+ update.state.facet(EditorView.perLineTextDirection);
2579
+ if (!always && !this.hasRTL && changeAddsRTL(update.changes))
2580
+ this.hasRTL = true;
2581
+ if (!always && !this.hasRTL)
2582
+ return;
2583
+ let tree = syntaxTree(update.state);
2584
+ if (always != this.always || tree != this.tree || update.docChanged || update.viewportChanged) {
2585
+ this.tree = tree;
2586
+ this.always = always;
2587
+ this.decorations = buildDeco(update.view, tree, always);
2588
+ }
2589
+ }
2590
+ }, {
2591
+ provide: plugin => {
2592
+ function access(view) {
2593
+ var _a, _b;
2594
+ return (_b = (_a = view.plugin(plugin)) === null || _a === void 0 ? void 0 : _a.decorations) !== null && _b !== void 0 ? _b : Decoration.none;
2595
+ }
2596
+ return [EditorView.outerDecorations.of(access),
2597
+ Prec.lowest(EditorView.bidiIsolatedRanges.of(access))];
2598
+ }
2599
+ });
2600
+ function buildDeco(view, tree, always) {
2601
+ let deco = new RangeSetBuilder();
2602
+ let ranges = view.visibleRanges;
2603
+ if (!always)
2604
+ ranges = clipRTLLines(ranges, view.state.doc);
2605
+ for (let { from, to } of ranges) {
2606
+ tree.iterate({
2607
+ enter: node => {
2608
+ let iso = node.type.prop(NodeProp.isolate);
2609
+ if (iso)
2610
+ deco.add(node.from, node.to, marks[iso]);
2611
+ },
2612
+ from, to
2613
+ });
2614
+ }
2615
+ return deco.finish();
2616
+ }
2617
+ function clipRTLLines(ranges, doc) {
2618
+ let cur = doc.iter(), pos = 0, result = [], last = null;
2619
+ for (let { from, to } of ranges) {
2620
+ if (from != pos) {
2621
+ if (pos < from)
2622
+ cur.next(from - pos);
2623
+ pos = from;
2624
+ }
2625
+ for (;;) {
2626
+ let start = pos, end = pos + cur.value.length;
2627
+ if (!cur.lineBreak && buildForLine(cur.value)) {
2628
+ if (last && last.to > start - 10)
2629
+ last.to = Math.min(to, end);
2630
+ else
2631
+ result.push(last = { from: start, to: Math.min(to, end) });
2632
+ }
2633
+ if (pos >= to)
2634
+ break;
2635
+ pos = end;
2636
+ cur.next();
2637
+ }
2638
+ }
2639
+ return result;
2640
+ }
2641
+ const marks = {
2642
+ rtl: /*@__PURE__*/Decoration.mark({ class: "cm-iso", inclusive: true, attributes: { dir: "rtl" }, bidiIsolate: Direction.RTL }),
2643
+ ltr: /*@__PURE__*/Decoration.mark({ class: "cm-iso", inclusive: true, attributes: { dir: "ltr" }, bidiIsolate: Direction.LTR }),
2644
+ auto: /*@__PURE__*/Decoration.mark({ class: "cm-iso", inclusive: true, attributes: { dir: "auto" }, bidiIsolate: null })
2645
+ };
2646
+
2647
+ export { DocInput, HighlightStyle, IndentContext, LRLanguage, Language, LanguageDescription, LanguageSupport, ParseContext, StreamLanguage, StringStream, TreeIndentContext, bidiIsolates, 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, sublanguageProp, syntaxHighlighting, syntaxParserRunning, syntaxTree, syntaxTreeAvailable, toggleFold, unfoldAll, unfoldCode, unfoldEffect };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codemirror/language",
3
- "version": "6.9.3",
3
+ "version": "6.10.1",
4
4
  "description": "Language support infrastructure for the CodeMirror code editor",
5
5
  "scripts": {
6
6
  "test": "cm-runtests",
@@ -27,7 +27,7 @@
27
27
  "license": "MIT",
28
28
  "dependencies": {
29
29
  "@codemirror/state": "^6.0.0",
30
- "@codemirror/view": "^6.0.0",
30
+ "@codemirror/view": "^6.23.0",
31
31
  "@lezer/common": "^1.1.0",
32
32
  "@lezer/highlight": "^1.0.0",
33
33
  "@lezer/lr": "^1.0.0",