Reference Manual

CodeMirror 6 is published as a set of NPM packages under the @codemirror scope. The core packages are listed in this reference guide.

Each package exposes ECMAScript and CommonJS modules. You'll have to use some kind of bundler or loader to run them in the browser.

The most important modules are state, which contains the data structures that model the editor state, and view, which provides the UI component for an editor.

A minimal editor might look like this:

import {EditorView} from "@codemirror/view"
import {EditorState} from "@codemirror/state"

let myView = new EditorView({
  state: EditorState.create({doc: "hello"}),
  parent: document.body
})

But such an editor is too primitive for real use. To get functionality like key bindings, highlighting, a line number gutter, or an undo history, you need to add various extensions to such a basic editor.

To quickly get started, the basic-setup package provides a bundle of extensions to set up a functioning editor.

@codemirror/text

The Text type stores documents in an immutable tree-shaped representation that allows:

Line numbers start at 1. Character positions are counted from zero, and count each line break and UTF-16 code unit as one unit.

abstract class Text implements Iterable<string>

The data structure for documents.

abstract length: number

The length of the string.

abstract lines: number

The number of lines in the string (always >= 1).

lineAt(posnumber) → Line

Get the line description around the given position.

line(nnumber) → Line

Get the description for the given (1-based) line number.

replace(fromnumber, tonumber, textText) → Text

Replace a range of the text with the given content.

append(otherText) → Text

Append another document to this one.

slice(fromnumber, to⁠?: number = this.length) → Text

Retrieve the text between the given points.

abstract sliceString(fromnumber, to⁠?: number, lineSep⁠?: string) → string

Retrieve a part of the document as a string

eq(otherText) → boolean

Test whether this text is equal to another instance.

iter(dir⁠?: 1 | -1 = 1) → TextIterator

Iterate over the text. When dir is -1, iteration happens from end to start. This will return lines and the breaks between them as separate strings, and for long lines, might split lines themselves into multiple chunks as well.

iterRange(fromnumber, to⁠?: number = this.length) → TextIterator

Iterate over a range of the text. When from > to, the iterator will run in reverse.

iterLines(from⁠?: number, to⁠?: number) → TextIterator

Return a cursor that iterates over the given range of lines, without returning the line breaks between, and yielding empty strings for empty lines.

When from and to are given, they should be 1-based line numbers.

toJSON() → string[]

Convert the document to an array of lines (which can be deserialized again via Text.of).

abstract children: readonly Text[] | null

If this is a branch node, children will hold the Text objects that it is made up of. For leaf nodes, this holds null.

[unique symbol]() → Iterator<string>
static of(textreadonly string[]) → Text

Create a Text instance for the given array of lines.

static empty: Text

The empty document.

class Line

This type describes a line in the document. It is created on-demand when lines are queried.

from: number

The position of the start of the line.

to: number

The position at the end of the line (before the line break, or at the end of document for the last line).

number: number

This line's line number (1-based).

text: string

The line's content.

length: number

The length of the line (not including any line break after it).

interface TextIterator extends Iterator<string> extends Iterable<string>

A text iterator iterates over a sequence of strings. When iterating over a Text document, result values will either be lines or line breaks.

next(skip⁠?: number) → TextIterator

Retrieve the next string. Optionally skip a given number of positions after the current position. Always returns the object itself.

value: string

The current string. Will be the empty string when the cursor is at its end or next hasn't been called on it yet.

done: boolean

Whether the end of the iteration has been reached. You should probably check this right after calling next.

lineBreak: boolean

Whether the current string represents a line break.

Column Utilities

countColumn(
to⁠?: number = string.length
) → number

Count the column position at the given offset into the string, taking extending characters and tab size into account.

findColumn() → number

Find the offset that corresponds to the given column position in a string, taking extending characters and tab size into account. By default, the string length is returned when it is too short to reach the column. Pass strict true to make it return -1 in that situation.

Code Points and Characters

If you support environments that don't yet have String.fromCodePoint and codePointAt, this package provides portable replacements for them.

codePointAt(strstring, posnumber) → number

Find the code point at the given position in a string (like the codePointAt string method).

fromCodePoint(codenumber) → string

Given a Unicode codepoint, return the JavaScript string that respresents it (like String.fromCodePoint).

codePointSize(codenumber) → 1 | 2

The first character that takes up two positions in a JavaScript string. It is often useful to compare with this after calling codePointAt, to figure out whether your character takes up 1 or 2 index positions.

findClusterBreak(
forward⁠?: boolean = true
) → number

Returns a next grapheme cluster break after (not equal to) pos, if forward is true, or before otherwise. Returns pos itself if no further cluster break is available in the string. Moves across surrogate pairs, extending characters, characters joined with zero-width joiners, and flag emoji.

@codemirror/state

In its most basic form, the editor state is made up of a current document and a selection. Because there are a lot of extra pieces that an editor might need to keep in its state (such as an undo history or syntax tree), it is possible for extensions to add additional fields to the state object.

interface EditorStateConfig

Options passed when creating an editor state.

doc⁠?: string | Text

The initial document. Defaults to an empty document. Can be provided either as a plain string (which will be split into lines according to the value of the lineSeparator facet), or an instance of the Text class (which is what the state will use to represent the document).

selection⁠?: EditorSelection | {anchornumber, head⁠?: number}

The starting selection. Defaults to a cursor at the very start of the document.

extensions⁠?: Extension

Extension(s) to associate with this state.

class EditorState

The editor state class is a persistent (immutable) data structure. To update a state, you create a transaction, which produces a new state instance, without modifying the original object.

As such, never mutate properties of a state directly. That'll just break things.

doc: Text

The current document.

selection: EditorSelection

The current selection.

field<T>(fieldStateField<T>) → T

Retrieve the value of a state field. Throws an error when the state doesn't have that field, unless you pass false as second parameter.

update(...specsreadonly TransactionSpec[]) → Transaction

Create a transaction that updates this state. Any number of transaction specs can be passed. Unless sequential is set, the changes (if any) of each spec are assumed to start in the current document (not the document produced by previous specs), and its selection and effects are assumed to refer to the document created by its own changes. The resulting transaction contains the combined effect of all the different specs. For selection, later specs take precedence over earlier ones.

replaceSelection(textstring | Text) → {
changesChangeSet,
selectionEditorSelection,
effectsreadonly StateEffect<any>[]
}

Create a transaction spec that replaces every selection range with the given content.

changeByRange(
ffn(rangeSelectionRange) → {
rangeSelectionRange,
changes⁠?: ChangeSpec,
effects⁠?: StateEffect<any> | readonly StateEffect<any>[]
}
) → {
changesChangeSet,
selectionEditorSelection,
effectsreadonly StateEffect<any>[]
}

Create a set of changes and a new selection by running the given function for each range in the active selection. The function can return an optional set of changes (in the coordinate space of the start document), plus an updated range (in the coordinate space of the document produced by the call's own changes). This method will merge all the changes and ranges into a single changeset and selection, and return it as a transaction spec, which can be passed to update.

changes(spec⁠?: ChangeSpec = []) → ChangeSet

Create a change set from the given change description, taking the state's document length and line separator into account.

toText(stringstring) → Text

Using the state's line separator, create a Text instance from the given string.

sliceDoc(
from⁠?: number = 0,
to⁠?: number = this.doc.length
) → string

Return the given range of the document as a string.

facet<Output>(facetFacet<any, Output>) → Output

Get the value of a state facet.

toJSON(fields⁠?: Object<StateField<any>>) → any

Convert this state to a JSON-serializable object. When custom fields should be serialized, you can pass them in as an object mapping property names (in the resulting object, which should not use doc or selection) to fields.

tabSize: number

The size (in columns) of a tab in the document, determined by the tabSize facet.

lineBreak: string

Get the proper line-break string for this state.

readOnly: boolean

Returns true when the editor is configured to be read-only.

phrase(phrasestring) → string

Look up a translation for the given phrase (via the phrases facet), or return the original string if no translation is found.

languageDataAt<T>(
side⁠?: -1 | 0 | 1 = -1
) → readonly T[]

Find the values for a given language data field, provided by the the languageData facet.

charCategorizer(atnumber) → fn(charstring) → CharCategory

Return a function that can categorize strings (expected to represent a single grapheme cluster) into one of:

  • Word (contains an alphanumeric character or a character explicitly listed in the local language's "wordChars" language data, which should be a string)
  • Space (contains only whitespace)
  • Other (anything else)
wordAt(posnumber) → SelectionRange | null

Find the word at the given position, meaning the range containing all word characters around it. If no word characters are adjacent to the position, this returns null.

static fromJSON(
jsonany,
) → EditorState

Deserialize a state from its JSON representation. When custom fields should be deserialized, pass the same object you passed to toJSON when serializing as third argument.

static create(config⁠?: EditorStateConfig = {}) → EditorState

Create a new state. You'll usually only need this when initializing an editor—updated states are created by applying transactions.

static allowMultipleSelections: Facet<boolean, boolean>

A facet that, when enabled, causes the editor to allow multiple ranges to be selected. Be careful though, because by default the editor relies on the native DOM selection, which cannot handle multiple selections. An extension like drawSelection can be used to make secondary selections visible to the user.

static tabSize: Facet<number, number>

Configures the tab size to use in this state. The first (highest-precedence) value of the facet is used. If no value is given, this defaults to 4.

static lineSeparator: Facet<string, string | undefined>

The line separator to use. By default, any of "\n", "\r\n" and "\r" is treated as a separator when splitting lines, and lines are joined with "\n".

When you configure a value here, only that precise separator will be used, allowing you to round-trip documents through the editor without normalizing line separators.

static readOnly: Facet<boolean, boolean>

This facet controls the value of the readOnly getter, which is consulted by commands and extensions that implement editing functionality to determine whether they should apply. It defaults to false, but when its highest-precedence value is true, such functionality disables itself.

Not to be confused with EditorView.editable, which controls whether the editor's DOM is set to be editable (and thus focusable).

static phrases: Facet<Object<string>>

Registers translation phrases. The phrase method will look through all objects registered with this facet to find translations for its argument.

static languageData: Facet<
fn(
side-1 | 0 | 1
) → readonly Object<any>[]
>

A facet used to register language data providers.

static changeFilter: Facet<
fn(trTransaction) → boolean | readonly number[]
>

Facet used to register change filters, which are called for each transaction (unless explicitly disabled), and can suppress part of the transaction's changes.

Such a function can return true to indicate that it doesn't want to do anything, false to completely stop the changes in the transaction, or a set of ranges in which changes should be suppressed. Such ranges are represented as an array of numbers, with each pair of two number indicating the start and end of a range. So for example [10, 20, 100, 110] suppresses changes between 10 and 20, and between 100 and 110.

static transactionFilter: Facet<>

Facet used to register a hook that gets a chance to update or replace transaction specs before they are applied. This will only be applied for transactions that don't have filter set to false. You can either return a single transaction spec (possibly the input transaction), or an array of specs (which will be combined in the same way as the arguments to EditorState.update).

When possible, it is recommended to avoid accessing Transaction.state in a filter, since it will force creation of a state that will then be discarded again, if the transaction is actually filtered.

(This functionality should be used with care. Indiscriminately modifying transaction is likely to break something or degrade the user experience.)

static transactionExtender: Facet<
fn(trTransaction) → Pick<TransactionSpec, "effects" | "annotations"> | null
>

This is a more limited form of transactionFilter, which can only add annotations and effects. But, this type of filter runs even the transaction has disabled regular filtering, making it suitable for effects that don't need to touch the changes or selection, but do want to process every transaction.

Extenders run after filters, when both are applied.

class SelectionRange

A single selection range. When allowMultipleSelections is enabled, a selection may hold multiple ranges. By default, selections hold exactly one range.

from: number

The lower boundary of the range.

to: number

The upper boundary of the range.

anchor: number

The anchor of the range—the side that doesn't move when you extend it.

head: number

The head of the range, which is moved when the range is extended.

empty: boolean

True when anchor and head are at the same position.

assoc: -1 | 0 | 1

If this is a cursor that is explicitly associated with the character on one of its sides, this returns the side. -1 means the character before its position, 1 the character after, and 0 means no association.

bidiLevel: number | null

The bidirectional text level associated with this cursor, if any.

goalColumn: number | undefined

The goal column (stored vertical offset) associated with a cursor. This is used to preserve the vertical position when moving across lines of different length.

map(changeChangeDesc, assoc⁠?: number = -1) → SelectionRange

Map this range through a change, producing a valid range in the updated document.

extend(fromnumber, to⁠?: number = from) → SelectionRange

Extend this range to cover at least from to to.

eq(otherSelectionRange) → boolean

Compare this range to another range.

toJSON() → any

Return a JSON-serializable object representing the range.

static fromJSON(jsonany) → SelectionRange

Convert a JSON representation of a range to a SelectionRange instance.

class EditorSelection

An editor selection holds one or more selection ranges.

ranges: readonly SelectionRange[]

The ranges in the selection, sorted by position. Ranges cannot overlap (but they may touch, if they aren't empty).

mainIndex: number

The index of the main range in the selection (which is usually the range that was added last).

map(changeChangeDesc, assoc⁠?: number = -1) → EditorSelection

Map a selection through a change. Used to adjust the selection position for changes.

eq(otherEditorSelection) → boolean

Compare this selection to another selection.

main: SelectionRange

Get the primary selection range. Usually, you should make sure your code applies to all ranges, by using methods like changeByRange.

asSingle() → EditorSelection

Make sure the selection only has one range. Returns a selection holding only the main range from this selection.

addRange(rangeSelectionRange, main⁠?: boolean = true) → EditorSelection

Extend this selection with an extra range.

replaceRange(
which⁠?: number = this.mainIndex
) → EditorSelection

Replace a given range with another range, and then normalize the selection to merge and sort ranges if necessary.

toJSON() → any

Convert this selection to an object that can be serialized to JSON.

static fromJSON(jsonany) → EditorSelection

Create a selection from a JSON representation.

static single(anchornumber, head⁠?: number = anchor) → EditorSelection

Create a selection holding a single range.

static create(
rangesreadonly SelectionRange[],
mainIndex⁠?: number = 0
) → EditorSelection

Sort and merge the given set of ranges, creating a valid selection.

static cursor(
assoc⁠?: number = 0,
) → SelectionRange

Create a cursor selection range at the given position. You can safely ignore the optional arguments in most situations.

static range() → SelectionRange

Create a selection range.

enum CharCategory

The categories produced by a character categorizer. These are used do things like selecting by word.

Word

Word characters.

Space

Whitespace.

Other

Anything else.

re-export Text

Changes and Transactions

CodeMirror treats changes to the document as objects, which are usually part of a transaction.

This is how you'd make a change to a document (replacing “world” with “editor”) and create a new state with the updated document:

let state = EditorState.create({doc: "hello world"})
let transaction = state.update({changes: {from: 6, to: 11, insert: "editor"}})
console.log(transaction.state.doc.toString()) // "hello editor"

interface TransactionSpec

Describes a transaction when calling the EditorState.update method.

changes⁠?: ChangeSpec

The changes to the document made by this transaction.

selection⁠?: EditorSelection | {anchornumber, head⁠?: number}

When set, this transaction explicitly updates the selection. Offsets in this selection should refer to the document as it is after the transaction.

effects⁠?: StateEffect<any> | readonly StateEffect<any>[]

Attach state effects to this transaction. Again, when they contain positions and this same spec makes changes, those positions should refer to positions in the updated document.

annotations⁠?: Annotation<any> | readonly Annotation<any>[]

Set annotations for this transaction.

userEvent⁠?: string

Shorthand for annotations:Transaction.userEvent[.of(...).

scrollIntoView⁠?: boolean

When set to true, the transaction is marked as needing to scroll the current selection into view.

filter⁠?: boolean

By default, transactions can be modified by change filters and transaction filters. You can set this to false to disable that.

sequential⁠?: boolean

Normally, when multiple specs are combined (for example by EditorState.update), the positions in changes are taken to refer to the document positions in the initial document. When a spec has sequental set to true, its positions will be taken to refer to the document created by the specs before it instead.

class Transaction

Changes to the editor state are grouped into transactions. Typically, a user action creates a single transaction, which may contain any number of document changes, may change the selection, or have other effects. Create a transaction by calling EditorState.update.

startState: EditorState

The state from which the transaction starts.

changes: ChangeSet

The document changes made by this transaction.

selection: EditorSelection | undefined

The selection set by this transaction, or undefined if it doesn't explicitly set a selection.

effects: readonly StateEffect<any>[]

The effects added to the transaction.

scrollIntoView: boolean

Whether the selection should be scrolled into view after this transaction is dispatched.

newDoc: Text

The new document produced by the transaction. Contrary to .state.doc, accessing this won't force the entire new state to be computed right away, so it is recommended that transaction filters use this getter when they need to look at the new document.

newSelection: EditorSelection

The new selection produced by the transaction. If this.selection is undefined, this will map the start state's current selection through the changes made by the transaction.

state: EditorState

The new state created by the transaction. Computed on demand (but retained for subsequent access), so itis recommended not to access it in transaction filters when possible.

annotation<T>(typeAnnotationType<T>) → T | undefined

Get the value of the given annotation type, if any.

docChanged: boolean

Indicates whether the transaction changed the document.

reconfigured: boolean

Indicates whether this transaction reconfigures the state (through a configuration compartment or with a top-level configuration effect.

isUserEvent(eventstring) → boolean | "" | undefined

Returns true if the transaction has a user event annotation that is equal to or more specific than event. For example, if the transaction has "select.pointer" as user event, "select" and "select.pointer" will match it.

static time: AnnotationType<number>

Annotation used to store transaction timestamps.

static userEvent: AnnotationType<string>

Annotation used to associate a transaction with a user interface event. Holds a string identifying the event, using a dot-separated format to support attaching more specific information. The events used by the core libraries are:

  • "input" when content is entered
    • "input.type" for typed input
      • "input.type.compose" for composition
    • "input.paste" for pasted input
    • "input.drop" when adding content with drag-and-drop
    • "input.complete" when autocompleting
  • "delete" when the user deletes content
    • "delete.selection" when deleting the selection
    • "delete.forward" when deleting forward from the selection
    • "delete.backward" when deleting backward from the selection
    • "delete.cut" when cutting to the clipboard
  • "move" when content is moved
    • "move.drop" when content is moved within the editor through drag-and-drop
  • "select" when explicitly changing the selection
    • "select.pointer" when selecting with a mouse or other pointing device
  • "undo" and "redo" for history actions

Use isUserEvent to check whether the annotation matches a given event.

static addToHistory: AnnotationType<boolean>

Annotation indicating whether a transaction should be added to the undo history or not.

static remote: AnnotationType<boolean>

Annotation indicating (when present and true) that a transaction represents a change made by some other actor, not the user. This is used, for example, to tag other people's changes in collaborative editing.

type ChangeSpec = {fromnumber, to⁠?: number, insert⁠?: string | Text} | ChangeSet | readonly ChangeSpec[]

This type is used as argument to EditorState.changes and in the changes field of transaction specs to succinctly describe document changes. It may either be a plain object describing a change (a deletion, insertion, or replacement, depending on which fields are present), a change set, or an array of change specs.

class ChangeDesc

A change description is a variant of change set that doesn't store the inserted text. As such, it can't be applied, but is cheaper to store and manipulate.

length: number

The length of the document before the change.

newLength: number

The length of the document after the change.

empty: boolean

False when there are actual changes in this set.

iterGaps()

Iterate over the unchanged parts left by these changes.

iterChangedRanges(
individual⁠?: boolean = false
)

Iterate over the ranges changed by these changes. (See ChangeSet.iterChanges for a variant that also provides you with the inserted text.)

When individual is true, adjacent changes (which are kept separate for position mapping) are reported separately.

invertedDesc: ChangeDesc

Get a description of the inverted form of these changes.

composeDesc(otherChangeDesc) → ChangeDesc

Compute the combined effect of applying another set of changes after this one. The length of the document after this set should match the length before other.

mapDesc(otherChangeDesc, before⁠?: boolean = false) → ChangeDesc

Map this description, which should start with the same document as other, over another set of changes, so that it can be applied after it. When before is true, map as if the changes in other happened before the ones in this.

mapPos(posnumber, assoc⁠?: number) → number

Map a given position through these changes, to produce a position pointing into the new document.

assoc indicates which side the position should be associated with. When it is negative or zero, the mapping will try to keep the position close to the character before it (if any), and will move it before insertions at that point or replacements across that point. When it is positive, the position is associated with the character after it, and will be moved forward for insertions at or replacements across the position. Defaults to -1.

mode determines whether deletions should be reported. It defaults to MapMode.Simple (don't report deletions).

touchesRange(fromnumber, to⁠?: number = from) → boolean | "cover"

Check whether these changes touch a given range. When one of the changes entirely covers the range, the string "cover" is returned.

toJSON() → readonly number[]

Serialize this change desc to a JSON-representable value.

static fromJSON(jsonany) → ChangeDesc

Create a change desc from its JSON representation (as produced by toJSON.

enum MapMode

Distinguishes different ways in which positions can be mapped.

Simple

Map a position to a valid new position, even when its context was deleted.

TrackDel

Return null if deletion happens across the position.

TrackBefore

Return null if the character before the position is deleted.

TrackAfter

Return null if the character after the position is deleted.

class ChangeSet extends ChangeDesc

A change set represents a group of modifications to a document. It stores the document length, and can only be applied to documents with exactly that length.

apply(docText) → Text

Apply the changes to a document, returning the modified document.

invert(docText) → ChangeSet

Given the document as it existed before the changes, return a change set that represents the inverse of this set, which could be used to go from the document created by the changes back to the document as it existed before the changes.

compose(otherChangeSet) → ChangeSet

Combine two subsequent change sets into a single set. other must start in the document produced by this. If this goes docAdocB and other represents docBdocC, the returned value will represent the change docAdocC.

map(otherChangeDesc, before⁠?: boolean = false) → ChangeSet

Given another change set starting in the same document, maps this change set over the other, producing a new change set that can be applied to the document produced by applying other. When before is true, order changes as if this comes before other, otherwise (the default) treat other as coming first.

Given two changes A and B, A.compose(B.map(A)) and B.compose(A.map(B, true)) will produce the same document. This provides a basic form of operational transformation, and can be used for collaborative editing.

iterChanges(
individual⁠?: boolean = false
)

Iterate over the changed ranges in the document, calling f for each.

When individual is true, adjacent changes are reported separately.

desc: ChangeDesc

Get a change description for this change set.

toJSON() → any

Serialize this change set to a JSON-representable value.

static of() → ChangeSet

Create a change set for the given changes, for a document of the given length, using lineSep as line separator.

static empty(lengthnumber) → ChangeSet

Create an empty changeset of the given length.

static fromJSON(jsonany) → ChangeSet

Create a changeset from its JSON representation (as produced by toJSON.

class Annotation<T>

Annotations are tagged values that are used to add metadata to transactions in an extensible way. They should be used to model things that effect the entire transaction (such as its time stamp or information about its origin). For effects that happen alongside the other changes made by the transaction, state effects are more appropriate.

type: AnnotationType<T>

The annotation type.

value: T

The value of this annotation.

static define<T>() → AnnotationType<T>

Define a new type of annotation.

class AnnotationType<T>

Marker that identifies a type of annotation.

of(valueT) → Annotation<T>

Create an instance of this annotation.

class StateEffect<Value>

State effects can be used to represent additional effects associated with a transaction. They are often useful to model changes to custom state fields, when those changes aren't implicit in document or selection changes.

value: Value

The value of this effect.

map(mappingChangeDesc) → StateEffect<Value> | undefined

Map this effect through a position mapping. Will return undefined when that ends up deleting the effect.

is<T>(typeStateEffectType<T>) → boolean

Tells you whether this effect object is of a given type.

static define<Value = null>(spec⁠?: Object = {}) → StateEffectType<Value>

Define a new effect type. The type parameter indicates the type of values that his effect holds.

spec
map⁠?: fn(valueValue, mappingChangeDesc) → Value | undefined

Provides a way to map an effect like this through a position mapping. When not given, the effects will simply not be mapped. When the function returns undefined, that means the mapping deletes the effect.

static mapEffects(
effectsreadonly StateEffect<any>[],
) → readonly StateEffect<any>[]

Map an array of effects through a change set.

static reconfigure: StateEffectType<Extension>

This effect can be used to reconfigure the root extensions of the editor. Doing this will discard any extensions appended, but does not reset the content of reconfigured compartments.

static appendConfig: StateEffectType<Extension>

Append extensions to the top-level configuration of the editor.

class StateEffectType<Value>

Representation of a type of state effect. Defined with StateEffect.define.

of(valueValue) → StateEffect<Value>

Create a state effect instance of this type.

Extending Editor State

The following are some types and mechanisms used when writing extensions for the editor state.

type StateCommand = fn(
target: {stateEditorState, dispatchfn(transactionTransaction)}
) → boolean

Subtype of Command that doesn't require access to the actual editor view. Mostly useful to define commands that can be run and tested outside of a browser environment.

type Extension = {extensionExtension} | readonly Extension[]

Extension values can be provided when creating a state to attach various kinds of configuration and behavior information. They can either be built-in extension-providing objects, such as state fields or facet providers, or objects with an extension in its extension property. Extensions can be nested in arrays arbitrarily deep—they will be flattened when processed.

class StateField<Value>

Fields can store additional information in an editor state, and keep it in sync with the rest of the state.

init(createfn(stateEditorState) → Value) → Extension

Returns an extension that enables this field and overrides the way it is initialized. Can be useful when you need to provide a non-default starting value for the field.

extension: Extension

State field instances can be used as Extension values to enable the field in a given state.

static define<Value>(configObject) → StateField<Value>

Define a state field.

config
create(stateEditorState) → Value

Creates the initial value for the field when a state is created.

update(valueValue, transactionTransaction) → Value

Compute a new value from the field's previous value and a transaction.

compare⁠?: fn(aValue, bValue) → boolean

Compare two values of the field, returning true when they are the same. This is used to avoid recomputing facets that depend on the field when its value did not change. Defaults to using ===.

provide⁠?: fn(fieldStateField<Value>) → Extension

Provide values for facets based on the value of this field. The given function will be called once with the initialized field. It will usually want to call some facet's from method to create facet inputs from this field, but can also return other extensions that should be enabled by this field.

toJSON⁠?: fn(valueValue, stateEditorState) → any

A function used to serialize this field's content to JSON. Only necessary when this field is included in the argument to EditorState.toJSON.

fromJSON⁠?: fn(jsonany, stateEditorState) → Value

A function that deserializes the JSON representation of this field's content.

class Facet<Input, Output = readonly Input[]>

A facet is a labeled value that is associated with an editor state. It takes inputs from any number of extensions, and combines those into a single output value.

Examples of facets are the theme styles associated with an editor or the tab size (which is reduced to a single value, using the input with the hightest precedence).

of(valueInput) → Extension

Returns an extension that adds the given value for this facet.

compute(
depsreadonly ("doc" | "selection" | Facet<any, any> | StateField<any>)[],
getfn(stateEditorState) → Input
) → Extension

Create an extension that computes a value for the facet from a state. You must take care to declare the parts of the state that this value depends on, since your function is only called again for a new state when one of those parts changed.

In most cases, you'll want to use the provide option when defining a field instead.

computeN(
depsreadonly ("doc" | "selection" | Facet<any, any> | StateField<any>)[],
getfn(stateEditorState) → readonly Input[]
) → Extension

Create an extension that computes zero or more values for this facet from a state.

from(fieldStateField<Input>) → Extension
from<T>(
getfn(valueT) → Input
) → Extension

Shorthand method for registering a facet source with a state field as input. If the field's type corresponds to this facet's input type, the getter function can be omitted. If given, it will be used to retrieve the input from the field value.

static define<Input, Output = readonly Input[]>(config⁠?: Object = {}) → Facet<Input, Output>

Define a new facet.

config
combine⁠?: fn(valuereadonly Input[]) → Output

How to combine the input values into a single output value. When not given, the array of input values becomes the output. This will immediately be called on creating the facet, with an empty array, to compute the facet's default value when no inputs are present.

compare⁠?: fn(aOutput, bOutput) → boolean

How to compare output values to determine whether the value of the facet changed. Defaults to comparing by === or, if no combine function was given, comparing each element of the array with ===.

compareInput⁠?: fn(aInput, bInput) → boolean

How to compare input values to avoid recomputing the output value when no inputs changed. Defaults to comparing with ===.

static⁠?: boolean

Static facets can not contain dynamic inputs.

enables⁠?: Extension

If given, these extension(s) will be added to any state where this facet is provided. (Note that, while a facet's default value can be read from a state even if the facet wasn't present in the state at all, these extensions won't be added in that situation.)

Prec: Object

By default extensions are registered in the order they are found in the flattened form of nested array that was provided. Individual extension values can be assigned a precedence to override this. Extensions that do not have a precedence set get the precedence of the nearest parent with a precedence, or default if there is no such parent. The final ordering of extensions is determined by first sorting by precedence and then by order within each precedence.

fallback(extExtension) → Extension

A precedence below the default precedence, which will cause default-precedence extensions to override it even if they are specified later in the extension ordering.

default(extExtension) → Extension

The regular default precedence.

extend(extExtension) → Extension

A higher-than-default precedence.

override(extExtension) → Extension

Precedence above the default and extend precedences.

class Compartment

Extension compartments can be used to make a configuration dynamic. By wrapping part of your configuration in a compartment, you can later replace that part through a transaction.

of(extExtension) → Extension

Create an instance of this compartment to add to your state configuration.

reconfigure(contentExtension) → StateEffect<unknown>

Create an effect that reconfigures this compartment.

get(stateEditorState) → Extension | undefined

Get the current content of the compartment in the state, or undefined if it isn't present.

Utilities

combineConfig<Config>(
configsreadonly Partial<Config>[],
combine⁠?: {
[P in keyof Config]: fn(firstConfig[P], secondConfig[P]) → Config[P]
} = {}
) → Config

Utility function for combining behaviors to fill in a config object from an array of provided configs. Will, by default, error when a field gets two values that aren't ===-equal, but you can provide combine functions per field to do something else.

@codemirror/rangeset

Range sets provide a data structure that can hold a collection of tagged, possibly overlapping ranges in such a way that they can efficiently be mapped though document changes. They are used for storing things like decorations or gutter markers.

abstract class RangeValue

Each range is associated with a value, which must inherit from this class.

eq(otherRangeValue) → boolean

Compare this value with another value. The default implementation compares by identity.

startSide: number

The bias value at the start of the range. Determines how the range is positioned relative to other ranges starting at this position. Defaults to 0.

endSide: number

The bias value at the end of the range. Defaults to 0.

mapMode: MapMode

The mode with which the location of the range should be mapped when its from and to are the same, to decide whether a change deletes the range. Defaults to MapMode.TrackDel.

point: boolean

Whether this value marks a point range, which is treated as atomic and shadows the ranges contained in it.

range(fromnumber, to⁠?: number = from) → Range<RangeValue>

Create a range with this value.

class Range<T extends RangeValue>

A range associates a value with a range of positions.

from: number

The range's start position.

to: number

Its end position.

value: T

The value associated with this range.

class RangeSet<T extends RangeValue>

A range set stores a collection of ranges in a way that makes them efficient to map and update. This is an immutable data structure.

size: number

The number of ranges in the set.

update<U extends T>(updateSpecObject) → RangeSet<T>

Update the range set, optionally adding new ranges or filtering out existing ones.

(The extra type parameter is just there as a kludge to work around TypeScript variance issues that prevented RangeSet<X> from being a subtype of RangeSet<Y> when X is a subtype of Y.)

updateSpec
add⁠?: readonly Range<U>[]

An array of ranges to add. If given, this should be sorted by from position and startSide unless sort is given as true.

sort⁠?: boolean

Indicates whether the library should sort the ranges in add. Defaults to false.

filter⁠?: fn(fromnumber, tonumber, valueU) → boolean

Filter the ranges already in the set. Only those for which this function returns true are kept.

filterFrom⁠?: number

Can be used to limit the range on which the filter is applied. Filtering only a small range, as opposed to the entire set, can make updates cheaper.

filterTo⁠?: number

The end position to apply the filter to.

map(changesChangeDesc) → RangeSet<T>

Map this range set through a set of changes, return the new set.

between(
ffn(fromnumber, tonumber, valueT) → false | undefined
)

Iterate over the ranges that touch the region from to to, calling f for each. There is no guarantee that the ranges will be reported in any specific order. When the callback returns false, iteration stops.

iter(from⁠?: number = 0) → RangeCursor<T>

Iterate over the ranges in this set, in order, including all ranges that end at or after from.

static iter<T extends RangeValue>(
setsreadonly RangeSet<T>[],
from⁠?: number = 0
) → RangeCursor<T>

Iterate over the ranges in a collection of sets, in order, starting from from.

static compare<T extends RangeValue>(
oldSetsreadonly RangeSet<T>[],
newSetsreadonly RangeSet<T>[],
textDiffChangeDesc,
minPointSize⁠?: number = -1
)

Iterate over two groups of sets, calling methods on comparator to notify it of possible differences.

textDiff

This indicates how the underlying data changed between these ranges, and is needed to synchronize the iteration. from and to are coordinates in the new space, after these changes.

minPointSize

Can be used to ignore all non-point ranges, and points below the given size. When -1, all ranges are compared.

static eq<T extends RangeValue>(
oldSetsreadonly RangeSet<T>[],
newSetsreadonly RangeSet<T>[],
from⁠?: number = 0,
to⁠?: number
) → boolean

Compare the contents of two groups of range sets, returning true if they are equivalent in the given range.

static spans<T extends RangeValue>(
setsreadonly RangeSet<T>[],
minPointSize⁠?: number = -1
) → number

Iterate over a group of range sets at the same time, notifying the iterator about the ranges covering every given piece of content. Returns the open count (see SpanIterator.span) at the end of the iteration.

minPointSize

When given and greater than -1, only points of at least this size are taken into account.

static of<T extends RangeValue>(
rangesreadonly Range<T>[] | Range<T>,
sort⁠?: boolean = false
) → RangeSet<T>

Create a range set for the given range or array of ranges. By default, this expects the ranges to be sorted (by start position and, if two start at the same position, value.startSide). You can pass true as second argument to cause the method to sort them.

static empty: RangeSet<any>

The empty set of ranges.

interface RangeCursor<T>

A range cursor is an object that moves to the next range every time you call next on it. Note that, unlike ES6 iterators, these start out pointing at the first element, so you should call next only after reading the first range (if any).

next()

Move the iterator forward.

value: T | null

The next range's value. Holds null when the cursor has reached its end.

from: number

The next range's start position.

to: number

The next end position.

class RangeSetBuilder<T extends RangeValue>

A range set builder is a data structure that helps build up a range set directly, without first allocating an array of Range objects.

new RangeSetBuilder()

Create an empty builder.

add(fromnumber, tonumber, valueT)

Add a range. Ranges should be added in sorted (by from and value.startSide) order.

finish() → RangeSet<T>

Finish the range set. Returns the new set. The builder can't be used anymore after this has been called.

interface RangeComparator<T extends RangeValue>

Collection of methods used when comparing range sets.

compareRange(
activeAT[],
)

Notifies the comparator that the given range has the given set of values associated with it.

comparePoint(
byAT | null,
byBT | null
)

Notification for a point range.

interface SpanIterator<T extends RangeValue>

Methods used when iterating over the spans created by a set of ranges. The entire iterated range will be covered with either span or point calls.

span(
activereadonly T[],
)

Called for any ranges not covered by point decorations. active holds the values that the range is marked with (and may be empty). openStart indicates how many of those ranges are open (continued) at the start of the span.

point(
valueT,
activereadonly T[],
)

Called when going over a point decoration. The active range decorations that cover the point and have a higher precedence are provided in active. The open count in openStart counts the number of those ranges that started before the point and. If the point started before the iterated range, openStart will be active.length + 1 to signal this.

@codemirror/view

The “view” is the part of the editor that the user sees—a DOM component that displays the editor state and allows text input.

class EditorView

An editor view represents the editor's user interface. It holds the editable DOM surface, and possibly other elements such as the line number gutter. It handles events and dispatches state transactions for editing actions.

new EditorView(config⁠?: Object = {})

Construct a new view. You'll usually want to put view.dom into your document after creating a view, so that the user can see it.

config

Initialization options.

state⁠?: EditorState

The view's initial state. Defaults to an extension-less state with an empty document.

root⁠?: Document | ShadowRoot

If the view is going to be mounted in a shadow root or document other than the one held by the global variable document (the default), you should pass it here.

dispatch⁠?: fn(trTransaction)

Override the transaction dispatch function for this editor view, which is the way updates get routed to the view. Your implementation, if provided, should probably call the view's update method.

parent⁠?: Element | DocumentFragment

When given, the editor is immediately appended to the given element on creation. (Otherwise, you'll have to place the view's dom element in the document yourself.)

state: EditorState

The current editor state.

viewport: {fromnumber, tonumber}

To be able to display large documents without consuming too much memory or overloading the browser, CodeMirror only draws the code that is visible (plus a margin around it) to the DOM. This property tells you the extent of the current drawn viewport, in document positions.

visibleRanges: readonly {fromnumber, tonumber}[]

When there are, for example, large collapsed ranges in the viewport, its size can be a lot bigger than the actual visible content. Thus, if you are doing something like styling the content in the viewport, it is preferable to only do so for these ranges, which are the subset of the viewport that is actually drawn.

inView: boolean

Returns false when the editor is entirely scrolled out of view or otherwise hidden.

composing: boolean

Indicates whether the user is currently composing text via IME.

root: DocumentOrShadowRoot

The document or shadow root that the view lives in.

dom: HTMLElement

The DOM element that wraps the entire editor view.

scrollDOM: HTMLElement

The DOM element that can be styled to scroll. (Note that it may not have been, so you can't assume this is scrollable.)

contentDOM: HTMLElement

The editable DOM element holding the editor content. You should not, usually, interact with this content directly though the DOM, since the editor will immediately undo most of the changes you make. Instead, dispatch transactions to modify content, and decorations to style it.

dispatch(trTransaction)

All regular editor state updates should go through this. It takes a transaction or transaction spec and updates the view to show the new state produced by that transaction. Its implementation can be overridden with an option. This function is bound to the view instance, so it does not have to be called as a method.

update(transactionsreadonly Transaction[])

Update the view for the given array of transactions. This will update the visible document and selection to match the state produced by the transactions, and notify view plugins of the change. You should usually call dispatch instead, which uses this as a primitive.

setState(newStateEditorState)

Reset the view to the given state. (This will cause the entire document to be redrawn and all view plugins to be reinitialized, so you should probably only use it when the new state isn't derived from the old state. Otherwise, use dispatch instead.)

themeClasses: string

Get the CSS classes for the currently active editor themes.

requestMeasure<T>(request⁠?: Object)

Schedule a layout measurement, optionally providing callbacks to do custom DOM measuring followed by a DOM write phase. Using this is preferable reading DOM layout directly from, for example, an event handler, because it'll make sure measuring and drawing done by other components is synchronized, avoiding unnecessary DOM layout computations.

request
read(viewEditorView) → T

Called in a DOM read phase to gather information that requires DOM layout. Should not mutate the document.

write(measureT, viewEditorView)

Called in a DOM write phase to update the document. Should not do anything that triggers DOM layout.

key⁠?: any

When multiple requests with the same key are scheduled, only the last one will actually be ran.

pluginField<T>(fieldPluginField<T>) → readonly T[]

Collect all values provided by the active plugins for a given field.

plugin<T>(pluginViewPlugin<T>) → T | null

Get the value of a specific plugin, if present. Note that plugins that crash can be dropped from a view, so even when you know you registered a given plugin, it is recommended to check the return value of this method.

blockAtHeight(heightnumber, docTop⁠?: number) → BlockInfo

Find the line or block widget at the given vertical position.

By default, this position is interpreted as a screen position, meaning docTop is set to the DOM top position of the editor content (forcing a layout). You can pass a different docTop value—for example 0 to interpret height as a document-relative position, or a precomputed document top (view.contentDOM.getBoundingClientRect().top) to limit layout queries.

visualLineAtHeight(heightnumber, docTop⁠?: number) → BlockInfo

Find information for the visual line (see visualLineAt) at the given vertical position. The resulting block info might hold another array of block info structs in its type field if this line consists of more than one block.

Defaults to treating height as a screen position. See blockAtHeight for the interpretation of the docTop parameter.

viewportLines(ffn(lineBlockInfo), docTop⁠?: number)

Iterate over the height information of the visual lines in the viewport. The heights of lines are reported relative to the given document top, which defaults to the screen position of the document (forcing a layout).

visualLineAt(posnumber, docTop⁠?: number = 0) → BlockInfo

Find the extent and height of the visual line (a range delimited on both sides by either non-hidden line breaks, or the start/end of the document) at the given position.

Vertical positions are computed relative to the docTop argument, which defaults to 0 for this method. You can pass view.contentDOM.getBoundingClientRect().top here to get screen coordinates.

contentHeight: number

The editor's total content height.

moveByChar(
by⁠?: fn(initialstring) → fn(nextstring) → boolean
) → SelectionRange

Move a cursor position by grapheme cluster. forward determines whether the motion is away from the line start, or towards it. Motion in bidirectional text is in visual order, in the editor's text direction. When the start position was the last one on the line, the returned position will be across the line break. If there is no further line, the original position is returned.

By default, this method moves over a single cluster. The optional by argument can be used to move across more. It will be called with the first cluster as argument, and should return a predicate that determines, for each subsequent cluster, whether it should also be moved over.

moveByGroup(startSelectionRange, forwardboolean) → SelectionRange

Move a cursor position across the next group of either letters or non-letter non-whitespace characters.

moveToLineBoundary(
includeWrap⁠?: boolean = true
) → SelectionRange

Move to the next line boundary in the given direction. If includeWrap is true, line wrapping is on, and there is a further wrap point on the current line, the wrap point will be returned. Otherwise this function will return the start or end of the line.

moveVertically() → SelectionRange

Move a cursor position vertically. When distance isn't given, it defaults to moving to the next line (including wrapped lines). Otherwise, distance should provide a positive distance in pixels.

When start has a goalColumn, the vertical motion will use that as a target horizontal position. Otherwise, the cursor's own horizontal position is used. The returned cursor will have its goal column set to whichever column was used.

scrollPosIntoView(posnumber)

Scroll the given document position into view.

domAtPos(posnumber) → {nodeNode, offsetnumber}

Find the DOM parent node and offset (child offset if node is an element, character offset when it is a text node) at the given document position.

posAtDOM(nodeNode, offset⁠?: number = 0) → number

Find the document position at the given DOM node. Can be useful for associating positions with DOM events. Will raise an error when node isn't part of the editor content.

posAtCoords(
coords: {xnumber, ynumber},
) → number
posAtCoords(coords: {xnumber, ynumber}) → number | null

Get the document position at the given screen coordinates. Returns null if no valid position could be found.

coordsAtPos(posnumber, side⁠?: -1 | 1 = 1) → Rect | null

Get the screen coordinates at the given document position. side determines whether the coordinates are based on the element before (-1) or after (1) the position (if no element is available on the given side, the method will transparently use another strategy to get reasonable coordinates).

defaultCharacterWidth: number

The default width of a character in the editor. May not accurately reflect the width of all characters (given variable width fonts or styling of invididual ranges).

defaultLineHeight: number

The default height of a line in the editor. May not be accurate for all lines.

textDirection: Direction

The text direction (direction CSS property) of the editor.

lineWrapping: boolean

Whether this editor wraps lines (as determined by the white-space CSS property of its content element).

bidiSpans(lineLine) → readonly BidiSpan[]

Returns the bidirectional text structure of the given line (which should be in the current document) as an array of span objects. The order of these spans matches the text direction—if that is left-to-right, the leftmost spans come first, otherwise the rightmost spans come first.

hasFocus: boolean

Check whether the editor has focus.

focus()

Put focus on the editor.

destroy()

Clean up this editor view, removing its element from the document, unregistering event handlers, and notifying plugins. The view instance can no longer be used after calling this.

static scrollTo: StateEffectType<SelectionRange>

Effect that can be added to a transaction to make it scroll the given range into view.

static styleModule: Facet<StyleModule>

Facet to add a style module to an editor view. The view will ensure that the module is mounted in its document root.

static domEventHandlers(handlersDOMEventHandlers<any>) → Extension

Facet that can be used to add DOM event handlers. The value should be an object mapping event names to handler functions. The first such function to return true will be assumed to have handled that event, and no other handlers or built-in behavior will be activated for it. These are registered on the content element, except for scroll handlers, which will be called any time the editor's scroll element or one of its parent nodes is scrolled.

static inputHandler: Facet<>

An input handler can override the way changes to the editable DOM content are handled. Handlers are passed the document positions between which the change was found, and the new content. When one returns true, no further input handlers are called and the default behavior is prevented.

static exceptionSink: Facet<fn(exceptionany)>

Allows you to provide a function that should be called when the library catches an exception from an extension (mostly from view plugins, but may be used by other extensions to route exceptions from user-code-provided callbacks). This is mostly useful for debugging and logging. See logException.

static updateListener: Facet<fn(updateViewUpdate)>

A facet that can be used to register a function to be called every time the view updates.

static editable: Facet<boolean, boolean>

Facet that controls whether the editor content DOM is editable. When its highest-precedence value is false, the element will not longer have its contenteditable attribute set. (Note that this doesn't affect API calls that change the editor content, even when those are bound to keys or buttons. See the readOnly facet for that.)

static mouseSelectionStyle: Facet<>

Allows you to influence the way mouse selection happens. The functions in this facet will be called for a mousedown event on the editor, and can return an object that overrides the way a selection is computed from that mouse click or drag.

static dragMovesSelection: Facet<fn(eventMouseEvent) → boolean>

Facet used to configure whether a given selection drag event should move or copy the selection. The given predicate will be called with the mousedown event, and can return true when the drag should move the content.

static clickAddsSelectionRange: Facet<fn(eventMouseEvent) → boolean>

Facet used to configure whether a given selecting click adds a new range to the existing selection or replaces it entirely.

static decorations: Facet<DecorationSet>

A facet that determines which decorations are shown in the view. See also view plugins, which have a separate mechanism for providing decorations.

static theme(
options⁠?: {dark⁠?: boolean}
) → Extension

Create a theme extension. The first argument can be a style-mod style spec providing the styles for the theme. These will be prefixed with a generated class for the style.

Because the selectors will be prefixed with a scope class, rule that directly match the editor's wrapper element—to which the scope class will be added—need to be explicitly differentiated by adding an & to the selector for that element—for example &.cm-focused.

When dark is set to true, the theme will be marked as dark, which will cause the &dark rules from base themes to be used (as opposed to &light when a light theme is active).

static baseTheme(specObject<StyleSpec>) → Extension

Create an extension that adds styles to the base theme. Like with theme, use & to indicate the place of the editor wrapper element when directly targeting that. You can also use &dark or &light instead to only target editors with a dark or light theme.

static contentAttributes: Facet<Object<string>, Object<string>>

Facet that provides additional DOM attributes for the editor's editable DOM element.

static editorAttributes: Facet<Object<string>, Object<string>>

Facet that provides DOM attributes for the editor's outer element.

static lineWrapping: Extension

An extension that enables line wrapping in the editor (by setting CSS white-space to pre-wrap in the content).

static announce: StateEffectType<string>

State effect used to include screen reader announcements in a transaction. These will be added to the DOM in a visually hidden element with aria-live="polite" set, and should be used to describe effects that are visually obvious but may not be noticed by screen reader users (such as moving to the next search match).

enum BlockType

The different types of blocks that can occur in an editor view.

Text

A line of text.

WidgetBefore

A block widget associated with the position after it.

WidgetAfter

A block widget associated with the position before it.

WidgetRange

A block widget replacing a range of content.

class BlockInfo

Record used to represent information about a block-level element in the editor view.

from: number

The start of the element in the document.

length: number

The length of the element.

top: number

The top position of the element.

height: number

Its height.

type: BlockType | readonly BlockInfo[]

The type of element this is. When querying lines, this may be an array of all the blocks that make up the line.

to: number

The end of the element as a document position.

bottom: number

The bottom position of the element.

enum Direction

Used to indicate text direction.

LTR

Left-to-right.

RTL

Right-to-left.

class BidiSpan

Represents a contiguous range of text that has a single direction (as in left-to-right or right-to-left).

from: number

The start of the span (relative to the start of the line).

to: number

The end of the span.

level: number

The "bidi level" of the span (in this context, 0 means left-to-right, 1 means right-to-left, 2 means left-to-right number inside right-to-left text).

dir: Direction

The direction of this span.

interface DOMEventMap extends HTMLElementEventMap

Helper type that maps event names to event object types, or the any type for unknown events.

[string]: any
type DOMEventHandlers<This> = {}

Event handlers are specified with objects like this. For event types known by TypeScript, this will infer the event argument type to hold the appropriate event object type. For unknown events, it is inferred to any, and should be explicitly set if you want type checking.

interface Rect

Basic rectangle type.

left: number
right: number
top: number
bottom: number

Extending the View

type Command = fn(targetEditorView) → boolean

Command functions are used in key bindings and other types of user actions. Given an editor view, they check whether their effect can apply to the editor, and if it can, perform it as a side effect (which usually means dispatching a transaction) and return true.

class ViewPlugin<V extends PluginValue>

View plugins associate stateful values with a view. They can influence the way the content is drawn, and are notified of things that happen in the view.

extension: Extension

Instances of this class act as extensions.

static define<V extends PluginValue>(
createfn(viewEditorView) → V,
) → ViewPlugin<V>

Define a plugin from a constructor function that creates the plugin's value, given an editor view.

static fromClass<V extends PluginValue>(
cls: {new (viewEditorView) → V},
) → ViewPlugin<V>

Create a plugin for a class whose constructor takes a single editor view as argument.

interface PluginSpec<V extends PluginValue>

Provides additional information when defining a view plugin.

eventHandlers⁠?: DOMEventHandlers<V>

Register the given event handlers for the plugin. When called, these will have their this bound to the plugin value.

decorations⁠?: fn(valueV) → DecorationSet

Allow the plugin to provide decorations. When given, this should a function that take the plugin value and return a decoration set. See also the caveat about layout-changing decorations from plugins.

provide⁠?: PluginFieldProvider<V> | readonly PluginFieldProvider<V>[]

Specify that the plugin provides plugin field values. Use a field's from method to create these providers.

interface PluginValue

This is the interface plugin objects conform to.

update⁠?: fn(_updateViewUpdate)

Notifies the plugin of an update that happened in the view. This is called before the view updates its own DOM. It is responsible for updating the plugin's internal state (including any state that may be read by plugin fields) and writing to the DOM for the changes in the update. To avoid unnecessary layout recomputations, it should not read the DOM layout—use requestMeasure to schedule your code in a DOM reading phase if you need to.

destroy⁠?: fn()

Called when the plugin is no longer going to be used. Should revert any changes the plugin made to the DOM.

class PluginField<T>

Plugin fields are a mechanism for allowing plugins to provide values that can be retrieved through the pluginField view method.

from<V extends PluginValue>(getfn(valueV) → T) → PluginFieldProvider<V>

Create a provider for this field, to use with a plugin's provide option.

static define<T>() → PluginField<T>

Define a new plugin field.

static decorations: PluginField<DecorationSet>

This field can be used by plugins to provide decorations.

Note: For reasons of data flow (plugins are only updated after the viewport is computed), decorations produced by plugins are not taken into account when predicting the vertical layout structure of the editor. Thus, things like large widgets or big replacements (i.e. code folding) should be provided through the state-level decorations facet, not this plugin field. Specifically, replacing decorations that cross line boundaries will break if provided through a plugin.

static atomicRanges: PluginField<RangeSet<any>>

Used to provide ranges that should be treated as atoms as far as cursor motion is concerned. This causes methods like moveByChar and moveVertically (and the commands built on top of them) to skip across such regions when a selection endpoint would enter them. This does not prevent direct programmatic selection updates from moving into such regions.

static scrollMargins: PluginField<Partial<Rect> | null>

Plugins can provide additional scroll margins (space around the sides of the scrolling element that should be considered invisible) through this field. This can be useful when the plugin introduces elements that cover part of that element (for example a horizontally fixed gutter).

class PluginFieldProvider<V>

Used to declare which fields a view plugin provides.

class ViewUpdate

View plugins are given instances of this class, which describe what happened, whenever the view is updated.

changes: ChangeSet

The changes made to the document by this update.

startState: EditorState

The previous editor state.

view: EditorView

The editor view that the update is associated with.

state: EditorState

The new editor state.

transactions: readonly Transaction[]

The transactions involved in the update. May be empty.

viewportChanged: boolean

Tells you whether the viewport or visible ranges changed in this update.

heightChanged: boolean

Indicates whether the line height in the editor changed in this update.

geometryChanged: boolean

Returns true when the document changed or the size of the editor or the lines or characters within it has changed.

focusChanged: boolean

True when this update indicates a focus change.

docChanged: boolean

Whether the document changed in this update.

selectionSet: boolean

Whether the selection was explicitly set in this update.

logException(
exceptionany,
)

Log or report an unhandled exception in client code. Should probably only be used by extension code that allows client code to provide functions, and calls those functions in a context where an exception can't be propagated to calling code in a reasonable way (for example when in an event handler).

Either calls a handler registered with EditorView.exceptionSink, window.onerror, if defined, or console.error (in which case it'll pass context, when given, as first argument).

interface MouseSelectionStyle

Interface that objects registered with EditorView.mouseSelectionStyle must conform to.

get() → EditorSelection

Return a new selection for the mouse gesture that starts with the event that was originally given to the constructor, and ends with the event passed here. In case of a plain click, those may both be the mousedown event, in case of a drag gesture, the latest mousemove event will be passed.

When extend is true, that means the new selection should, if possible, extend the start selection. If multiple is true, the new selection should be added to the original selection.

update(updateViewUpdate) → boolean | undefined

Called when the view is updated while the gesture is in progress. When the document changes, it may be necessary to map some data (like the original selection or start position) through the changes.

This may return true to indicate that the get method should get queried again after the update, because something in the update could change its result. Be wary of infinite loops when using this (where get returns a new selection, which will trigger update, which schedules another get in response).

drawSelection(config⁠?: Object = {}) → Extension

Returns an extension that hides the browser's native selection and cursor, replacing the selection with a background behind the text (with the cm-selectionBackground class), and the cursors with elements overlaid over the code (using cm-cursor-primary and cm-cursor-secondary).

This allows the editor to display secondary selection ranges, and tends to produce a type of selection more in line with that users expect in a text editor (the native selection styling will often leave gaps between lines and won't fill the horizontal space after a line when the selection continues past it).

It does have a performance cost, in that it requires an extra DOM layout cycle for many updates (the selection is drawn based on DOM layout information that's only available after laying out the content).

config
cursorBlinkRate⁠?: number

The length of a full cursor blink cycle, in milliseconds. Defaults to 1200. Can be set to 0 to disable blinking.

drawRangeCursor⁠?: boolean

Whether to show a cursor for non-empty ranges. Defaults to true.

highlightActiveLine() → Extension

Mark lines that have a cursor on them with the "cm-activeLine" DOM class.

highlightSpecialChars(config⁠?: Object = {}) → Extension

Returns an extension that installs highlighting of special characters.

config

Configuration options.

render⁠?: fn() → HTMLElement

An optional function that renders the placeholder elements.

The description argument will be text that clarifies what the character is, which should be provided to screen readers (for example with the aria-label attribute) and optionally shown to the user in other ways (such as the title attribute).

The given placeholder string is a suggestion for how to display the character visually.

specialChars⁠?: RegExp

Regular expression that matches the special characters to highlight. Must have its 'g'/global flag set.

addSpecialChars⁠?: RegExp

Regular expression that can be used to add characters to the default set of characters to highlight.

placeholder(contentstring | HTMLElement) → Extension

Extension that enables a placeholder—a piece of example content to show when the editor is empty.

scrollPastEnd() → Extension

Returns a plugin that makes sure the content has a bottom margin equivalent to the height of the editor, minus one line height, so that every line in the document can be scrolled to the top of the editor.

This is only meaningful when the editor is scrollable, and should not be enabled in editors that take the size of their content.

Key bindings

interface KeyBinding

Key bindings associate key names with command-style functions.

Key names may be strings like "Shift-Ctrl-Enter"—a key identifier prefixed with zero or more modifiers. Key identifiers are based on the strings that can appear in KeyEvent.key. Use lowercase letters to refer to letter keys (or uppercase letters if you want shift to be held). You may use "Space" as an alias for the " " name.

Modifiers can be given in any order. Shift- (or s-), Alt- (or a-), Ctrl- (or c- or Control-) and Cmd- (or m- or Meta-) are recognized.

When a key binding contains multiple key names separated by spaces, it represents a multi-stroke binding, which will fire when the user presses the given keys after each other order.

You can use Mod- as a shorthand for Cmd- on Mac and Ctrl- on other platforms. So Mod-b is Ctrl-b on Linux but Cmd-b on macOS.

key⁠?: string

The key name to use for this binding. If the platform-specific property (mac, win, or linux) for the current platform is used as well in the binding, that one takes precedence. If key isn't defined and the platform-specific binding isn't either, a binding is ignored.

mac⁠?: string

Key to use specifically on macOS.

win⁠?: string

Key to use specifically on Windows.

linux⁠?: string

Key to use specifically on Linux.

run: Command

The command to execute when this binding is triggered. When the command function returns false, further bindings will be tried for the key.

shift⁠?: Command

When given, this defines a second binding, using the (possibly platform-specific) key name prefixed with Shift- to activate this command.

scope⁠?: string

By default, key bindings apply when focus is on the editor content (the "editor" scope). Some extensions, mostly those that define their own panels, might want to allow you to register bindings local to that panel. Such bindings should use a custom scope name. You may also set multiple scope names, separated by spaces.

preventDefault⁠?: boolean

When set to true (the default is false), this will always prevent the further handling for the bound key, even if the command(s) return false. This can be useful for cases where the native behavior of the key is annoying or irrelevant but the command doesn't always apply (such as, Mod-u for undo selection, which would cause the browser to view source instead when no selection can be undone).

keymap: Facet<readonly KeyBinding[]>

Facet used for registering keymaps.

You can add multiple keymaps to an editor. Their priorities determine their precedence (the ones specified early or with high priority get checked first). When a handler has returned true for a given key, no further handlers are called.

runScopeHandlers() → boolean

Run the key handlers registered for a given scope. The event object should be "keydown" event. Returns true if any of the handlers handled it.

Decorations

Your code should never, never directly change the DOM structure CodeMirror creates for its content. Instead, the way to influence how things are drawn is by providing decorations, which can add styling or replace content with an alternative representation.

abstract class Decoration extends RangeValue

A decoration provides information on how to draw or style a piece of content. You'll usually use it wrapped in a Range, which adds a start and end position.

spec: any

The config object used to create this decoration. You can include additional properties in there to store metadata about your decoration.

static mark(specObject) → Decoration

Create a mark decoration, which influences the styling of the content in its range. Nested mark decorations will cause nested DOM elements to be created. Nesting order is determined by precedence of the facet or (below the facet-provided decorations) view plugin. Such elements are split on line boundaries and on the boundaries of higher-precedence decorations.

spec
inclusive⁠?: boolean

Whether the mark covers its start and end position or not. This influences whether content inserted at those positions becomes part of the mark. Defaults to false.

inclusiveStart⁠?: boolean

Specify whether the start position of the marked range should be inclusive. Overrides inclusive, when both are present.

inclusiveEnd⁠?: boolean

Whether the end should be inclusive.

attributes⁠?: Object<string>

Add attributes to the DOM elements that hold the text in the marked range.

class⁠?: string

Shorthand for {attributes: {class: value}}.

tagName⁠?: string

Add a wrapping element around the text in the marked range. Note that there will not be a single element covering the entire range—content is split on mark starts and ends, and each piece gets its own element.

[string]: any

Decoration specs allow extra properties, which can be retrieved through the decoration's spec property.

static widget(specObject) → Decoration

Create a widget decoration, which adds an element at the given position.

spec
widget: WidgetType

The type of widget to draw here.

side⁠?: number

Which side of the given position the widget is on. When this is positive, the widget will be drawn after the cursor if the cursor is on the same position. Otherwise, it'll be drawn before it. When multiple widgets sit at the same position, their side values will determine their ordering—those with a lower value come first. Defaults to 0.

block⁠?: boolean

Determines whether this is a block widgets, which will be drawn between lines, or an inline widget (the default) which is drawn between the surrounding text.

Note that block-level decorations should not have vertical margins, and if you dynamically change their height, you should make sure to call requestMeasure, so that the editor can update its information about its vertical layout.

[string]: any

Other properties are allowed.

static replace(specObject) → Decoration

Create a replace decoration which replaces the given range with a widget, or simply hides it.

spec
widget⁠?: WidgetType

An optional widget to drawn in the place of the replaced content.

inclusive⁠?: boolean

Whether this range covers the positions on its sides. This influences whether new content becomes part of the range and whether the cursor can be drawn on its sides. Defaults to false.

inclusiveStart⁠?: boolean

Set inclusivity at the start.

inclusiveEnd⁠?: boolean

Set inclusivity at the end.

block⁠?: boolean

Whether this is a block-level decoration. Defaults to false.

[string]: any

Other properties are allowed.

static line(specObject) → Decoration

Create a line decoration, which can add DOM attributes to the line starting at the given position.

spec
attributes⁠?: Object<string>

DOM attributes to add to the element wrapping the line.

[string]: any

Other properties are allowed.

static set(
ofRange<Decoration> | readonly Range<Decoration>[],
sort⁠?: boolean = false
) → DecorationSet

Build a DecorationSet from the given decorated range or ranges. If the ranges aren't already sorted, pass true for sort to make the library sort them for you.

static none: DecorationSet

The empty set of decorations.

type DecorationSet = RangeSet<Decoration>

A decoration set represents a collection of decorated ranges, organized for efficient access and mapping. See RangeSet for its methods.

abstract class WidgetType

Widgets added to the content are described by subclasses of this class. Using a description object like that makes it possible to delay creating of the DOM structure for a widget until it is needed, and to avoid redrawing widgets even when the decorations that define them are recreated.

abstract toDOM(viewEditorView) → HTMLElement

Build the DOM structure for this widget instance.

eq(_widgetWidgetType) → boolean

Compare this instance to another instance of the same type. (TypeScript can't express this, but only instances of the same specific class will be passed to this method.) This is used to avoid redrawing widgets when they are replaced by a new decoration of the same type. The default implementation just returns false, which will cause new instances of the widget to always be redrawn.

updateDOM(_domHTMLElement) → boolean

Update a DOM element created by a widget of the same type (but different, non-eq content) to reflect this widget. May return true to indicate that it could update, false to indicate it couldn't (in which case the widget will be redrawn). The default implementation just returns false.

estimatedHeight: number

The estimated height this widget will have, to be used when estimating the height of content that hasn't been drawn. May return -1 to indicate you don't know. The default implementation returns -1.

ignoreEvent(_eventEvent) → boolean

Can be used to configure which kinds of events inside the widget should be ignored by the editor. The default is to ignore all events.

re-export Range

class MatchDecorator

Helper class used to make it easier to maintain decorations on visible code that matches a given regular expression. To be used in a view plugin. Instances of this object represent a matching configuration.

new MatchDecorator(configObject)

Create a decorator.

config
regexp: RegExp

The regular expression to match against the content. Will only be matched inside lines (not across them). Should have its 'g' flag set.

decoration: Decoration | fn() → Decoration

The decoration to apply to matches, either directly or as a function of the match.

boundary⁠?: RegExp

By default, changed lines are re-matched entirely. You can provide a boundary expression, which should match single character strings that can never occur in regexp, to reduce the amount of re-matching.

createDeco(viewEditorView) → RangeSet<Decoration>

Compute the full set of decorations for matches in the given view's viewport. You'll want to call this when initializing your plugin.

updateDeco(updateViewUpdate, decoDecorationSet) → DecorationSet

Update a set of decorations for a view update. deco must be the set of decorations produced by this MatchDecorator for the view state before the update.

@codemirror/language

class Language

A language object manages parsing and per-language metadata. Parse data is managed as a Lezer tree. You'll want to subclass this class for custom parsers, or use the LRLanguage or StreamLanguage abstractions for Lezer or stream parsers.

new Language(
dataFacet<Object<any>>,
)

Construct a language object. You usually don't need to invoke this directly. But when you do, make sure you use defineLanguageFacet to create the first argument.

extension: Extension

The extension value to install this provider.

parser: Parser

The parser object. Can be useful when using this as a nested parser.

data: Facet<Object<any>>

The language data data facet used for this language.

topNode: NodeType

The node type of the top node of trees produced by this parser.

isActiveAt(
side⁠?: -1 | 0 | 1 = -1
) → boolean

Query whether this language is active at the given position.

findRegions(stateEditorState) → {fromnumber, tonumber}[]

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.

allowsNesting: boolean

Indicates whether this language allows nested languages. The default implementation returns true.

defineLanguageFacet(baseData⁠?: Object<any>) → Facet<Object<any>>

Helper function to define a facet (to be added to the top syntax node(s) for a language via languageDataProp), that will be used to associate language data with the language. You probably only need this when subclassing Language.

languageDataProp: NodeProp<Facet<Object<any>>>

Node prop stored in a grammar's top syntax node to provide the facet that stores language data for that language.

language: Facet<Language, Language | null>

The facet used to associate a language with an editor state.

class LRLanguage extends Language

A subclass of Language for use with Lezer LR parsers parsers.

configure(optionsParserConfig) → LRLanguage

Create a new instance of this language with a reconfigured version of its parser.

static define(specObject) → LRLanguage

Define a language from a parser.

spec
parser: LRParser

The parser to use. Should already have added editor-relevant node props (and optionally things like dialect and top rule) configured.

languageData⁠?: Object<any>

Language data to register for this language.

class ParseContext

A parse context provided to parsers working on the editor content.

state: EditorState

The current editor state.

fragments: readonly TreeFragment[]

Tree fragments that can be reused by incremental re-parses.

treeLen: number
viewport: {fromnumber, tonumber}

The current editor viewport (or some overapproximation thereof). Intended to be used for opportunistically avoiding work (in which case skipUntilInView should be called to make sure the parser is restarted when the skipped region becomes visible).

skipUntilInView(fromnumber, tonumber)

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.

static getSkippingParser(until⁠?: Promise<unknown>) → {
createParsefn(
fragmentsreadonly TreeFragment[],
rangesreadonly {fromnumber, tonumber}[]
) → PartialParse, startParsefn(
fragments⁠?: readonly TreeFragment[],
ranges⁠?: readonly {fromnumber, tonumber}[]
) → PartialParse, parsefn(
fragments⁠?: readonly TreeFragment[],
ranges⁠?: readonly {fromnumber, tonumber}[]
) → Tree}

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 get() → ParseContext | null

Get the context for the current parse, or null if no editor parse is in progress.

syntaxTree(stateEditorState) → Tree

Get the syntax tree for a state, which is the current (possibly incomplete) parse tree of active language, or the empty tree if there is no language available.

ensureSyntaxTree(
timeout⁠?: number = 50
) → Tree | null

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.

class LanguageSupport

This class bundles a language object 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.

new LanguageSupport(languageLanguage, support⁠?: Extension = [])

Create a support object.

extension: Extension

An extension including both the language and its support extensions. (Allowing the object to be used as an extension value itself.)

language: Language

The language object.

support: Extension

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.

class LanguageDescription

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.

support: LanguageSupport | undefined

If the language has been loaded, this will hold its value.

name: string

The name of this language.

alias: readonly string[]

Alternative names for the mode (lowercased, includes this.name).

extensions: readonly string[]

File extensions associated with this language.

filename: RegExp | undefined

Optional filename pattern that should be associated with this language.

load() → Promise<LanguageSupport>

Start loading the the language. Will return a promise that resolves to a LanguageSupport object when the language successfully loads.

static of(specObject) → LanguageDescription

Create a language description.

spec
name: string

The language's name.

alias⁠?: readonly string[]

An optional array of alternative names.

extensions⁠?: readonly string[]

An optional array of extensions associated with this language.

filename⁠?: RegExp

An optional filename pattern associated with this language.

load() → Promise<LanguageSupport>

A function that will asynchronously load the language.

static matchFilename(
descsreadonly LanguageDescription[],
) → LanguageDescription | null

Look for a language in the given array of descriptions that matches the filename. Will first match filename patterns, and then extensions, and return the first language that matches.

static matchLanguageName(
descsreadonly LanguageDescription[],
fuzzy⁠?: boolean = true
) → LanguageDescription | 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).

Folding

foldService: Facet<
fn() → {fromnumber, tonumber} | null
>

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.

foldNodeProp: NodeProp<
fn(nodeSyntaxNode, stateEditorState) → {fromnumber, tonumber} | null
>

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.

foldInside(nodeSyntaxNode) → {fromnumber, tonumber} | null

Fold function that folds everything but the first and the last child of a syntax node. Useful for nodes that start and end with delimiters.

foldable() → {fromnumber, tonumber} | null

Check whether the given line is foldable. First asks any fold services registered through foldService, and if none of them return a result, tries to query the fold node prop of syntax nodes that cover the end of the line.

Indentation

indentService: Facet<>

Facet that defines a way to provide a function that computes the appropriate indentation depth at the start of a given line, or null to indicate no appropriate indentation could be determined.

indentNodeProp: NodeProp<>

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 or null, where null indicates that no definitive indentation can be determined.

getIndentation() → number | null

Get the indentation at the given position. Will first consult any indent services that are registered, and if none of those return an indentation, this will check the syntax tree for the indent node prop and use that if found. Returns a number when an indentation could be determined, and null otherwise.

indentUnit: Facet<string, string>

Facet for overriding the unit by which indentation happens. Should be a string consisting either entirely of spaces or entirely of tabs. When not set, this defaults to 2 spaces.

getIndentUnit(stateEditorState) → number

Return the column width of an indent unit in the state. Determined by the indentUnit facet, and tabSize when that contains tabs.

indentString(stateEditorState, colsnumber) → string

Create an indentation string that covers columns 0 to cols. Will use tabs for as much of the columns as possible when the indentUnit facet contains tabs.

class IndentContext

Indentation contexts are used when calling indentation services. They provide helper utilities useful in indentation logic, and can selectively override the indentation reported for some lines.

new IndentContext(stateEditorState, options⁠?: Object = {})

Create an indent context.

options
overrideIndentation⁠?: fn(posnumber) → number

Override line indentations provided to the indentation helper function, which is useful when implementing region indentation, where indentation for later lines needs to refer to previous lines, which may have been reindented compared to the original start state. If given, this function should return -1 for lines (given by start position) that didn't change, and an updated indentation otherwise.

simulateBreak⁠?: number

Make it look, to the indent logic, like a line break was added at the given position (which is mostly just useful for implementing something like insertNewlineAndIndent).

simulateDoubleBreak⁠?: boolean

When simulateBreak is given, this can be used to make the simulate break behave like a double line break.

unit: number

The indent unit (number of columns per indentation level).

state: EditorState

The editor state.

lineAt(posnumber, bias⁠?: -1 | 1 = 1) → {textstring, fromnumber}

Get a description of the line at the given position, taking simulated line breaks 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.

textAfterPos(posnumber, bias⁠?: -1 | 1 = 1) → string

Get the text directly after pos, either the entire line or the next 100 characters, whichever is shorter.

column(posnumber, bias⁠?: -1 | 1 = 1) → number

Find the column for the given position.

countColumn(linestring, pos⁠?: number = line.length) → number

Find the column position (taking tabs into account) of the given position in the given string.

lineIndent(posnumber, bias⁠?: -1 | 1 = 1) → number

Find the indentation column of the line at the given point.

simulatedBreak: number | null

Returns the simulated line break for this context, if any.

class TreeIndentContext extends IndentContext

Objects of this type provide context information and helper methods to indentation functions.

pos: number

The position at which indentation is being computed.

node: SyntaxNode

The syntax tree node to which the indentation strategy applies.

textAfter: string

Get the text directly after this.pos, either the entire line or the next 100 characters, whichever is shorter.

baseIndent: number

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.

continue() → number | null

Continue looking for indentations in the node's parent nodes, and return the result of that.

delimitedIndent(
{closingstring, align⁠?: boolean, units⁠?: number}
) → fn(contextTreeIndentContext) → number

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)
continuedIndent({except⁠?: RegExp, units⁠?: number} = {}) → fn(contextTreeIndentContext) → number

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.

flatIndent(contextTreeIndentContext) → number

An indentation strategy that aligns a node's content to its base indentation.

indentOnInput() → Extension

Enables reindentation on input. When a language defines an indentOnInput field in its language data, 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.

@codemirror/commands

This package exports a collection of generic editing commands, along with key bindings for a lot of them.

Keymaps

standardKeymap: readonly KeyBinding[]

An array of key bindings closely sticking to platform-standard or widely used bindings. (This includes the bindings from emacsStyleKeymap, with their key property changed to mac.)

defaultKeymap: readonly KeyBinding[]

The default keymap. Includes all bindings from standardKeymap plus the following:

emacsStyleKeymap: readonly KeyBinding[]

Array of key bindings containing the Emacs-style bindings that are available on macOS by default.

indentWithTab: KeyBinding

A binding that binds Tab to indentMore and Shift-Tab to indentLess. Please see the Tab example before using this.

Selection

simplifySelection: StateCommand

Simplify the current selection. When multiple ranges are selected, reduce it to its main range. Otherwise, if the selection is non-empty, convert it to a cursor selection.

By character

cursorCharLeft: Command

Move the selection one character to the left (which is backward in left-to-right text, forward in right-to-left text).

selectCharLeft: Command

Move the selection head one character to the left, while leaving the anchor in place.

cursorCharRight: Command

Move the selection one character to the right.

selectCharRight: Command

Move the selection head one character to the right.

cursorCharForward: Command

Move the selection one character forward.

selectCharForward: Command

Move the selection head one character forward.

cursorCharBackward: Command

Move the selection one character backward.

selectCharBackward: Command

Move the selection head one character backward.

By group

cursorGroupLeft: Command

Move the selection to the left across one group of word or non-word (but also non-space) characters.

selectGroupLeft: Command

Move the selection head one group to the left.

cursorGroupRight: Command

Move the selection one group to the right.

selectGroupRight: Command

Move the selection head one group to the right.

cursorGroupForward: Command

Move the selection one group forward.

selectGroupForward: Command

Move the selection head one group forward.

cursorGroupBackward: Command

Move the selection one group backward.

selectGroupBackward: Command

Move the selection head one group backward.

cursorSubwordForward: Command

Move the selection one group or camel-case subword forward.

selectSubwordForward: Command

Move the selection head one group or camel-case subword forward.

cursorSubwordBackward: Command

Move the selection one group or camel-case subword backward.

selectSubwordBackward: Command

Move the selection head one group or subword backward.

Vertical motion

cursorLineUp: Command

Move the selection one line up.

selectLineUp: Command

Move the selection head one line up.

cursorLineDown: Command

Move the selection one line down.

selectLineDown: Command

Move the selection head one line down.

cursorPageUp: Command

Move the selection one page up.

selectPageUp: Command

Move the selection head one page up.

cursorPageDown: Command

Move the selection one page down.

selectPageDown: Command

Move the selection head one page down.

By line boundary

cursorLineBoundaryForward: Command

Move the selection to the next line wrap point, or to the end of the line if there isn't one left on this line.

selectLineBoundaryForward: Command

Move the selection head to the next line boundary.

cursorLineBoundaryBackward: Command

Move the selection to previous line wrap point, or failing that to the start of the line. If the line is indented, and the cursor isn't already at the end of the indentation, this will move to the end of the indentation instead of the start of the line.

selectLineBoundaryBackward: Command

Move the selection head to the previous line boundary.

cursorLineStart: Command

Move the selection to the start of the line.

selectLineStart: Command

Move the selection head to the start of the line.

cursorLineEnd: Command

Move the selection to the end of the line.

selectLineEnd: Command

Move the selection head to the end of the line.

selectLine: StateCommand

Expand the selection to cover entire lines.

By document boundary

cursorDocStart: StateCommand

Move the selection to the start of the document.

selectDocStart: StateCommand

Move the selection head to the start of the document.

cursorDocEnd: StateCommand

Move the selection to the end of the document.

selectDocEnd: StateCommand

Move the selection head to the end of the document.

selectAll: StateCommand

Select the entire document.

By syntax

cursorSyntaxLeft: Command

Move the cursor over the next syntactic element to the left.

selectSyntaxLeft: Command

Move the selection head over the next syntactic element to the left.

cursorSyntaxRight: Command

Move the cursor over the next syntactic element to the right.

selectSyntaxRight: Command

Move the selection head over the next syntactic element to the right.

selectParentSyntax: StateCommand

Select the next syntactic construct that is larger than the selection. Note that this will only work insofar as the language provider you use builds up a full syntax tree.

cursorMatchingBracket: StateCommand

Move the selection to the bracket matching the one it is currently on, if any.

selectMatchingBracket: StateCommand

Extend the selection to the bracket matching the one the selection head is currently on, if any.

Deletion

deleteCharBackward: Command

Delete the selection, or, for cursor selections, the character before the cursor.

deleteCharForward: Command

Delete the selection or the character after the cursor.

deleteGroupBackward: StateCommand

Delete the selection or backward until the end of the next group, only skipping groups of whitespace when they consist of a single space.

deleteGroupForward: StateCommand

Delete the selection or forward until the end of the next group.

deleteToLineStart: Command

Delete the selection, or, if it is a cursor selection, delete to the start of the line. If the cursor is directly at the start of the line, delete the line break before it.

deleteToLineEnd: Command

Delete the selection, or, if it is a cursor selection, delete to the end of the line. If the cursor is directly at the end of the line, delete the line break after it.

deleteTrailingWhitespace: StateCommand

Delete all whitespace directly before a line end from the document.

Line manipulation

splitLine: StateCommand

Replace each selection range with a line break, leaving the cursor on the line before the break.

moveLineUp: StateCommand

Move the selected lines up one line.

moveLineDown: StateCommand

Move the selected lines down one line.

copyLineUp: StateCommand

Create a copy of the selected lines. Keep the selection in the top copy.

copyLineDown: StateCommand

Create a copy of the selected lines. Keep the selection in the bottom copy.

deleteLine: Command

Delete selected lines.

Indentation

indentSelection: StateCommand

Auto-indent the selected lines. This uses the indentation service facet as source for auto-indent information.

indentMore: StateCommand

Add a unit of indentation to all selected lines.

indentLess: StateCommand

Remove a unit of indentation from all selected lines.

insertTab: StateCommand

Insert a tab character at the cursor or, if something is selected, use indentMore to indent the entire selection.

Character manipulation

transposeChars: StateCommand

Flip the characters before and after the cursor(s).

insertNewline: StateCommand

Replace the selection with a newline.

insertNewlineAndIndent: StateCommand

Replace the selection with a newline and indent the newly created line(s). If the current line consists only of whitespace, this will also delete that whitespace. When the cursor is between matching brackets, an additional newline will be inserted after the cursor.

insertBlankLine: StateCommand

Create a blank, indented line below the current line.

@codemirror/panel

Panels are UI elements positioned above or below the editor (things like a search dialog). They will take space from the editor when it has a fixed height, and will stay in view even when the editor is partially scrolled out of view.

See also the panel example.

showPanel: Facet<PanelConstructor | null>

Opening a panel is done by providing a constructor function for the panel through this facet. (The panel is closed again when its constructor is no longer provided.) Values of null are ignored.

type PanelConstructor = fn(viewEditorView) → Panel

A function that initializes a panel. Used in showPanel.

interface Panel

Object that describes an active panel.

dom: HTMLElement

The element representing this panel. The library will add the "cm-panel" DOM class to this.

mount⁠?: fn()

Optionally called after the panel has been added to the editor.

update⁠?: fn(updateViewUpdate)

Update the DOM for a given view update.

top⁠?: boolean

Whether the panel should be at the top or bottom of the editor. Defaults to false.

pos⁠?: number

An optional number that is used to determine the ordering when there are multiple panels. Those with a lower pos value will come first. Defaults to 0.

getPanel(viewEditorView, panelPanelConstructor) → Panel | null

Get the active panel created by the given constructor, if any. This can be useful when you need access to your panels' DOM structure.

panels(config⁠?: Object) → Extension

Configures the panel-managing extension.

config
topContainer⁠?: HTMLElement

By default, panels will be placed inside the editor's DOM structure. You can use this option to override where panels with top: true are placed.

bottomContainer⁠?: HTMLElement

Override where panels with top: false are placed.

@codemirror/tooltip

Tooltips are DOM elements overlaid on the editor near a given document position. This package helps manage and position such elements.

See also the tooltip example.

showTooltip: Facet<Tooltip | null>

Behavior by which an extension can provide a tooltip to be shown.

interface Tooltip

Describes a tooltip. Values of this type, when provided through the showTooltip facet, control the individual tooltips on the editor.

pos: number

The document position at which to show the tooltip.

end⁠?: number

The end of the range annotated by this tooltip, if different from pos.

create(viewEditorView) → TooltipView

A constructor function that creates the tooltip's DOM representation.

above⁠?: boolean

Whether the tooltip should be shown above or below the target position. Not guaranteed for hover tooltips since all hover tooltips for the same range are always positioned together. Defaults to false.

strictSide⁠?: boolean

Whether the above option should be honored when there isn't enough space on that side to show the tooltip inside the viewport. Not guaranteed for hover tooltips. Defaults to false.

arrow⁠?: boolean

When set to true, show a triangle connecting the tooltip element to position pos.

interface TooltipView

Describes the way a tooltip is displayed.

dom: HTMLElement

The DOM element to position over the editor.

mount⁠?: fn(viewEditorView)

Called after the tooltip is added to the DOM for the first time.

update⁠?: fn(updateViewUpdate)

Update the DOM element for a change in the view's state.

positioned⁠?: fn()

Called when the tooltip has been (re)positioned.

tooltips(config⁠?: Object = {}) → Extension

Return an extension that configures tooltip behavior.

config
position⁠?: "fixed" | "absolute"

By default, tooltips use "fixed" positioning, which has the advantage that tooltips don't get cut off by scrollable parent elements. However, CSS rules like contain: layout can break fixed positioning in child nodes, which can be worked about by using "absolute" here.

On iOS, which at the time of writing still doesn't properly support fixed positioning, the library always uses absolute positioning.

hoverTooltip(
sourcefn(
side-1 | 1
) → Tooltip | Promise<Tooltip | null> | null,
options⁠?: {hideOnChange⁠?: boolean} = {}
) → Extension

Enable a hover tooltip, which shows up when the pointer hovers over ranges of text. The callback is called when the mouse hovers over the document text. It should, if there is a tooltip associated with position pos return the tooltip description (either directly or in a promise). The side argument indicates on which side of the position the pointer is—it will be -1 if the pointer is before the position, 1 if after the position.

Note that all hover tooltips are hosted within a single tooltip container element. This allows multiple tooltips over the same range to be "merged" together without overlapping.

@codemirror/history

history(config⁠?: Object = {}) → Extension

Create a history extension with the given configuration.

config
minDepth⁠?: number

The minimum depth (amount of events) to store. Defaults to 100.

newGroupDelay⁠?: number

The maximum time (in milliseconds) that adjacent events can be apart and still be grouped together. Defaults to 500.

historyKeymap: readonly KeyBinding[]

Default key bindings for the undo history.

historyField: StateField<unknown>

The state field used to store the history data. Should probably only be used when you want to serialize or deserialize state objects in a way that preserves history.

undo: StateCommand

Undo a single group of history events. Returns false if no group was available.

redo: StateCommand

Redo a group of history events. Returns false if no group was available.

undoSelection: StateCommand

Undo a selection change.

redoSelection: StateCommand

Redo a selection change.

undoDepth(stateEditorState) → number

The amount of undoable change events available in a given state.

redoDepth(stateEditorState) → number

The amount of redoable change events available in a given state.

isolateHistory: AnnotationType<"before" | "after" | "full">

Transaction annotation that will prevent that transaction from being combined with other transactions in the undo history. Given "before", it'll prevent merging with previous transactions. With "after", subsequent transactions won't be combined with this one. With "full", the transaction is isolated on both sides.

invertedEffects: Facet<
fn(trTransaction) → readonly StateEffect<any>[]
>

This facet provides a way to register functions that, given a transaction, provide a set of effects that the history should store when inverting the transaction. This can be used to integrate some kinds of effects in the history, so that they can be undone (and redone again).

@codemirror/gutter

This package provides functionality for showing "gutters" (for line numbers or other purposes) on the side of the editor. See also the gutter example.

lineNumbers(config⁠?: Object = {}) → Extension

Create a line number gutter extension.

config
formatNumber⁠?: fn(lineNonumber, stateEditorState) → string

How to display line numbers. Defaults to simply converting them to string.

domEventHandlers⁠?: Object<>

Supply event handlers for DOM events on this gutter.

highlightActiveLineGutter() → Extension

Returns an extension that adds a cm-activeLineGutter class to all gutter elements on the active line.

gutter(configObject) → Extension

Define an editor gutter. The order in which the gutters appear is determined by their extension priority.

config
class⁠?: string

An extra CSS class to be added to the wrapper (cm-gutter) element.

renderEmptyElements⁠?: boolean

Controls whether empty gutter elements should be rendered. Defaults to false.

markers⁠?: fn(viewEditorView) → RangeSet<GutterMarker> | readonly RangeSet<GutterMarker>[]

Retrieve a set of markers to use in this gutter.

lineMarker⁠?: fn() → GutterMarker | null

Can be used to optionally add a single marker to every line.

initialSpacer⁠?: fn(viewEditorView) → GutterMarker

Add a hidden spacer element that gives the gutter its base width.

updateSpacer⁠?: fn(spacerGutterMarker, updateViewUpdate) → GutterMarker

Update the spacer element when the view is updated.

domEventHandlers⁠?: Object<>

Supply event handlers for DOM events on this gutter.

gutters(config⁠?: {fixed⁠?: boolean}) → Extension

The gutter-drawing plugin is automatically enabled when you add a gutter, but you can use this function to explicitly configure it.

Unless fixed is explicitly set to false, the gutters are fixed, meaning they don't scroll along with the content horizontally (except on Internet Explorer, which doesn't support CSS position: sticky).

abstract class GutterMarker extends RangeValue

A gutter marker represents a bit of information attached to a line in a specific gutter. Your own custom markers have to extend this class.

eq(otherGutterMarker) → boolean

Compare this marker to another marker of the same type.

toDOM⁠?: fn(_viewEditorView) → Node

Render the DOM node for this marker, if any.

elementClass: string

This property can be used to add CSS classes to the gutter element that contains this marker.

gutterLineClass: Facet<RangeSet<GutterMarker>>

Facet used to add a class to all gutter elements for a given line. Markers given to this facet should only define an elementclass, not a toDOM (or the marker will appear in all gutters for the line).

lineNumberMarkers: Facet<RangeSet<GutterMarker>>

Facet used to provide markers to the line number gutter.

@codemirror/collab

This package provides the scaffolding for basic operational-transform based collaborative editing. When it is enabled, the editor will accumulate local changes, which can be sent to a central service. When new changes are received from the service, they can be applied to the state with receiveUpdates.

See the collaborative editing example for a more detailed description of the protocol.

collab(config⁠?: Object = {}) → Extension

Create an instance of the collaborative editing plugin.

config
startVersion⁠?: number

The starting document version. Defaults to 0.

clientID⁠?: string

This client's identifying ID. Will be a randomly generated string if not provided.

sharedEffects⁠?: fn(trTransaction) → readonly StateEffect<any>[]

It is possible to share information other than document changes through this extension. If you provide this option, your function will be called on each transaction, and the effects it returns will be sent to the server, much like changes are. Such effects are automatically remapped when conflicting remote changes come in.

interface Update

An update is a set of changes and effects.

changes: ChangeSet

The changes made by this update.

effects⁠?: readonly StateEffect<any>[]

The effects in this update. There'll only ever be effects here when you configure your collab extension with a sharedEffects option.

clientID: string

The ID of the client who created this update.

receiveUpdates(
updatesreadonly Update[]
) → Transaction

Create a transaction that represents a set of new updates received from the authority. Applying this transaction moves the state forward to adjust to the authority's view of the document.

sendableUpdates(stateEditorState) → readonly (Update & {originTransaction})[]

Returns the set of locally made updates that still have to be sent to the authority. The returned objects will also have an origin property that points at the transaction that created them. This may be useful if you want to send along metadata like timestamps. (But note that the updates may have been mapped in the meantime, whereas the transaction is just the original transaction that created them.)

getSyncedVersion(stateEditorState) → number

Get the version up to which the collab plugin has synced with the central authority.

getClientID(stateEditorState) → string

Get this editor's collaborative editing client ID.

@codemirror/language-data

languages: LanguageDescription[]

An array of language descriptions for known language packages.

@codemirror/fold

This package provides commands and other functionality related to code folding (temporarily hiding pieces of code).

foldCode: Command

Fold the lines that are selected, if possible.

unfoldCode: Command

Unfold folded ranges on selected lines.

foldAll: Command

Fold all top-level foldable ranges.

unfoldAll: Command

Unfold all folded code.

foldKeymap: readonly KeyBinding[]

Default fold-related key bindings.

codeFolding(config⁠?: Object) → Extension

Create an extension that configures code folding.

config
placeholderDOM⁠?: fn() → HTMLElement

A function that creates the DOM element used to indicate the position of folded code. When not given, the placeholderText option will be used instead.

placeholderText⁠?: string

Text to use as placeholder for folded text. Defaults to "…". Will be styled with the "cm-foldPlaceholder" class.

foldGutter(config⁠?: Object = {}) → Extension

Create an extension that registers a fold gutter, which shows a fold status indicator before foldable lines (which can be clicked to fold or unfold the line).

config
markerDOM⁠?: fn(openboolean) → HTMLElement

A function that creates the DOM element used to indicate a given line is folded or can be folded. When not given, the openText/closeText option will be used instead.

openText⁠?: string

Text used to indicate that a given line can be folded. Defaults to "⌄".

closedText⁠?: string

Text used to indicate that a given line is folded. Defaults to "›".

The following functions provide more direct, low-level control over the fold state.

foldedRanges(stateEditorState) → DecorationSet

Get a range set containing the folded ranges in the given state.

foldEffect: StateEffectType<{fromnumber, tonumber}>

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 and the fold gutter create the transactions.)

unfoldEffect: StateEffectType<{fromnumber, tonumber}>

State effect that unfolds the given range (if it was folded).

@codemirror/matchbrackets

bracketMatching(config⁠?: Config = {}) → Extension

Create an extension that enables bracket matching. Whenever the cursor is next to a bracket, that bracket and the one it matches are highlighted. Or, when no matching bracket is found, another highlighting style is used to indicate this.

interface Config

afterCursor⁠?: boolean

Whether the bracket matching should look at the character after the cursor when matching (if the one before isn't a bracket). Defaults to true.

brackets⁠?: string

The bracket characters to match, as a string of pairs. Defaults to "()[]{}". Note that these are only used as fallback when there is no matching information in the syntax tree.

maxScanDistance⁠?: number

The maximum distance to scan for matching brackets. This is only relevant for brackets not encoded in the syntax tree. Defaults to 10 000.

matchBrackets(
dir-1 | 1,
config⁠?: Config = {}
) → MatchResult | null

Find the matching bracket for the token at pos, scanning direction dir. Only the brackets and maxScanDistance properties are used from config, if given. Returns null if no bracket was found at pos, or a match result otherwise.

interface MatchResult

The result returned from matchBrackets.

start: {fromnumber, tonumber}

The extent of the bracket token found.

end⁠?: {fromnumber, tonumber}

The extent of the matched token, if any was found.

matched: boolean

Whether the tokens match. This can be false even when end has a value, if that token doesn't match the opening token.

@codemirror/closebrackets

interface CloseBracketConfig

Configures bracket closing behavior for a syntax (via language data) using the "closeBrackets" identifier.

brackets⁠?: string[]

The opening brackets to close. Defaults to ["(", "[", "{", "'", '"']. Brackets may be single characters or a triple of quotes (as in "''''").

before⁠?: string

Characters in front of which newly opened brackets are automatically closed. Closing always happens in front of whitespace. Defaults to ")]}'\":;>".

closeBrackets() → Extension

Extension to enable bracket-closing behavior. When a closeable bracket is typed, its closing bracket is immediately inserted after the cursor. When closing a bracket directly in front of a closing bracket inserted by the extension, the cursor moves over that bracket.

closeBracketsKeymap: readonly KeyBinding[]

Close-brackets related key bindings. Binds Backspace to deleteBracketPair.

deleteBracketPair: StateCommand

Command that implements deleting a pair of matching brackets when the cursor is between them.

insertBracket(stateEditorState, bracketstring) → Transaction | null

Implements the extension's behavior on text insertion. If the given string counts as a bracket in the language around the selection, and replacing the selection with it requires custom behavior (inserting a closing version or skipping past a previously-closed bracket), this function returns a transaction representing that custom behavior. (You only need this if you want to programmatically insert brackets—the closeBrackets extension will take care of running this for user input.)

@codemirror/lint

lintKeymap: readonly KeyBinding[]

A set of default key bindings for the lint functionality.

interface Diagnostic

Describes a problem or hint for a piece of code.

from: number

The start position of the relevant text.

to: number

The end position. May be equal to from, though actually covering text is preferable.

severity: "info" | "warning" | "error"

The severity of the problem. This will influence how it is displayed.

source⁠?: string

An optional source string indicating where the diagnostic is coming from. You can put the name of your linter here, if applicable.

message: string

The message associated with this diagnostic.

actions⁠?: readonly Action[]

An optional array of actions that can be taken on this diagnostic.

interface Action

An action associated with a diagnostic.

name: string

The label to show to the user. Should be relatively short.

apply(viewEditorView, fromnumber, tonumber)

The function to call when the user activates this action. Is given the diagnostic's current position, which may have changed since the creation of the diagnostic due to editing.

linter(
sourcefn(viewEditorView) → readonly Diagnostic[] | Promise<readonly Diagnostic[]>,
config⁠?: Object = {}
) → Extension

Given a diagnostic source, this function returns an extension that enables linting with that source. It will be called whenever the editor is idle (after its content changed).

config
delay⁠?: number

Time to wait (in milliseconds) after a change before running the linter. Defaults to 750ms.

diagnosticCount(stateEditorState) → number

Returns the number of active lint diagnostics in the given state.

forceLinting(viewEditorView)

Forces any linters configured to run when the editor is idle to run right away.

openLintPanel: Command

Command to open and focus the lint panel.

closeLintPanel: Command

Command to close the lint panel, when open.

nextDiagnostic: Command

Move the selection to the next diagnostic.

setDiagnostics(
diagnosticsreadonly Diagnostic[]
) → TransactionSpec

Returns a transaction spec which updates the current set of diagnostics.

@codemirror/highlight

Syntax highlighting is done by a highlight style, which maps style tags associated with a syntax tree to CSS styles, making sure each syntactic element is styled appropriately.

class Tag

Highlighting tags are markers that denote a highlighting category. They are associated with parts of a syntax tree by a language mode, and then mapped to an actual CSS style by a highlight style.

Because syntax tree node types and highlight styles have to be able to talk the same language, CodeMirror uses a mostly closed vocabulary of syntax tags (as opposed to traditional open string-based systems, which make it hard for highlighting themes to cover all the tokens produced by the various languages).

It is possible to define your own highlighting tags for system-internal use (where you control both the language package and the highlighter), but such tags will not be picked up by regular highlighters (though you can derive them from standard tags to allow highlighters to fall back to those).

static define(parent⁠?: Tag) → Tag

Define a new tag. If parent is given, the tag is treated as a sub-tag of that parent, and highlight styles that don't mention this tag will try to fall back to the parent tag (or grandparent tag, etc).

static defineModifier() → fn(tagTag) → Tag

Define a tag modifier, which is a function that, given a tag, will return a tag that is a subtag of the original. Applying the same modifier to a twice tag will return the same value (m1(t1) == m1(t1)) and applying multiple modifiers will, regardless or order, produce the same tag (m1(m2(t1)) == m2(m1(t1))).

When multiple modifiers are applied to a given base tag, each smaller set of modifiers is registered as a parent, so that for example m1(m2(m3(t1))) is a subtype of m1(m2(t1)), m1(m3(t1), and so on.

tags: Object

The default set of highlighting tags used by regular language packages and themes.

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 to propose a new tag, or 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.

comment: Tag

A comment.

lineComment: Tag

A line comment.

blockComment: Tag

A block comment.

docComment: Tag

A documentation comment.

name: Tag

Any kind of identifier.

variableName: Tag

The name of a variable.

typeName: Tag

A type name.

tagName: Tag

A tag name (subtag of typeName).

propertyName: Tag

A property or field name.

attributeName: Tag

An attribute name (subtag of propertyName).

className: Tag

The name of a class.

labelName: Tag

A label name.

namespace: Tag

A namespace name.

macroName: Tag

The name of a macro.

literal: Tag

A literal value.

string: Tag

A string literal.

docString: Tag

A documentation string.

character: Tag

A character literal (subtag of string).

attributeValue: Tag

An attribute value (subtag of string).

number: Tag

A number literal.

integer: Tag

An integer number literal.

float: Tag

A floating-point number literal.

bool: Tag

A boolean literal.

regexp: Tag

Regular expression literal.

escape: Tag

An escape literal, for example a backslash escape in a string.

color: Tag

A color literal.

url: Tag

A URL literal.

keyword: Tag

A language keyword.

self: Tag

The keyword for the self or this object.

null: Tag

The keyword for null.

atom: Tag

A keyword denoting some atomic value.

unit: Tag

A keyword that represents a unit.

modifier: Tag

A modifier keyword.

operatorKeyword: Tag

A keyword that acts as an operator.

controlKeyword: Tag

A control-flow related keyword.

definitionKeyword: Tag

A keyword that defines something.

operator: Tag

An operator.

derefOperator: Tag

An operator that defines something.

arithmeticOperator: Tag

Arithmetic-related operator.

logicOperator: Tag

Logical operator.

bitwiseOperator: Tag

Bit operator.

compareOperator: Tag

Comparison operator.

updateOperator: Tag

Operator that updates its operand.

definitionOperator: Tag

Operator that defines something.

typeOperator: Tag

Type-related operator.

controlOperator: Tag

Control-flow operator.

punctuation: Tag

Program or markup punctuation.

separator: Tag

Punctuation that separates things.

bracket: Tag

Bracket-style punctuation.

angleBracket: Tag

Angle brackets (usually < and > tokens).

squareBracket: Tag

Square brackets (usually [ and ] tokens).

paren: Tag

Parentheses (usually ( and ) tokens). Subtag of bracket.

brace: Tag

Braces (usually { and } tokens). Subtag of bracket.

content: Tag

Content, for example plain text in XML or markup documents.

heading: Tag

Content that represents a heading.

heading1: Tag

A level 1 heading.

heading2: Tag

A level 2 heading.

heading3: Tag

A level 3 heading.

heading4: Tag

A level 4 heading.

heading5: Tag

A level 5 heading.

heading6: Tag

A level 6 heading.

contentSeparator: Tag

A prose separator (such as a horizontal rule).

list: Tag

Content that represents a list.

quote: Tag

Content that represents a quote.

emphasis: Tag

Content that is emphasized.

strong: Tag

Content that is styled strong.

Content that is part of a link.

monospace: Tag

Content that is styled as code or monospace.

strikethrough: Tag

Content that has a strike-through style.

inserted: Tag

Inserted text in a change-tracking format.

deleted: Tag

Deleted text.

changed: Tag

Changed text.

invalid: Tag

An invalid or unsyntactic element.

meta: Tag

Metadata or meta-instruction.

documentMeta: Tag

Metadata that applies to the entire document.

annotation: Tag

Metadata that annotates or adds attributes to a given syntactic element.

processingInstruction: Tag

Processing instruction or preprocessor directive. Subtag of meta.

definition(tagTag) → Tag

Modifier that indicates that a given element is being defined. Expected to be used with the various name tags.

constant(tagTag) → Tag

Modifier that indicates that something is constant. Mostly expected to be used with variable names.

function(tagTag) → Tag

Modifier used to indicate that a variable or property name is being called or defined as a function.

standard(tagTag) → Tag

Modifier that can be applied to names to indicate that they belong to the language's standard environment.

local(tagTag) → Tag

Modifier that indicates a given names is local to some scope.

special(tagTag) → Tag

A generic variant modifier that can be used to tag language-specific alternative variants of some common tag. It is recommended for themes to define special forms of at least the string and variable name tags, since those come up a lot.

class HighlightStyle

A highlight style associates CSS styles with higlighting tags.

extension: Extension

Extension that registers this style with an editor. When multiple highlight styles are given, they all apply, assigning the combination of their matching styles to tokens.

fallback: Extension

An extension that installs this highlighter as a fallback highlight style, which will only be used if no other highlight styles are configured.

module: StyleModule | null

A style module holding the CSS rules for this highlight style. When using highlightTree, you may want to manually mount this module to show the highlighting.

match(tagTag, scopeNodeType) → string | null

Returns the CSS class associated with the given tag, if any. This method is bound to the instance by the constructor.

static combinedMatch(stylesreadonly HighlightStyle[]) → fn(tagTag, scopeNodeType) → any

Combines an array of highlight styles into a single match function that returns all of the classes assigned by the styles for a given tag.

static define(
specsreadonly TagStyle[],
) → HighlightStyle

Create a highlighter style that associates the given styles to the given tags. The spec must be objects that hold a style tag or array of tags in their tag property, and either a single class property providing a static CSS class (for highlighters like classHighlightStyle that rely on external styling), or a style-mod-style set of CSS properties (which define the styling for those tags).

The CSS rules created for a highlighter will be emitted in the order of the spec's properties. That means that for elements that have multiple tags associated with them, styles defined further down in the list will have a higher CSS precedence than styles defined earlier.

options
scope⁠?: NodeType

By default, highlighters apply to the entire document. You can scope them to a single language by providing the language's top node here.

all⁠?: string | StyleSpec

Add a style to all content. Probably only useful in combination with scope.

static get(
tagTag,
) → string | null

Returns the CSS classes (if any) that the highlight styles active in the given state would assign to the given a style tag and (optional) language scope.

interface TagStyle

The type of object used in HighlightStyle.define. Assigns a style to one or more highlighting tags, which can either be a fixed class name (which must be defined elsewhere), or a set of CSS properties, for which the library will define an anonymous class.

tag: Tag | readonly Tag[]

The tag or tags to target.

class⁠?: string

If given, this maps the tags to a fixed class name.

[string]: any

Any further properties (if class isn't given) will be interpreted as in style objects given to style-mod. The type here is any because of TypeScript limitations.

highlightTree(
getStylefn(tagTag, scopeNodeType) → string | null,
putStylefn(fromnumber, tonumber, classesstring),
from⁠?: number = 0,
to⁠?: number = tree.length
)

Run the tree highlighter over the given tree.

getStyle(tagTag, scopeNodeType) → string | null

Get the CSS classes used to style a given tag, or null if it isn't styled. (You'll often want to pass a highlight style's match method here.)

putStyle(fromnumber, tonumber, classesstring)

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.

from

The start of the range to highlight.

to

The end of the range.

defaultHighlightStyle: HighlightStyle

A default highlight style (works well with light themes).

classHighlightStyle: HighlightStyle

This is a highlight style that adds stable, predictable classes to tokens, for styling with external CSS.

These tags are mapped to their name prefixed with "cmt-" (for example "cmt-comment"):

In addition, these mappings are provided:

styleTags(specObject<Tag | readonly Tag[]>) → NodePropSource

This function is used to add a set of tags to a language syntax via LRParser.configure.

The argument object maps node selectors to highlighting tags or arrays of tags.

Node selectors may hold one or more (space-separated) node paths. Such a path can be a node name, or multiple node names (or * wildcards) separated by slash characters, as in "Block/Declaration/VariableName". Such a path matches the final node but only if its direct parent nodes are the other nodes mentioned. A * in such a path matches any parent, but only a single level—wildcards that match multiple parents aren't supported, both for efficiency reasons and because Lezer trees make it rather hard to reason about what they would match.)

A path can be ended with /... to indicate that the tag assigned to the node should also apply to all child nodes, even if they match their own style (by default, only the innermost style is used).

When a path ends in !, as in Attribute!, no further matching happens for the node's child nodes, and the entire node gets the given style.

In this notation, node names that contain /, !, *, or ... must be quoted as JSON strings.

For example:

parser.withProps(
  styleTags({
    // Style Number and BigNumber nodes
    "Number BigNumber": tags.number,
    // Style Escape nodes whose parent is String
    "String/Escape": tags.escape,
    // Style anything inside Attributes nodes
    "Attributes!": tags.meta,
    // Add a style to all content inside Italic nodes
    "Italic/...": tags.emphasis,
    // Style InvalidString nodes as both `string` and `invalid`
    "InvalidString": [tags.string, tags.invalid],
    // Style the node named "/" as punctuation
    '"/"': tags.punctuation
  })
)

@codemirror/stream-parser

class StreamLanguage<State> extends Language

A language class based on a streaming parser.

static define<State>(specStreamParser<State>) → StreamLanguage<State>

interface StreamParser<State>

A stream parser parses or tokenizes content from start to end, emitting tokens as it goes over it. It keeps a mutable (but copyable) object with state, in which it can store information about the current context.

token(streamStringStream, stateState) → string | null

Read one token, advancing the stream past it, and returning a string indicating the token's style tag—either the name of one of the tags in tags, or such a name suffixed by one or more tag modifier names, separated by spaces. For example "keyword" or "variableName.constant".

It is okay to return a zero-length token, but only if that updates the state so that the next call will return a non-empty token again.

blankLine⁠?: fn(stateState, indentUnitnumber)

This notifies the parser of a blank line in the input. It can update its state here if it needs to.

startState⁠?: fn(indentUnitnumber) → State

Produce a start state for the parser.

copyState⁠?: fn(stateState) → State

Copy a given state. By default, a shallow object copy is done which also copies arrays held at the top level of the object.

indent⁠?: fn() → number | null

Compute automatic indentation for the line that starts with the given state and text.

languageData⁠?: Object<any>

Default language data to attach to this language.

class StringStream

Encapsulates a single line of input. Given to stream syntax code, which uses it to tokenize the content.

pos: number

The current position on the line.

start: number

The start position of the current token.

string: string

The line.

indentUnit: number

The current indent unit size.

eol() → boolean

True if we are at the end of the line.

sol() → boolean

True if we are at the start of the line.

peek() → string | undefined

Get the next code unit after the current position, or undefined if we're at the end of the line.

next() → string | undefined

Read the next code unit and advance this.pos.

eat(
matchstring | RegExp | fn(chstring) → boolean
) → string | undefined

Match the next character against the given string, regular expression, or predicate. Consume and return it if it matches.

eatWhile(
matchstring | RegExp | fn(chstring) → boolean
) → boolean

Continue matching characters that match the given string, regular expression, or predicate function. Return true if any characters were consumed.

eatSpace() → boolean

Consume whitespace ahead of this.pos. Return true if any was found.

skipToEnd()

Move to the end of the line.

skipTo(chstring) → boolean | undefined

Move to directly before the given character, if found on the current line.

backUp(nnumber)

Move back n characters.

column() → number

Get the column position at this.pos.

indentation() → number

Get the indentation column of the current line.

match() → boolean | RegExpMatchArray | null

Match the input against the given string or regular expression (which should start with a ^). Return true or the regexp match if it matches.

Unless consume is set to false, this will move this.pos past the matched text.

When matching a string caseInsensitive can be set to true to make the match case-insensitive.

current() → string

Get the current token.

@codemirror/autocomplete

interface Completion

Objects type used to represent individual completions.

label: string

The label to show in the completion picker. This is what input is matched agains to determine whether a completion matches (and how well it matches).

detail⁠?: string

An optional short piece of information to show (with a different style) after the label.

info⁠?: string | fn(completionCompletion) → Node | Promise<Node>

Additional info to show when the completion is selected. Can be a plain string or a function that'll render the DOM structure to show when invoked.

apply⁠?: string | fn()

How to apply the completion. The default is to replace it with its label. When this holds a string, the completion range is replaced by that string. When it is a function, that function is called to perform the completion.

type⁠?: string

The type of the completion. This is used to pick an icon to show for the completion. Icons are styled with a CSS class created by appending the type name to "cm-completionIcon-". You can define or restyle icons by defining these selectors. The base library defines simple icons for class, constant, enum, function, interface, keyword, method, namespace, property, text, type, and variable.

Multiple types can be provided by separating them with spaces.

boost⁠?: number

When given, should be a number from -99 to 99 that adjusts how this completion is ranked compared to other completions that match the input as well as this one. A negative number moves it down the list, a positive number moves it up.

autocompletion(config⁠?: Object = {}) → Extension

Returns an extension that enables autocompletion.

config
activateOnTyping⁠?: boolean

When enabled (defaults to true), autocompletion will start whenever the user types something that can be completed.

override⁠?: readonly CompletionSource[]

Override the completion sources used. By default, they will be taken from the "autocomplete" language data (which should hold completion sources or arrays of completions).

maxRenderedOptions⁠?: number

The maximum number of options to render to the DOM.

defaultKeymap⁠?: boolean

Set this to false to disable the default completion keymap. (This requires you to add bindings to control completion yourself. The bindings should probably have a higher precedence than other bindings for the same keys.)

optionClass⁠?: fn(completionCompletion) → string

This can be used to add additional CSS classes to completion options.

icons⁠?: boolean

By default, the library will render icons based on the completion's type in front of each option. Set this to false to turn that off.

addToOptions⁠?: {
renderfn(completionCompletion, stateEditorState) → Node | null,
positionnumber
}[]

This option can be used to inject additional content into options. The render function will be called for each visible completion, and should produce a DOM node to show. position determines where in the DOM the result appears, relative to other added widgets and the standard content. The default icons have position 20, the label position 50, and the detail position

completionStatus(stateEditorState) → "active" | "pending" | null

Get the current completion status. When completions are available, this will return "active". When completions are pending (in the process of being queried), this returns "pending". Otherwise, it returns null.

currentCompletions(stateEditorState) → readonly Completion[]

Returns the available completions as an array.

Sources

class CompletionContext

An instance of this is passed to completion source functions.

new CompletionContext()

Create a new completion context. (Mostly useful for testing completion sources—in the editor, the extension will create these for you.)

state: EditorState

The editor state that the completion happens in.

pos: number

The position at which the completion is happening.

explicit: boolean

Indicates whether completion was activated explicitly, or implicitly by typing. The usual way to respond to this is to only return completions when either there is part of a completable entity before the cursor, or explicit is true.

tokenBefore(typesreadonly string[]) → {fromnumber, tonumber, textstring, typeNodeType} | null

Get the extent, content, and (if there is a token) type of the token before this.pos.

matchBefore(exprRegExp) → {fromnumber, tonumber, textstring} | null

Get the match of the given expression directly before the cursor.

aborted: boolean

Yields true when the query has been aborted. Can be useful in asynchronous queries to avoid doing work that will be ignored.

addEventListener(type"abort", listenerfn())

Allows you to register abort handlers, which will be called when the query is aborted.

interface CompletionResult

Interface for objects returned by completion sources.

from: number

The start of the range that is being completed.

to⁠?: number

The end of the range that is being completed. Defaults to the main cursor position.

options: readonly Completion[]

The completions returned. These don't have to be compared with the input by the source—the autocompletion system will do its own matching (against the text between from and to) and sorting.

span⁠?: RegExp

When given, further input that causes the part of the document between (mapped) from and to to match this regular expression will not query the completion source again, but continue with this list of options. This can help a lot with responsiveness, since it allows the completion list to be updated synchronously.

filter⁠?: boolean

By default, the library filters and scores completions. Set filter to false to disable this, and cause your completions to all be included, in the order they were given. When there are other sources, unfiltered completions appear at the top of the list of completions. span must not be given when filter is false, because it only works when filtering.

type CompletionSource = fn(contextCompletionContext) → CompletionResult | Promise<CompletionResult | null> | null

The function signature for a completion source. Such a function may return its result synchronously or as a promise. Returning null indicates no completions are available.

completeFromList(listreadonly (string | Completion)[]) → CompletionSource

Given a a fixed array of options, return an autocompleter that completes them.

ifIn(
nodesreadonly string[],
) → CompletionSource

Wrap the given completion source so that it will only fire when the cursor is in a syntax node with one of the given names.

ifNotIn(
nodesreadonly string[],
) → CompletionSource

Wrap the given completion source so that it will not fire when the cursor is in a syntax node with one of the given names.

completeAnyWord: CompletionSource

A completion source that will scan the document for words (using a character categorizer), and return those as completions.

Commands

startCompletion: Command

Explicitly start autocompletion.

closeCompletion: Command

Close the currently active completion.

acceptCompletion: Command

Accept the current completion.

moveCompletionSelection(
by⁠?: "option" | "page" = "option"
) → Command

Returns a command that moves the completion selection forward or backward by the given amount.

completionKeymap: readonly KeyBinding[]

Basic keybindings for autocompletion.

Snippets

snippet(templatestring) → fn(
editor: {stateEditorState, dispatchfn(trTransaction)},
)

Convert a snippet template to a function that can apply it. Snippets are written using syntax like this:

"for (let ${index} = 0; ${index} < ${end}; ${index}++) {\n\t${}\n}"

Each ${} placeholder (you may also use #{}) indicates a field that the user can fill in. Its name, if any, will be the default content for the field.

When the snippet is activated by calling the returned function, the code is inserted at the given position. Newlines in the template are indented by the indentation of the start line, plus one indent unit per tab character after the newline.

On activation, (all instances of) the first field are selected. The user can move between fields with Tab and Shift-Tab as long as the fields are active. Moving to the last field or moving the cursor out of the current field deactivates the fields.

The order of fields defaults to textual order, but you can add numbers to placeholders (${1} or ${1:defaultText}) to provide a custom order.

snippetCompletion(templatestring, completionCompletion) → Completion

Create a completion from a snippet. Returns an object with the properties from completion, plus an apply function that applies the snippet.

nextSnippetField: StateCommand

Move to the next snippet field, if available.

prevSnippetField: StateCommand

Move to the previous snippet field, if available.

clearSnippet: StateCommand

A command that clears the active snippet, if any.

snippetKeymap: Facet<
readonly KeyBinding[],
readonly KeyBinding[]
>

A facet that can be used to configure the key bindings used by snippets. The default binds Tab to nextSnippetField, Shift-Tab to prevSnippetField, and Escape to clearSnippet.

@codemirror/comment

This package provides commands for automatically commenting and uncommenting the selection.

interface CommentTokens

An object of this type can be provided as language data under a "commentTokens" property to configure comment syntax for a language.

block⁠?: {openstring, closestring}

The block comment syntax, if any. For example, for HTML you'd provide {open: "<!--", close: "-->"}.

line⁠?: string

The line comment syntax. For example "//".

toggleComment: StateCommand

Comment or uncomment the current selection. Will use line comments if available, otherwise falling back to block comments.

toggleLineComment: StateCommand

Comment or uncomment the current selection using line comments. The line comment syntax is taken from the commentTokens language data.

lineComment: StateCommand

Comment the current selection using line comments.

lineUncomment: StateCommand

Uncomment the current selection using line comments.

toggleBlockComment: StateCommand

Comment or uncomment the current selection using block comments. The block comment syntax is taken from the commentTokens language data.

blockComment: StateCommand

Comment the current selection using block comments.

blockUncomment: StateCommand

Uncomment the current selection using block comments.

commentKeymap: readonly KeyBinding[]

Default key bindings for this package.

@codemirror/rectangular-selection

rectangularSelection(options⁠?: Object) → Extension

Create an extension that enables rectangular selections. By default, it will react to left mouse drag with the Alt key held down. When such a selection occurs, the text within the rectangle that was dragged over will be selected, as one selection range per line.

options
eventFilter⁠?: fn(eventMouseEvent) → boolean

A custom predicate function, which takes a mousedown event and returns true if it should be used for rectangular selection.

@codemirror/basic-setup

basicSetup: Extension

This is an extension value that just pulls together a whole lot of extensions that you might want in a basic editor. It is meant as a convenient helper to quickly set up CodeMirror without installing and importing a lot of packages.

Specifically, it includes...

(You'll probably want to add some language package to your setup too.)

This package does not allow customization. The idea is that, once you decide you want to configure your editor more precisely, you take this package's source (which is just a bunch of imports and an array literal), copy it into your own code, and adjust it as desired.

re-export EditorView
re-export EditorState