@codemirror/state
Version:
Editor state data structures for the CodeMirror code editor
1,423 lines (1,413 loc) • 82.5 kB
JavaScript
import { Text, findClusterBreak } from '@codemirror/text';
export { Text } from '@codemirror/text';
const DefaultSplit = /\r\n?|\n/;
/**
Distinguishes different ways in which positions can be mapped.
*/
var MapMode = /*@__PURE__*/(function (MapMode) {
/**
Map a position to a valid new position, even when its context
was deleted.
*/
MapMode[MapMode["Simple"] = 0] = "Simple";
/**
Return null if deletion happens across the position.
*/
MapMode[MapMode["TrackDel"] = 1] = "TrackDel";
/**
Return null if the character _before_ the position is deleted.
*/
MapMode[MapMode["TrackBefore"] = 2] = "TrackBefore";
/**
Return null if the character _after_ the position is deleted.
*/
MapMode[MapMode["TrackAfter"] = 3] = "TrackAfter";
return MapMode})(MapMode || (MapMode = {}));
/**
A change description is a variant of [change set](https://codemirror.net/6/docs/ref/#state.ChangeSet)
that doesn't store the inserted text. As such, it can't be
applied, but is cheaper to store and manipulate.
*/
class ChangeDesc {
// Sections are encoded as pairs of integers. The first is the
// length in the current document, and the second is -1 for
// unaffected sections, and the length of the replacement content
// otherwise. So an insertion would be (0, n>0), a deletion (n>0,
// 0), and a replacement two positive numbers.
/**
@internal
*/
constructor(
/**
@internal
*/
sections) {
this.sections = sections;
}
/**
The length of the document before the change.
*/
get length() {
let result = 0;
for (let i = 0; i < this.sections.length; i += 2)
result += this.sections[i];
return result;
}
/**
The length of the document after the change.
*/
get newLength() {
let result = 0;
for (let i = 0; i < this.sections.length; i += 2) {
let ins = this.sections[i + 1];
result += ins < 0 ? this.sections[i] : ins;
}
return result;
}
/**
False when there are actual changes in this set.
*/
get empty() { return this.sections.length == 0 || this.sections.length == 2 && this.sections[1] < 0; }
/**
Iterate over the unchanged parts left by these changes.
*/
iterGaps(f) {
for (let i = 0, posA = 0, posB = 0; i < this.sections.length;) {
let len = this.sections[i++], ins = this.sections[i++];
if (ins < 0) {
f(posA, posB, len);
posB += len;
}
else {
posB += ins;
}
posA += len;
}
}
/**
Iterate over the ranges changed by these changes. (See
[`ChangeSet.iterChanges`](https://codemirror.net/6/docs/ref/#state.ChangeSet.iterChanges) for a
variant that also provides you with the inserted text.)
When `individual` is true, adjacent changes (which are kept
separate for [position mapping](https://codemirror.net/6/docs/ref/#state.ChangeDesc.mapPos)) are
reported separately.
*/
iterChangedRanges(f, individual = false) {
iterChanges(this, f, individual);
}
/**
Get a description of the inverted form of these changes.
*/
get invertedDesc() {
let sections = [];
for (let i = 0; i < this.sections.length;) {
let len = this.sections[i++], ins = this.sections[i++];
if (ins < 0)
sections.push(len, ins);
else
sections.push(ins, len);
}
return new ChangeDesc(sections);
}
/**
Compute the combined effect of applying another set of changes
after this one. The length of the document after this set should
match the length before `other`.
*/
composeDesc(other) { return this.empty ? other : other.empty ? this : composeSets(this, other); }
/**
Map this description, which should start with the same document
as `other`, over another set of changes, so that it can be
applied after it. When `before` is true, map as if the changes
in `other` happened before the ones in `this`.
*/
mapDesc(other, before = false) { return other.empty ? this : mapSet(this, other, before); }
mapPos(pos, assoc = -1, mode = MapMode.Simple) {
let posA = 0, posB = 0;
for (let i = 0; i < this.sections.length;) {
let len = this.sections[i++], ins = this.sections[i++], endA = posA + len;
if (ins < 0) {
if (endA > pos)
return posB + (pos - posA);
posB += len;
}
else {
if (mode != MapMode.Simple && endA >= pos &&
(mode == MapMode.TrackDel && posA < pos && endA > pos ||
mode == MapMode.TrackBefore && posA < pos ||
mode == MapMode.TrackAfter && endA > pos))
return null;
if (endA > pos || endA == pos && assoc < 0 && !len)
return pos == posA || assoc < 0 ? posB : posB + ins;
posB += ins;
}
posA = endA;
}
if (pos > posA)
throw new RangeError(`Position ${pos} is out of range for changeset of length ${posA}`);
return posB;
}
/**
Check whether these changes touch a given range. When one of the
changes entirely covers the range, the string `"cover"` is
returned.
*/
touchesRange(from, to = from) {
for (let i = 0, pos = 0; i < this.sections.length && pos <= to;) {
let len = this.sections[i++], ins = this.sections[i++], end = pos + len;
if (ins >= 0 && pos <= to && end >= from)
return pos < from && end > to ? "cover" : true;
pos = end;
}
return false;
}
/**
@internal
*/
toString() {
let result = "";
for (let i = 0; i < this.sections.length;) {
let len = this.sections[i++], ins = this.sections[i++];
result += (result ? " " : "") + len + (ins >= 0 ? ":" + ins : "");
}
return result;
}
/**
Serialize this change desc to a JSON-representable value.
*/
toJSON() { return this.sections; }
/**
Create a change desc from its JSON representation (as produced
by [`toJSON`](https://codemirror.net/6/docs/ref/#state.ChangeDesc.toJSON).
*/
static fromJSON(json) {
if (!Array.isArray(json) || json.length % 2 || json.some(a => typeof a != "number"))
throw new RangeError("Invalid JSON representation of ChangeDesc");
return new ChangeDesc(json);
}
}
/**
A change set represents a group of modifications to a document. It
stores the document length, and can only be applied to documents
with exactly that length.
*/
class ChangeSet extends ChangeDesc {
/**
@internal
*/
constructor(sections,
/**
@internal
*/
inserted) {
super(sections);
this.inserted = inserted;
}
/**
Apply the changes to a document, returning the modified
document.
*/
apply(doc) {
if (this.length != doc.length)
throw new RangeError("Applying change set to a document with the wrong length");
iterChanges(this, (fromA, toA, fromB, _toB, text) => doc = doc.replace(fromB, fromB + (toA - fromA), text), false);
return doc;
}
mapDesc(other, before = false) { return mapSet(this, other, before, true); }
/**
Given the document as it existed _before_ the changes, return a
change set that represents the inverse of this set, which could
be used to go from the document created by the changes back to
the document as it existed before the changes.
*/
invert(doc) {
let sections = this.sections.slice(), inserted = [];
for (let i = 0, pos = 0; i < sections.length; i += 2) {
let len = sections[i], ins = sections[i + 1];
if (ins >= 0) {
sections[i] = ins;
sections[i + 1] = len;
let index = i >> 1;
while (inserted.length < index)
inserted.push(Text.empty);
inserted.push(len ? doc.slice(pos, pos + len) : Text.empty);
}
pos += len;
}
return new ChangeSet(sections, inserted);
}
/**
Combine two subsequent change sets into a single set. `other`
must start in the document produced by `this`. If `this` goes
`docA` → `docB` and `other` represents `docB` → `docC`, the
returned value will represent the change `docA` → `docC`.
*/
compose(other) { return this.empty ? other : other.empty ? this : composeSets(this, other, true); }
/**
Given another change set starting in the same document, maps this
change set over the other, producing a new change set that can be
applied to the document produced by applying `other`. When
`before` is `true`, order changes as if `this` comes before
`other`, otherwise (the default) treat `other` as coming first.
Given two changes `A` and `B`, `A.compose(B.map(A))` and
`B.compose(A.map(B, true))` will produce the same document. This
provides a basic form of [operational
transformation](https://en.wikipedia.org/wiki/Operational_transformation),
and can be used for collaborative editing.
*/
map(other, before = false) { return other.empty ? this : mapSet(this, other, before, true); }
/**
Iterate over the changed ranges in the document, calling `f` for
each.
When `individual` is true, adjacent changes are reported
separately.
*/
iterChanges(f, individual = false) {
iterChanges(this, f, individual);
}
/**
Get a [change description](https://codemirror.net/6/docs/ref/#state.ChangeDesc) for this change
set.
*/
get desc() { return new ChangeDesc(this.sections); }
/**
@internal
*/
filter(ranges) {
let resultSections = [], resultInserted = [], filteredSections = [];
let iter = new SectionIter(this);
done: for (let i = 0, pos = 0;;) {
let next = i == ranges.length ? 1e9 : ranges[i++];
while (pos < next || pos == next && iter.len == 0) {
if (iter.done)
break done;
let len = Math.min(iter.len, next - pos);
addSection(filteredSections, len, -1);
let ins = iter.ins == -1 ? -1 : iter.off == 0 ? iter.ins : 0;
addSection(resultSections, len, ins);
if (ins > 0)
addInsert(resultInserted, resultSections, iter.text);
iter.forward(len);
pos += len;
}
let end = ranges[i++];
while (pos < end) {
if (iter.done)
break done;
let len = Math.min(iter.len, end - pos);
addSection(resultSections, len, -1);
addSection(filteredSections, len, iter.ins == -1 ? -1 : iter.off == 0 ? iter.ins : 0);
iter.forward(len);
pos += len;
}
}
return { changes: new ChangeSet(resultSections, resultInserted),
filtered: new ChangeDesc(filteredSections) };
}
/**
Serialize this change set to a JSON-representable value.
*/
toJSON() {
let parts = [];
for (let i = 0; i < this.sections.length; i += 2) {
let len = this.sections[i], ins = this.sections[i + 1];
if (ins < 0)
parts.push(len);
else if (ins == 0)
parts.push([len]);
else
parts.push([len].concat(this.inserted[i >> 1].toJSON()));
}
return parts;
}
/**
Create a change set for the given changes, for a document of the
given length, using `lineSep` as line separator.
*/
static of(changes, length, lineSep) {
let sections = [], inserted = [], pos = 0;
let total = null;
function flush(force = false) {
if (!force && !sections.length)
return;
if (pos < length)
addSection(sections, length - pos, -1);
let set = new ChangeSet(sections, inserted);
total = total ? total.compose(set.map(total)) : set;
sections = [];
inserted = [];
pos = 0;
}
function process(spec) {
if (Array.isArray(spec)) {
for (let sub of spec)
process(sub);
}
else if (spec instanceof ChangeSet) {
if (spec.length != length)
throw new RangeError(`Mismatched change set length (got ${spec.length}, expected ${length})`);
flush();
total = total ? total.compose(spec.map(total)) : spec;
}
else {
let { from, to = from, insert } = spec;
if (from > to || from < 0 || to > length)
throw new RangeError(`Invalid change range ${from} to ${to} (in doc of length ${length})`);
let insText = !insert ? Text.empty : typeof insert == "string" ? Text.of(insert.split(lineSep || DefaultSplit)) : insert;
let insLen = insText.length;
if (from == to && insLen == 0)
return;
if (from < pos)
flush();
if (from > pos)
addSection(sections, from - pos, -1);
addSection(sections, to - from, insLen);
addInsert(inserted, sections, insText);
pos = to;
}
}
process(changes);
flush(!total);
return total;
}
/**
Create an empty changeset of the given length.
*/
static empty(length) {
return new ChangeSet(length ? [length, -1] : [], []);
}
/**
Create a changeset from its JSON representation (as produced by
[`toJSON`](https://codemirror.net/6/docs/ref/#state.ChangeSet.toJSON).
*/
static fromJSON(json) {
if (!Array.isArray(json))
throw new RangeError("Invalid JSON representation of ChangeSet");
let sections = [], inserted = [];
for (let i = 0; i < json.length; i++) {
let part = json[i];
if (typeof part == "number") {
sections.push(part, -1);
}
else if (!Array.isArray(part) || typeof part[0] != "number" || part.some((e, i) => i && typeof e != "string")) {
throw new RangeError("Invalid JSON representation of ChangeSet");
}
else if (part.length == 1) {
sections.push(part[0], 0);
}
else {
while (inserted.length < i)
inserted.push(Text.empty);
inserted[i] = Text.of(part.slice(1));
sections.push(part[0], inserted[i].length);
}
}
return new ChangeSet(sections, inserted);
}
}
function addSection(sections, len, ins, forceJoin = false) {
if (len == 0 && ins <= 0)
return;
let last = sections.length - 2;
if (last >= 0 && ins <= 0 && ins == sections[last + 1])
sections[last] += len;
else if (len == 0 && sections[last] == 0)
sections[last + 1] += ins;
else if (forceJoin) {
sections[last] += len;
sections[last + 1] += ins;
}
else
sections.push(len, ins);
}
function addInsert(values, sections, value) {
if (value.length == 0)
return;
let index = (sections.length - 2) >> 1;
if (index < values.length) {
values[values.length - 1] = values[values.length - 1].append(value);
}
else {
while (values.length < index)
values.push(Text.empty);
values.push(value);
}
}
function iterChanges(desc, f, individual) {
let inserted = desc.inserted;
for (let posA = 0, posB = 0, i = 0; i < desc.sections.length;) {
let len = desc.sections[i++], ins = desc.sections[i++];
if (ins < 0) {
posA += len;
posB += len;
}
else {
let endA = posA, endB = posB, text = Text.empty;
for (;;) {
endA += len;
endB += ins;
if (ins && inserted)
text = text.append(inserted[(i - 2) >> 1]);
if (individual || i == desc.sections.length || desc.sections[i + 1] < 0)
break;
len = desc.sections[i++];
ins = desc.sections[i++];
}
f(posA, endA, posB, endB, text);
posA = endA;
posB = endB;
}
}
}
function mapSet(setA, setB, before, mkSet = false) {
let sections = [], insert = mkSet ? [] : null;
let a = new SectionIter(setA), b = new SectionIter(setB);
for (let posA = 0, posB = 0;;) {
if (a.ins == -1) {
posA += a.len;
a.next();
}
else if (b.ins == -1 && posB < posA) {
let skip = Math.min(b.len, posA - posB);
b.forward(skip);
addSection(sections, skip, -1);
posB += skip;
}
else if (b.ins >= 0 && (a.done || posB < posA || posB == posA && (b.len < a.len || b.len == a.len && !before))) {
addSection(sections, b.ins, -1);
while (posA > posB && !a.done && posA + a.len < posB + b.len) {
posA += a.len;
a.next();
}
posB += b.len;
b.next();
}
else if (a.ins >= 0) {
let len = 0, end = posA + a.len;
for (;;) {
if (b.ins >= 0 && posB > posA && posB + b.len < end) {
len += b.ins;
posB += b.len;
b.next();
}
else if (b.ins == -1 && posB < end) {
let skip = Math.min(b.len, end - posB);
len += skip;
b.forward(skip);
posB += skip;
}
else {
break;
}
}
addSection(sections, len, a.ins);
if (insert)
addInsert(insert, sections, a.text);
posA = end;
a.next();
}
else if (a.done && b.done) {
return insert ? new ChangeSet(sections, insert) : new ChangeDesc(sections);
}
else {
throw new Error("Mismatched change set lengths");
}
}
}
function composeSets(setA, setB, mkSet = false) {
let sections = [];
let insert = mkSet ? [] : null;
let a = new SectionIter(setA), b = new SectionIter(setB);
for (let open = false;;) {
if (a.done && b.done) {
return insert ? new ChangeSet(sections, insert) : new ChangeDesc(sections);
}
else if (a.ins == 0) { // Deletion in A
addSection(sections, a.len, 0, open);
a.next();
}
else if (b.len == 0 && !b.done) { // Insertion in B
addSection(sections, 0, b.ins, open);
if (insert)
addInsert(insert, sections, b.text);
b.next();
}
else if (a.done || b.done) {
throw new Error("Mismatched change set lengths");
}
else {
let len = Math.min(a.len2, b.len), sectionLen = sections.length;
if (a.ins == -1) {
let insB = b.ins == -1 ? -1 : b.off ? 0 : b.ins;
addSection(sections, len, insB, open);
if (insert && insB)
addInsert(insert, sections, b.text);
}
else if (b.ins == -1) {
addSection(sections, a.off ? 0 : a.len, len, open);
if (insert)
addInsert(insert, sections, a.textBit(len));
}
else {
addSection(sections, a.off ? 0 : a.len, b.off ? 0 : b.ins, open);
if (insert && !b.off)
addInsert(insert, sections, b.text);
}
open = (a.ins > len || b.ins >= 0 && b.len > len) && (open || sections.length > sectionLen);
a.forward2(len);
b.forward(len);
}
}
}
class SectionIter {
constructor(set) {
this.set = set;
this.i = 0;
this.next();
}
next() {
let { sections } = this.set;
if (this.i < sections.length) {
this.len = sections[this.i++];
this.ins = sections[this.i++];
}
else {
this.len = 0;
this.ins = -2;
}
this.off = 0;
}
get done() { return this.ins == -2; }
get len2() { return this.ins < 0 ? this.len : this.ins; }
get text() {
let { inserted } = this.set, index = (this.i - 2) >> 1;
return index >= inserted.length ? Text.empty : inserted[index];
}
textBit(len) {
let { inserted } = this.set, index = (this.i - 2) >> 1;
return index >= inserted.length && !len ? Text.empty
: inserted[index].slice(this.off, len == null ? undefined : this.off + len);
}
forward(len) {
if (len == this.len)
this.next();
else {
this.len -= len;
this.off += len;
}
}
forward2(len) {
if (this.ins == -1)
this.forward(len);
else if (len == this.ins)
this.next();
else {
this.ins -= len;
this.off += len;
}
}
}
/**
A single selection range. When
[`allowMultipleSelections`](https://codemirror.net/6/docs/ref/#state.EditorState^allowMultipleSelections)
is enabled, a [selection](https://codemirror.net/6/docs/ref/#state.EditorSelection) may hold
multiple ranges. By default, selections hold exactly one range.
*/
class SelectionRange {
/**
@internal
*/
constructor(
/**
The lower boundary of the range.
*/
from,
/**
The upper boundary of the range.
*/
to, flags) {
this.from = from;
this.to = to;
this.flags = flags;
}
/**
The anchor of the range—the side that doesn't move when you
extend it.
*/
get anchor() { return this.flags & 16 /* Inverted */ ? this.to : this.from; }
/**
The head of the range, which is moved when the range is
[extended](https://codemirror.net/6/docs/ref/#state.SelectionRange.extend).
*/
get head() { return this.flags & 16 /* Inverted */ ? this.from : this.to; }
/**
True when `anchor` and `head` are at the same position.
*/
get empty() { return this.from == this.to; }
/**
If this is a cursor that is explicitly associated with the
character on one of its sides, this returns the side. -1 means
the character before its position, 1 the character after, and 0
means no association.
*/
get assoc() { return this.flags & 4 /* AssocBefore */ ? -1 : this.flags & 8 /* AssocAfter */ ? 1 : 0; }
/**
The bidirectional text level associated with this cursor, if
any.
*/
get bidiLevel() {
let level = this.flags & 3 /* BidiLevelMask */;
return level == 3 ? null : level;
}
/**
The goal column (stored vertical offset) associated with a
cursor. This is used to preserve the vertical position when
[moving](https://codemirror.net/6/docs/ref/#view.EditorView.moveVertically) across
lines of different length.
*/
get goalColumn() {
let value = this.flags >> 5 /* GoalColumnOffset */;
return value == 33554431 /* NoGoalColumn */ ? undefined : value;
}
/**
Map this range through a change, producing a valid range in the
updated document.
*/
map(change, assoc = -1) {
let from = change.mapPos(this.from, assoc), to = change.mapPos(this.to, assoc);
return from == this.from && to == this.to ? this : new SelectionRange(from, to, this.flags);
}
/**
Extend this range to cover at least `from` to `to`.
*/
extend(from, to = from) {
if (from <= this.anchor && to >= this.anchor)
return EditorSelection.range(from, to);
let head = Math.abs(from - this.anchor) > Math.abs(to - this.anchor) ? from : to;
return EditorSelection.range(this.anchor, head);
}
/**
Compare this range to another range.
*/
eq(other) {
return this.anchor == other.anchor && this.head == other.head;
}
/**
Return a JSON-serializable object representing the range.
*/
toJSON() { return { anchor: this.anchor, head: this.head }; }
/**
Convert a JSON representation of a range to a `SelectionRange`
instance.
*/
static fromJSON(json) {
if (!json || typeof json.anchor != "number" || typeof json.head != "number")
throw new RangeError("Invalid JSON representation for SelectionRange");
return EditorSelection.range(json.anchor, json.head);
}
}
/**
An editor selection holds one or more selection ranges.
*/
class EditorSelection {
/**
@internal
*/
constructor(
/**
The ranges in the selection, sorted by position. Ranges cannot
overlap (but they may touch, if they aren't empty).
*/
ranges,
/**
The index of the _main_ range in the selection (which is
usually the range that was added last).
*/
mainIndex = 0) {
this.ranges = ranges;
this.mainIndex = mainIndex;
}
/**
Map a selection through a change. Used to adjust the selection
position for changes.
*/
map(change, assoc = -1) {
if (change.empty)
return this;
return EditorSelection.create(this.ranges.map(r => r.map(change, assoc)), this.mainIndex);
}
/**
Compare this selection to another selection.
*/
eq(other) {
if (this.ranges.length != other.ranges.length ||
this.mainIndex != other.mainIndex)
return false;
for (let i = 0; i < this.ranges.length; i++)
if (!this.ranges[i].eq(other.ranges[i]))
return false;
return true;
}
/**
Get the primary selection range. Usually, you should make sure
your code applies to _all_ ranges, by using methods like
[`changeByRange`](https://codemirror.net/6/docs/ref/#state.EditorState.changeByRange).
*/
get main() { return this.ranges[this.mainIndex]; }
/**
Make sure the selection only has one range. Returns a selection
holding only the main range from this selection.
*/
asSingle() {
return this.ranges.length == 1 ? this : new EditorSelection([this.main]);
}
/**
Extend this selection with an extra range.
*/
addRange(range, main = true) {
return EditorSelection.create([range].concat(this.ranges), main ? 0 : this.mainIndex + 1);
}
/**
Replace a given range with another range, and then normalize the
selection to merge and sort ranges if necessary.
*/
replaceRange(range, which = this.mainIndex) {
let ranges = this.ranges.slice();
ranges[which] = range;
return EditorSelection.create(ranges, this.mainIndex);
}
/**
Convert this selection to an object that can be serialized to
JSON.
*/
toJSON() {
return { ranges: this.ranges.map(r => r.toJSON()), main: this.mainIndex };
}
/**
Create a selection from a JSON representation.
*/
static fromJSON(json) {
if (!json || !Array.isArray(json.ranges) || typeof json.main != "number" || json.main >= json.ranges.length)
throw new RangeError("Invalid JSON representation for EditorSelection");
return new EditorSelection(json.ranges.map((r) => SelectionRange.fromJSON(r)), json.main);
}
/**
Create a selection holding a single range.
*/
static single(anchor, head = anchor) {
return new EditorSelection([EditorSelection.range(anchor, head)], 0);
}
/**
Sort and merge the given set of ranges, creating a valid
selection.
*/
static create(ranges, mainIndex = 0) {
if (ranges.length == 0)
throw new RangeError("A selection needs at least one range");
for (let pos = 0, i = 0; i < ranges.length; i++) {
let range = ranges[i];
if (range.empty ? range.from <= pos : range.from < pos)
return normalized(ranges.slice(), mainIndex);
pos = range.to;
}
return new EditorSelection(ranges, mainIndex);
}
/**
Create a cursor selection range at the given position. You can
safely ignore the optional arguments in most situations.
*/
static cursor(pos, assoc = 0, bidiLevel, goalColumn) {
return new SelectionRange(pos, pos, (assoc == 0 ? 0 : assoc < 0 ? 4 /* AssocBefore */ : 8 /* AssocAfter */) |
(bidiLevel == null ? 3 : Math.min(2, bidiLevel)) |
((goalColumn !== null && goalColumn !== void 0 ? goalColumn : 33554431 /* NoGoalColumn */) << 5 /* GoalColumnOffset */));
}
/**
Create a selection range.
*/
static range(anchor, head, goalColumn) {
let goal = (goalColumn !== null && goalColumn !== void 0 ? goalColumn : 33554431 /* NoGoalColumn */) << 5 /* GoalColumnOffset */;
return head < anchor ? new SelectionRange(head, anchor, 16 /* Inverted */ | goal) : new SelectionRange(anchor, head, goal);
}
}
function normalized(ranges, mainIndex = 0) {
let main = ranges[mainIndex];
ranges.sort((a, b) => a.from - b.from);
mainIndex = ranges.indexOf(main);
for (let i = 1; i < ranges.length; i++) {
let range = ranges[i], prev = ranges[i - 1];
if (range.empty ? range.from <= prev.to : range.from < prev.to) {
let from = prev.from, to = Math.max(range.to, prev.to);
if (i <= mainIndex)
mainIndex--;
ranges.splice(--i, 2, range.anchor > range.head ? EditorSelection.range(to, from) : EditorSelection.range(from, to));
}
}
return new EditorSelection(ranges, mainIndex);
}
function checkSelection(selection, docLength) {
for (let range of selection.ranges)
if (range.to > docLength)
throw new RangeError("Selection points outside of document");
}
let nextID = 0;
/**
A facet is a labeled value that is associated with an editor
state. It takes inputs from any number of extensions, and combines
those into a single output value.
Examples of facets are the [theme](https://codemirror.net/6/docs/ref/#view.EditorView^theme) styles
associated with an editor or the [tab
size](https://codemirror.net/6/docs/ref/#state.EditorState^tabSize) (which is reduced to a single
value, using the input with the hightest precedence).
*/
class Facet {
constructor(
/**
@internal
*/
combine,
/**
@internal
*/
compareInput,
/**
@internal
*/
compare, isStatic,
/**
@internal
*/
extensions) {
this.combine = combine;
this.compareInput = compareInput;
this.compare = compare;
this.isStatic = isStatic;
this.extensions = extensions;
/**
@internal
*/
this.id = nextID++;
this.default = combine([]);
}
/**
Define a new facet.
*/
static define(config = {}) {
return new Facet(config.combine || ((a) => a), config.compareInput || ((a, b) => a === b), config.compare || (!config.combine ? sameArray : (a, b) => a === b), !!config.static, config.enables);
}
/**
Returns an extension that adds the given value for this facet.
*/
of(value) {
return new FacetProvider([], this, 0 /* Static */, value);
}
/**
Create an extension that computes a value for the facet from a
state. You must take care to declare the parts of the state that
this value depends on, since your function is only called again
for a new state when one of those parts changed.
In most cases, you'll want to use the
[`provide`](https://codemirror.net/6/docs/ref/#state.StateField^define^config.provide) option when
defining a field instead.
*/
compute(deps, get) {
if (this.isStatic)
throw new Error("Can't compute a static facet");
return new FacetProvider(deps, this, 1 /* Single */, get);
}
/**
Create an extension that computes zero or more values for this
facet from a state.
*/
computeN(deps, get) {
if (this.isStatic)
throw new Error("Can't compute a static facet");
return new FacetProvider(deps, this, 2 /* Multi */, get);
}
from(field, get) {
if (!get)
get = x => x;
return this.compute([field], state => get(state.field(field)));
}
}
function sameArray(a, b) {
return a == b || a.length == b.length && a.every((e, i) => e === b[i]);
}
class FacetProvider {
constructor(dependencies, facet, type, value) {
this.dependencies = dependencies;
this.facet = facet;
this.type = type;
this.value = value;
this.id = nextID++;
}
dynamicSlot(addresses) {
var _a;
let getter = this.value;
let compare = this.facet.compareInput;
let idx = addresses[this.id] >> 1, multi = this.type == 2 /* Multi */;
let depDoc = false, depSel = false, depAddrs = [];
for (let dep of this.dependencies) {
if (dep == "doc")
depDoc = true;
else if (dep == "selection")
depSel = true;
else if ((((_a = addresses[dep.id]) !== null && _a !== void 0 ? _a : 1) & 1) == 0)
depAddrs.push(addresses[dep.id]);
}
return (state, tr) => {
if (!tr || tr.reconfigured) {
state.values[idx] = getter(state);
return 1 /* Changed */;
}
else {
let depChanged = (depDoc && tr.docChanged) || (depSel && (tr.docChanged || tr.selection)) ||
depAddrs.some(addr => (ensureAddr(state, addr) & 1 /* Changed */) > 0);
if (!depChanged)
return 0;
let newVal = getter(state), oldVal = tr.startState.values[idx];
if (multi ? compareArray(newVal, oldVal, compare) : compare(newVal, oldVal))
return 0;
state.values[idx] = newVal;
return 1 /* Changed */;
}
};
}
}
function compareArray(a, b, compare) {
if (a.length != b.length)
return false;
for (let i = 0; i < a.length; i++)
if (!compare(a[i], b[i]))
return false;
return true;
}
function dynamicFacetSlot(addresses, facet, providers) {
let providerAddrs = providers.map(p => addresses[p.id]);
let providerTypes = providers.map(p => p.type);
let dynamic = providerAddrs.filter(p => !(p & 1));
let idx = addresses[facet.id] >> 1;
return (state, tr) => {
let oldAddr = !tr ? null : tr.reconfigured ? tr.startState.config.address[facet.id] : idx << 1;
let changed = oldAddr == null;
for (let dynAddr of dynamic) {
if (ensureAddr(state, dynAddr) & 1 /* Changed */)
changed = true;
}
if (!changed)
return 0;
let values = [];
for (let i = 0; i < providerAddrs.length; i++) {
let value = getAddr(state, providerAddrs[i]);
if (providerTypes[i] == 2 /* Multi */)
for (let val of value)
values.push(val);
else
values.push(value);
}
let newVal = facet.combine(values);
if (oldAddr != null && facet.compare(newVal, getAddr(tr.startState, oldAddr)))
return 0;
state.values[idx] = newVal;
return 1 /* Changed */;
};
}
function maybeIndex(state, id) {
let found = state.config.address[id];
return found == null ? null : found >> 1;
}
const initField = /*@__PURE__*/Facet.define({ static: true });
/**
Fields can store additional information in an editor state, and
keep it in sync with the rest of the state.
*/
class StateField {
constructor(
/**
@internal
*/
id, createF, updateF, compareF,
/**
@internal
*/
spec) {
this.id = id;
this.createF = createF;
this.updateF = updateF;
this.compareF = compareF;
this.spec = spec;
/**
@internal
*/
this.provides = undefined;
}
/**
Define a state field.
*/
static define(config) {
let field = new StateField(nextID++, config.create, config.update, config.compare || ((a, b) => a === b), config);
if (config.provide)
field.provides = config.provide(field);
return field;
}
create(state) {
let init = state.facet(initField).find(i => i.field == this);
return ((init === null || init === void 0 ? void 0 : init.create) || this.createF)(state);
}
/**
@internal
*/
slot(addresses) {
let idx = addresses[this.id] >> 1;
return (state, tr) => {
if (!tr || (tr.reconfigured && maybeIndex(tr.startState, this.id) == null)) {
state.values[idx] = this.create(state);
return 1 /* Changed */;
}
let oldVal, changed = 0;
if (tr.reconfigured) {
oldVal = tr.startState.values[maybeIndex(tr.startState, this.id)];
changed = 1 /* Changed */;
}
else {
oldVal = tr.startState.values[idx];
}
let value = this.updateF(oldVal, tr);
if (!changed && !this.compareF(oldVal, value))
changed = 1 /* Changed */;
if (changed)
state.values[idx] = value;
return changed;
};
}
/**
Returns an extension that enables this field and overrides the
way it is initialized. Can be useful when you need to provide a
non-default starting value for the field.
*/
init(create) {
return [this, initField.of({ field: this, create })];
}
/**
State field instances can be used as
[`Extension`](https://codemirror.net/6/docs/ref/#state.Extension) values to enable the field in a
given state.
*/
get extension() { return this; }
}
const Prec_ = { fallback: 3, default: 2, extend: 1, override: 0 };
function prec(value) {
return (ext) => new PrecExtension(ext, value);
}
/**
By default extensions are registered in the order they are found
in the flattened form of nested array that was provided.
Individual extension values can be assigned a precedence to
override this. Extensions that do not have a precedence set get
the precedence of the nearest parent with a precedence, or
[`default`](https://codemirror.net/6/docs/ref/#state.Prec.default) if there is no such parent. The
final ordering of extensions is determined by first sorting by
precedence and then by order within each precedence.
*/
const Prec = {
/**
A precedence below the default precedence, which will cause
default-precedence extensions to override it even if they are
specified later in the extension ordering.
*/
fallback: /*@__PURE__*/prec(Prec_.fallback),
/**
The regular default precedence.
*/
default: /*@__PURE__*/prec(Prec_.default),
/**
A higher-than-default precedence.
*/
extend: /*@__PURE__*/prec(Prec_.extend),
/**
Precedence above the `default` and `extend` precedences.
*/
override: /*@__PURE__*/prec(Prec_.override)
};
class PrecExtension {
constructor(inner, prec) {
this.inner = inner;
this.prec = prec;
}
}
/**
Extension compartments can be used to make a configuration
dynamic. By [wrapping](https://codemirror.net/6/docs/ref/#state.Compartment.of) part of your
configuration in a compartment, you can later
[replace](https://codemirror.net/6/docs/ref/#state.Compartment.reconfigure) that part through a
transaction.
*/
class Compartment {
/**
Create an instance of this compartment to add to your [state
configuration](https://codemirror.net/6/docs/ref/#state.EditorStateConfig.extensions).
*/
of(ext) { return new CompartmentInstance(this, ext); }
/**
Create an [effect](https://codemirror.net/6/docs/ref/#state.TransactionSpec.effects) that
reconfigures this compartment.
*/
reconfigure(content) {
return Compartment.reconfigure.of({ compartment: this, extension: content });
}
/**
Get the current content of the compartment in the state, or
`undefined` if it isn't present.
*/
get(state) {
return state.config.compartments.get(this);
}
}
class CompartmentInstance {
constructor(compartment, inner) {
this.compartment = compartment;
this.inner = inner;
}
}
class Configuration {
constructor(base, compartments, dynamicSlots, address, staticValues) {
this.base = base;
this.compartments = compartments;
this.dynamicSlots = dynamicSlots;
this.address = address;
this.staticValues = staticValues;
this.statusTemplate = [];
while (this.statusTemplate.length < dynamicSlots.length)
this.statusTemplate.push(0 /* Uninitialized */);
}
staticFacet(facet) {
let addr = this.address[facet.id];
return addr == null ? facet.default : this.staticValues[addr >> 1];
}
static resolve(base, compartments, oldState) {
let fields = [];
let facets = Object.create(null);
let newCompartments = new Map();
for (let ext of flatten(base, compartments, newCompartments)) {
if (ext instanceof StateField)
fields.push(ext);
else
(facets[ext.facet.id] || (facets[ext.facet.id] = [])).push(ext);
}
let address = Object.create(null);
let staticValues = [];
let dynamicSlots = [];
for (let field of fields) {
address[field.id] = dynamicSlots.length << 1;
dynamicSlots.push(a => field.slot(a));
}
for (let id in facets) {
let providers = facets[id], facet = providers[0].facet;
if (providers.every(p => p.type == 0 /* Static */)) {
address[facet.id] = (staticValues.length << 1) | 1;
let value = facet.combine(providers.map(p => p.value));
let oldAddr = oldState ? oldState.config.address[facet.id] : null;
if (oldAddr != null) {
let oldVal = getAddr(oldState, oldAddr);
if (facet.compare(value, oldVal))
value = oldVal;
}
staticValues.push(value);
}
else {
for (let p of providers) {
if (p.type == 0 /* Static */) {
address[p.id] = (staticValues.length << 1) | 1;
staticValues.push(p.value);
}
else {
address[p.id] = dynamicSlots.length << 1;
dynamicSlots.push(a => p.dynamicSlot(a));
}
}
address[facet.id] = dynamicSlots.length << 1;
dynamicSlots.push(a => dynamicFacetSlot(a, facet, providers));
}
}
return new Configuration(base, newCompartments, dynamicSlots.map(f => f(address)), address, staticValues);
}
}
function flatten(extension, compartments, newCompartments) {
let result = [[], [], [], []];
let seen = new Map();
function inner(ext, prec) {
let known = seen.get(ext);
if (known != null) {
if (known >= prec)
return;
let found = result[known].indexOf(ext);
if (found > -1)
result[known].splice(found, 1);
if (ext instanceof CompartmentInstance)
newCompartments.delete(ext.compartment);
}
seen.set(ext, prec);
if (Array.isArray(ext)) {
for (let e of ext)
inner(e, prec);
}
else if (ext instanceof CompartmentInstance) {
if (newCompartments.has(ext.compartment))
throw new RangeError(`Duplicate use of compartment in extensions`);
let content = compartments.get(ext.compartment) || ext.inner;
newCompartments.set(ext.compartment, content);
inner(content, prec);
}
else if (ext instanceof PrecExtension) {
inner(ext.inner, ext.prec);
}
else if (ext instanceof StateField) {
result[prec].push(ext);
if (ext.provides)
inner(ext.provides, prec);
}
else if (ext instanceof FacetProvider) {
result[prec].push(ext);
if (ext.facet.extensions)
inner(ext.facet.extensions, prec);
}
else {
let content = ext.extension;
if (!content)
throw new Error(`Unrecognized extension value in extension set (${ext}). This sometimes happens because multiple instances of @codemirror/state are loaded, breaking instanceof checks.`);
inner(content, prec);
}
}
inner(extension, Prec_.default);
return result.reduce((a, b) => a.concat(b));
}
function ensureAddr(state, addr) {
if (addr & 1)
return 2 /* Computed */;
let idx = addr >> 1;
let status = state.status[idx];
if (status == 4 /* Computing */)
throw new Error("Cyclic dependency between fields and/or facets");
if (status & 2 /* Computed */)
return status;
state.status[idx] = 4 /* Computing */;
let changed = state.config.dynamicSlots[idx](state, state.applying);
return state.status[idx] = 2 /* Computed */ | changed;
}
function getAddr(state, addr) {
return addr & 1 ? state.config.staticValues[addr >> 1] : state.values[addr >> 1];
}
const languageData = /*@__PURE__*/Facet.define();
const allowMultipleSelections = /*@__PURE__*/Facet.define({
combine: values => values.some(v => v),
static: true
});
const lineSeparator = /*@__PURE__*/Facet.define({
combine: values => values.length ? values[0] : undefined,
static: true
});
const changeFilter = /*@__PURE__*/Facet.define();
const transactionFilter = /*@__PURE__*/Facet.define();
const transactionExtender = /*@__PURE__*/Facet.define();
const readOnly = /*@__PURE__*/Facet.define({
combine: values => values.length ? values[0] : false
});
/**
Annotations are tagged values that are used to add metadata to
transactions in an extensible way. They should be used to model
things that effect the entire transaction (such as its [time
stamp](https://codemirror.net/6/docs/ref/#state.Transaction^time) or information about its
[origin](https://codemirror.net/6/docs/ref/#state.Transaction^userEvent)). For effects that happen
_alongside_ the other changes made by the transaction, [state
effects](https://codemirror.net/6/docs/ref/#state.StateEffect) are more appropriate.
*/
class Annotation {
/**
@internal
*/
constructor(
/**
The annotation type.
*/
type,
/**
The value of this annotation.
*/
value) {
this.type = type;
this.value = value;
}
/**
Define a new type of annotation.
*/
static define() { return new AnnotationType(); }
}
/**
Marker that identifies a type of [annotation](https://codemirror.net/6/docs/ref/#state.Annotation).
*/
class AnnotationType {
/**
Create an instance of this annotation.
*/
of(value) { return new Annotation(this, value); }
}
/**
Representation of a type of state effect. Defined with
[`StateEffect.define`](https://codemirror.net/6/docs/ref/#state.StateEffect^define).
*/
class StateEffectType {
/**
@internal
*/
constructor(
// The `any` types in these function types are there to work
// around TypeScript issue #37631, where the type guard on
// `StateEffect.is` mysteriously stops working when these properly
// have type `Value`.
/**
@internal
*/
map) {
this.map = map;
}
/**
Create a [state effect](https://codemirror.net/6/docs/ref/#state.StateEffect) instance of this
type.
*/
of(value) { return new StateEffect(this, value); }
}
/**
State effects can be used to represent additional effects
associated with a [transaction](https://codemirror.net/6/docs/ref/#state.Transaction.effects). They
are often useful to model changes to custom [state
fields](https://codemirror.net/6/docs/ref/#state.StateField), when those changes aren't implicit in
document or selection changes.
*/
class StateEffect {
/**
@internal
*/
constructor(
/**
@internal
*/
type,
/**
The value of this effect.
*/
value) {
this.type = type;
this.value = value;
}
/**
Map this effect through a position mapping. Will return
`undefined` when that ends up deleting the effect.
*/
map(mapping) {
let mapped = this.type.map(this.value, mapping);
return mapped === undefined ? undefined : mapped == this.value ? this : new StateEffect(this.type, mapped);
}
/**
Tells you whether this effect object is of a given
[type](https://codemirror.net/6/docs/ref/#state.StateEffectType).
*/
is(type) { retur