@codemirror/view 6.0.3 → 6.1.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.1.0 (2022-07-19)
2
+
3
+ ### New features
4
+
5
+ `MatchDecorator` now supports a `decorate` option that can be used to customize the way decorations are added for each match.
6
+
1
7
  ## 6.0.3 (2022-07-08)
2
8
 
3
9
  ### Bug fixes
package/dist/index.cjs CHANGED
@@ -7381,7 +7381,7 @@ function iterMatches(doc, re, from, to, f) {
7381
7381
  for (let cursor = doc.iterRange(from, to), pos = from, m; !cursor.next().done; pos += cursor.value.length) {
7382
7382
  if (!cursor.lineBreak)
7383
7383
  while (m = re.exec(cursor.value))
7384
- f(pos + m.index, pos + m.index + m[0].length, m);
7384
+ f(pos + m.index, m);
7385
7385
  }
7386
7386
  }
7387
7387
  function matchRanges(view, maxLength) {
@@ -7411,11 +7411,20 @@ class MatchDecorator {
7411
7411
  Create a decorator.
7412
7412
  */
7413
7413
  constructor(config) {
7414
- let { regexp, decoration, boundary, maxLength = 1000 } = config;
7414
+ const { regexp, decoration, decorate, boundary, maxLength = 1000 } = config;
7415
7415
  if (!regexp.global)
7416
7416
  throw new RangeError("The regular expression given to MatchDecorator should have its 'g' flag set");
7417
7417
  this.regexp = regexp;
7418
- this.getDeco = typeof decoration == "function" ? decoration : () => decoration;
7418
+ if (decorate) {
7419
+ this.addMatch = (match, view, from, add) => decorate(add, from, from + match[0].length, match, view);
7420
+ }
7421
+ else if (decoration) {
7422
+ let getDeco = typeof decoration == "function" ? decoration : () => decoration;
7423
+ this.addMatch = (match, view, from, add) => add(from, from + match[0].length, getDeco(match, view, from));
7424
+ }
7425
+ else {
7426
+ throw new RangeError("Either 'decorate' or 'decoration' should be provided to MatchDecorator");
7427
+ }
7419
7428
  this.boundary = boundary;
7420
7429
  this.maxLength = maxLength;
7421
7430
  }
@@ -7425,9 +7434,9 @@ class MatchDecorator {
7425
7434
  plugin.
7426
7435
  */
7427
7436
  createDeco(view) {
7428
- let build = new state.RangeSetBuilder();
7437
+ let build = new state.RangeSetBuilder(), add = build.add.bind(build);
7429
7438
  for (let { from, to } of matchRanges(view, this.maxLength))
7430
- iterMatches(view.state.doc, this.regexp, from, to, (a, b, m) => build.add(a, b, this.getDeco(m, view, a)));
7439
+ iterMatches(view.state.doc, this.regexp, from, to, (from, m) => this.addMatch(m, view, from, add));
7431
7440
  return build.finish();
7432
7441
  }
7433
7442
  /**
@@ -7469,15 +7478,14 @@ class MatchDecorator {
7469
7478
  }
7470
7479
  }
7471
7480
  let ranges = [], m;
7481
+ let add = (from, to, deco) => ranges.push(deco.range(from, to));
7472
7482
  if (fromLine == toLine) {
7473
7483
  this.regexp.lastIndex = start - fromLine.from;
7474
- while ((m = this.regexp.exec(fromLine.text)) && m.index < end - fromLine.from) {
7475
- let pos = m.index + fromLine.from;
7476
- ranges.push(this.getDeco(m, view, pos).range(pos, pos + m[0].length));
7477
- }
7484
+ while ((m = this.regexp.exec(fromLine.text)) && m.index < end - fromLine.from)
7485
+ this.addMatch(m, view, m.index + fromLine.from, add);
7478
7486
  }
7479
7487
  else {
7480
- iterMatches(view.state.doc, this.regexp, start, end, (from, to, m) => ranges.push(this.getDeco(m, view, from).range(from, to)));
7488
+ iterMatches(view.state.doc, this.regexp, start, end, (from, m) => this.addMatch(m, view, from, add));
7481
7489
  }
7482
7490
  deco = deco.update({ filterFrom: start, filterTo: end, filter: (from, to) => from < start || to > end, add: ranges });
7483
7491
  }
package/dist/index.d.ts CHANGED
@@ -1357,7 +1357,7 @@ represent a matching configuration.
1357
1357
  */
1358
1358
  declare class MatchDecorator {
1359
1359
  private regexp;
1360
- private getDeco;
1360
+ private addMatch;
1361
1361
  private boundary;
1362
1362
  private maxLength;
1363
1363
  /**
@@ -1374,7 +1374,18 @@ declare class MatchDecorator {
1374
1374
  The decoration to apply to matches, either directly or as a
1375
1375
  function of the match.
1376
1376
  */
1377
- decoration: Decoration | ((match: RegExpExecArray, view: EditorView, pos: number) => Decoration);
1377
+ decoration?: Decoration | ((match: RegExpExecArray, view: EditorView, pos: number) => Decoration);
1378
+ /**
1379
+ Customize the way decorations are added for matches. This
1380
+ function, when given, will be called for matches and should
1381
+ call `add` to create decorations for them. Note that the
1382
+ decorations should appear *in* the given range, and the
1383
+ function should have no side effects beyond calling `add`.
1384
+
1385
+ The `decoration` option is ignored when `decorate` is
1386
+ provided.
1387
+ */
1388
+ decorate?: (add: (from: number, to: number, decoration: Decoration) => void, from: number, to: number, match: RegExpExecArray, view: EditorView) => void;
1378
1389
  /**
1379
1390
  By default, changed lines are re-matched entirely. You can
1380
1391
  provide a boundary expression, which should match single
package/dist/index.js CHANGED
@@ -7374,7 +7374,7 @@ function iterMatches(doc, re, from, to, f) {
7374
7374
  for (let cursor = doc.iterRange(from, to), pos = from, m; !cursor.next().done; pos += cursor.value.length) {
7375
7375
  if (!cursor.lineBreak)
7376
7376
  while (m = re.exec(cursor.value))
7377
- f(pos + m.index, pos + m.index + m[0].length, m);
7377
+ f(pos + m.index, m);
7378
7378
  }
7379
7379
  }
7380
7380
  function matchRanges(view, maxLength) {
@@ -7404,11 +7404,20 @@ class MatchDecorator {
7404
7404
  Create a decorator.
7405
7405
  */
7406
7406
  constructor(config) {
7407
- let { regexp, decoration, boundary, maxLength = 1000 } = config;
7407
+ const { regexp, decoration, decorate, boundary, maxLength = 1000 } = config;
7408
7408
  if (!regexp.global)
7409
7409
  throw new RangeError("The regular expression given to MatchDecorator should have its 'g' flag set");
7410
7410
  this.regexp = regexp;
7411
- this.getDeco = typeof decoration == "function" ? decoration : () => decoration;
7411
+ if (decorate) {
7412
+ this.addMatch = (match, view, from, add) => decorate(add, from, from + match[0].length, match, view);
7413
+ }
7414
+ else if (decoration) {
7415
+ let getDeco = typeof decoration == "function" ? decoration : () => decoration;
7416
+ this.addMatch = (match, view, from, add) => add(from, from + match[0].length, getDeco(match, view, from));
7417
+ }
7418
+ else {
7419
+ throw new RangeError("Either 'decorate' or 'decoration' should be provided to MatchDecorator");
7420
+ }
7412
7421
  this.boundary = boundary;
7413
7422
  this.maxLength = maxLength;
7414
7423
  }
@@ -7418,9 +7427,9 @@ class MatchDecorator {
7418
7427
  plugin.
7419
7428
  */
7420
7429
  createDeco(view) {
7421
- let build = new RangeSetBuilder();
7430
+ let build = new RangeSetBuilder(), add = build.add.bind(build);
7422
7431
  for (let { from, to } of matchRanges(view, this.maxLength))
7423
- iterMatches(view.state.doc, this.regexp, from, to, (a, b, m) => build.add(a, b, this.getDeco(m, view, a)));
7432
+ iterMatches(view.state.doc, this.regexp, from, to, (from, m) => this.addMatch(m, view, from, add));
7424
7433
  return build.finish();
7425
7434
  }
7426
7435
  /**
@@ -7462,15 +7471,14 @@ class MatchDecorator {
7462
7471
  }
7463
7472
  }
7464
7473
  let ranges = [], m;
7474
+ let add = (from, to, deco) => ranges.push(deco.range(from, to));
7465
7475
  if (fromLine == toLine) {
7466
7476
  this.regexp.lastIndex = start - fromLine.from;
7467
- while ((m = this.regexp.exec(fromLine.text)) && m.index < end - fromLine.from) {
7468
- let pos = m.index + fromLine.from;
7469
- ranges.push(this.getDeco(m, view, pos).range(pos, pos + m[0].length));
7470
- }
7477
+ while ((m = this.regexp.exec(fromLine.text)) && m.index < end - fromLine.from)
7478
+ this.addMatch(m, view, m.index + fromLine.from, add);
7471
7479
  }
7472
7480
  else {
7473
- iterMatches(view.state.doc, this.regexp, start, end, (from, to, m) => ranges.push(this.getDeco(m, view, from).range(from, to)));
7481
+ iterMatches(view.state.doc, this.regexp, start, end, (from, m) => this.addMatch(m, view, from, add));
7474
7482
  }
7475
7483
  deco = deco.update({ filterFrom: start, filterTo: end, filter: (from, to) => from < start || to > end, add: ranges });
7476
7484
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codemirror/view",
3
- "version": "6.0.3",
3
+ "version": "6.1.0",
4
4
  "description": "DOM view component for the CodeMirror code editor",
5
5
  "scripts": {
6
6
  "test": "cm-runtests",