@codemirror/language 6.7.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,9 @@
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
+
1
7
  ## 6.7.0 (2023-05-19)
2
8
 
3
9
  ### New features
package/dist/index.cjs CHANGED
@@ -538,14 +538,14 @@ class LanguageState {
538
538
  // state updates with parse work beyond the viewport.
539
539
  let upto = this.context.treeLen == tr.startState.doc.length ? undefined
540
540
  : Math.max(tr.changes.mapPos(this.context.treeLen), newCx.viewport.to);
541
- if (!newCx.work(20 /* Work.Apply */, upto))
541
+ if (!newCx.work(20 /* Apply */, upto))
542
542
  newCx.takeTree();
543
543
  return new LanguageState(newCx);
544
544
  }
545
545
  static init(state) {
546
- let vpTo = Math.min(3000 /* Work.InitViewport */, state.doc.length);
546
+ let vpTo = Math.min(3000 /* InitViewport */, state.doc.length);
547
547
  let parseState = ParseContext.create(state.facet(language).parser, state, { from: 0, to: vpTo });
548
- if (!parseState.work(20 /* Work.Apply */, vpTo))
548
+ if (!parseState.work(20 /* Apply */, vpTo))
549
549
  parseState.takeTree();
550
550
  return new LanguageState(parseState);
551
551
  }
@@ -562,14 +562,14 @@ Language.state = state.StateField.define({
562
562
  }
563
563
  });
564
564
  let requestIdle = (callback) => {
565
- let timeout = setTimeout(() => callback(), 500 /* Work.MaxPause */);
565
+ let timeout = setTimeout(() => callback(), 500 /* MaxPause */);
566
566
  return () => clearTimeout(timeout);
567
567
  };
568
568
  if (typeof requestIdleCallback != "undefined")
569
569
  requestIdle = (callback) => {
570
570
  let idle = -1, timeout = setTimeout(() => {
571
- idle = requestIdleCallback(callback, { timeout: 500 /* Work.MaxPause */ - 100 /* Work.MinPause */ });
572
- }, 100 /* Work.MinPause */);
571
+ idle = requestIdleCallback(callback, { timeout: 500 /* MaxPause */ - 100 /* MinPause */ });
572
+ }, 100 /* MinPause */);
573
573
  return () => idle < 0 ? clearTimeout(timeout) : cancelIdleCallback(idle);
574
574
  };
575
575
  const isInputPending = typeof navigator != "undefined" && ((_a = navigator.scheduling) === null || _a === void 0 ? void 0 : _a.isInputPending)
@@ -592,7 +592,7 @@ const parseWorker = view.ViewPlugin.fromClass(class ParseWorker {
592
592
  this.scheduleWork();
593
593
  if (update.docChanged) {
594
594
  if (this.view.hasFocus)
595
- this.chunkBudget += 50 /* Work.ChangeBonus */;
595
+ this.chunkBudget += 50 /* ChangeBonus */;
596
596
  this.scheduleWork();
597
597
  }
598
598
  this.checkAsyncSchedule(cx);
@@ -608,19 +608,19 @@ const parseWorker = view.ViewPlugin.fromClass(class ParseWorker {
608
608
  this.working = null;
609
609
  let now = Date.now();
610
610
  if (this.chunkEnd < now && (this.chunkEnd < 0 || this.view.hasFocus)) { // Start a new chunk
611
- this.chunkEnd = now + 30000 /* Work.ChunkTime */;
612
- this.chunkBudget = 3000 /* Work.ChunkBudget */;
611
+ this.chunkEnd = now + 30000 /* ChunkTime */;
612
+ this.chunkBudget = 3000 /* ChunkBudget */;
613
613
  }
614
614
  if (this.chunkBudget <= 0)
615
615
  return; // No more budget
616
616
  let { state, viewport: { to: vpTo } } = this.view, field = state.field(Language.state);
617
- 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 */))
618
618
  return;
619
- 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);
620
620
  let viewportFirst = field.context.treeLen < vpTo && state.doc.length > vpTo + 1000;
621
621
  let done = field.context.work(() => {
622
622
  return isInputPending && isInputPending() || Date.now() > endTime;
623
- }, vpTo + (viewportFirst ? 0 : 100000 /* Work.MaxParseAhead */));
623
+ }, vpTo + (viewportFirst ? 0 : 100000 /* MaxParseAhead */));
624
624
  this.chunkBudget -= Date.now() - now;
625
625
  if (done || this.chunkBudget <= 0) {
626
626
  field.context.takeTree();
@@ -1062,13 +1062,20 @@ class TreeIndentContext extends IndentContext {
1062
1062
  on if it is covered by another such node.
1063
1063
  */
1064
1064
  get baseIndent() {
1065
- 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);
1066
1073
  // Skip line starts that are covered by a sibling (or cousin, etc)
1067
1074
  for (;;) {
1068
- let atBreak = this.node.resolve(line.from);
1075
+ let atBreak = node.resolve(line.from);
1069
1076
  while (atBreak.parent && atBreak.parent.from == atBreak.from)
1070
1077
  atBreak = atBreak.parent;
1071
- if (isParent(atBreak, this.node))
1078
+ if (isParent(atBreak, node))
1072
1079
  break;
1073
1080
  line = this.state.doc.lineAt(atBreak.from);
1074
1081
  }
@@ -2200,7 +2207,7 @@ class StreamLanguage extends Language {
2200
2207
  state = this.streamParser.startState(cx.unit);
2201
2208
  statePos = 0;
2202
2209
  }
2203
- if (pos - statePos > 10000 /* C.MaxIndentScanDist */)
2210
+ if (pos - statePos > 10000 /* MaxIndentScanDist */)
2204
2211
  return null;
2205
2212
  while (statePos < pos) {
2206
2213
  let line = cx.state.doc.lineAt(statePos), end = Math.min(pos, line.to);
@@ -2282,7 +2289,7 @@ class Parse {
2282
2289
  this.chunks.push(tree.children[i]);
2283
2290
  this.chunkPos.push(tree.positions[i]);
2284
2291
  }
2285
- if (context && this.parsedPos < context.viewport.from - 100000 /* C.MaxDistanceBeforeViewport */) {
2292
+ if (context && this.parsedPos < context.viewport.from - 100000 /* MaxDistanceBeforeViewport */) {
2286
2293
  this.state = this.lang.streamParser.startState(getIndentUnit(context.state));
2287
2294
  context.skipUntilInView(this.parsedPos, context.viewport.from);
2288
2295
  this.parsedPos = context.viewport.from;
@@ -2292,7 +2299,7 @@ class Parse {
2292
2299
  advance() {
2293
2300
  let context = ParseContext.get();
2294
2301
  let parseEnd = this.stoppedAt == null ? this.to : Math.min(this.to, this.stoppedAt);
2295
- let end = Math.min(parseEnd, this.chunkStart + 2048 /* C.ChunkSize */);
2302
+ let end = Math.min(parseEnd, this.chunkStart + 2048 /* ChunkSize */);
2296
2303
  if (context)
2297
2304
  end = Math.min(end, context.viewport.to);
2298
2305
  while (this.parsedPos < end)
@@ -2376,7 +2383,7 @@ class Parse {
2376
2383
  let token = readToken(streamParser.token, stream, this.state);
2377
2384
  if (token)
2378
2385
  offset = this.emitToken(this.lang.tokenTable.resolve(token), this.parsedPos + stream.start, this.parsedPos + stream.pos, 4, offset);
2379
- if (stream.start > 10000 /* C.MaxLineLength */)
2386
+ if (stream.start > 10000 /* MaxLineLength */)
2380
2387
  break;
2381
2388
  }
2382
2389
  }
@@ -2392,7 +2399,7 @@ class Parse {
2392
2399
  length: this.parsedPos - this.chunkStart,
2393
2400
  nodeSet,
2394
2401
  topID: 0,
2395
- maxBufferLength: 2048 /* C.ChunkSize */,
2402
+ maxBufferLength: 2048 /* ChunkSize */,
2396
2403
  reused: this.chunkReused
2397
2404
  });
2398
2405
  tree = new common.Tree(tree.type, tree.children, tree.positions, tree.length, [[this.lang.stateAfter, this.lang.streamParser.copyState(this.state)]]);