UNPKG

@uiw/react-codemirror

Version:
1,272 lines (1,250 loc) 209 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(require("react"), require("@codemirror/basic-setup"), require("@codemirror/state"), require("@codemirror/theme-one-dark"), require("@codemirror/view")); else if(typeof define === 'function' && define.amd) define(["react", , , , ], factory); else if(typeof exports === 'object') exports["@uiw/codemirror"] = factory(require("react"), require("@codemirror/basic-setup"), require("@codemirror/state"), require("@codemirror/theme-one-dark"), require("@codemirror/view")); else root["@uiw/codemirror"] = factory(root["React"], root["CM"]["@codemirror/basic-setup"], root["CM"]["@codemirror/state"], root["CM"]["@codemirror/theme-one-dark"], root["CM"]["@codemirror/view"]); })(self, function(__WEBPACK_EXTERNAL_MODULE__787__, __WEBPACK_EXTERNAL_MODULE__573__, __WEBPACK_EXTERNAL_MODULE__242__, __WEBPACK_EXTERNAL_MODULE__362__, __WEBPACK_EXTERNAL_MODULE__105__) { return /******/ (() => { // webpackBootstrap /******/ "use strict"; /******/ var __webpack_modules__ = ({ /***/ 497: /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { // EXPORTS __webpack_require__.d(__webpack_exports__, { "U": () => (/* binding */ useCodeMirror) }); ;// CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/arrayWithHoles.js function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } ;// CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/iterableToArrayLimit.js function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } ;// CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/arrayLikeToArray.js function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } ;// CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/unsupportedIterableToArray.js function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } ;// CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/nonIterableRest.js function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } ;// CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/slicedToArray.js function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } // EXTERNAL MODULE: external {"root":"React","commonjs2":"react","commonjs":"react","amd":"react"} var external_root_React_commonjs2_react_commonjs_react_amd_react_ = __webpack_require__(787); // EXTERNAL MODULE: external {"root":["CM","@codemirror/basic-setup"],"commonjs":"@codemirror/basic-setup","commonjs2":"@codemirror/basic-setup"} var basic_setup_ = __webpack_require__(573); // EXTERNAL MODULE: external {"root":["CM","@codemirror/state"],"commonjs":"@codemirror/state","commonjs2":"@codemirror/state"} var state_ = __webpack_require__(242); ;// CONCATENATED MODULE: ./node_modules/@codemirror/text/dist/index.js // Compressed representation of the Grapheme_Cluster_Break=Extend // information from // http://www.unicode.org/Public/13.0.0/ucd/auxiliary/GraphemeBreakProperty.txt. // Each pair of elements represents a range, as an offet from the // previous range and a length. Numbers are in base-36, with the empty // string being a shorthand for 1. let extend = /*@__PURE__*/"lc,34,7n,7,7b,19,,,,2,,2,,,20,b,1c,l,g,,2t,7,2,6,2,2,,4,z,,u,r,2j,b,1m,9,9,,o,4,,9,,3,,5,17,3,3b,f,,w,1j,,,,4,8,4,,3,7,a,2,t,,1m,,,,2,4,8,,9,,a,2,q,,2,2,1l,,4,2,4,2,2,3,3,,u,2,3,,b,2,1l,,4,5,,2,4,,k,2,m,6,,,1m,,,2,,4,8,,7,3,a,2,u,,1n,,,,c,,9,,14,,3,,1l,3,5,3,,4,7,2,b,2,t,,1m,,2,,2,,3,,5,2,7,2,b,2,s,2,1l,2,,,2,4,8,,9,,a,2,t,,20,,4,,2,3,,,8,,29,,2,7,c,8,2q,,2,9,b,6,22,2,r,,,,,,1j,e,,5,,2,5,b,,10,9,,2u,4,,6,,2,2,2,p,2,4,3,g,4,d,,2,2,6,,f,,jj,3,qa,3,t,3,t,2,u,2,1s,2,,7,8,,2,b,9,,19,3,3b,2,y,,3a,3,4,2,9,,6,3,63,2,2,,1m,,,7,,,,,2,8,6,a,2,,1c,h,1r,4,1c,7,,,5,,14,9,c,2,w,4,2,2,,3,1k,,,2,3,,,3,1m,8,2,2,48,3,,d,,7,4,,6,,3,2,5i,1m,,5,ek,,5f,x,2da,3,3x,,2o,w,fe,6,2x,2,n9w,4,,a,w,2,28,2,7k,,3,,4,,p,2,5,,47,2,q,i,d,,12,8,p,b,1a,3,1c,,2,4,2,2,13,,1v,6,2,2,2,2,c,,8,,1b,,1f,,,3,2,2,5,2,,,16,2,8,,6m,,2,,4,,fn4,,kh,g,g,g,a6,2,gt,,6a,,45,5,1ae,3,,2,5,4,14,3,4,,4l,2,fx,4,ar,2,49,b,4w,,1i,f,1k,3,1d,4,2,2,1x,3,10,5,,8,1q,,c,2,1g,9,a,4,2,,2n,3,2,,,2,6,,4g,,3,8,l,2,1l,2,,,,,m,,e,7,3,5,5f,8,2,3,,,n,,29,,2,6,,,2,,,2,,2,6j,,2,4,6,2,,2,r,2,2d,8,2,,,2,2y,,,,2,6,,,2t,3,2,4,,5,77,9,,2,6t,,a,2,,,4,,40,4,2,2,4,,w,a,14,6,2,4,8,,9,6,2,3,1a,d,,2,ba,7,,6,,,2a,m,2,7,,2,,2,3e,6,3,,,2,,7,,,20,2,3,,,,9n,2,f0b,5,1n,7,t4,,1r,4,29,,f5k,2,43q,,,3,4,5,8,8,2,7,u,4,44,3,1iz,1j,4,1e,8,,e,,m,5,,f,11s,7,,h,2,7,,2,,5,79,7,c5,4,15s,7,31,7,240,5,gx7k,2o,3k,6o".split(",").map(s => s ? parseInt(s, 36) : 1); // Convert offsets into absolute values for (let i = 1; i < extend.length; i++) extend[i] += extend[i - 1]; function isExtendingChar(code) { for (let i = 1; i < extend.length; i += 2) if (extend[i] > code) return extend[i - 1] <= code; return false; } function isRegionalIndicator(code) { return code >= 0x1F1E6 && code <= 0x1F1FF; } const ZWJ = 0x200d; /** Returns a next grapheme cluster break _after_ (not equal to) `pos`, if `forward` is true, or before otherwise. Returns `pos` itself if no further cluster break is available in the string. Moves across surrogate pairs, extending characters (when `includeExtending` is true), characters joined with zero-width joiners, and flag emoji. */ function findClusterBreak(str, pos, forward = true, includeExtending = true) { return (forward ? nextClusterBreak : prevClusterBreak)(str, pos, includeExtending); } function nextClusterBreak(str, pos, includeExtending) { if (pos == str.length) return pos; // If pos is in the middle of a surrogate pair, move to its start if (pos && surrogateLow(str.charCodeAt(pos)) && surrogateHigh(str.charCodeAt(pos - 1))) pos--; let prev = codePointAt(str, pos); pos += codePointSize(prev); while (pos < str.length) { let next = codePointAt(str, pos); if (prev == ZWJ || next == ZWJ || includeExtending && isExtendingChar(next)) { pos += codePointSize(next); prev = next; } else if (isRegionalIndicator(next)) { let countBefore = 0, i = pos - 2; while (i >= 0 && isRegionalIndicator(codePointAt(str, i))) { countBefore++; i -= 2; } if (countBefore % 2 == 0) break; else pos += 2; } else { break; } } return pos; } function prevClusterBreak(str, pos, includeExtending) { while (pos > 0) { let found = nextClusterBreak(str, pos - 2, includeExtending); if (found < pos) return found; pos--; } return 0; } function surrogateLow(ch) { return ch >= 0xDC00 && ch < 0xE000; } function surrogateHigh(ch) { return ch >= 0xD800 && ch < 0xDC00; } /** Find the code point at the given position in a string (like the [`codePointAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt) string method). */ function codePointAt(str, pos) { let code0 = str.charCodeAt(pos); if (!surrogateHigh(code0) || pos + 1 == str.length) return code0; let code1 = str.charCodeAt(pos + 1); if (!surrogateLow(code1)) return code0; return ((code0 - 0xd800) << 10) + (code1 - 0xdc00) + 0x10000; } /** Given a Unicode codepoint, return the JavaScript string that respresents it (like [`String.fromCodePoint`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint)). */ function fromCodePoint(code) { if (code <= 0xffff) return String.fromCharCode(code); code -= 0x10000; return String.fromCharCode((code >> 10) + 0xd800, (code & 1023) + 0xdc00); } /** The first character that takes up two positions in a JavaScript string. It is often useful to compare with this after calling `codePointAt`, to figure out whether your character takes up 1 or 2 index positions. */ function codePointSize(code) { return code < 0x10000 ? 1 : 2; } /** Count the column position at the given offset into the string, taking extending characters and tab size into account. */ function dist_countColumn(string, tabSize, to = string.length) { let n = 0; for (let i = 0; i < to;) { if (string.charCodeAt(i) == 9) { n += tabSize - (n % tabSize); i++; } else { n++; i = findClusterBreak(string, i); } } return n; } /** Find the offset that corresponds to the given column position in a string, taking extending characters and tab size into account. By default, the string length is returned when it is too short to reach the column. Pass `strict` true to make it return -1 in that situation. */ function findColumn(string, col, tabSize, strict) { for (let i = 0, n = 0;;) { if (n >= col) return i; if (i == string.length) break; n += string.charCodeAt(i) == 9 ? tabSize - (n % tabSize) : 1; i = findClusterBreak(string, i); } return strict === true ? -1 : string.length; } /** The data structure for documents. */ class dist_Text { /** @internal */ constructor() { } /** Get the line description around the given position. */ lineAt(pos) { if (pos < 0 || pos > this.length) throw new RangeError(`Invalid position ${pos} in document of length ${this.length}`); return this.lineInner(pos, false, 1, 0); } /** Get the description for the given (1-based) line number. */ line(n) { if (n < 1 || n > this.lines) throw new RangeError(`Invalid line number ${n} in ${this.lines}-line document`); return this.lineInner(n, true, 1, 0); } /** Replace a range of the text with the given content. */ replace(from, to, text) { let parts = []; this.decompose(0, from, parts, 2 /* To */); if (text.length) text.decompose(0, text.length, parts, 1 /* From */ | 2 /* To */); this.decompose(to, this.length, parts, 1 /* From */); return TextNode.from(parts, this.length - (to - from) + text.length); } /** Append another document to this one. */ append(other) { return this.replace(this.length, this.length, other); } /** Retrieve the text between the given points. */ slice(from, to = this.length) { let parts = []; this.decompose(from, to, parts, 0); return TextNode.from(parts, to - from); } /** Test whether this text is equal to another instance. */ eq(other) { if (other == this) return true; if (other.length != this.length || other.lines != this.lines) return false; let start = this.scanIdentical(other, 1), end = this.length - this.scanIdentical(other, -1); let a = new RawTextCursor(this), b = new RawTextCursor(other); for (let skip = start, pos = start;;) { a.next(skip); b.next(skip); skip = 0; if (a.lineBreak != b.lineBreak || a.done != b.done || a.value != b.value) return false; pos += a.value.length; if (a.done || pos >= end) return true; } } /** Iterate over the text. When `dir` is `-1`, iteration happens from end to start. This will return lines and the breaks between them as separate strings, and for long lines, might split lines themselves into multiple chunks as well. */ iter(dir = 1) { return new RawTextCursor(this, dir); } /** Iterate over a range of the text. When `from` > `to`, the iterator will run in reverse. */ iterRange(from, to = this.length) { return new PartialTextCursor(this, from, to); } /** Return a cursor that iterates over the given range of lines, _without_ returning the line breaks between, and yielding empty strings for empty lines. When `from` and `to` are given, they should be 1-based line numbers. */ iterLines(from, to) { let inner; if (from == null) { inner = this.iter(); } else { if (to == null) to = this.lines + 1; let start = this.line(from).from; inner = this.iterRange(start, Math.max(start, to == this.lines + 1 ? this.length : to <= 1 ? 0 : this.line(to - 1).to)); } return new LineCursor(inner); } /** @internal */ toString() { return this.sliceString(0); } /** Convert the document to an array of lines (which can be deserialized again via [`Text.of`](https://codemirror.net/6/docs/ref/#text.Text^of)). */ toJSON() { let lines = []; this.flatten(lines); return lines; } /** Create a `Text` instance for the given array of lines. */ static of(text) { if (text.length == 0) throw new RangeError("A document must have at least one line"); if (text.length == 1 && !text[0]) return dist_Text.empty; return text.length <= 32 /* Branch */ ? new TextLeaf(text) : TextNode.from(TextLeaf.split(text, [])); } } // Leaves store an array of line strings. There are always line breaks // between these strings. Leaves are limited in size and have to be // contained in TextNode instances for bigger documents. class TextLeaf extends dist_Text { constructor(text, length = textLength(text)) { super(); this.text = text; this.length = length; } get lines() { return this.text.length; } get children() { return null; } lineInner(target, isLine, line, offset) { for (let i = 0;; i++) { let string = this.text[i], end = offset + string.length; if ((isLine ? line : end) >= target) return new Line(offset, end, line, string); offset = end + 1; line++; } } decompose(from, to, target, open) { let text = from <= 0 && to >= this.length ? this : new TextLeaf(sliceText(this.text, from, to), Math.min(to, this.length) - Math.max(0, from)); if (open & 1 /* From */) { let prev = target.pop(); let joined = appendText(text.text, prev.text.slice(), 0, text.length); if (joined.length <= 32 /* Branch */) { target.push(new TextLeaf(joined, prev.length + text.length)); } else { let mid = joined.length >> 1; target.push(new TextLeaf(joined.slice(0, mid)), new TextLeaf(joined.slice(mid))); } } else { target.push(text); } } replace(from, to, text) { if (!(text instanceof TextLeaf)) return super.replace(from, to, text); let lines = appendText(this.text, appendText(text.text, sliceText(this.text, 0, from)), to); let newLen = this.length + text.length - (to - from); if (lines.length <= 32 /* Branch */) return new TextLeaf(lines, newLen); return TextNode.from(TextLeaf.split(lines, []), newLen); } sliceString(from, to = this.length, lineSep = "\n") { let result = ""; for (let pos = 0, i = 0; pos <= to && i < this.text.length; i++) { let line = this.text[i], end = pos + line.length; if (pos > from && i) result += lineSep; if (from < end && to > pos) result += line.slice(Math.max(0, from - pos), to - pos); pos = end + 1; } return result; } flatten(target) { for (let line of this.text) target.push(line); } scanIdentical() { return 0; } static split(text, target) { let part = [], len = -1; for (let line of text) { part.push(line); len += line.length + 1; if (part.length == 32 /* Branch */) { target.push(new TextLeaf(part, len)); part = []; len = -1; } } if (len > -1) target.push(new TextLeaf(part, len)); return target; } } // Nodes provide the tree structure of the `Text` type. They store a // number of other nodes or leaves, taking care to balance themselves // on changes. There are implied line breaks _between_ the children of // a node (but not before the first or after the last child). class TextNode extends dist_Text { constructor(children, length) { super(); this.children = children; this.length = length; this.lines = 0; for (let child of children) this.lines += child.lines; } lineInner(target, isLine, line, offset) { for (let i = 0;; i++) { let child = this.children[i], end = offset + child.length, endLine = line + child.lines - 1; if ((isLine ? endLine : end) >= target) return child.lineInner(target, isLine, line, offset); offset = end + 1; line = endLine + 1; } } decompose(from, to, target, open) { for (let i = 0, pos = 0; pos <= to && i < this.children.length; i++) { let child = this.children[i], end = pos + child.length; if (from <= end && to >= pos) { let childOpen = open & ((pos <= from ? 1 /* From */ : 0) | (end >= to ? 2 /* To */ : 0)); if (pos >= from && end <= to && !childOpen) target.push(child); else child.decompose(from - pos, to - pos, target, childOpen); } pos = end + 1; } } replace(from, to, text) { if (text.lines < this.lines) for (let i = 0, pos = 0; i < this.children.length; i++) { let child = this.children[i], end = pos + child.length; // Fast path: if the change only affects one child and the // child's size remains in the acceptable range, only update // that child if (from >= pos && to <= end) { let updated = child.replace(from - pos, to - pos, text); let totalLines = this.lines - child.lines + updated.lines; if (updated.lines < (totalLines >> (5 /* BranchShift */ - 1)) && updated.lines > (totalLines >> (5 /* BranchShift */ + 1))) { let copy = this.children.slice(); copy[i] = updated; return new TextNode(copy, this.length - (to - from) + text.length); } return super.replace(pos, end, updated); } pos = end + 1; } return super.replace(from, to, text); } sliceString(from, to = this.length, lineSep = "\n") { let result = ""; for (let i = 0, pos = 0; i < this.children.length && pos <= to; i++) { let child = this.children[i], end = pos + child.length; if (pos > from && i) result += lineSep; if (from < end && to > pos) result += child.sliceString(from - pos, to - pos, lineSep); pos = end + 1; } return result; } flatten(target) { for (let child of this.children) child.flatten(target); } scanIdentical(other, dir) { if (!(other instanceof TextNode)) return 0; let length = 0; let [iA, iB, eA, eB] = dir > 0 ? [0, 0, this.children.length, other.children.length] : [this.children.length - 1, other.children.length - 1, -1, -1]; for (;; iA += dir, iB += dir) { if (iA == eA || iB == eB) return length; let chA = this.children[iA], chB = other.children[iB]; if (chA != chB) return length + chA.scanIdentical(chB, dir); length += chA.length + 1; } } static from(children, length = children.reduce((l, ch) => l + ch.length + 1, -1)) { let lines = 0; for (let ch of children) lines += ch.lines; if (lines < 32 /* Branch */) { let flat = []; for (let ch of children) ch.flatten(flat); return new TextLeaf(flat, length); } let chunk = Math.max(32 /* Branch */, lines >> 5 /* BranchShift */), maxChunk = chunk << 1, minChunk = chunk >> 1; let chunked = [], currentLines = 0, currentLen = -1, currentChunk = []; function add(child) { let last; if (child.lines > maxChunk && child instanceof TextNode) { for (let node of child.children) add(node); } else if (child.lines > minChunk && (currentLines > minChunk || !currentLines)) { flush(); chunked.push(child); } else if (child instanceof TextLeaf && currentLines && (last = currentChunk[currentChunk.length - 1]) instanceof TextLeaf && child.lines + last.lines <= 32 /* Branch */) { currentLines += child.lines; currentLen += child.length + 1; currentChunk[currentChunk.length - 1] = new TextLeaf(last.text.concat(child.text), last.length + 1 + child.length); } else { if (currentLines + child.lines > chunk) flush(); currentLines += child.lines; currentLen += child.length + 1; currentChunk.push(child); } } function flush() { if (currentLines == 0) return; chunked.push(currentChunk.length == 1 ? currentChunk[0] : TextNode.from(currentChunk, currentLen)); currentLen = -1; currentLines = currentChunk.length = 0; } for (let child of children) add(child); flush(); return chunked.length == 1 ? chunked[0] : new TextNode(chunked, length); } } dist_Text.empty = /*@__PURE__*/new TextLeaf([""], 0); function textLength(text) { let length = -1; for (let line of text) length += line.length + 1; return length; } function appendText(text, target, from = 0, to = 1e9) { for (let pos = 0, i = 0, first = true; i < text.length && pos <= to; i++) { let line = text[i], end = pos + line.length; if (end >= from) { if (end > to) line = line.slice(0, to - pos); if (pos < from) line = line.slice(from - pos); if (first) { target[target.length - 1] += line; first = false; } else target.push(line); } pos = end + 1; } return target; } function sliceText(text, from, to) { return appendText(text, [""], from, to); } class RawTextCursor { constructor(text, dir = 1) { this.dir = dir; this.done = false; this.lineBreak = false; this.value = ""; this.nodes = [text]; this.offsets = [dir > 0 ? 1 : (text instanceof TextLeaf ? text.text.length : text.children.length) << 1]; } nextInner(skip, dir) { this.done = this.lineBreak = false; for (;;) { let last = this.nodes.length - 1; let top = this.nodes[last], offsetValue = this.offsets[last], offset = offsetValue >> 1; let size = top instanceof TextLeaf ? top.text.length : top.children.length; if (offset == (dir > 0 ? size : 0)) { if (last == 0) { this.done = true; this.value = ""; return this; } if (dir > 0) this.offsets[last - 1]++; this.nodes.pop(); this.offsets.pop(); } else if ((offsetValue & 1) == (dir > 0 ? 0 : 1)) { this.offsets[last] += dir; if (skip == 0) { this.lineBreak = true; this.value = "\n"; return this; } skip--; } else if (top instanceof TextLeaf) { // Move to the next string let next = top.text[offset + (dir < 0 ? -1 : 0)]; this.offsets[last] += dir; if (next.length > Math.max(0, skip)) { this.value = skip == 0 ? next : dir > 0 ? next.slice(skip) : next.slice(0, next.length - skip); return this; } skip -= next.length; } else { let next = top.children[offset + (dir < 0 ? -1 : 0)]; if (skip > next.length) { skip -= next.length; this.offsets[last] += dir; } else { if (dir < 0) this.offsets[last]--; this.nodes.push(next); this.offsets.push(dir > 0 ? 1 : (next instanceof TextLeaf ? next.text.length : next.children.length) << 1); } } } } next(skip = 0) { if (skip < 0) { this.nextInner(-skip, (-this.dir)); skip = this.value.length; } return this.nextInner(skip, this.dir); } } class PartialTextCursor { constructor(text, start, end) { this.value = ""; this.done = false; this.cursor = new RawTextCursor(text, start > end ? -1 : 1); this.pos = start > end ? text.length : 0; this.from = Math.min(start, end); this.to = Math.max(start, end); } nextInner(skip, dir) { if (dir < 0 ? this.pos <= this.from : this.pos >= this.to) { this.value = ""; this.done = true; return this; } skip += Math.max(0, dir < 0 ? this.pos - this.to : this.from - this.pos); let limit = dir < 0 ? this.pos - this.from : this.to - this.pos; if (skip > limit) skip = limit; limit -= skip; let { value } = this.cursor.next(skip); this.pos += (value.length + skip) * dir; this.value = value.length <= limit ? value : dir < 0 ? value.slice(value.length - limit) : value.slice(0, limit); this.done = !this.value; return this; } next(skip = 0) { if (skip < 0) skip = Math.max(skip, this.from - this.pos); else if (skip > 0) skip = Math.min(skip, this.to - this.pos); return this.nextInner(skip, this.cursor.dir); } get lineBreak() { return this.cursor.lineBreak && this.value != ""; } } class LineCursor { constructor(inner) { this.inner = inner; this.afterBreak = true; this.value = ""; this.done = false; } next(skip = 0) { let { done, lineBreak, value } = this.inner.next(skip); if (done) { this.done = true; this.value = ""; } else if (lineBreak) { if (this.afterBreak) { this.value = ""; } else { this.afterBreak = true; this.next(); } } else { this.value = value; this.afterBreak = false; } return this; } get lineBreak() { return false; } } if (typeof Symbol != "undefined") { dist_Text.prototype[Symbol.iterator] = function () { return this.iter(); }; RawTextCursor.prototype[Symbol.iterator] = PartialTextCursor.prototype[Symbol.iterator] = LineCursor.prototype[Symbol.iterator] = function () { return this; }; } /** This type describes a line in the document. It is created on-demand when lines are [queried](https://codemirror.net/6/docs/ref/#text.Text.lineAt). */ class Line { /** @internal */ constructor( /** The position of the start of the line. */ from, /** The position at the end of the line (_before_ the line break, or at the end of document for the last line). */ to, /** This line's line number (1-based). */ number, /** The line's content. */ text) { this.from = from; this.to = to; this.number = number; this.text = text; } /** The length of the line (not including any line break after it). */ get length() { return this.to - this.from; } } // EXTERNAL MODULE: external {"root":["CM","@codemirror/view"],"commonjs":"@codemirror/view","commonjs2":"@codemirror/view"} var view_ = __webpack_require__(105); ;// CONCATENATED MODULE: ./node_modules/@lezer/common/dist/index.js // FIXME profile adding a per-Tree TreeNode cache, validating it by // parent pointer /// The default maximum length of a `TreeBuffer` node (1024). const DefaultBufferLength = 1024; let nextPropID = 0; class Range { constructor(from, to) { this.from = from; this.to = to; } } /// Each [node type](#common.NodeType) or [individual tree](#common.Tree) /// can have metadata associated with it in props. Instances of this /// class represent prop names. class dist_NodeProp { /// Create a new node prop type. constructor(config = {}) { this.id = nextPropID++; this.perNode = !!config.perNode; this.deserialize = config.deserialize || (() => { throw new Error("This node type doesn't define a deserialize function"); }); } /// This is meant to be used with /// [`NodeSet.extend`](#common.NodeSet.extend) or /// [`LRParser.configure`](#lr.ParserConfig.props) to compute /// prop values for each node type in the set. Takes a [match /// object](#common.NodeType^match) or function that returns undefined /// if the node type doesn't get this prop, and the prop's value if /// it does. add(match) { if (this.perNode) throw new RangeError("Can't add per-node props to node types"); if (typeof match != "function") match = NodeType.match(match); return (type) => { let result = match(type); return result === undefined ? null : [this, result]; }; } } /// Prop that is used to describe matching delimiters. For opening /// delimiters, this holds an array of node names (written as a /// space-separated string when declaring this prop in a grammar) /// for the node types of closing delimiters that match it. dist_NodeProp.closedBy = new dist_NodeProp({ deserialize: str => str.split(" ") }); /// The inverse of [`closedBy`](#common.NodeProp^closedBy). This is /// attached to closing delimiters, holding an array of node names /// of types of matching opening delimiters. dist_NodeProp.openedBy = new dist_NodeProp({ deserialize: str => str.split(" ") }); /// Used to assign node types to groups (for example, all node /// types that represent an expression could be tagged with an /// `"Expression"` group). dist_NodeProp.group = new dist_NodeProp({ deserialize: str => str.split(" ") }); /// The hash of the [context](#lr.ContextTracker.constructor) /// that the node was parsed in, if any. Used to limit reuse of /// contextual nodes. dist_NodeProp.contextHash = new dist_NodeProp({ perNode: true }); /// The distance beyond the end of the node that the tokenizer /// looked ahead for any of the tokens inside the node. (The LR /// parser only stores this when it is larger than 25, for /// efficiency reasons.) dist_NodeProp.lookAhead = new dist_NodeProp({ perNode: true }); /// This per-node prop is used to replace a given node, or part of a /// node, with another tree. This is useful to include trees from /// different languages. dist_NodeProp.mounted = new dist_NodeProp({ perNode: true }); /// A mounted tree, which can be [stored](#common.NodeProp^mounted) on /// a tree node to indicate that parts of its content are /// represented by another tree. class MountedTree { constructor( /// The inner tree. tree, /// If this is null, this tree replaces the entire node (it will /// be included in the regular iteration instead of its host /// node). If not, only the given ranges are considered to be /// covered by this tree. This is used for trees that are mixed in /// a way that isn't strictly hierarchical. Such mounted trees are /// only entered by [`resolveInner`](#common.Tree.resolveInner) /// and [`enter`](#common.SyntaxNode.enter). overlay, /// The parser used to create this subtree. parser) { this.tree = tree; this.overlay = overlay; this.parser = parser; } } const noProps = Object.create(null); /// Each node in a syntax tree has a node type associated with it. class NodeType { /// @internal constructor( /// The name of the node type. Not necessarily unique, but if the /// grammar was written properly, different node types with the /// same name within a node set should play the same semantic /// role. name, /// @internal props, /// The id of this node in its set. Corresponds to the term ids /// used in the parser. id, /// @internal flags = 0) { this.name = name; this.props = props; this.id = id; this.flags = flags; } static define(spec) { let props = spec.props && spec.props.length ? Object.create(null) : noProps; let flags = (spec.top ? 1 /* Top */ : 0) | (spec.skipped ? 2 /* Skipped */ : 0) | (spec.error ? 4 /* Error */ : 0) | (spec.name == null ? 8 /* Anonymous */ : 0); let type = new NodeType(spec.name || "", props, spec.id, flags); if (spec.props) for (let src of spec.props) { if (!Array.isArray(src)) src = src(type); if (src) { if (src[0].perNode) throw new RangeError("Can't store a per-node prop on a node type"); props[src[0].id] = src[1]; } } return type; } /// Retrieves a node prop for this type. Will return `undefined` if /// the prop isn't present on this node. prop(prop) { return this.props[prop.id]; } /// True when this is the top node of a grammar. get isTop() { return (this.flags & 1 /* Top */) > 0; } /// True when this node is produced by a skip rule. get isSkipped() { return (this.flags & 2 /* Skipped */) > 0; } /// Indicates whether this is an error node. get isError() { return (this.flags & 4 /* Error */) > 0; } /// When true, this node type doesn't correspond to a user-declared /// named node, for example because it is used to cache repetition. get isAnonymous() { return (this.flags & 8 /* Anonymous */) > 0; } /// Returns true when this node's name or one of its /// [groups](#common.NodeProp^group) matches the given string. is(name) { if (typeof name == 'string') { if (this.name == name) return true; let group = this.prop(dist_NodeProp.group); return group ? group.indexOf(name) > -1 : false; } return this.id == name; } /// Create a function from node types to arbitrary values by /// specifying an object whose property names are node or /// [group](#common.NodeProp^group) names. Often useful with /// [`NodeProp.add`](#common.NodeProp.add). You can put multiple /// names, separated by spaces, in a single property name to map /// multiple node names to a single value. static match(map) { let direct = Object.create(null); for (let prop in map) for (let name of prop.split(" ")) direct[name] = map[prop]; return (node) => { for (let groups = node.prop(dist_NodeProp.group), i = -1; i < (groups ? groups.length : 0); i++) { let found = direct[i < 0 ? node.name : groups[i]]; if (found) return found; } }; } } /// An empty dummy node type to use when no actual type is available. NodeType.none = new NodeType("", Object.create(null), 0, 8 /* Anonymous */); /// A node set holds a collection of node types. It is used to /// compactly represent trees by storing their type ids, rather than a /// full pointer to the type object, in a numeric array. Each parser /// [has](#lr.LRParser.nodeSet) a node set, and [tree /// buffers](#common.TreeBuffer) can only store collections of nodes /// from the same set. A set can have a maximum of 2**16 (65536) node /// types in it, so that the ids fit into 16-bit typed array slots. class NodeSet { /// Create a set with the given types. The `id` property of each /// type should correspond to its position within the array. constructor( /// The node types in this set, by id. types) { this.types = types; for (let i = 0; i < types.length; i++) if (types[i].id != i) throw new RangeError("Node type ids should correspond to array positions when creating a node set"); } /// Create a copy of this set with some node properties added. The /// arguments to this method should be created with /// [`NodeProp.add`](#common.NodeProp.add). extend(...props) { let newTypes = []; for (let type of this.types) { let newProps = null; for (let source of props) { let add = source(type); if (add) { if (!newProps) newProps = Object.assign({}, type.props); newProps[add[0].id] = add[1]; } } newTypes.push(newProps ? new NodeType(type.name, newProps, type.id, type.flags) : type); } return new NodeSet(newTypes); } } const CachedNode = new WeakMap(), CachedInnerNode = new WeakMap(); /// A piece of syntax tree. There are two ways to approach these /// trees: the way they are actually stored in memory, and the /// convenient way. /// /// Syntax trees are stored as a tree of `Tree` and `TreeBuffer` /// objects. By packing detail information into `TreeBuffer` leaf /// nodes, the representation is made a lot more memory-efficient. /// /// However, when you want to actually work with tree nodes, this /// representation is very awkward, so most client code will want to /// use the [`TreeCursor`](#common.TreeCursor) or /// [`SyntaxNode`](#common.SyntaxNode) interface instead, which provides /// a view on some part of this data structure, and can be used to /// move around to adjacent nodes. class Tree { /// Construct a new tree. See also [`Tree.build`](#common.Tree^build). constructor( /// The type of the top node. type, /// This node's child nodes. children, /// The positions (offsets relative to the start of this tree) of /// the children. positions, /// The total length of this tree length, /// Per-node [node props](#common.NodeProp) to associate with this node. props) { this.type = type; this.children = children; this.positions = positions; this.length = length; /// @internal this.props = null; if (props && props.length) { this.props = Object.create(null); for (let [prop, value] of props) this.props[typeof prop == "number" ? prop : prop.id] = value; } } /// @internal toString() { let mounted = this.prop(dist_NodeProp.mounted); if (mounted && !mounted.overlay) return mounted.tree.toString(); let children = ""; for (let ch of this.children) { let str = ch.toString(); if (str) { if (children) children += ","; children += str; } } return !this.type.name ? children : (/\W/.test(this.type.name) && !this.type.isError ? JSON.stringify(this.type.name) : this.type.name) + (children.length ? "(" + children + ")" : ""); } /// Get a [tree cursor](#common.TreeCursor) rooted at this tree. When /// `pos` is given, the cursor is [moved](#common.TreeCursor.moveTo) /// to the given position and side. cursor(pos, side = 0) { let scope = (pos != null && CachedNode.get(this)) || this.topNode; let cursor = new TreeCursor(scope); if (pos != null) { cursor.moveTo(pos, side); CachedNode.set(this, cursor._tree); } return cursor; } /// Get a [tree cursor](#common.TreeCursor) that, unlike regular /// cursors, doesn't skip through /// [anonymous](#common.NodeType.isAnonymous) nodes and doesn't /// automatically enter mounted nodes. fullCursor() { return new TreeCursor(this.topNode, 1 /* Full */); } /// Get a [syntax node](#common.SyntaxNode) object for the top of the /// tree. get topNode() { return new TreeNode(this, 0, 0, null); } /// Get the [syntax node](#common.SyntaxNode) at the given position. /// If `side` is -1, this will move into nodes that end at the /// position. If 1, it'll move into nodes that start at the /// position. With 0, it'll only enter nodes that cover the position /// from both sides. resolve(pos, side = 0) { let node = resolveNode(CachedNode.get(this) || this.topNode, pos, side, false); CachedNode.set(this, node); return node; } /// Like [`resolve`](#common.Tree.resolve), but will enter /// [overlaid](#common.MountedTree.overlay) nodes, producing a syntax node /// pointing into the innermost overlaid tree at the given position /// (with parent links going through all parent structure, including /// the host trees). resolveInner(pos, side = 0) { let node = resolveNode(CachedInnerNode.get(this) || this.topNode, pos, side, true); CachedInnerNode.set(this, node); return node; } /// Iterate over the tree and its children, calling `enter` for any /// node that touches the `from`/`to` region (if given) before /// running over such a node's children, and `leave` (if given) when /// leaving the node. When `enter` returns `false`, that node will /// not have its children iterated over (or `leave` called). iterate(spec) { let { enter, leave, from = 0, to = this.length } = spec; for (let c = this.cursor(), get = () => c.node;;) { let mustLeave = false; if (c.from <= to && c.to >= from && (c.type.isAnonymous || enter(c.type, c.from, c.to, get) !== false)) { if (c.firstChild()) continue; if (!c.type.isAnonymous) mustLeave = true; } for (;;) { if (mustLeave && leave) leave(c.type, c.from, c.to, get); mustLeave = c.type.isAnonymous; if (c.nextSibling()) break; if (!c.parent()) return; mustLeave = true; } } } /// Get the value of the given [node prop](#common.NodeProp) for this /// node. Works with both per-node and per-type props. prop(prop) { return !prop.perNode ? this.type.prop(prop) : this.props ? this.props[prop.id] : undefined; } /// Returns the node's [per-node props](#common.NodeProp.perNode) in a /// format that can be passed to the [`Tree`](#common.Tree) /// constructor. get propValues() { let result = []; if (this.props) for (let id in this.props) result.push([+id, this.props[id]]); return result; } /// Balance the direct children of this tree, producing a copy of /// which may have children grouped into subtrees with type /// [`NodeType.none`](#common.NodeType^none). balance(config = {}) { return this.children.length <= 8 /* BranchFactor */ ? this : balanceRange(NodeType.none, this.children, this.positions, 0, this.children.length, 0, this.length, (children, positions, length) => new Tree(this.type, children, positions, length, this.propValues), config.makeTree || ((children, positions, length) => new Tree(NodeType.none, children, positions, length))); } /// Build a tree from a postfix-ordered buffer of node information, /// or a cursor over such a buffer. static build(data) { return buildTree(data); } } /// The empty tree Tree.empty = new Tree(NodeType.none, [], [], 0); class FlatBufferCursor { constructor(buffer, index) { this.buffer = buffer; this.index = index; } get id() { return this.buffer[this.index - 4]; } get start() { return this.buffer[this.index - 3]; } get end() { return this.buffer[this.index - 2]; } get size() { return this.buffer[this.index - 1]; } get pos() { return this.index; } next() { this.index -= 4; } fork() { return new FlatBufferCursor(this.buffer, this.index); } } /// Tree buffers contain (type, start, end, endIndex) quads for each /// node. In such a buffer, nodes are stored in prefix order (parents /// before children, with the endIndex of the parent indicating which /// children belong to it) class TreeBuffer { /// Create a tree buffer. constructor( /// The buffer's content. buffer, /// The total length of the group of nodes in the buffer. length, /// The node set used in this buffer. set) { this.buffer = buffer; this.length = length; this.set = set; } /// @internal get type() { return NodeType.none; } /// @internal toString() { let result = []; for (let index = 0; index < this.buffer.length;) { result.push(this.childString(index)); index = this.buffer[index + 3]; } return result.join(","); } /// @internal childString(index) { let id = this.buffer[index], endIndex = this.buffer[index + 3]; let type = this.set.types[id], result = type.name; if (/\W/.test(result) && !type.isError) result = JSON.stringify(result); index += 4; if (endIndex == index) return result; let children = []; while (index < endIndex) { children.push(this.childString(index)); index = this.buffer[index + 3]; } return result + "(" + children.join(",") + ")"; } /// @internal findChild(startIndex, endIndex, dir, pos, side) { let { buffer } = this, pick = -1; for (let i = startIndex; i != endIndex; i = buffer[i + 3]) { if (checkSide(side, pos, buffer[i + 1], buffer[i + 2])) { pick = i; if (dir > 0) break; } } return pick; } /// @internal slice(startI, endI, from, to) { let b = this.buffer; let copy = new Uint16Array(endI - startI); for (let i = startI, j = 0; i < endI;) { copy[j++] = b[i++]; copy[j++] = b[i++] - from; copy[j++] = b[i++] - from; copy[j++] = b[i++] - startI; } return new TreeBuffer(copy, to - from, this.set); } } function checkSide(side, pos, from, to) { switch (side) { case -2 /* Before */: return from < pos; case -1 /* AtOrBefore */: return to >= pos && from < pos; case 0 /* Around */: return from < pos && to > pos; case 1 /* AtOrAfter */: return from <= pos && to > pos; case 2 /* After */: return to > pos; case 4 /* DontCare */: return true; } } function enterUnfinishedNodesBefore(node, pos) { let scan = node.childBefore(pos); while (scan) { let last = scan.lastChild; if (!last || last.to != scan.to) break; if (last.type.isError && last.from == last.to) { node = scan; scan = last.prevSibling; } else { scan = last; } } return node; } function resolveNode(node, pos, side, overlays) { var _a; // Move up to