Skip to main content

Module code

Module code 

Source
Expand description

@lexical/code-core (the code feature), from packages/lexical-code-core/src: CodeNode and CodeHighlightNode with their generated JSON (LexicalCodeCoreGeneratedJSON.ts), $exitCodeNodeOnEnter (CodeNode.ts), the line functions of FlatStructureUtils.ts, CodeExtension’s transform (register_code) and registerCodeIndentation’s command handlers, Tab, the arrow keys (escapeWithArrows), MOVE_TO_START and MOVE_TO_END among them (register_code_indentation); with the extension feature, CodeExtension and CodeIndentExtension (code_extension, code_indent_extension).

Not ported: the DOM parts (createDOM, updateDOM, exportDOM, importDOM, CodeImportRules, the VS Code paste overlay), which come with html. registerCodeGutter (register_code_gutter) gives the app the line numbers JS writes to the block’s data-gutter.

With the code-highlight feature, the highlighting loop of @lexical/code-prism (CodeHighlighterPrism.ts, with the lazy languages of @lexical/code-shiki) over a Tokenizer the app supplies: register_code_highlighting, register_highlighting_only and, with extension, code_highlighter_extension (CodePrismExtension). Prism’s grammars and Swift’s tree-sitter tokenizer aren’t ported.

Structs§

CodeHighlightNode
CodeHighlightNode: a text node in a CodeNode, with the token type a highlighter gave it. It takes no format.
CodeHighlighterConfig
CodePrismConfig / CodeShikiConfig.
CodeHighlighterConfigOverrides
Partial<CodePrismConfig>.
CodeHighlighterOutput
namedSignals(CodePrismConfig): the extension’s output.
CodeIndentConfig
CodeIndentConfig.
CodeIndentConfigOverrides
Partial<CodeIndentConfig>.
CodeIndentOutput
namedSignals(CodeIndentConfig): CodeIndentExtension’s output.
CodeLinePoint
A point $getStartOfCodeInLine returns: {node, offset}.
CodeNode
CodeNode: a code block, holding CodeHighlightNodes, tabs and line breaks.
PlainTokenizer
A tokenizer that knows no language: every code block is split into plain tokens, so the indent and line-move handlers can work on it. The default of CodeHighlighterConfig, as Swift’s PlainCodeTokenizer is; JS’s default is PrismTokenizer, whose grammars aren’t ported.
Token
Token: a token of type (a Prism token name such as keyword, string, comment, function, number, operator or punctuation).
TokenizerRef
A Tokenizer in a config, compared by identity, as JS compares objects.

Enums§

CodeLanguageState
Whether a tokenizer can highlight a language.
CodeToken
string | Token: an item of what a tokenizer returns (Prism’s TokenStream), text without a type or a typed Token.
TokenContent
TokenContent: what a Token holds.

Constants§

DEFAULT_CODE_LANGUAGE
DEFAULT_CODE_LANGUAGE.

Traits§

Tokenizer
Tokenizer: what the highlighter asks a highlighting engine.

Functions§

code_extension
CodeExtension: CodeNode and CodeHighlightNode, and register_code. In an editor built with it, Enter (INSERT_PARAGRAPH_COMMAND) in a code block always adds a line; JS exits the block on KEY_ENTER_COMMAND instead.
code_highlighter_extension
CodePrismExtension, over the configured tokenizer (by default a PlainTokenizer defaulting to javascript, since Prism isn’t ported): highlights code blocks with register_highlighting_only, unless disabled. code_extension registers the nodes, and code_indent_extension brings the indent handlers.
code_import_rules
CodeImportRules (@experimental): the import rules for CodeNode, the GitHub detectors first. CodeExtension contributes them, with CoreImportExtension and install_vscode_code_paste_overlay.
code_indent_extension
CodeIndentExtension: code_extension and register_code_indentation with its tab_size, unless disabled.
code_nodes
The node classes of this package (CodeExtension’s nodes): CodeNode and CodeHighlightNode.
create_code_highlight_node
$createCodeHighlightNode(text, highlightType): the highlight type as given, an empty one included (only setHighlightType drops it).
create_code_node
$createCodeNode(language, theme): None and "" are both no language (no theme).
exit_code_node_on_enter
$exitCodeNodeOnEnter(selection): at the end of a code block that ends with two line breaks, a collapsed selection removes them and moves to a new paragraph after the block, which it returns.
get_code_line_direction
$getCodeLineDirection(anchor): the direction of the first strongly directional character of the line’s code highlight nodes, else the code block’s own direction, else None.
get_default_code_language
getDefaultCodeLanguage().
get_end_of_code_in_line
$getEndOfCodeInLine(anchor): the last node of the line.
get_first_code_node_of_line
$getFirstCodeNodeOfLine(anchor).
get_last_code_node_of_line
$getLastCodeNodeOfLine(anchor).
get_start_of_code_in_line
$getStartOfCodeInLine(anchor, offset): where the code of the line starts, past its leading spaces and tabs (the first non-blank character before the anchor when there is one), the line break before the line (offset 1), or None at the start of the block. Offsets are UTF-16 code units.
install_vscode_code_paste_overlay
$installVscodeCodePasteOverlay (@experimental): installs the VS Code overlay (its wrapper and line run rules) when the DOM looks like a VS Code code block paste.
is_code_highlight_node
$isCodeHighlightNode(node): instanceof CodeHighlightNode, so a class that extends it (LexicalNode::super_types) is one too.
is_code_node
$isCodeNode(node).
map_tokens_to_lexical_structure
$mapTokensToLexicalStructure(tokens, type) (FacadePrism.ts): the CodeHighlightNodes, TabNodes and LineBreakNodes of tokens, each string split at tabs and line breaks (tokenizeRawText) and typed by its innermost token: a prefix with string content by its alias, the tokens of an unchanged one by none.
outdent_leading_spaces
$outdentLeadingSpaces(node, tabSize, selection): removes up to tab_size leading spaces from the code highlight node that starts a line, and moves the range selection’s points in it back by as many (not before 0). Returns whether it removed any. JS takes any number and does nothing unless it is a positive integer; here it is a usize, and 0 does nothing.
plainify_code_content
$plainifyCodeContent(text): text as code highlight nodes with no highlight type, line breaks and tabs (tokenizeRawText).
register_code
CodeExtension’s register(editor), without the extension: a KEY_ENTER_COMMAND handler that leaves a code block after two line breaks at its end ($exitCodeNodeOnEnter), and a transform that clears the format of a tab in a code block. Returns what unregisters them.
register_code_gutter
registerCodeGutter(editor) (@internal in JS, which registerCodeHighlighting calls): calls set_gutter with a code node’s key and its line numbers (what JS writes to data-gutter) when the node is rendered, and again when its number of children changes, as JS rewrites the attribute only then. A mutation listener of CodeNode, so it hears only while the editor has a root element; a headless editor registers nothing, as in JS. Returns what unregisters it, or an error when the editor has no CodeNode, as JS’s registerMutationListener throws.
register_code_highlighting
registerCodeHighlighting(editor, tokenizer): highlighting with tokenizer and the indent handlers of register_code_indentation (without a tabSize), for code that doesn’t use code_highlighter_extension. Fails when the editor lacks CodeNode or CodeHighlightNode. Returns what unregisters both.
register_code_indentation
registerCodeIndentation(editor, tabSize, escapeWithArrows): the key and command handlers of code-block indentation, all at COMMAND_PRIORITY_LOW, before rich text’s: Tab and Shift+Tab dispatch the command $handleTab picks; INSERT_TAB_COMMAND inserts a tab in a code block, and INDENT_CONTENT_COMMAND and OUTDENT_CONTENT_COMMAND indent and outdent its selected lines (with tab_size, outdenting also removes up to that many leading spaces); the up and down arrows keep a caret at the very start or end of the document in the block, and otherwise go to $handleShiftLines; MOVE_TO_START and MOVE_TO_END go to $handleMoveTo. With escape_with_arrows, the arrows first leave a code block at its edge by inserting a paragraph beside it. Returns what unregisters them.
register_highlighting_only
registerHighlightingOnly(editor, tokenizer): the highlighting transforms of CodeNode, TextNode and CodeHighlightNode, without the indent handlers of register_code_indentation and without registerCodeGutter, which an app that draws line numbers registers itself (super::register_code_gutter). Returns what unregisters them.

Type Aliases§

CodeExtensionType
CodeExtension.
CodeHighlighterExtensionType
CodePrismExtension, over the configured tokenizer.
CodeIndentExtensionType
CodeIndentExtension.