UNPKG

@codemirror/language

Version:

Language support infrastructure for the CodeMirror code editor

1,327 lines (1,321 loc) 102 kB
import { NodeProp, IterMode, Tree, TreeFragment, Parser, NodeType, NodeSet } from '@lezer/common'; import { StateEffect, StateField, Facet, EditorState, countColumn, combineConfig, RangeSet, RangeSetBuilder, Prec } from '@codemirror/state'; import { ViewPlugin, logException, EditorView, Decoration, WidgetType, gutter, GutterMarker, Direction } from '@codemirror/view'; import { tags, tagHighlighter, highlightTree, styleTags } from '@lezer/highlight'; import { StyleModule } from 'style-mod'; var _a; /** Node prop stored in a parser's top syntax node to provide the facet that stores language-specific data for that language. */ const languageDataProp = /*@__PURE__*/new NodeProp(); /** Helper function to define a facet (to be added to the top syntax node(s) for a language via [`languageDataProp`](https://codemirror.net/6/docs/ref/#language.languageDataProp)), that will be used to associate language data with the language. You probably only need this when subclassing [`Language`](https://codemirror.net/6/docs/ref/#language.Language). */ function defineLanguageFacet(baseData) { return Facet.define({ combine: baseData ? values => values.concat(baseData) : undefined }); } /** Syntax node prop used to register sublanguages. Should be added to the top level node type for the language. */ const sublanguageProp = /*@__PURE__*/new NodeProp(); /** A language object manages parsing and per-language [metadata](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt). Parse data is managed as a [Lezer](https://lezer.codemirror.net) tree. The class can be used directly, via the [`LRLanguage`](https://codemirror.net/6/docs/ref/#language.LRLanguage) subclass for [Lezer](https://lezer.codemirror.net/) LR parsers, or via the [`StreamLanguage`](https://codemirror.net/6/docs/ref/#language.StreamLanguage) subclass for stream parsers. */ class Language { /** Construct a language object. If you need to invoke this directly, first define a data facet with [`defineLanguageFacet`](https://codemirror.net/6/docs/ref/#language.defineLanguageFacet), and then configure your parser to [attach](https://codemirror.net/6/docs/ref/#language.languageDataProp) it to the language's outer syntax node. */ constructor( /** The [language data](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt) facet used for this language. */ data, parser, extraExtensions = [], /** A language name. */ name = "") { this.data = data; this.name = name; // Kludge to define EditorState.tree as a debugging helper, // without the EditorState package actually knowing about // languages and lezer trees. if (!EditorState.prototype.hasOwnProperty("tree")) Object.defineProperty(EditorState.prototype, "tree", { get() { return syntaxTree(this); } }); this.parser = parser; this.extension = [ language.of(this), EditorState.languageData.of((state, pos, side) => { let top = topNodeAt(state, pos, side), data = top.type.prop(languageDataProp); if (!data) return []; let base = state.facet(data), sub = top.type.prop(sublanguageProp); if (sub) { let innerNode = top.resolve(pos - top.from, side); for (let sublang of sub) if (sublang.test(innerNode, state)) { let data = state.facet(sublang.facet); return sublang.type == "replace" ? data : data.concat(base); } } return base; }) ].concat(extraExtensions); } /** Query whether this language is active at the given position. */ isActiveAt(state, pos, side = -1) { return topNodeAt(state, pos, side).type.prop(languageDataProp) == this.data; } /** Find the document regions that were parsed using this language. The returned regions will _include_ any nested languages rooted in this language, when those exist. */ findRegions(state) { let lang = state.facet(language); if ((lang === null || lang === void 0 ? void 0 : lang.data) == this.data) return [{ from: 0, to: state.doc.length }]; if (!lang || !lang.allowsNesting) return []; let result = []; let explore = (tree, from) => { if (tree.prop(languageDataProp) == this.data) { result.push({ from, to: from + tree.length }); return; } let mount = tree.prop(NodeProp.mounted); if (mount) { if (mount.tree.prop(languageDataProp) == this.data) { if (mount.overlay) for (let r of mount.overlay) result.push({ from: r.from + from, to: r.to + from }); else result.push({ from: from, to: from + tree.length }); return; } else if (mount.overlay) { let size = result.length; explore(mount.tree, mount.overlay[0].from + from); if (result.length > size) return; } } for (let i = 0; i < tree.children.length; i++) { let ch = tree.children[i]; if (ch instanceof Tree) explore(ch, tree.positions[i] + from); } }; explore(syntaxTree(state), 0); return result; } /** Indicates whether this language allows nested languages. The default implementation returns true. */ get allowsNesting() { return true; } } /** @internal */ Language.setState = /*@__PURE__*/StateEffect.define(); function topNodeAt(state, pos, side) { let topLang = state.facet(language), tree = syntaxTree(state).topNode; if (!topLang || topLang.allowsNesting) { for (let node = tree; node; node = node.enter(pos, side, IterMode.ExcludeBuffers)) if (node.type.isTop) tree = node; } return tree; } /** A subclass of [`Language`](https://codemirror.net/6/docs/ref/#language.Language) for use with Lezer [LR parsers](https://lezer.codemirror.net/docs/ref#lr.LRParser) parsers. */ class LRLanguage extends Language { constructor(data, parser, name) { super(data, parser, [], name); this.parser = parser; } /** Define a language from a parser. */ static define(spec) { let data = defineLanguageFacet(spec.languageData); return new LRLanguage(data, spec.parser.configure({ props: [languageDataProp.add(type => type.isTop ? data : undefined)] }), spec.name); } /** Create a new instance of this language with a reconfigured version of its parser and optionally a new name. */ configure(options, name) { return new LRLanguage(this.data, this.parser.configure(options), name || this.name); } get allowsNesting() { return this.parser.hasWrappers(); } } /** Get the syntax tree for a state, which is the current (possibly incomplete) parse tree of the active [language](https://codemirror.net/6/docs/ref/#language.Language), or the empty tree if there is no language available. */ function syntaxTree(state) { let field = state.field(Language.state, false); return field ? field.tree : Tree.empty; } /** Try to get a parse tree that spans at least up to `upto`. The method will do at most `timeout` milliseconds of work to parse up to that point if the tree isn't already available. */ function ensureSyntaxTree(state, upto, timeout = 50) { var _a; let parse = (_a = state.field(Language.state, false)) === null || _a === void 0 ? void 0 : _a.context; if (!parse) return null; let oldVieport = parse.viewport; parse.updateViewport({ from: 0, to: upto }); let result = parse.isDone(upto) || parse.work(timeout, upto) ? parse.tree : null; parse.updateViewport(oldVieport); return result; } /** Queries whether there is a full syntax tree available up to the given document position. If there isn't, the background parse process _might_ still be working and update the tree further, but there is no guarantee of that—the parser will [stop working](https://codemirror.net/6/docs/ref/#language.syntaxParserRunning) when it has spent a certain amount of time or has moved beyond the visible viewport. Always returns false if no language has been enabled. */ function syntaxTreeAvailable(state, upto = state.doc.length) { var _a; return ((_a = state.field(Language.state, false)) === null || _a === void 0 ? void 0 : _a.context.isDone(upto)) || false; } /** Move parsing forward, and update the editor state afterwards to reflect the new tree. Will work for at most `timeout` milliseconds. Returns true if the parser managed get to the given position in that time. */ function forceParsing(view, upto = view.viewport.to, timeout = 100) { let success = ensureSyntaxTree(view.state, upto, timeout); if (success != syntaxTree(view.state)) view.dispatch({}); return !!success; } /** Tells you whether the language parser is planning to do more parsing work (in a `requestIdleCallback` pseudo-thread) or has stopped running, either because it parsed the entire document, because it spent too much time and was cut off, or because there is no language parser enabled. */ function syntaxParserRunning(view) { var _a; return ((_a = view.plugin(parseWorker)) === null || _a === void 0 ? void 0 : _a.isWorking()) || false; } /** Lezer-style [`Input`](https://lezer.codemirror.net/docs/ref#common.Input) object for a [`Text`](https://codemirror.net/6/docs/ref/#state.Text) object. */ class DocInput { /** Create an input object for the given document. */ constructor(doc) { this.doc = doc; this.cursorPos = 0; this.string = ""; this.cursor = doc.iter(); } get length() { return this.doc.length; } syncTo(pos) { this.string = this.cursor.next(pos - this.cursorPos).value; this.cursorPos = pos + this.string.length; return this.cursorPos - this.string.length; } chunk(pos) { this.syncTo(pos); return this.string; } get lineChunks() { return true; } read(from, to) { let stringStart = this.cursorPos - this.string.length; if (from < stringStart || to >= this.cursorPos) return this.doc.sliceString(from, to); else return this.string.slice(from - stringStart, to - stringStart); } } let currentContext = null; /** A parse context provided to parsers working on the editor content. */ class ParseContext { constructor(parser, /** The current editor state. */ state, /** Tree fragments that can be reused by incremental re-parses. */ fragments = [], /** @internal */ tree, /** @internal */ treeLen, /** The current editor viewport (or some overapproximation thereof). Intended to be used for opportunistically avoiding work (in which case [`skipUntilInView`](https://codemirror.net/6/docs/ref/#language.ParseContext.skipUntilInView) should be called to make sure the parser is restarted when the skipped region becomes visible). */ viewport, /** @internal */ skipped, /** This is where skipping parsers can register a promise that, when resolved, will schedule a new parse. It is cleared when the parse worker picks up the promise. @internal */ scheduleOn) { this.parser = parser; this.state = state; this.fragments = fragments; this.tree = tree; this.treeLen = treeLen; this.viewport = viewport; this.skipped = skipped; this.scheduleOn = scheduleOn; this.parse = null; /** @internal */ this.tempSkipped = []; } /** @internal */ static create(parser, state, viewport) { return new ParseContext(parser, state, [], Tree.empty, 0, viewport, [], null); } startParse() { return this.parser.startParse(new DocInput(this.state.doc), this.fragments); } /** @internal */ work(until, upto) { if (upto != null && upto >= this.state.doc.length) upto = undefined; if (this.tree != Tree.empty && this.isDone(upto !== null && upto !== void 0 ? upto : this.state.doc.length)) { this.takeTree(); return true; } return this.withContext(() => { var _a; if (typeof until == "number") { let endTime = Date.now() + until; until = () => Date.now() > endTime; } if (!this.parse) this.parse = this.startParse(); if (upto != null && (this.parse.stoppedAt == null || this.parse.stoppedAt > upto) && upto < this.state.doc.length) this.parse.stopAt(upto); for (;;) { let done = this.parse.advance(); if (done) { this.fragments = this.withoutTempSkipped(TreeFragment.addTree(done, this.fragments, this.parse.stoppedAt != null)); this.treeLen = (_a = this.parse.stoppedAt) !== null && _a !== void 0 ? _a : this.state.doc.length; this.tree = done; this.parse = null; if (this.treeLen < (upto !== null && upto !== void 0 ? upto : this.state.doc.length)) this.parse = this.startParse(); else return true; } if (until()) return false; } }); } /** @internal */ takeTree() { let pos, tree; if (this.parse && (pos = this.parse.parsedPos) >= this.treeLen) { if (this.parse.stoppedAt == null || this.parse.stoppedAt > pos) this.parse.stopAt(pos); this.withContext(() => { while (!(tree = this.parse.advance())) { } }); this.treeLen = pos; this.tree = tree; this.fragments = this.withoutTempSkipped(TreeFragment.addTree(this.tree, this.fragments, true)); this.parse = null; } } withContext(f) { let prev = currentContext; currentContext = this; try { return f(); } finally { currentContext = prev; } } withoutTempSkipped(fragments) { for (let r; r = this.tempSkipped.pop();) fragments = cutFragments(fragments, r.from, r.to); return fragments; } /** @internal */ changes(changes, newState) { let { fragments, tree, treeLen, viewport, skipped } = this; this.takeTree(); if (!changes.empty) { let ranges = []; changes.iterChangedRanges((fromA, toA, fromB, toB) => ranges.push({ fromA, toA, fromB, toB })); fragments = TreeFragment.applyChanges(fragments, ranges); tree = Tree.empty; treeLen = 0; viewport = { from: changes.mapPos(viewport.from, -1), to: changes.mapPos(viewport.to, 1) }; if (this.skipped.length) { skipped = []; for (let r of this.skipped) { let from = changes.mapPos(r.from, 1), to = changes.mapPos(r.to, -1); if (from < to) skipped.push({ from, to }); } } } return new ParseContext(this.parser, newState, fragments, tree, treeLen, viewport, skipped, this.scheduleOn); } /** @internal */ updateViewport(viewport) { if (this.viewport.from == viewport.from && this.viewport.to == viewport.to) return false; this.viewport = viewport; let startLen = this.skipped.length; for (let i = 0; i < this.skipped.length; i++) { let { from, to } = this.skipped[i]; if (from < viewport.to && to > viewport.from) { this.fragments = cutFragments(this.fragments, from, to); this.skipped.splice(i--, 1); } } if (this.skipped.length >= startLen) return false; this.reset(); return true; } /** @internal */ reset() { if (this.parse) { this.takeTree(); this.parse = null; } } /** Notify the parse scheduler that the given region was skipped because it wasn't in view, and the parse should be restarted when it comes into view. */ skipUntilInView(from, to) { this.skipped.push({ from, to }); } /** Returns a parser intended to be used as placeholder when asynchronously loading a nested parser. It'll skip its input and mark it as not-really-parsed, so that the next update will parse it again. When `until` is given, a reparse will be scheduled when that promise resolves. */ static getSkippingParser(until) { return new class extends Parser { createParse(input, fragments, ranges) { let from = ranges[0].from, to = ranges[ranges.length - 1].to; let parser = { parsedPos: from, advance() { let cx = currentContext; if (cx) { for (let r of ranges) cx.tempSkipped.push(r); if (until) cx.scheduleOn = cx.scheduleOn ? Promise.all([cx.scheduleOn, until]) : until; } this.parsedPos = to; return new Tree(NodeType.none, [], [], to - from); }, stoppedAt: null, stopAt() { } }; return parser; } }; } /** @internal */ isDone(upto) { upto = Math.min(upto, this.state.doc.length); let frags = this.fragments; return this.treeLen >= upto && frags.length && frags[0].from == 0 && frags[0].to >= upto; } /** Get the context for the current parse, or `null` if no editor parse is in progress. */ static get() { return currentContext; } } function cutFragments(fragments, from, to) { return TreeFragment.applyChanges(fragments, [{ fromA: from, toA: to, fromB: from, toB: to }]); } class LanguageState { constructor( // A mutable parse state that is used to preserve work done during // the lifetime of a state when moving to the next state. context) { this.context = context; this.tree = context.tree; } apply(tr) { if (!tr.docChanged && this.tree == this.context.tree) return this; let newCx = this.context.changes(tr.changes, tr.state); // If the previous parse wasn't done, go forward only up to its // end position or the end of the viewport, to avoid slowing down // state updates with parse work beyond the viewport. let upto = this.context.treeLen == tr.startState.doc.length ? undefined : Math.max(tr.changes.mapPos(this.context.treeLen), newCx.viewport.to); if (!newCx.work(20 /* Work.Apply */, upto)) newCx.takeTree(); return new LanguageState(newCx); } static init(state) { let vpTo = Math.min(3000 /* Work.InitViewport */, state.doc.length); let parseState = ParseContext.create(state.facet(language).parser, state, { from: 0, to: vpTo }); if (!parseState.work(20 /* Work.Apply */, vpTo)) parseState.takeTree(); return new LanguageState(parseState); } } Language.state = /*@__PURE__*/StateField.define({ create: LanguageState.init, update(value, tr) { for (let e of tr.effects) if (e.is(Language.setState)) return e.value; if (tr.startState.facet(language) != tr.state.facet(language)) return LanguageState.init(tr.state); return value.apply(tr); } }); let requestIdle = (callback) => { let timeout = setTimeout(() => callback(), 500 /* Work.MaxPause */); return () => clearTimeout(timeout); }; if (typeof requestIdleCallback != "undefined") requestIdle = (callback) => { let idle = -1, timeout = setTimeout(() => { idle = requestIdleCallback(callback, { timeout: 500 /* Work.MaxPause */ - 100 /* Work.MinPause */ }); }, 100 /* Work.MinPause */); return () => idle < 0 ? clearTimeout(timeout) : cancelIdleCallback(idle); }; const isInputPending = typeof navigator != "undefined" && ((_a = navigator.scheduling) === null || _a === void 0 ? void 0 : _a.isInputPending) ? () => navigator.scheduling.isInputPending() : null; const parseWorker = /*@__PURE__*/ViewPlugin.fromClass(class ParseWorker { constructor(view) { this.view = view; this.working = null; this.workScheduled = 0; // End of the current time chunk this.chunkEnd = -1; // Milliseconds of budget left for this chunk this.chunkBudget = -1; this.work = this.work.bind(this); this.scheduleWork(); } update(update) { let cx = this.view.state.field(Language.state).context; if (cx.updateViewport(update.view.viewport) || this.view.viewport.to > cx.treeLen) this.scheduleWork(); if (update.docChanged || update.selectionSet) { if (this.view.hasFocus) this.chunkBudget += 50 /* Work.ChangeBonus */; this.scheduleWork(); } this.checkAsyncSchedule(cx); } scheduleWork() { if (this.working) return; let { state } = this.view, field = state.field(Language.state); if (field.tree != field.context.tree || !field.context.isDone(state.doc.length)) this.working = requestIdle(this.work); } work(deadline) { this.working = null; let now = Date.now(); if (this.chunkEnd < now && (this.chunkEnd < 0 || this.view.hasFocus)) { // Start a new chunk this.chunkEnd = now + 30000 /* Work.ChunkTime */; this.chunkBudget = 3000 /* Work.ChunkBudget */; } if (this.chunkBudget <= 0) return; // No more budget let { state, viewport: { to: vpTo } } = this.view, field = state.field(Language.state); if (field.tree == field.context.tree && field.context.isDone(vpTo + 100000 /* Work.MaxParseAhead */)) return; let endTime = Date.now() + Math.min(this.chunkBudget, 100 /* Work.Slice */, deadline && !isInputPending ? Math.max(25 /* Work.MinSlice */, deadline.timeRemaining() - 5) : 1e9); let viewportFirst = field.context.treeLen < vpTo && state.doc.length > vpTo + 1000; let done = field.context.work(() => { return isInputPending && isInputPending() || Date.now() > endTime; }, vpTo + (viewportFirst ? 0 : 100000 /* Work.MaxParseAhead */)); this.chunkBudget -= Date.now() - now; if (done || this.chunkBudget <= 0) { field.context.takeTree(); this.view.dispatch({ effects: Language.setState.of(new LanguageState(field.context)) }); } if (this.chunkBudget > 0 && !(done && !viewportFirst)) this.scheduleWork(); this.checkAsyncSchedule(field.context); } checkAsyncSchedule(cx) { if (cx.scheduleOn) { this.workScheduled++; cx.scheduleOn .then(() => this.scheduleWork()) .catch(err => logException(this.view.state, err)) .then(() => this.workScheduled--); cx.scheduleOn = null; } } destroy() { if (this.working) this.working(); } isWorking() { return !!(this.working || this.workScheduled > 0); } }, { eventHandlers: { focus() { this.scheduleWork(); } } }); /** The facet used to associate a language with an editor state. Used by `Language` object's `extension` property (so you don't need to manually wrap your languages in this). Can be used to access the current language on a state. */ const language = /*@__PURE__*/Facet.define({ combine(languages) { return languages.length ? languages[0] : null; }, enables: language => [ Language.state, parseWorker, EditorView.contentAttributes.compute([language], state => { let lang = state.facet(language); return lang && lang.name ? { "data-language": lang.name } : {}; }) ] }); /** This class bundles a [language](https://codemirror.net/6/docs/ref/#language.Language) with an optional set of supporting extensions. Language packages are encouraged to export a function that optionally takes a configuration object and returns a `LanguageSupport` instance, as the main way for client code to use the package. */ class LanguageSupport { /** Create a language support object. */ constructor( /** The language object. */ language, /** An optional set of supporting extensions. When nesting a language in another language, the outer language is encouraged to include the supporting extensions for its inner languages in its own set of support extensions. */ support = []) { this.language = language; this.support = support; this.extension = [language, support]; } } /** Language descriptions are used to store metadata about languages and to dynamically load them. Their main role is finding the appropriate language for a filename or dynamically loading nested parsers. */ class LanguageDescription { constructor( /** The name of this language. */ name, /** Alternative names for the mode (lowercased, includes `this.name`). */ alias, /** File extensions associated with this language. */ extensions, /** Optional filename pattern that should be associated with this language. */ filename, loadFunc, /** If the language has been loaded, this will hold its value. */ support = undefined) { this.name = name; this.alias = alias; this.extensions = extensions; this.filename = filename; this.loadFunc = loadFunc; this.support = support; this.loading = null; } /** Start loading the the language. Will return a promise that resolves to a [`LanguageSupport`](https://codemirror.net/6/docs/ref/#language.LanguageSupport) object when the language successfully loads. */ load() { return this.loading || (this.loading = this.loadFunc().then(support => this.support = support, err => { this.loading = null; throw err; })); } /** Create a language description. */ static of(spec) { let { load, support } = spec; if (!load) { if (!support) throw new RangeError("Must pass either 'load' or 'support' to LanguageDescription.of"); load = () => Promise.resolve(support); } return new LanguageDescription(spec.name, (spec.alias || []).concat(spec.name).map(s => s.toLowerCase()), spec.extensions || [], spec.filename, load, support); } /** Look for a language in the given array of descriptions that matches the filename. Will first match [`filename`](https://codemirror.net/6/docs/ref/#language.LanguageDescription.filename) patterns, and then [extensions](https://codemirror.net/6/docs/ref/#language.LanguageDescription.extensions), and return the first language that matches. */ static matchFilename(descs, filename) { for (let d of descs) if (d.filename && d.filename.test(filename)) return d; let ext = /\.([^.]+)$/.exec(filename); if (ext) for (let d of descs) if (d.extensions.indexOf(ext[1]) > -1) return d; return null; } /** Look for a language whose name or alias matches the the given name (case-insensitively). If `fuzzy` is true, and no direct matchs is found, this'll also search for a language whose name or alias occurs in the string (for names shorter than three characters, only when surrounded by non-word characters). */ static matchLanguageName(descs, name, fuzzy = true) { name = name.toLowerCase(); for (let d of descs) if (d.alias.some(a => a == name)) return d; if (fuzzy) for (let d of descs) for (let a of d.alias) { let found = name.indexOf(a); if (found > -1 && (a.length > 2 || !/\w/.test(name[found - 1]) && !/\w/.test(name[found + a.length]))) return d; } return null; } } /** Facet that defines a way to provide a function that computes the appropriate indentation depth, as a column number (see [`indentString`](https://codemirror.net/6/docs/ref/#language.indentString)), at the start of a given line. A return value of `null` indicates no indentation can be determined, and the line should inherit the indentation of the one above it. A return value of `undefined` defers to the next indent service. */ const indentService = /*@__PURE__*/Facet.define(); /** Facet for overriding the unit by which indentation happens. Should be a string consisting entirely of the same whitespace character. When not set, this defaults to 2 spaces. */ const indentUnit = /*@__PURE__*/Facet.define({ combine: values => { if (!values.length) return " "; let unit = values[0]; if (!unit || /\S/.test(unit) || Array.from(unit).some(e => e != unit[0])) throw new Error("Invalid indent unit: " + JSON.stringify(values[0])); return unit; } }); /** Return the _column width_ of an indent unit in the state. Determined by the [`indentUnit`](https://codemirror.net/6/docs/ref/#language.indentUnit) facet, and [`tabSize`](https://codemirror.net/6/docs/ref/#state.EditorState^tabSize) when that contains tabs. */ function getIndentUnit(state) { let unit = state.facet(indentUnit); return unit.charCodeAt(0) == 9 ? state.tabSize * unit.length : unit.length; } /** Create an indentation string that covers columns 0 to `cols`. Will use tabs for as much of the columns as possible when the [`indentUnit`](https://codemirror.net/6/docs/ref/#language.indentUnit) facet contains tabs. */ function indentString(state, cols) { let result = "", ts = state.tabSize, ch = state.facet(indentUnit)[0]; if (ch == "\t") { while (cols >= ts) { result += "\t"; cols -= ts; } ch = " "; } for (let i = 0; i < cols; i++) result += ch; return result; } /** Get the indentation, as a column number, at the given position. Will first consult any [indent services](https://codemirror.net/6/docs/ref/#language.indentService) that are registered, and if none of those return an indentation, this will check the syntax tree for the [indent node prop](https://codemirror.net/6/docs/ref/#language.indentNodeProp) and use that if found. Returns a number when an indentation could be determined, and null otherwise. */ function getIndentation(context, pos) { if (context instanceof EditorState) context = new IndentContext(context); for (let service of context.state.facet(indentService)) { let result = service(context, pos); if (result !== undefined) return result; } let tree = syntaxTree(context.state); return tree.length >= pos ? syntaxIndentation(context, tree, pos) : null; } /** Create a change set that auto-indents all lines touched by the given document range. */ function indentRange(state, from, to) { let updated = Object.create(null); let context = new IndentContext(state, { overrideIndentation: start => { var _a; return (_a = updated[start]) !== null && _a !== void 0 ? _a : -1; } }); let changes = []; for (let pos = from; pos <= to;) { let line = state.doc.lineAt(pos); pos = line.to + 1; let indent = getIndentation(context, line.from); if (indent == null) continue; if (!/\S/.test(line.text)) indent = 0; let cur = /^\s*/.exec(line.text)[0]; let norm = indentString(state, indent); if (cur != norm) { updated[line.from] = indent; changes.push({ from: line.from, to: line.from + cur.length, insert: norm }); } } return state.changes(changes); } /** Indentation contexts are used when calling [indentation services](https://codemirror.net/6/docs/ref/#language.indentService). They provide helper utilities useful in indentation logic, and can selectively override the indentation reported for some lines. */ class IndentContext { /** Create an indent context. */ constructor( /** The editor state. */ state, /** @internal */ options = {}) { this.state = state; this.options = options; this.unit = getIndentUnit(state); } /** Get a description of the line at the given position, taking [simulated line breaks](https://codemirror.net/6/docs/ref/#language.IndentContext.constructor^options.simulateBreak) into account. If there is such a break at `pos`, the `bias` argument determines whether the part of the line line before or after the break is used. */ lineAt(pos, bias = 1) { let line = this.state.doc.lineAt(pos); let { simulateBreak, simulateDoubleBreak } = this.options; if (simulateBreak != null && simulateBreak >= line.from && simulateBreak <= line.to) { if (simulateDoubleBreak && simulateBreak == pos) return { text: "", from: pos }; else if (bias < 0 ? simulateBreak < pos : simulateBreak <= pos) return { text: line.text.slice(simulateBreak - line.from), from: simulateBreak }; else return { text: line.text.slice(0, simulateBreak - line.from), from: line.from }; } return line; } /** Get the text directly after `pos`, either the entire line or the next 100 characters, whichever is shorter. */ textAfterPos(pos, bias = 1) { if (this.options.simulateDoubleBreak && pos == this.options.simulateBreak) return ""; let { text, from } = this.lineAt(pos, bias); return text.slice(pos - from, Math.min(text.length, pos + 100 - from)); } /** Find the column for the given position. */ column(pos, bias = 1) { let { text, from } = this.lineAt(pos, bias); let result = this.countColumn(text, pos - from); let override = this.options.overrideIndentation ? this.options.overrideIndentation(from) : -1; if (override > -1) result += override - this.countColumn(text, text.search(/\S|$/)); return result; } /** Find the column position (taking tabs into account) of the given position in the given string. */ countColumn(line, pos = line.length) { return countColumn(line, this.state.tabSize, pos); } /** Find the indentation column of the line at the given point. */ lineIndent(pos, bias = 1) { let { text, from } = this.lineAt(pos, bias); let override = this.options.overrideIndentation; if (override) { let overriden = override(from); if (overriden > -1) return overriden; } return this.countColumn(text, text.search(/\S|$/)); } /** Returns the [simulated line break](https://codemirror.net/6/docs/ref/#language.IndentContext.constructor^options.simulateBreak) for this context, if any. */ get simulatedBreak() { return this.options.simulateBreak || null; } } /** A syntax tree node prop used to associate indentation strategies with node types. Such a strategy is a function from an indentation context to a column number (see also [`indentString`](https://codemirror.net/6/docs/ref/#language.indentString)) or null, where null indicates that no definitive indentation can be determined. */ const indentNodeProp = /*@__PURE__*/new NodeProp(); // Compute the indentation for a given position from the syntax tree. function syntaxIndentation(cx, ast, pos) { let stack = ast.resolveStack(pos); let inner = ast.resolveInner(pos, -1).resolve(pos, 0).enterUnfinishedNodesBefore(pos); if (inner != stack.node) { let add = []; for (let cur = inner; cur && !(cur.from < stack.node.from || cur.to > stack.node.to || cur.from == stack.node.from && cur.type == stack.node.type); cur = cur.parent) add.push(cur); for (let i = add.length - 1; i >= 0; i--) stack = { node: add[i], next: stack }; } return indentFor(stack, cx, pos); } function indentFor(stack, cx, pos) { for (let cur = stack; cur; cur = cur.next) { let strategy = indentStrategy(cur.node); if (strategy) return strategy(TreeIndentContext.create(cx, pos, cur)); } return 0; } function ignoreClosed(cx) { return cx.pos == cx.options.simulateBreak && cx.options.simulateDoubleBreak; } function indentStrategy(tree) { let strategy = tree.type.prop(indentNodeProp); if (strategy) return strategy; let first = tree.firstChild, close; if (first && (close = first.type.prop(NodeProp.closedBy))) { let last = tree.lastChild, closed = last && close.indexOf(last.name) > -1; return cx => delimitedStrategy(cx, true, 1, undefined, closed && !ignoreClosed(cx) ? last.from : undefined); } return tree.parent == null ? topIndent : null; } function topIndent() { return 0; } /** Objects of this type provide context information and helper methods to indentation functions registered on syntax nodes. */ class TreeIndentContext extends IndentContext { constructor(base, /** The position at which indentation is being computed. */ pos, /** @internal */ context) { super(base.state, base.options); this.base = base; this.pos = pos; this.context = context; } /** The syntax tree node to which the indentation strategy applies. */ get node() { return this.context.node; } /** @internal */ static create(base, pos, context) { return new TreeIndentContext(base, pos, context); } /** Get the text directly after `this.pos`, either the entire line or the next 100 characters, whichever is shorter. */ get textAfter() { return this.textAfterPos(this.pos); } /** Get the indentation at the reference line for `this.node`, which is the line on which it starts, unless there is a node that is _not_ a parent of this node covering the start of that line. If so, the line at the start of that node is tried, again skipping on if it is covered by another such node. */ get baseIndent() { return this.baseIndentFor(this.node); } /** Get the indentation for the reference line of the given node (see [`baseIndent`](https://codemirror.net/6/docs/ref/#language.TreeIndentContext.baseIndent)). */ baseIndentFor(node) { let line = this.state.doc.lineAt(node.from); // Skip line starts that are covered by a sibling (or cousin, etc) for (;;) { let atBreak = node.resolve(line.from); while (atBreak.parent && atBreak.parent.from == atBreak.from) atBreak = atBreak.parent; if (isParent(atBreak, node)) break; line = this.state.doc.lineAt(atBreak.from); } return this.lineIndent(line.from); } /** Continue looking for indentations in the node's parent nodes, and return the result of that. */ continue() { return indentFor(this.context.next, this.base, this.pos); } } function isParent(parent, of) { for (let cur = of; cur; cur = cur.parent) if (parent == cur) return true; return false; } // Check whether a delimited node is aligned (meaning there are // non-skipped nodes on the same line as the opening delimiter). And // if so, return the opening token. function bracketedAligned(context) { let tree = context.node; let openToken = tree.childAfter(tree.from), last = tree.lastChild; if (!openToken) return null; let sim = context.options.simulateBreak; let openLine = context.state.doc.lineAt(openToken.from); let lineEnd = sim == null || sim <= openLine.from ? openLine.to : Math.min(openLine.to, sim); for (let pos = openToken.to;;) { let next = tree.childAfter(pos); if (!next || next == last) return null; if (!next.type.isSkipped) { if (next.from >= lineEnd) return null; let space = /^ */.exec(openLine.text.slice(openToken.to - openLine.from))[0].length; return { from: openToken.from, to: openToken.to + space }; } pos = next.to; } } /** An indentation strategy for delimited (usually bracketed) nodes. Will, by default, indent one unit more than the parent's base indent unless the line starts with a closing token. When `align` is true and there are non-skipped nodes on the node's opening line, the content of the node will be aligned with the end of the opening node, like this: foo(bar, baz) */ function delimitedIndent({ closing, align = true, units = 1 }) { return (context) => delimitedStrategy(context, align, units, closing); } function delimitedStrategy(context, align, units, closing, closedAt) { let after = context.textAfter, space = after.match(/^\s*/)[0].length; let closed = closing && after.slice(space, space + closing.length) == closing || closedAt == context.pos + space; let aligned = align ? bracketedAligned(context) : null; if (aligned) return closed ? context.column(aligned.from) : context.column(aligned.to); return context.baseIndent + (closed ? 0 : context.unit * units); } /** An indentation strategy that aligns a node's content to its base indentation. */ const flatIndent = (context) => context.baseIndent; /** Creates an indentation strategy that, by default, indents continued lines one unit more than the node's base indentation. You can provide `except` to prevent indentation of lines that match a pattern (for example `/^else\b/` in `if`/`else` constructs), and you can change the amount of units used with the `units` option. */ function continuedIndent({ except, units = 1 } = {}) { return (context) => { let matchExcept = except && except.test(context.textAfter); return context.baseIndent + (matchExcept ? 0 : units * context.unit); }; } const DontIndentBeyond = 200; /** Enables reindentation on input. When a language defines an `indentOnInput` field in its [language data](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt), which must hold a regular expression, the line at the cursor will be reindented whenever new text is typed and the input from the start of the line up to the cursor matches that regexp. To avoid unneccesary reindents, it is recommended to start the regexp with `^` (usually followed by `\s*`), and end it with `$`. For example, `/^\s*\}$/` will reindent when a closing brace is added at the start of a line. */ function indentOnInput() { return EditorState.transactionFilter.of(tr => { if (!tr.docChanged || !tr.isUserEvent("input.type") && !tr.isUserEvent("input.complete")) return tr; let rules = tr.startState.languageDataAt("indentOnInput", tr.startState.selection.main.head); if (!rules.length) return tr; let doc = tr.newDoc, { head } = tr.newSelection.main, line = doc.lineAt(head); if (head > line.from + DontIndentBeyond) return tr; let lineStart = doc.sliceString(line.from, head); if (!rules.some(r => r.test(lineStart))) return tr; let { state } = tr, last = -1, changes = []; for (let { head } of state.selection.ranges) { let line = state.doc.lineAt(head); if (line.from == last) continue; last = line.from; let indent = getIndentation(state, line.from); if (indent == null) continue; let cur = /^\s*/.exec(line.text)[0]; let norm = indentString(state, indent); if (cur != norm) changes.push({ from: line.from, to: line.from + cur.length, insert: norm }); } return changes.length ? [tr, { changes, sequential: true }] : tr; }); } /** A facet that registers a code folding service. When called with the extent of a line, such a function should return a foldable range that starts on that line (but continues beyond it), if one can be found. */ const foldService = /*@__PURE__*/Facet.define(); /** This node prop is used to associate folding information with syntax node types. Given a syntax node, it should check whether that tree is foldable and return the range that can be collapsed when it is. */ const foldNodeProp = /*@__PURE__*/new NodeProp(); /** [Fold](https://codemirror.net/6/docs/ref/#language.foldNodeProp) function that folds everything but the first and the last child of a syntax node. Useful for nodes that start and end with delimiters. */ function foldInside(node) { let first = node.firstChild, last = node.lastChild; return first && first.to < last.from ? { from: first.to, to: last.type.isError ? node.to : last.from } : null; } function syntaxFolding(state, start, end) { let tree = syntaxTree(state); if (tree.length < end) return null; let stack = tree.resolveStack(end, 1); let found = null; for (let iter = stack; iter; iter = iter.next) { let cur = iter.node; if (cur.to <= end || cur.from > end) continue; if (found && cur.from < start) break; let prop = cur.type.prop(foldNodeProp); if (prop && (cur.to < tree.length - 50 || tree.length == state.doc.length || !isUnfinished(cur))) { let value = prop(cur, state); if (value && value.from <= end && value.from >= start && value.to > end) found = value; } } return found; } function isUnfinished(node) { let ch = node.lastChild; return ch && ch.to == node.to && ch.type.isError; } /** Check whether the given line is foldable. First asks any fold services registered through [`foldService`](https://codemirror.net/6/docs/ref/#language.foldService), and if none of them return a result, tries to query the [fold node prop](https://codemirror.net/6/docs/ref/#language.foldNodeProp) of syntax nodes that cover the end of the line. */ function foldable(state, lineStart, lineEnd) { for (let service of state.facet(foldService)) { let result = service(state, lineStart, lineEnd); if (result) return result; } return syntaxFolding(state, lineStart, lineEnd); } function mapRange(range, mapping) { let from = mapping.mapPos(range.from, 1), to = mapping.mapPos(range.to, -1); return from >= to ? undefined : { from, to }; } /** State effect that can be attached to a transaction to fold the given range. (You probably only need this in exceptional circumstances—usually you'll just want to let [`foldCode`](https://codemirror.net/6/docs/ref/#language.foldCode) and the [fold gutter](https://codemirror.net/6/docs/ref/#language.foldGutter) create the transactions.) */ const foldEffect = /*@__PURE__*/StateEffect.define({ map: mapRange }); /** State effect that unfolds the given range (if it was folded). */ const unfoldEffect = /*@__PURE__*/StateEffect.define({ map: mapRange }); function selectedLines(view) { let lines = []; for (let { head } of view.state.selection.ranges) { if (lines.some(l => l.from <= head && l.to >= head)) continue; lines.push(view.lineBlockAt(head)); } return lines; } /** The state field that stores the folded ranges (as a [decoration set](https://codemirror.net/6/docs/ref/#view.DecorationSet)). Can be passed to [`EditorState.toJSON`](https://codemirror.net/6/docs/ref/#state.EditorState.toJSON) and [`fromJSON`](https://codemirror.net/6/docs/ref/#state.EditorState^fromJSON) to serialize the fold state. */ const foldState = /*@__PURE__*/StateField.define({ create() { return Decoration.none; }, update(folded, tr) { if (tr.isUserEvent("delete")) tr.changes.iterChangedRanges((fromA, toA) => folded = clearTouchedFolds(folded, fromA, toA)); folded = folded.map(tr.changes); for (let e of tr.effects) { if (e.is(foldEffect) && !foldExists(folded, e.value.from, e.value.to)) { let { preparePlaceholder } = tr.state.facet(foldConfig); let widget = !preparePlaceholder ? foldWidget : Decoration.replace({ widget: new PreparedFoldWidget(preparePlaceholder(tr.state, e.value)) }); folded = folded.