Skip to main content

Crate editor_core

Crate editor_core 

Source
Expand description

Editor Core - Industrial-Grade Headless Code Editor Kernel

§Overview

editor-core is a headless code editor kernel focused on state management, text metrics, and coordinate transformations.

It does not render UI. Hosts render from snapshots (e.g. HeadlessGrid) and drive edits via the command/state APIs.

§Core Features

  • Efficient Text Storage: based Piece Table O(1) insertion/deletion
  • Fast Line Index: based Rope O(log n) line access
  • Soft Wrapping Support: Headless layout engine, supporting arbitrary container widths
  • Style Management: Interval tree structure, O(log n + k) query complexity
  • Code Folding: Supports arbitrary levels of code folding
  • State Tracking: Version number mechanism and Change Notifications system
  • Workspace model: Multi-buffer + multi-view orchestration (Workspace, BufferId, ViewId) for tabs and split panes

§Architecture Layers

┌─────────────────────────────────────────────┐
│  Command Interface & State Management       │  ← Public API
├─────────────────────────────────────────────┤
│  Snapshot API (HeadlessGrid)                │  ← Rendering Data
├─────────────────────────────────────────────┤
│  Intervals & Visibility (Styles + Folding)  │  ← Visual Enhancement
├─────────────────────────────────────────────┤
│  Layout Engine (Soft Wrapping)              │  ← Text Layout
├─────────────────────────────────────────────┤
│  Line Index (Rope-based)                    │  ← Line Access
├─────────────────────────────────────────────┤
│  Piece Table Storage                        │  ← Text Storage
└─────────────────────────────────────────────┘

§Quick Start

§Using Command Interface

use editor_core::{CommandExecutor, Command, EditCommand, CursorCommand, Position};

let mut executor = CommandExecutor::empty(80);

// Insert text
executor.execute(Command::Edit(EditCommand::Insert {
    offset: 0,
    text: "fn main() {\n    println!(\"Hello\");\n}\n".to_string(),
})).unwrap();

// Move cursor
executor.execute(Command::Cursor(CursorCommand::MoveTo {
    line: 1,
    column: 4,
})).unwrap();

assert_eq!(executor.editor().cursor_position(), Position::new(1, 4));

§Using State Management

use editor_core::{EditorStateManager, StateChangeType};

let mut manager = EditorStateManager::new("Initial text", 80);

// Subscribe toState changed
manager.subscribe(|change| {
    println!("State changed: {:?}", change.change_type);
});

// Query state
let doc_state = manager.get_document_state();
println!("Line count: {}, Characters: {}", doc_state.line_count, doc_state.char_count);

§Using Workspace (multi-buffer / multi-view)

use editor_core::{Command, CursorCommand, EditCommand, Workspace};

let mut ws = Workspace::new();
let opened = ws
    .open_buffer(Some("file:///demo.txt".to_string()), "Hello\nWorld\n", 80)
    .unwrap();

// Commands always target a view.
let view = opened.view_id;
ws.execute(view, Command::Cursor(CursorCommand::MoveTo { line: 1, column: 0 }))
    .unwrap();
ws.execute(view, Command::Edit(EditCommand::InsertText { text: ">> ".into() }))
    .unwrap();

let grid = ws.get_viewport_content_styled(view, 0, 10).unwrap();
assert!(grid.actual_line_count() > 0);

§Module Description

  • storage - Piece Table text storage layer
  • line_index - Rope based line index
  • layout - soft wrappinglayout engine
  • intervals - Style interval tree andcode foldingmanagement
  • snapshot - Headless snapshot API (HeadlessGrid)
  • commands - Unified command interface
  • state - State management and query interface

§Performance Goals

  • Loading: 1000 line document < 100ms
  • insertion: 100 random insertions < 100ms
  • line access: 1000 line accesses < 10ms
  • Memory: 100 modifications, memory growth limited to AddBuffer size

§Unicode Support

  • UTF-8 internal encoding
  • Proper handling of CJK double-width characters
  • Grapheme/word-aware cursor + delete commands (UAX #29), while keeping char-indexed coordinates at the API boundary
  • via editor-core-lsp provides UTF-16 code unit coordinate conversion (for upper-layer protocols/integrations)
  • via editor-core-sublime provides .sublime-syntax syntax highlighting and folding (optional integration)

Re-exports§

pub use commands::Command;
pub use commands::CommandError;
pub use commands::CommandExecutor;
pub use commands::CommandResult;
pub use commands::CursorCommand;
pub use commands::EditCommand;
pub use commands::EditorCore;
pub use commands::Position;
pub use commands::Selection;
pub use commands::SelectionDirection;
pub use commands::StyleCommand;
pub use commands::TabKeyBehavior;
pub use commands::TextEditSpec;
pub use commands::ViewCommand;
pub use decorations::Decoration;
pub use decorations::DecorationKind;
pub use decorations::DecorationLayerId;
pub use decorations::DecorationPlacement;
pub use decorations::DecorationRange;
pub use delta::TextDelta;
pub use delta::TextDeltaEdit;
pub use diagnostics::Diagnostic;
pub use diagnostics::DiagnosticRange;
pub use diagnostics::DiagnosticSeverity;
pub use intervals::DOCUMENT_HIGHLIGHT_READ_STYLE_ID;
pub use intervals::DOCUMENT_HIGHLIGHT_TEXT_STYLE_ID;
pub use intervals::DOCUMENT_HIGHLIGHT_WRITE_STYLE_ID;
pub use intervals::FOLD_PLACEHOLDER_STYLE_ID;
pub use intervals::FoldRegion;
pub use intervals::FoldingManager;
pub use intervals::IntervalTree;
pub use intervals::StyleLayerId;
pub use layout::LayoutEngine;
pub use layout::WrapIndent;
pub use layout::WrapMode;
pub use line_ending::LineEnding;
pub use line_index::LineIndex;
pub use processing::DocumentProcessor;
pub use processing::ProcessingEdit;
pub use search::SearchError;
pub use search::SearchMatch;
pub use search::SearchOptions;
pub use snapshot::Cell;
pub use snapshot::ComposedCell;
pub use snapshot::ComposedCellSource;
pub use snapshot::ComposedGrid;
pub use snapshot::ComposedLine;
pub use snapshot::ComposedLineKind;
pub use snapshot::HeadlessGrid;
pub use snapshot::HeadlessLine;
pub use snapshot::MinimapGrid;
pub use snapshot::MinimapLine;
pub use snapshot::SnapshotGenerator;
pub use state::CursorState;
pub use state::DecorationsState;
pub use state::DiagnosticsState;
pub use state::DocumentState;
pub use state::EditorState;
pub use state::EditorStateManager;
pub use state::FoldingState;
pub use state::SmoothScrollState;
pub use state::StateChange;
pub use state::StateChangeCallback;
pub use state::StateChangeType;
pub use state::StyleState;
pub use state::UndoRedoState;
pub use state::ViewportState;
pub use storage::PieceTable;
pub use symbols::DocumentOutline;
pub use symbols::DocumentSymbol;
pub use symbols::SymbolKind;
pub use symbols::SymbolLocation;
pub use symbols::SymbolRange;
pub use symbols::Utf16Position;
pub use symbols::Utf16Range;
pub use symbols::WorkspaceSymbol;
pub use workspace::BufferId;
pub use workspace::BufferMetadata;
pub use workspace::OpenBufferResult;
pub use workspace::ViewId;
pub use workspace::ViewSmoothScrollState;
pub use workspace::Workspace;
pub use workspace::WorkspaceError;
pub use workspace::WorkspaceSearchResult;
pub use workspace::WorkspaceViewportState;

Modules§

commands
Command Interface Layer
decorations
First-class decorations (virtual text) data model.
delta
Structured text change deltas.
diagnostics
First-class diagnostics data model.
intervals
Phase 4: Styles and Folding (Intervals & Visibility)
layout
Phase 3: Layout and Soft Wrapping (Headless Layout Engine)
line_ending
Line ending helpers.
line_index
Stage 2: Logical Line Index
processing
Generic document processing interfaces.
search
Text search helpers.
snapshot
Phase 6: Headless Output Snapshot (Headless Snapshot API)
state
Editor State Interface
storage
Stage 1: Linear Storage Layer
symbols
First-class symbol / outline data model.
workspace
Workspace and multi-buffer / multi-view model.

Structs§

CommentConfig
Comment tokens/config for a given language.