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
7//! coordinate transformations.
8//!
9//! It does not render UI. Hosts render from snapshots (e.g. [`HeadlessGrid`]) and drive edits via
10//! the command/state APIs.
11//!
12//! # Core Features
13//!
14//! - **Efficient Text Storage**: based Piece Table O(1) insertion/deletion
15//! - **Fast Line Index**: based Rope O(log n) line access
16//! - **Soft Wrapping Support**: Headless layout engine, supporting arbitrary container widths
17//! - **Style Management**: Interval tree structure, O(log n + k) query complexity
18//! - **Code Folding**: Supports arbitrary levels of code folding
19//! - **State Tracking**: Version number mechanism and Change Notifications system
20//! - **Workspace model**: Multi-buffer + multi-view orchestration (`Workspace`, `BufferId`,
21//! `ViewId`) for tabs and split panes
22//!
23//! # Architecture Layers
24//!
25//! ```text
26//! ┌─────────────────────────────────────────────┐
27//! │ Command Interface & State Management │ ← Public API
28//! ├─────────────────────────────────────────────┤
29//! │ Snapshot API (HeadlessGrid) │ ← Rendering Data
30//! ├─────────────────────────────────────────────┤
31//! │ Intervals & Visibility (Styles + Folding) │ ← Visual Enhancement
32//! ├─────────────────────────────────────────────┤
33//! │ Layout Engine (Soft Wrapping) │ ← Text Layout
34//! ├─────────────────────────────────────────────┤
35//! │ Line Index (Rope-based) │ ← Line Access
36//! ├─────────────────────────────────────────────┤
37//! │ Piece Table Storage │ ← Text Storage
38//! └─────────────────────────────────────────────┘
39//! ```
40//!
41//! # Quick Start
42//!
43//! ## Using Command Interface
44//!
45//! ```rust
46//! use editor_core::{CommandExecutor, Command, EditCommand, CursorCommand, Position};
47//!
48//! let mut executor = CommandExecutor::empty(80);
49//!
50//! // Insert text
51//! executor.execute(Command::Edit(EditCommand::Insert {
52//! offset: 0,
53//! text: "fn main() {\n println!(\"Hello\");\n}\n".to_string(),
54//! })).unwrap();
55//!
56//! // Move cursor
57//! executor.execute(Command::Cursor(CursorCommand::MoveTo {
58//! line: 1,
59//! column: 4,
60//! })).unwrap();
61//!
62//! assert_eq!(executor.editor().cursor_position(), Position::new(1, 4));
63//! ```
64//!
65//! ## Using State Management
66//!
67//! ```rust
68//! use editor_core::{EditorStateManager, StateChangeType};
69//!
70//! let mut manager = EditorStateManager::new("Initial text", 80);
71//!
72//! // Subscribe toState changed
73//! manager.subscribe(|change| {
74//! println!("State changed: {:?}", change.change_type);
75//! });
76//!
77//! // Query state
78//! let doc_state = manager.get_document_state();
79//! println!("Line count: {}, Characters: {}", doc_state.line_count, doc_state.char_count);
80//! ```
81//!
82//! ## Using Workspace (multi-buffer / multi-view)
83//!
84//! ```rust
85//! use editor_core::{Command, CursorCommand, EditCommand, Workspace};
86//!
87//! let mut ws = Workspace::new();
88//! let opened = ws
89//! .open_buffer(Some("file:///demo.txt".to_string()), "Hello\nWorld\n", 80)
90//! .unwrap();
91//!
92//! // Commands always target a view.
93//! let view = opened.view_id;
94//! ws.execute(view, Command::Cursor(CursorCommand::MoveTo { line: 1, column: 0 }))
95//! .unwrap();
96//! ws.execute(view, Command::Edit(EditCommand::InsertText { text: ">> ".into() }))
97//! .unwrap();
98//!
99//! let grid = ws.get_viewport_content_styled(view, 0, 10).unwrap();
100//! assert!(grid.actual_line_count() > 0);
101//! ```
102//!
103//! # Module Description
104//!
105//! - [`storage`] - Piece Table text storage layer
106//! - [`line_index`] - Rope based line index
107//! - [`layout`] - soft wrappinglayout engine
108//! - [`intervals`] - Style interval tree andcode foldingmanagement
109//! - [`snapshot`] - Headless snapshot API (HeadlessGrid)
110//! - [`commands`] - Unified command interface
111//! - [`state`] - State management and query interface
112//!
113//! # Performance Goals
114//!
115//! - **Loading**: 1000 line document < 100ms
116//! - **insertion**: 100 random insertions < 100ms
117//! - **line access**: 1000 line accesses < 10ms
118//! - **Memory**: 100 modifications, memory growth limited to AddBuffer size
119//!
120//! # Unicode Support
121//!
122//! - UTF-8 internal encoding
123//! - Proper handling of CJK double-width characters
124//! - Grapheme/word-aware cursor + delete commands (UAX #29), while keeping `char`-indexed
125//! coordinates at the API boundary
126//! - via `editor-core-lsp` provides UTF-16 code unit coordinate conversion (for upper-layer protocols/integrations)
127//! - via `editor-core-sublime` provides `.sublime-syntax` syntax highlighting and folding (optional integration)
128
129pub mod commands;
130pub mod decorations;
131pub mod delta;
132pub mod diagnostics;
133pub mod intervals;
134pub mod layout;
135pub mod line_ending;
136pub mod line_index;
137pub mod processing;
138pub mod search;
139mod selection_set;
140pub mod snapshot;
141pub mod state;
142pub mod storage;
143pub mod symbols;
144mod text;
145pub mod workspace;
146
147pub use commands::{
148 Command, CommandError, CommandExecutor, CommandResult, CursorCommand, EditCommand, EditorCore,
149 Position, Selection, SelectionDirection, StyleCommand, TabKeyBehavior, TextEditSpec,
150 ViewCommand,
151};
152pub use decorations::{
153 Decoration, DecorationKind, DecorationLayerId, DecorationPlacement, DecorationRange,
154};
155pub use delta::{TextDelta, TextDeltaEdit};
156pub use diagnostics::{Diagnostic, DiagnosticRange, DiagnosticSeverity};
157pub use editor_core_lang::CommentConfig;
158pub use intervals::{
159 DOCUMENT_HIGHLIGHT_READ_STYLE_ID, DOCUMENT_HIGHLIGHT_TEXT_STYLE_ID,
160 DOCUMENT_HIGHLIGHT_WRITE_STYLE_ID, FOLD_PLACEHOLDER_STYLE_ID, FoldRegion, FoldingManager,
161 IntervalTree, StyleLayerId,
162};
163pub use layout::{LayoutEngine, WrapIndent, WrapMode};
164pub use line_ending::LineEnding;
165pub use line_index::LineIndex;
166pub use processing::{DocumentProcessor, ProcessingEdit};
167pub use search::{SearchError, SearchMatch, SearchOptions};
168pub use snapshot::{
169 Cell, ComposedCell, ComposedCellSource, ComposedGrid, ComposedLine, ComposedLineKind,
170 HeadlessGrid, HeadlessLine, MinimapGrid, MinimapLine, SnapshotGenerator,
171};
172pub use state::{
173 CursorState, DecorationsState, DiagnosticsState, DocumentState, EditorState,
174 EditorStateManager, FoldingState, SmoothScrollState, StateChange, StateChangeCallback,
175 StateChangeType, StyleState, UndoRedoState, ViewportState,
176};
177pub use storage::PieceTable;
178pub use symbols::{
179 DocumentOutline, DocumentSymbol, SymbolKind, SymbolLocation, SymbolRange, Utf16Position,
180 Utf16Range, WorkspaceSymbol,
181};
182pub use workspace::{
183 BufferId, BufferMetadata, OpenBufferResult, ViewId, ViewSmoothScrollState, Workspace,
184 WorkspaceError, WorkspaceSearchResult, WorkspaceViewportState,
185};