123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649 |
- import { NodeProp } from '@lezer/common';
- let nextTagID = 0;
- class Tag {
-
- constructor(
- /// The set of this tag and all its parent tags, starting with
- /// this one itself and sorted in order of decreasing specificity.
- set,
- /// The base unmodified tag that this one is based on, if it's
- /// modified @internal
- base,
- /// The modifiers applied to this.base @internal
- modified) {
- this.set = set;
- this.base = base;
- this.modified = modified;
- /// @internal
- this.id = nextTagID++;
- }
- /// Define a new tag. If `parent` is given, the tag is treated as a
- /// sub-tag of that parent, and
- /// [highlighters](#highlight.tagHighlighter) that don't mention
- /// this tag will try to fall back to the parent tag (or grandparent
- /// tag, etc).
- static define(parent) {
- if (parent === null || parent === void 0 ? void 0 : parent.base)
- throw new Error("Can not derive from a modified tag");
- let tag = new Tag([], null, []);
- tag.set.push(tag);
- if (parent)
- for (let t of parent.set)
- tag.set.push(t);
- return tag;
- }
-
-
-
-
-
-
-
-
-
-
- static defineModifier() {
- let mod = new Modifier;
- return (tag) => {
- if (tag.modified.indexOf(mod) > -1)
- return tag;
- return Modifier.get(tag.base || tag, tag.modified.concat(mod).sort((a, b) => a.id - b.id));
- };
- }
- }
- let nextModifierID = 0;
- class Modifier {
- constructor() {
- this.instances = [];
- this.id = nextModifierID++;
- }
- static get(base, mods) {
- if (!mods.length)
- return base;
- let exists = mods[0].instances.find(t => t.base == base && sameArray(mods, t.modified));
- if (exists)
- return exists;
- let set = [], tag = new Tag(set, base, mods);
- for (let m of mods)
- m.instances.push(tag);
- let configs = permute(mods);
- for (let parent of base.set)
- for (let config of configs)
- set.push(Modifier.get(parent, config));
- return tag;
- }
- }
- function sameArray(a, b) {
- return a.length == b.length && a.every((x, i) => x == b[i]);
- }
- function permute(array) {
- let result = [array];
- for (let i = 0; i < array.length; i++) {
- for (let a of permute(array.slice(0, i).concat(array.slice(i + 1))))
- result.push(a);
- }
- return result;
- }
- function styleTags(spec) {
- let byName = Object.create(null);
- for (let prop in spec) {
- let tags = spec[prop];
- if (!Array.isArray(tags))
- tags = [tags];
- for (let part of prop.split(" "))
- if (part) {
- let pieces = [], mode = 2 , rest = part;
- for (let pos = 0;;) {
- if (rest == "..." && pos > 0 && pos + 3 == part.length) {
- mode = 1 ;
- break;
- }
- let m = /^"(?:[^"\\]|\\.)*?"|[^\/!]+/.exec(rest);
- if (!m)
- throw new RangeError("Invalid path: " + part);
- pieces.push(m[0] == "*" ? "" : m[0][0] == '"' ? JSON.parse(m[0]) : m[0]);
- pos += m[0].length;
- if (pos == part.length)
- break;
- let next = part[pos++];
- if (pos == part.length && next == "!") {
- mode = 0 ;
- break;
- }
- if (next != "/")
- throw new RangeError("Invalid path: " + part);
- rest = part.slice(pos);
- }
- let last = pieces.length - 1, inner = pieces[last];
- if (!inner)
- throw new RangeError("Invalid path: " + part);
- let rule = new Rule(tags, mode, last > 0 ? pieces.slice(0, last) : null);
- byName[inner] = rule.sort(byName[inner]);
- }
- }
- return ruleNodeProp.add(byName);
- }
- const ruleNodeProp = new NodeProp();
- class Rule {
- constructor(tags, mode, context, next) {
- this.tags = tags;
- this.mode = mode;
- this.context = context;
- this.next = next;
- }
- sort(other) {
- if (!other || other.depth < this.depth) {
- this.next = other;
- return this;
- }
- other.next = this.sort(other.next);
- return other;
- }
- get depth() { return this.context ? this.context.length : 0; }
- }
- function tagHighlighter(tags, options) {
- let map = Object.create(null);
- for (let style of tags) {
- if (!Array.isArray(style.tag))
- map[style.tag.id] = style.class;
- else
- for (let tag of style.tag)
- map[tag.id] = style.class;
- }
- let { scope, all = null } = options || {};
- return {
- style: (tags) => {
- let cls = all;
- for (let tag of tags) {
- for (let sub of tag.set) {
- let tagClass = map[sub.id];
- if (tagClass) {
- cls = cls ? cls + " " + tagClass : tagClass;
- break;
- }
- }
- }
- return cls;
- },
- scope: scope
- };
- }
- function highlightTags(highlighters, tags) {
- let result = null;
- for (let highlighter of highlighters) {
- let value = highlighter.style(tags);
- if (value)
- result = result ? result + " " + value : value;
- }
- return result;
- }
- function highlightTree(tree, highlighter,
- /// Assign styling to a region of the text. Will be called, in order
- /// of position, for any ranges where more than zero classes apply.
- /// `classes` is a space separated string of CSS classes.
- putStyle,
- /// The start of the range to highlight.
- from = 0,
- /// The end of the range.
- to = tree.length) {
- let builder = new HighlightBuilder(from, Array.isArray(highlighter) ? highlighter : [highlighter], putStyle);
- builder.highlightRange(tree.cursor(), from, to, "", builder.highlighters);
- builder.flush(to);
- }
- class HighlightBuilder {
- constructor(at, highlighters, span) {
- this.at = at;
- this.highlighters = highlighters;
- this.span = span;
- this.class = "";
- }
- startSpan(at, cls) {
- if (cls != this.class) {
- this.flush(at);
- if (at > this.at)
- this.at = at;
- this.class = cls;
- }
- }
- flush(to) {
- if (to > this.at && this.class)
- this.span(this.at, to, this.class);
- }
- highlightRange(cursor, from, to, inheritedClass, highlighters) {
- let { type, from: start, to: end } = cursor;
- if (start >= to || end <= from)
- return;
- if (type.isTop)
- highlighters = this.highlighters.filter(h => !h.scope || h.scope(type));
- let cls = inheritedClass;
- let rule = type.prop(ruleNodeProp), opaque = false;
- while (rule) {
- if (!rule.context || cursor.matchContext(rule.context)) {
- let tagCls = highlightTags(highlighters, rule.tags);
- if (tagCls) {
- if (cls)
- cls += " ";
- cls += tagCls;
- if (rule.mode == 1 /* Inherit */)
- inheritedClass += (inheritedClass ? " " : "") + tagCls;
- else if (rule.mode == 0 /* Opaque */)
- opaque = true;
- }
- break;
- }
- rule = rule.next;
- }
- this.startSpan(cursor.from, cls);
- if (opaque)
- return;
- let mounted = cursor.tree && cursor.tree.prop(NodeProp.mounted);
- if (mounted && mounted.overlay) {
- let inner = cursor.node.enter(mounted.overlay[0].from + start, 1);
- let innerHighlighters = this.highlighters.filter(h => !h.scope || h.scope(mounted.tree.type));
- let hasChild = cursor.firstChild();
- for (let i = 0, pos = start;; i++) {
- let next = i < mounted.overlay.length ? mounted.overlay[i] : null;
- let nextPos = next ? next.from + start : end;
- let rangeFrom = Math.max(from, pos), rangeTo = Math.min(to, nextPos);
- if (rangeFrom < rangeTo && hasChild) {
- while (cursor.from < rangeTo) {
- this.highlightRange(cursor, rangeFrom, rangeTo, inheritedClass, highlighters);
- this.startSpan(Math.min(to, cursor.to), cls);
- if (cursor.to >= nextPos || !cursor.nextSibling())
- break;
- }
- }
- if (!next || nextPos > to)
- break;
- pos = next.to + start;
- if (pos > from) {
- this.highlightRange(inner.cursor(), Math.max(from, next.from + start), Math.min(to, pos), inheritedClass, innerHighlighters);
- this.startSpan(pos, cls);
- }
- }
- if (hasChild)
- cursor.parent();
- }
- else if (cursor.firstChild()) {
- do {
- if (cursor.to <= from)
- continue;
- if (cursor.from >= to)
- break;
- this.highlightRange(cursor, from, to, inheritedClass, highlighters);
- this.startSpan(Math.min(to, cursor.to), cls);
- } while (cursor.nextSibling());
- cursor.parent();
- }
- }
- }
- const t = Tag.define;
- const comment = t(), name = t(), typeName = t(name), propertyName = t(name), literal = t(), string = t(literal), number = t(literal), content = t(), heading = t(content), keyword = t(), operator = t(), punctuation = t(), bracket = t(punctuation), meta = t();
- /// The default set of highlighting [tags](#highlight.Tag).
- ///
- /// This collection is heavily biased towards programming languages,
- /// and necessarily incomplete. A full ontology of syntactic
- /// constructs would fill a stack of books, and be impractical to
- /// write themes for. So try to make do with this set. If all else
- /// fails, [open an
- /// issue](https://github.com/codemirror/codemirror.next) to propose a
- /// new tag, or [define](#highlight.Tag^define) a local custom tag for
- /// your use case.
- ///
- /// Note that it is not obligatory to always attach the most specific
- /// tag possible to an element—if your grammar can't easily
- /// distinguish a certain type of element (such as a local variable),
- /// it is okay to style it as its more general variant (a variable).
- ///
- /// For tags that extend some parent tag, the documentation links to
- /// the parent.
- const tags = {
-
- comment,
-
- lineComment: t(comment),
-
- blockComment: t(comment),
-
- docComment: t(comment),
-
- name,
-
- variableName: t(name),
-
- typeName: typeName,
-
- tagName: t(typeName),
-
- propertyName: propertyName,
-
- attributeName: t(propertyName),
-
- className: t(name),
-
- labelName: t(name),
-
- namespace: t(name),
-
- macroName: t(name),
-
- literal,
-
- string,
-
- docString: t(string),
-
- character: t(string),
-
- attributeValue: t(string),
-
- number,
-
- integer: t(number),
-
- float: t(number),
-
- bool: t(literal),
-
- regexp: t(literal),
-
-
- escape: t(literal),
-
- color: t(literal),
-
- url: t(literal),
-
- keyword,
-
-
- self: t(keyword),
-
- null: t(keyword),
-
- atom: t(keyword),
-
- unit: t(keyword),
-
- modifier: t(keyword),
-
- operatorKeyword: t(keyword),
-
- controlKeyword: t(keyword),
-
- definitionKeyword: t(keyword),
-
-
- moduleKeyword: t(keyword),
-
- operator,
-
- derefOperator: t(operator),
-
- arithmeticOperator: t(operator),
-
- logicOperator: t(operator),
-
- bitwiseOperator: t(operator),
-
- compareOperator: t(operator),
-
- updateOperator: t(operator),
-
- definitionOperator: t(operator),
-
- typeOperator: t(operator),
-
- controlOperator: t(operator),
-
- punctuation,
-
-
- separator: t(punctuation),
-
- bracket,
-
-
- angleBracket: t(bracket),
-
-
- squareBracket: t(bracket),
-
-
- paren: t(bracket),
-
-
- brace: t(bracket),
-
- content,
-
- heading,
-
- heading1: t(heading),
-
- heading2: t(heading),
-
- heading3: t(heading),
-
- heading4: t(heading),
-
- heading5: t(heading),
-
- heading6: t(heading),
-
- contentSeparator: t(content),
-
- list: t(content),
-
- quote: t(content),
-
- emphasis: t(content),
-
- strong: t(content),
-
- link: t(content),
-
-
- monospace: t(content),
-
-
- strikethrough: t(content),
-
- inserted: t(),
-
- deleted: t(),
-
- changed: t(),
-
- invalid: t(),
-
- meta,
-
-
- documentMeta: t(meta),
-
-
- annotation: t(meta),
-
-
- processingInstruction: t(meta),
-
-
-
- definition: Tag.defineModifier(),
-
-
-
- constant: Tag.defineModifier(),
-
-
-
-
- function: Tag.defineModifier(),
-
-
-
- standard: Tag.defineModifier(),
-
-
- local: Tag.defineModifier(),
-
-
-
-
-
-
- special: Tag.defineModifier()
- };
- const classHighlighter = tagHighlighter([
- { tag: tags.link, class: "tok-link" },
- { tag: tags.heading, class: "tok-heading" },
- { tag: tags.emphasis, class: "tok-emphasis" },
- { tag: tags.strong, class: "tok-strong" },
- { tag: tags.keyword, class: "tok-keyword" },
- { tag: tags.atom, class: "tok-atom" },
- { tag: tags.bool, class: "tok-bool" },
- { tag: tags.url, class: "tok-url" },
- { tag: tags.labelName, class: "tok-labelName" },
- { tag: tags.inserted, class: "tok-inserted" },
- { tag: tags.deleted, class: "tok-deleted" },
- { tag: tags.literal, class: "tok-literal" },
- { tag: tags.string, class: "tok-string" },
- { tag: tags.number, class: "tok-number" },
- { tag: [tags.regexp, tags.escape, tags.special(tags.string)], class: "tok-string2" },
- { tag: tags.variableName, class: "tok-variableName" },
- { tag: tags.local(tags.variableName), class: "tok-variableName tok-local" },
- { tag: tags.definition(tags.variableName), class: "tok-variableName tok-definition" },
- { tag: tags.special(tags.variableName), class: "tok-variableName2" },
- { tag: tags.definition(tags.propertyName), class: "tok-propertyName tok-definition" },
- { tag: tags.typeName, class: "tok-typeName" },
- { tag: tags.namespace, class: "tok-namespace" },
- { tag: tags.className, class: "tok-className" },
- { tag: tags.macroName, class: "tok-macroName" },
- { tag: tags.propertyName, class: "tok-propertyName" },
- { tag: tags.operator, class: "tok-operator" },
- { tag: tags.comment, class: "tok-comment" },
- { tag: tags.meta, class: "tok-meta" },
- { tag: tags.invalid, class: "tok-invalid" },
- { tag: tags.punctuation, class: "tok-punctuation" }
- ]);
- export { Tag, classHighlighter, highlightTree, styleTags, tagHighlighter, tags };
|