Skip to main content

editor_core/
lib.rs

1#![warn(missing_docs)]
2//! Editor Core - Industrial-Grade Headless Code Editor Kernel
3//!
4//! # Overview
5//!
6//! `editor-core` is a headless code editor kernel focused on state management, text metrics, and coordinate transformations.
7//! It does not involve the rendering process, assuming the upper layer provides a"text grid"(Text Grid)based view renderer, Support Unicode wide characters.
8//!
9//! # Core Features
10//!
11//! - **Efficient Text Storage**: based Piece Table  O(1) insertion/deletion
12//! - **Fast Line Index**: based Rope  O(log n) line access
13//! - **Soft Wrapping Support**: Headless layout engine, supporting arbitrary container widths
14//! - **Style Management**: Interval tree structure, O(log n + k) query complexity
15//! - **Code Folding**: Supports arbitrary levels of code folding
16//! - **State Tracking**: Version number mechanism and Change Notifications system
17//!
18//! # Architecture Layers
19//!
20//! ```text
21//! ┌─────────────────────────────────────────────┐
22//! │  Command Interface & State Management       │  ← Public API
23//! ├─────────────────────────────────────────────┤
24//! │  Snapshot API (HeadlessGrid)                │  ← Rendering Data
25//! ├─────────────────────────────────────────────┤
26//! │  Intervals & Visibility (Styles + Folding)  │  ← Visual Enhancement
27//! ├─────────────────────────────────────────────┤
28//! │  Layout Engine (Soft Wrapping)              │  ← Text Layout
29//! ├─────────────────────────────────────────────┤
30//! │  Line Index (Rope-based)                    │  ← Line Access
31//! ├─────────────────────────────────────────────┤
32//! │  Piece Table Storage                        │  ← Text Storage
33//! └─────────────────────────────────────────────┘
34//! ```
35//!
36//! # Quick Start
37//!
38//! ## Using Command Interface
39//!
40//! ```rust
41//! use editor_core::{CommandExecutor, Command, EditCommand, CursorCommand, Position};
42//!
43//! let mut executor = CommandExecutor::empty(80);
44//!
45//! // Insert text
46//! executor.execute(Command::Edit(EditCommand::Insert {
47//!     offset: 0,
48//!     text: "fn main() {\n    println!(\"Hello\");\n}\n".to_string(),
49//! })).unwrap();
50//!
51//! // Move cursor
52//! executor.execute(Command::Cursor(CursorCommand::MoveTo {
53//!     line: 1,
54//!     column: 4,
55//! })).unwrap();
56//!
57//! assert_eq!(executor.editor().cursor_position(), Position::new(1, 4));
58//! ```
59//!
60//! ## Using State Management
61//!
62//! ```rust
63//! use editor_core::{EditorStateManager, StateChangeType};
64//!
65//! let mut manager = EditorStateManager::new("Initial text", 80);
66//!
67//! // Subscribe toState changed
68//! manager.subscribe(|change| {
69//!     println!("State changed: {:?}", change.change_type);
70//! });
71//!
72//! // Query state
73//! let doc_state = manager.get_document_state();
74//! println!("Line count: {}, Characters: {}", doc_state.line_count, doc_state.char_count);
75//! ```
76//!
77//! # Module Description
78//!
79//! - [`storage`] - Piece Table text storage layer
80//! - [`line_index`] - Rope based line index
81//! - [`layout`] - soft wrappinglayout engine
82//! - [`intervals`] - Style interval tree andcode foldingmanagement
83//! - [`snapshot`] - Headless snapshot API (HeadlessGrid)
84//! - [`commands`] - Unified command interface
85//! - [`state`] - State management and query interface
86//!
87//! # Performance Goals
88//!
89//! - **Loading**: 1000 line document < 100ms
90//! - **insertion**: 100 random insertions < 100ms
91//! - **line access**: 1000 line accesses < 10ms
92//! - **Memory**: 100 modifications, memory growth limited to AddBuffer size
93//!
94//! # Unicode Support
95//!
96//! - UTF-8 internal encoding
97//! - Proper handling of CJK double-width characters
98//! - Support Grapheme Clusters (Emoji combinations)
99//! - via `editor-core-lsp` provides UTF-16 code unit coordinate conversion (for upper-layer protocols/integrations)
100//! - via `editor-core-sublime` provides `.sublime-syntax` syntax highlighting and folding (optional integration)
101
102pub mod commands;
103pub mod intervals;
104pub mod layout;
105pub mod line_index;
106pub mod processing;
107pub mod search;
108mod selection_set;
109pub mod snapshot;
110pub mod state;
111pub mod storage;
112mod text;
113
114pub use commands::{
115    Command, CommandError, CommandExecutor, CommandResult, CursorCommand, EditCommand, EditorCore,
116    Position, Selection, SelectionDirection, StyleCommand, ViewCommand,
117};
118pub use intervals::{FOLD_PLACEHOLDER_STYLE_ID, FoldingManager, IntervalTree, StyleLayerId};
119pub use layout::LayoutEngine;
120pub use line_index::LineIndex;
121pub use processing::{DocumentProcessor, ProcessingEdit};
122pub use search::{SearchError, SearchMatch, SearchOptions};
123pub use snapshot::{Cell, HeadlessGrid, HeadlessLine, SnapshotGenerator};
124pub use state::{
125    CursorState, DocumentState, EditorState, EditorStateManager, FoldingState, StateChange,
126    StateChangeCallback, StateChangeType, StyleState, UndoRedoState, ViewportState,
127};
128pub use storage::PieceTable;