@codemirror/language 6.6.0 → 6.8.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.8.0 (2023-06-12)
2
+
3
+ ### New features
4
+
5
+ The new `baseIndentFor` method in `TreeIndentContext` can be used to find the base indentation for an arbitrary node.
6
+
7
+ ## 6.7.0 (2023-05-19)
8
+
9
+ ### New features
10
+
11
+ Export `DocInput` class for feeding editor documents to a Lezer parser.
12
+
1
13
  ## 6.6.0 (2023-02-13)
2
14
 
3
15
  ### New features
package/dist/index.cjs CHANGED
@@ -28,7 +28,7 @@ function defineLanguageFacet(baseData) {
28
28
  });
29
29
  }
30
30
  /**
31
- Syntax node prop used to register sublangauges. Should be added to
31
+ Syntax node prop used to register sublanguages. Should be added to
32
32
  the top level node type for the language.
33
33
  */
34
34
  const sublanguageProp = new common.NodeProp();
@@ -244,8 +244,15 @@ function syntaxParserRunning(view) {
244
244
  var _a;
245
245
  return ((_a = view.plugin(parseWorker)) === null || _a === void 0 ? void 0 : _a.isWorking()) || false;
246
246
  }
247
- // Lezer-style Input object for a Text document.
247
+ /**
248
+ Lezer-style
249
+ [`Input`](https://lezer.codemirror.net/docs/ref#common.Input)
250
+ object for a [`Text`](https://codemirror.net/6/docs/ref/#state.Text) object.
251
+ */
248
252
  class DocInput {
253
+ /**
254
+ Create an input object for the given document.
255
+ */
249
256
  constructor(doc) {
250
257
  this.doc = doc;
251
258
  this.cursorPos = 0;
@@ -531,14 +538,14 @@ class LanguageState {
531
538
  // state updates with parse work beyond the viewport.
532
539
  let upto = this.context.treeLen == tr.startState.doc.length ? undefined
533
540
  : Math.max(tr.changes.mapPos(this.context.treeLen), newCx.viewport.to);
534
- if (!newCx.work(20 /* Work.Apply */, upto))
541
+ if (!newCx.work(20 /* Apply */, upto))
535
542
  newCx.takeTree();
536
543
  return new LanguageState(newCx);
537
544
  }
538
545
  static init(state) {
539
- let vpTo = Math.min(3000 /* Work.InitViewport */, state.doc.length);
546
+ let vpTo = Math.min(3000 /* InitViewport */, state.doc.length);
540
547
  let parseState = ParseContext.create(state.facet(language).parser, state, { from: 0, to: vpTo });
541
- if (!parseState.work(20 /* Work.Apply */, vpTo))
548
+ if (!parseState.work(20 /* Apply */, vpTo))
542
549
  parseState.takeTree();
543
550
  return new LanguageState(parseState);
544
551
  }
@@ -555,14 +562,14 @@ Language.state = state.StateField.define({
555
562
  }
556
563
  });
557
564
  let requestIdle = (callback) => {
558
- let timeout = setTimeout(() => callback(), 500 /* Work.MaxPause */);
565
+ let timeout = setTimeout(() => callback(), 500 /* MaxPause */);
559
566
  return () => clearTimeout(timeout);
560
567
  };
561
568
  if (typeof requestIdleCallback != "undefined")
562
569
  requestIdle = (callback) => {
563
570
  let idle = -1, timeout = setTimeout(() => {
564
- idle = requestIdleCallback(callback, { timeout: 500 /* Work.MaxPause */ - 100 /* Work.MinPause */ });
565
- }, 100 /* Work.MinPause */);
571
+ idle = requestIdleCallback(callback, { timeout: 500 /* MaxPause */ - 100 /* MinPause */ });
572
+ }, 100 /* MinPause */);
566
573
  return () => idle < 0 ? clearTimeout(timeout) : cancelIdleCallback(idle);
567
574
  };
568
575
  const isInputPending = typeof navigator != "undefined" && ((_a = navigator.scheduling) === null || _a === void 0 ? void 0 : _a.isInputPending)
@@ -585,7 +592,7 @@ const parseWorker = view.ViewPlugin.fromClass(class ParseWorker {
585
592
  this.scheduleWork();
586
593
  if (update.docChanged) {
587
594
  if (this.view.hasFocus)
588
- this.chunkBudget += 50 /* Work.ChangeBonus */;
595
+ this.chunkBudget += 50 /* ChangeBonus */;
589
596
  this.scheduleWork();
590
597
  }
591
598
  this.checkAsyncSchedule(cx);
@@ -601,19 +608,19 @@ const parseWorker = view.ViewPlugin.fromClass(class ParseWorker {
601
608
  this.working = null;
602
609
  let now = Date.now();
603
610
  if (this.chunkEnd < now && (this.chunkEnd < 0 || this.view.hasFocus)) { // Start a new chunk
604
- this.chunkEnd = now + 30000 /* Work.ChunkTime */;
605
- this.chunkBudget = 3000 /* Work.ChunkBudget */;
611
+ this.chunkEnd = now + 30000 /* ChunkTime */;
612
+ this.chunkBudget = 3000 /* ChunkBudget */;
606
613
  }
607
614
  if (this.chunkBudget <= 0)
608
615
  return; // No more budget
609
616
  let { state, viewport: { to: vpTo } } = this.view, field = state.field(Language.state);
610
- if (field.tree == field.context.tree && field.context.isDone(vpTo + 100000 /* Work.MaxParseAhead */))
617
+ if (field.tree == field.context.tree && field.context.isDone(vpTo + 100000 /* MaxParseAhead */))
611
618
  return;
612
- let endTime = Date.now() + Math.min(this.chunkBudget, 100 /* Work.Slice */, deadline && !isInputPending ? Math.max(25 /* Work.MinSlice */, deadline.timeRemaining() - 5) : 1e9);
619
+ let endTime = Date.now() + Math.min(this.chunkBudget, 100 /* Slice */, deadline && !isInputPending ? Math.max(25 /* MinSlice */, deadline.timeRemaining() - 5) : 1e9);
613
620
  let viewportFirst = field.context.treeLen < vpTo && state.doc.length > vpTo + 1000;
614
621
  let done = field.context.work(() => {
615
622
  return isInputPending && isInputPending() || Date.now() > endTime;
616
- }, vpTo + (viewportFirst ? 0 : 100000 /* Work.MaxParseAhead */));
623
+ }, vpTo + (viewportFirst ? 0 : 100000 /* MaxParseAhead */));
617
624
  this.chunkBudget -= Date.now() - now;
618
625
  if (done || this.chunkBudget <= 0) {
619
626
  field.context.takeTree();
@@ -1055,13 +1062,20 @@ class TreeIndentContext extends IndentContext {
1055
1062
  on if it is covered by another such node.
1056
1063
  */
1057
1064
  get baseIndent() {
1058
- let line = this.state.doc.lineAt(this.node.from);
1065
+ return this.baseIndentFor(this.node);
1066
+ }
1067
+ /**
1068
+ Get the indentation for the reference line of the given node
1069
+ (see [`baseIndent`](https://codemirror.net/6/docs/ref/#language.TreeIndentContext.baseIndent)).
1070
+ */
1071
+ baseIndentFor(node) {
1072
+ let line = this.state.doc.lineAt(node.from);
1059
1073
  // Skip line starts that are covered by a sibling (or cousin, etc)
1060
1074
  for (;;) {
1061
- let atBreak = this.node.resolve(line.from);
1075
+ let atBreak = node.resolve(line.from);
1062
1076
  while (atBreak.parent && atBreak.parent.from == atBreak.from)
1063
1077
  atBreak = atBreak.parent;
1064
- if (isParent(atBreak, this.node))
1078
+ if (isParent(atBreak, node))
1065
1079
  break;
1066
1080
  line = this.state.doc.lineAt(atBreak.from);
1067
1081
  }
@@ -2193,7 +2207,7 @@ class StreamLanguage extends Language {
2193
2207
  state = this.streamParser.startState(cx.unit);
2194
2208
  statePos = 0;
2195
2209
  }
2196
- if (pos - statePos > 10000 /* C.MaxIndentScanDist */)
2210
+ if (pos - statePos > 10000 /* MaxIndentScanDist */)
2197
2211
  return null;
2198
2212
  while (statePos < pos) {
2199
2213
  let line = cx.state.doc.lineAt(statePos), end = Math.min(pos, line.to);
@@ -2275,7 +2289,7 @@ class Parse {
2275
2289
  this.chunks.push(tree.children[i]);
2276
2290
  this.chunkPos.push(tree.positions[i]);
2277
2291
  }
2278
- if (context && this.parsedPos < context.viewport.from - 100000 /* C.MaxDistanceBeforeViewport */) {
2292
+ if (context && this.parsedPos < context.viewport.from - 100000 /* MaxDistanceBeforeViewport */) {
2279
2293
  this.state = this.lang.streamParser.startState(getIndentUnit(context.state));
2280
2294
  context.skipUntilInView(this.parsedPos, context.viewport.from);
2281
2295
  this.parsedPos = context.viewport.from;
@@ -2285,7 +2299,7 @@ class Parse {
2285
2299
  advance() {
2286
2300
  let context = ParseContext.get();
2287
2301
  let parseEnd = this.stoppedAt == null ? this.to : Math.min(this.to, this.stoppedAt);
2288
- let end = Math.min(parseEnd, this.chunkStart + 2048 /* C.ChunkSize */);
2302
+ let end = Math.min(parseEnd, this.chunkStart + 2048 /* ChunkSize */);
2289
2303
  if (context)
2290
2304
  end = Math.min(end, context.viewport.to);
2291
2305
  while (this.parsedPos < end)
@@ -2369,7 +2383,7 @@ class Parse {
2369
2383
  let token = readToken(streamParser.token, stream, this.state);
2370
2384
  if (token)
2371
2385
  offset = this.emitToken(this.lang.tokenTable.resolve(token), this.parsedPos + stream.start, this.parsedPos + stream.pos, 4, offset);
2372
- if (stream.start > 10000 /* C.MaxLineLength */)
2386
+ if (stream.start > 10000 /* MaxLineLength */)
2373
2387
  break;
2374
2388
  }
2375
2389
  }
@@ -2385,7 +2399,7 @@ class Parse {
2385
2399
  length: this.parsedPos - this.chunkStart,
2386
2400
  nodeSet,
2387
2401
  topID: 0,
2388
- maxBufferLength: 2048 /* C.ChunkSize */,
2402
+ maxBufferLength: 2048 /* ChunkSize */,
2389
2403
  reused: this.chunkReused
2390
2404
  });
2391
2405
  tree = new common.Tree(tree.type, tree.children, tree.positions, tree.length, [[this.lang.stateAfter, this.lang.streamParser.copyState(this.state)]]);
@@ -2480,6 +2494,7 @@ function docID(data) {
2480
2494
  return type;
2481
2495
  }
2482
2496
 
2497
+ exports.DocInput = DocInput;
2483
2498
  exports.HighlightStyle = HighlightStyle;
2484
2499
  exports.IndentContext = IndentContext;
2485
2500
  exports.LRLanguage = LRLanguage;