Skip to main content

lexical_ui/code/
mod.rs

1//! `@lexical/code-core` (the `code` feature), from
2//! `packages/lexical-code-core/src`: `CodeNode` and `CodeHighlightNode`
3//! with their generated JSON (`LexicalCodeCoreGeneratedJSON.ts`),
4//! `$exitCodeNodeOnEnter` (`CodeNode.ts`), the line functions of
5//! `FlatStructureUtils.ts`, `CodeExtension`'s transform ([`register_code`])
6//! and `registerCodeIndentation`'s command handlers, Tab, the arrow keys
7//! (`escapeWithArrows`), `MOVE_TO_START` and `MOVE_TO_END` among them
8//! ([`register_code_indentation`]); with the `extension` feature,
9//! `CodeExtension` and `CodeIndentExtension` (`code_extension`,
10//! `code_indent_extension`).
11//!
12//! Not ported: the DOM parts (`createDOM`, `updateDOM`, `exportDOM`,
13//! `importDOM`, `CodeImportRules`, the VS Code paste overlay), which come
14//! with `html`. `registerCodeGutter` ([`register_code_gutter`]) gives the
15//! app the line numbers JS writes to the block's `data-gutter`.
16//!
17//! With the `code-highlight` feature, the highlighting loop of
18//! `@lexical/code-prism` (`CodeHighlighterPrism.ts`, with the lazy languages
19//! of `@lexical/code-shiki`) over a [`Tokenizer`] the app supplies:
20//! [`register_code_highlighting`], [`register_highlighting_only`] and, with
21//! `extension`, `code_highlighter_extension` (`CodePrismExtension`).
22//! Prism's grammars and Swift's tree-sitter tokenizer aren't ported.
23
24mod code_extension;
25mod code_gutter;
26mod code_highlight_node;
27#[cfg(feature = "code-highlight")]
28mod code_highlighter;
29#[cfg(all(feature = "html", feature = "extension"))]
30mod code_import_extension;
31mod code_indentation;
32mod code_node;
33#[cfg(feature = "code-highlight")]
34mod code_tokenizer;
35mod flat_structure_utils;
36mod generated_json;
37
38pub use code_extension::register_code;
39#[cfg(feature = "extension")]
40pub use code_extension::{
41    CodeExtensionType, CodeIndentConfig, CodeIndentConfigOverrides, CodeIndentExtensionType,
42    CodeIndentOutput, code_extension, code_indent_extension,
43};
44pub use code_gutter::register_code_gutter;
45pub use code_highlight_node::{
46    CodeHighlightNode, create_code_highlight_node, is_code_highlight_node,
47};
48#[cfg(all(feature = "code-highlight", feature = "extension"))]
49pub use code_highlighter::{
50    CodeHighlighterConfig, CodeHighlighterConfigOverrides, CodeHighlighterExtensionType,
51    CodeHighlighterOutput, TokenizerRef, code_highlighter_extension,
52};
53#[cfg(feature = "code-highlight")]
54pub use code_highlighter::{register_code_highlighting, register_highlighting_only};
55#[cfg(all(feature = "html", feature = "extension"))]
56pub use code_import_extension::{code_import_rules, install_vscode_code_paste_overlay};
57pub use code_indentation::register_code_indentation;
58pub use code_node::{
59    CodeNode, DEFAULT_CODE_LANGUAGE, create_code_node, exit_code_node_on_enter,
60    get_default_code_language, is_code_node,
61};
62#[cfg(feature = "code-highlight")]
63pub use code_tokenizer::{
64    CodeLanguageState, CodeToken, PlainTokenizer, Token, TokenContent, Tokenizer,
65    map_tokens_to_lexical_structure,
66};
67pub use flat_structure_utils::{
68    CodeLinePoint, get_code_line_direction, get_end_of_code_in_line, get_first_code_node_of_line,
69    get_last_code_node_of_line, get_start_of_code_in_line, outdent_leading_spaces,
70    plainify_code_content,
71};
72
73use crate::{NodeClass, Unregister};
74
75/// The node classes of this package (`CodeExtension`'s `nodes`):
76/// `CodeNode` and `CodeHighlightNode`.
77pub fn code_nodes() -> Vec<NodeClass> {
78    vec![
79        NodeClass::of::<CodeNode>(),
80        NodeClass::of::<CodeHighlightNode>(),
81    ]
82}
83
84/// `mergeRegister(...)`.
85fn merge(unregisters: Vec<Unregister>) -> Unregister {
86    Box::new(move || {
87        for unregister in unregisters {
88            unregister();
89        }
90    })
91}