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§
- Code
Highlight Node CodeHighlightNode: a text node in aCodeNode, with the token type a highlighter gave it. It takes no format.- Code
Highlighter Config CodePrismConfig/CodeShikiConfig.- Code
Highlighter Config Overrides Partial<CodePrismConfig>.- Code
Highlighter Output namedSignals(CodePrismConfig): the extension’s output.- Code
Indent Config CodeIndentConfig.- Code
Indent Config Overrides Partial<CodeIndentConfig>.- Code
Indent Output namedSignals(CodeIndentConfig):CodeIndentExtension’s output.- Code
Line Point - A point
$getStartOfCodeInLinereturns:{node, offset}. - Code
Node CodeNode: a code block, holdingCodeHighlightNodes, tabs and line breaks.- Plain
Tokenizer - 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’sPlainCodeTokenizeris; JS’s default isPrismTokenizer, whose grammars aren’t ported. - Token
Token: a token oftype(a Prism token name such askeyword,string,comment,function,number,operatororpunctuation).- Tokenizer
Ref - A
Tokenizerin a config, compared by identity, as JS compares objects.
Enums§
- Code
Language State - Whether a tokenizer can highlight a language.
- Code
Token string | Token: an item of what a tokenizer returns (Prism’sTokenStream), text without a type or a typedToken.- Token
Content TokenContent: what aTokenholds.
Constants§
- DEFAULT_
CODE_ LANGUAGE DEFAULT_CODE_LANGUAGE.
Traits§
- Tokenizer
Tokenizer: what the highlighter asks a highlighting engine.
Functions§
- code_
extension CodeExtension:CodeNodeandCodeHighlightNode, andregister_code. In an editor built with it, Enter (INSERT_PARAGRAPH_COMMAND) in a code block always adds a line; JS exits the block onKEY_ENTER_COMMANDinstead.- code_
highlighter_ extension CodePrismExtension, over the configured tokenizer (by default aPlainTokenizerdefaulting tojavascript, since Prism isn’t ported): highlights code blocks withregister_highlighting_only, unlessdisabled.code_extensionregisters the nodes, andcode_indent_extensionbrings the indent handlers.- code_
import_ rules CodeImportRules(@experimental): the import rules forCodeNode, the GitHub detectors first.CodeExtensioncontributes them, withCoreImportExtensionandinstall_vscode_code_paste_overlay.- code_
indent_ extension CodeIndentExtension:code_extensionandregister_code_indentationwith itstab_size, unlessdisabled.- code_
nodes - The node classes of this package (
CodeExtension’snodes):CodeNodeandCodeHighlightNode. - create_
code_ highlight_ node $createCodeHighlightNode(text, highlightType): the highlight type as given, an empty one included (onlysetHighlightTypedrops it).- create_
code_ node $createCodeNode(language, theme):Noneand""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, elseNone.- 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), orNoneat 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 oftokens, each string split at tabs and line breaks (tokenizeRawText) and typed by its innermost token: aprefixwith string content by its alias, the tokens of anunchangedone by none.- outdent_
leading_ spaces $outdentLeadingSpaces(node, tabSize, selection): removes up totab_sizeleading 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 ausize, and 0 does nothing.- plainify_
code_ content $plainifyCodeContent(text):textas code highlight nodes with no highlight type, line breaks and tabs (tokenizeRawText).- register_
code CodeExtension’sregister(editor), without the extension: aKEY_ENTER_COMMANDhandler 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)(@internalin JS, whichregisterCodeHighlightingcalls): callsset_gutterwith a code node’s key and its line numbers (what JS writes todata-gutter) when the node is rendered, and again when its number of children changes, as JS rewrites the attribute only then. A mutation listener ofCodeNode, 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 noCodeNode, as JS’sregisterMutationListenerthrows.- register_
code_ highlighting registerCodeHighlighting(editor, tokenizer): highlighting withtokenizerand the indent handlers ofregister_code_indentation(without atabSize), for code that doesn’t usecode_highlighter_extension. Fails when the editor lacksCodeNodeorCodeHighlightNode. Returns what unregisters both.- register_
code_ indentation registerCodeIndentation(editor, tabSize, escapeWithArrows): the key and command handlers of code-block indentation, all atCOMMAND_PRIORITY_LOW, before rich text’s: Tab and Shift+Tab dispatch the command$handleTabpicks;INSERT_TAB_COMMANDinserts a tab in a code block, andINDENT_CONTENT_COMMANDandOUTDENT_CONTENT_COMMANDindent and outdent its selected lines (withtab_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_STARTandMOVE_TO_ENDgo to$handleMoveTo. Withescape_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 ofCodeNode,TextNodeandCodeHighlightNode, without the indent handlers ofregister_code_indentationand withoutregisterCodeGutter, which an app that draws line numbers registers itself (super::register_code_gutter). Returns what unregisters them.
Type Aliases§
- Code
Extension Type CodeExtension.- Code
Highlighter Extension Type CodePrismExtension, over the configured tokenizer.- Code
Indent Extension Type CodeIndentExtension.