UNPKG

@uiw/react-codemirror

Version:
1,222 lines (1,185 loc) 451 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(require("react"), 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/state"), require("@codemirror/theme-one-dark"), require("@codemirror/view")); else root["@uiw/codemirror"] = factory(root["React"], root["CM"]["@codemirror/state"], root["CM"]["@codemirror/theme-one-dark"], root["CM"]["@codemirror/view"]); })(self, (__WEBPACK_EXTERNAL_MODULE__787__, __WEBPACK_EXTERNAL_MODULE__242__, __WEBPACK_EXTERNAL_MODULE__362__, __WEBPACK_EXTERNAL_MODULE__105__) => { return /******/ (() => { // webpackBootstrap /******/ "use strict"; /******/ var __webpack_modules__ = ({ /***/ 298: /***/ ((__unused_webpack_module, exports, __webpack_require__) => { var __webpack_unused_export__; /** * @license React * react-jsx-runtime.production.min.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var f=__webpack_require__(787),k=Symbol.for("react.element"),l=Symbol.for("react.fragment"),m=Object.prototype.hasOwnProperty,n=f.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,p={key:!0,ref:!0,__self:!0,__source:!0}; function q(c,a,g){var b,d={},e=null,h=null;void 0!==g&&(e=""+g);void 0!==a.key&&(e=""+a.key);void 0!==a.ref&&(h=a.ref);for(b in a)m.call(a,b)&&!p.hasOwnProperty(b)&&(d[b]=a[b]);if(c&&c.defaultProps)for(b in a=c.defaultProps,a)void 0===d[b]&&(d[b]=a[b]);return{$$typeof:k,type:c,key:e,ref:h,props:d,_owner:n.current}}__webpack_unused_export__=l;exports.jsx=q;__webpack_unused_export__=q; /***/ }), /***/ 605: /***/ ((module, __unused_webpack_exports, __webpack_require__) => { if (true) { module.exports = __webpack_require__(298); } else {} /***/ }), /***/ 787: /***/ ((module) => { module.exports = __WEBPACK_EXTERNAL_MODULE__787__; /***/ }), /***/ 242: /***/ ((module) => { module.exports = __WEBPACK_EXTERNAL_MODULE__242__; /***/ }), /***/ 362: /***/ ((module) => { module.exports = __WEBPACK_EXTERNAL_MODULE__362__; /***/ }), /***/ 105: /***/ ((module) => { module.exports = __WEBPACK_EXTERNAL_MODULE__105__; /***/ }) /******/ }); /************************************************************************/ /******/ // The module cache /******/ var __webpack_module_cache__ = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ var cachedModule = __webpack_module_cache__[moduleId]; /******/ if (cachedModule !== undefined) { /******/ return cachedModule.exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = __webpack_module_cache__[moduleId] = { /******/ // no module.id needed /******/ // no module.loaded needed /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__); /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /************************************************************************/ /******/ /* webpack/runtime/define property getters */ /******/ (() => { /******/ // define getter functions for harmony exports /******/ __webpack_require__.d = (exports, definition) => { /******/ for(var key in definition) { /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); /******/ } /******/ } /******/ }; /******/ })(); /******/ /******/ /* webpack/runtime/hasOwnProperty shorthand */ /******/ (() => { /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) /******/ })(); /******/ /******/ /* webpack/runtime/make namespace object */ /******/ (() => { /******/ // define __esModule on exports /******/ __webpack_require__.r = (exports) => { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ })(); /******/ /************************************************************************/ var __webpack_exports__ = {}; // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk. (() => { // ESM COMPAT FLAG __webpack_require__.r(__webpack_exports__); // EXPORTS __webpack_require__.d(__webpack_exports__, { "basicSetup": () => (/* reexport */ basicSetup), "default": () => (/* binding */ src), "getStatistics": () => (/* reexport */ getStatistics), "minimalSetup": () => (/* reexport */ minimalSetup), "useCodeMirror": () => (/* reexport */ useCodeMirror) }); ;// CONCATENATED MODULE: ../node_modules/@babel/runtime/helpers/esm/typeof.js function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } ;// CONCATENATED MODULE: ../node_modules/@babel/runtime/helpers/esm/toPrimitive.js function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } ;// CONCATENATED MODULE: ../node_modules/@babel/runtime/helpers/esm/toPropertyKey.js function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } ;// CONCATENATED MODULE: ../node_modules/@babel/runtime/helpers/esm/defineProperty.js function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } ;// CONCATENATED MODULE: ../node_modules/@babel/runtime/helpers/esm/objectSpread2.js function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread2(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } ;// CONCATENATED MODULE: ../node_modules/@babel/runtime/helpers/esm/objectWithoutPropertiesLoose.js function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } ;// CONCATENATED MODULE: ../node_modules/@babel/runtime/helpers/esm/objectWithoutProperties.js function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } // EXTERNAL MODULE: external {"root":"React","commonjs2":"react","commonjs":"react","amd":"react"} var external_root_React_commonjs2_react_commonjs_react_amd_react_ = __webpack_require__(787); ;// 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 = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0) { ; } } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) 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":["CM","@codemirror/state"],"commonjs":"@codemirror/state","commonjs2":"@codemirror/state"} var state_ = __webpack_require__(242); // 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. 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 = dist_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 in mixed-language parsers. 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 dist_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; } /// Define a node type. static define(spec) { let props = spec.props && spec.props.length ? Object.create(null) : noProps; let flags = (spec.top ? 1 /* NodeFlag.Top */ : 0) | (spec.skipped ? 2 /* NodeFlag.Skipped */ : 0) | (spec.error ? 4 /* NodeFlag.Error */ : 0) | (spec.name == null ? 8 /* NodeFlag.Anonymous */ : 0); let type = new dist_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 /* NodeFlag.Top */) > 0; } /// True when this node is produced by a skip rule. get isSkipped() { return (this.flags & 2 /* NodeFlag.Skipped */) > 0; } /// Indicates whether this is an error node. get isError() { return (this.flags & 4 /* NodeFlag.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 /* NodeFlag.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. dist_NodeType.none = new dist_NodeType("", Object.create(null), 0, 8 /* NodeFlag.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 can 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 dist_NodeType(type.name, newProps, type.id, type.flags) : type); } return new NodeSet(newTypes); } } const CachedNode = new WeakMap(), CachedInnerNode = new WeakMap(); /// Options that control iteration. Can be combined with the `|` /// operator to enable multiple ones. var IterMode; (function (IterMode) { /// When enabled, iteration will only visit [`Tree`](#common.Tree) /// objects, not nodes packed into /// [`TreeBuffer`](#common.TreeBuffer)s. IterMode[IterMode["ExcludeBuffers"] = 1] = "ExcludeBuffers"; /// Enable this to make iteration include anonymous nodes (such as /// the nodes that wrap repeated grammar constructs into a balanced /// tree). IterMode[IterMode["IncludeAnonymous"] = 2] = "IncludeAnonymous"; /// By default, regular [mounted](#common.NodeProp^mounted) nodes /// replace their base node in iteration. Enable this to ignore them /// instead. IterMode[IterMode["IgnoreMounts"] = 4] = "IgnoreMounts"; /// This option only applies in /// [`enter`](#common.SyntaxNode.enter)-style methods. It tells the /// library to not enter mounted overlays if one covers the given /// position. IterMode[IterMode["IgnoreOverlays"] = 8] = "IgnoreOverlays"; })(IterMode || (IterMode = {})); /// 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 dist_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) positioned at the top of /// the tree. Mode can be used to [control](#common.IterMode) which /// nodes the cursor visits. cursor(mode = 0) { return new TreeCursor(this.topNode, mode); } /// Get a [tree cursor](#common.TreeCursor) pointing into this tree /// at the given position and side (see /// [`moveTo`](#common.TreeCursor.moveTo). cursorAt(pos, side = 0, mode = 0) { let scope = CachedNode.get(this) || this.topNode; let cursor = new TreeCursor(scope); cursor.moveTo(pos, side); CachedNode.set(this, cursor._tree); return cursor; } /// 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. /// /// Note that this will not enter /// [overlays](#common.MountedTree.overlay), and you often want /// [`resolveInner`](#common.Tree.resolveInner) instead. 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((spec.mode || 0) | IterMode.IncludeAnonymous);;) { let entered = false; if (c.from <= to && c.to >= from && (c.type.isAnonymous || enter(c) !== false)) { if (c.firstChild()) continue; entered = true; } for (;;) { if (entered && leave && !c.type.isAnonymous) leave(c); if (c.nextSibling()) break; if (!c.parent()) return; entered = 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 /* Balance.BranchFactor */ ? this : balanceRange(dist_NodeType.none, this.children, this.positions, 0, this.children.length, 0, this.length, (children, positions, length) => new dist_Tree(this.type, children, positions, length, this.propValues), config.makeTree || ((children, positions, length) => new dist_Tree(dist_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 dist_Tree.empty = new dist_Tree(dist_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 dist_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) { let b = this.buffer; let copy = new Uint16Array(endI - startI), len = 0; for (let i = startI, j = 0; i < endI;) { copy[j++] = b[i++]; copy[j++] = b[i++] - from; let to = copy[j++] = b[i++] - from; copy[j++] = b[i++] - startI; len = Math.max(len, to); } return new TreeBuffer(copy, len, this.set); } } function checkSide(side, pos, from, to) { switch (side) { case -2 /* Side.Before */: return from < pos; case -1 /* Side.AtOrBefore */: return to >= pos && from < pos; case 0 /* Side.Around */: return from < pos && to > pos; case 1 /* Side.AtOrAfter */: return from <= pos && to > pos; case 2 /* Side.After */: return to > pos; case 4 /* Side.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 a node that actually holds the position, if possible while (node.from == node.to || (side < 1 ? node.from >= pos : node.from > pos) || (side > -1 ? node.to <= pos : node.to < pos)) { let parent = !overlays && node instanceof TreeNode && node.index < 0 ? null : node.parent; if (!parent) return node; node = parent; } let mode = overlays ? 0 : IterMode.IgnoreOverlays; // Must go up out of overlays when those do not overlap with pos if (overlays) for (let scan = node, parent = scan.parent; parent; scan = parent, parent = scan.parent) { if (scan instanceof TreeNode && scan.index < 0 && ((_a = parent.enter(pos, side, mode)) === null || _a === void 0 ? void 0 : _a.from) != scan.from) node = parent; } for (;;) { let inner = node.enter(pos, side, mode); if (!inner) return node; node = inner; } } class TreeNode { constructor(_tree, from, // Index in parent node, set to -1 if the node is not a direct child of _parent.node (overlay) index, _parent) { this._tree = _tree; this.from = from; this.index = index; this._parent = _parent; } get type() { return this._tree.type; } get name() { return this._tree.type.name; } get to() { return this.from + this._tree.length; } nextChild(i, dir, pos, side, mode = 0) { for (let parent = this;;) { for (let { children, positions } = parent._tree, e = dir > 0 ? children.length : -1; i != e; i += dir) { let next = children[i], start = positions[i] + parent.from; if (!checkSide(side, pos, start, start + next.length)) continue; if (next instanceof TreeBuffer) { if (mode & IterMode.ExcludeBuffers) continue; let index = next.findChild(0, next.buffer.length, dir, pos - start, side); if (index > -1) return new BufferNode(new BufferContext(parent, next, i, start), null, index); } else if ((mode & IterMode.IncludeAnonymous) || (!next.type.isAnonymous || hasChild(next))) { let mounted; if (!(mode & IterMode.IgnoreMounts) && next.props && (mounted = next.prop(dist_NodeProp.mounted)) && !mounted.overlay) return new TreeNode(mounted.tree, start, i, parent); let inner = new TreeNode(next, start, i, parent); return (mode & IterMode.IncludeAnonymous) || !inner.type.isAnonymous ? inner : inner.nextChild(dir < 0 ? next.children.length - 1 : 0, dir, pos, side); } } if ((mode & IterMode.IncludeAnonymous) || !parent.type.isAnonymous) return null; if (parent.index >= 0) i = parent.index + dir; else i = dir < 0 ? -1 : parent._parent._tree.children.length; parent = parent._parent; if (!parent) return null; } } get firstChild() { return this.nextChild(0, 1, 0, 4 /* Side.DontCare */); } get lastChild() { return this.nextChild(this._tree.children.length - 1, -1, 0, 4 /* Side.DontCare */); } childAfter(pos) { return this.nextChild(0, 1, pos, 2 /* Side.After */); } childBefore(pos) { return this.nextChild(this._tree.children.length - 1, -1, pos, -2 /* Side.Before */); } enter(pos, side, mode = 0) { let mounted; if (!(mode & IterMode.IgnoreOverlays) && (mounted = this._tree.prop(dist_NodeProp.mounted)) && mounted.overlay) { let rPos = pos - this.from; for (let { from, to } of mounted.overlay) { if ((side > 0 ? from <= rPos : from < rPos) && (side < 0 ? to >= rPos : to > rPos)) return new TreeNode(mounted.tree, mounted.overlay[0].from + this.from, -1, this); } } return this.nextChild(0, 1, pos, side, mode); } nextSignificantParent() { let val = this; while (val.type.isAnonymous && val._parent) val = val._parent; return val; } get parent() { return this._parent ? this._parent.nextSignificantParent() : null; } get nextSibling() { return this._parent && this.index >= 0 ? this._parent.nextChild(this.index + 1, 1, 0, 4 /* Side.DontCare */) : null; } get prevSibling() { return this._parent && this.index >= 0 ? this._parent.nextChild(this.index - 1, -1, 0, 4 /* Side.DontCare */) : null; } cursor(mode = 0) { return new TreeCursor(this, mode); } get tree() { return this._tree; } toTree() { return this._tree; } resolve(pos, side = 0) { return resolveNode(this, pos, side, false); } resolveInner(pos, side = 0) { return resolveNode(this, pos, side, true); } enterUnfinishedNodesBefore(pos) { return enterUnfinishedNodesBefore(this, pos); } getChild(type, before = null, after = null) { let r = getChildren(this, type, before, after); return r.length ? r[0] : null; } getChildren(type, before = null, after = null) { return getChildren(this, type, before, after); } /// @internal toString() { return this._tree.toString(); } get node() { return this; } matchContext(context) { return matchNodeContext(this, context); } } function getChildren(node, type, before, after) { let cur = node.cursor(), result = []; if (!cur.firstChild()) return result; if (before != null) while (!cur.type.is(before)) if (!cur.nextSibling()) return result; for (;;) { if (after != null && cur.type.is(after)) return result; if (cur.type.is(type)) result.push(cur.node); if (!cur.nextSibling()) return after == null ? result : []; } } function matchNodeContext(node, context, i = context.length - 1) { for (let p = node.parent; i >= 0; p = p.parent) { if (!p) return false; if (!p.type.isAnonymous) { if (context[i] && context[i] != p.name) return false; i--; } } return true; } class BufferContext { constructor(parent, buffer, index, start) { this.parent = parent; this.buffer = buffer; this.index = index; this.start = start; } } class BufferNode { get name() { return this.type.name; } get from() { return this.context.start + this.context.buffer.buffer[this.index + 1]; } get to() { return this.context.start + this.context.buffer.buffer[this.index + 2]; } constructor(context, _parent, index) { this.context = context; this._parent = _parent; this.index = index; this.type = context.buffer.set.types[context.buffer.buffer[index]]; } child(dir, pos, side) { let { buffer } = this.context; let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, pos - this.context.start, side); return index < 0 ? null : new BufferNode(this.context, this, index); } get firstChild() { return this.child(1, 0, 4 /* Side.DontCare */); } get lastChild() { return this.child(-1, 0, 4 /* Side.DontCare */); } childAfter(pos) { return this.child(1, pos, 2 /* Side.After */); } childBefore(pos) { return this.child(-1, pos, -2 /* Side.Before */); } enter(pos, side, mode = 0) { if (mode & IterMode.ExcludeBuffers) return null; let { buffer } = this.context; let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], side > 0 ? 1 : -1, pos - this.context.start, side); return index < 0 ? null : new BufferNode(this.context, this, index); } get parent() { return this._parent || this.context.parent.nextSignificantParent(); } externalSibling(dir) { return this._parent ? null : this.context.parent.nextChild(this.context.index + dir, dir, 0, 4 /* Side.DontCare */); } get nextSibling() { let { buffer } = this.context; let after = buffer.buffer[this.index + 3]; if (after < (this._parent ? buffer.buffer[this._parent.index + 3] : buffer.buffer.length)) return new BufferNode(this.context, this._parent, after); return this.externalSibling(1); } get prevSibling() { let { buffer } = this.context; let parentStart = this._parent ? this._parent.index + 4 : 0; if (this.index == parentStart) return this.externalSibling(-1); return new BufferNode(this.context, this._parent, buffer.findChild(parentStart, this.index, -1, 0, 4 /* Side.DontCare */)); } cursor(mode = 0) { return new TreeCursor(this, mode); } get tree() { return null; } toTree() { let children = [], positions = []; let { buffer } = this.context; let startI = this.index + 4, endI = buffer.buffer[this.index + 3]; if (endI > startI) { let from = buffer.buffer[this.index + 1]; children.push(buffer.slice(startI, endI, from)); positions.push(0); } return new dist_Tree(this.type, children, positions, this.to - this.from); } resolve(pos, side = 0) { return resolveNode(this, pos, side, false); } resolveInner(pos, side = 0) { return resolveNode(this, pos, side, true); } enterUnfinishedNodesBefore(pos) { return enterUnfinishedNodesBefore(this, pos); } /// @internal toString() { return this.context.buffer.childString(this.index); } getChild(type, before = null, after = null) { let r = getChildren(this, type, before, after); return r.length ? r[0] : null; } getChildren(type, before = null, after = null) { return getChildren(this, type, before, after); } get node() { return this; } matchContext(context) { return matchNodeContext(this, context); } } /// A tree cursor object focuses on a given node in a syntax tree, and /// allows you to move to adjacent nodes. class TreeCursor { /// Shorthand for `.type.name`. get name() { return this.type.name; } /// @internal constructor(node, /// @internal mode = 0) { this.mode = mode; /// @internal this.buffer = null; this.stack = []; /// @internal this.index = 0; this.bufferNode = null; if (node instanceof TreeNode) { this.yieldNode(node); } else { this._tree = node.context.parent; this.buffer = node.context; for (let n = node._parent; n; n = n._parent) this.stack.unshift(n.index); this.bufferNode = node; this.yieldBuf(node.index); } } yieldNode(node) { if (!node) return false; this._tree = node; this.type = node.type; this.from = node.from; this.to = node.to; return true; } yieldBuf(index, type) { this.index = index; let { start, buffer } = this.buffer; this.type = type || buffer.set.types[buffer.buffer[index]]; this.from = start + buffer.buffer[index + 1]; this.to = start + buffer.buffer[index + 2]; return true; } yield(node) { if (!node) return false; if (node instanceof TreeNode) { this.buffer = null; return this.yieldNode(node); } this.buffer = node.context; return this.yieldBuf(node.index, node.type); } /// @internal toString() { return this.buffer ? this.buffer.buffer.childString(this.index) : this._tree.toString(); } /// @internal enterChild(dir, pos, side) { if (!this.buffer) return this.yield(this._tree.nextChild(dir < 0 ? this._tree._tree.children.length - 1 : 0, dir, pos, side, this.mode)); let { buffer } = this.buffer; let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, pos - this.buffer.start, side); if (index < 0) return false; this.stack.push(this.index); return this.yieldBuf(index); } /// Move the cursor to this node's first child. When this returns /// false, the node has no child, and the cursor has not been moved. firstChild() { return this.enterChild(1, 0, 4 /* Side.DontCare */); } /// Move the cursor to this node's last child. lastChild() { return this.enterChild(-1, 0, 4 /* Side.DontCare */); } /// Move the cursor to the first child that ends after `pos`. childAfter(pos) { return this.enterChild(1, pos, 2 /* Side.After */); } /// Move to the last child that starts before `pos`. childBefore(pos) { return this.enterChild(-1, pos, -2 /* Side.Before */); } /// Move the cursor to the child around `pos`. If side is -1 the /// child may end at that position, when 1 it may start there. This /// will also enter [overlaid](#common.MountedTree.overlay) /// [mounted](#common.NodeProp^mounted) trees unless `overlays` is /// set to false. enter(pos, side, mode = this.mode) { if (!this.buffer) return this.yield(this._tree.enter(pos, side, mode)); return mode & IterMode.ExcludeBuffers ? false : this.enterChild(1, pos, side); } /// Move to the node's parent node, if this isn't the top node. parent() { if (!this.buffer) return this.yieldNode((this.mode & IterMode.IncludeAnonymous) ? this._tree._parent : this._tree.parent); if (this.stack.length) return this.yieldBuf(this.stack.pop()); let parent = (this.mode & IterMode.IncludeAnonymous) ? this.buffer.parent : this.buffer.parent.nextSignificantParent(); this.buffer = null; return this.yieldNode(parent); } /// @internal sibling(dir) { if (!this.buffer) return !this._tree._parent ? false : this.yield(this._tree.index < 0 ? null : this._tree._parent.nextChild(this._tree.index + dir, dir, 0, 4 /* Side.DontCare */, this.mode)); let { buffer } = this.buffer, d = this.stack.length - 1; if (dir < 0) { let parentStart = d < 0 ? 0 : this.stack[d] + 4; if (this.index != parentStart) return this.yieldBuf(buffer.findChild(parentStart, this.index, -1, 0, 4 /* Side.DontCare */)); } else { let after = buffer.buffer[this.index + 3]; if (after < (d < 0 ? buffer.buffer.length : buffer.buffer[this.stack[d] + 3])) return this.yieldBuf(after); } return d < 0 ? this.yield(this.buffer.parent.nextChild(this.buffer.index + dir, dir, 0, 4 /* Side.DontCare */, this.mode)) : false; } /// Move to this node's next sibling, if any. nextSibling() { return this.sibling(1); } /// Move to this node's previous sibling, if any. prevSibling() { return this.sibling(-1); } atLastNode(dir) { let index, parent, { buffer } = this; if (buffer) { if (dir > 0) { if (this.index < buffer.buffer.buffer.length) return false; } else { for (let i = 0; i < this.index; i++) if (buffer.buffer.buffer[i + 3] < this.index) return false; } ({ index, parent } = buffer); } else { ({ index, _parent: parent } = this._tree); } for (; parent; { index, _parent: parent } = parent) { if (index > -1) for (let i = index + dir, e = dir < 0 ? -1 : parent._tree.children.length; i != e; i += dir) { let child = parent._tree.children[i]; if ((this.mode & IterMode.IncludeAnonymous) || child instanceof TreeBuffer || !child.type.isAnonymous || hasChild(child)) return false; } } return true; } move(dir, enter) { if (enter && this.enterChild(dir, 0, 4 /* Side.DontCare */)) return true; for (;;) { if (this.sibling(dir)) return true; if (this.atLastNode(dir) || !this.parent()) return false; } } /// Move to the next node in a /// [pre-order](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR) /// traversal, going from a node to its first child or, if the /// current node is empty or `enter` is false, its next sibling or /// the next sibling of the first parent node that has one. next(enter = true) { return this.move(1, enter); } /// Move to the next node in a last-to-first pre-order traveral. A /// node is followed by its last child