Expand description
A high-performance code editor widget for Iced.
This crate provides a canvas-based code editor with syntax highlighting, line numbers, and text selection capabilities for the Iced GUI framework.
§Features
- Syntax highlighting for multiple programming languages
- Line numbers with styled gutter
- Text selection via mouse drag and keyboard
- Clipboard operations (copy, paste)
- Custom scrollbars with themed styling
- Focus management for multiple editors
- Dark & light themes support with customizable colors
- Undo/Redo with command history
- Command palette (
Ctrl+Shift+P), extensible with the host’s own commands - Optional Vim mode with Normal, Insert, Visual, and Visual Line modes
§Example
use iced::widget::container;
use iced::{Element, Task};
use iced_code_editor::{CodeEditor, Message as EditorMessage};
struct MyApp {
editor: CodeEditor,
}
#[derive(Debug, Clone)]
enum Message {
EditorEvent(EditorMessage),
}
impl Default for MyApp {
fn default() -> Self {
let code = r#"fn main() {
println!("Hello, world!");
}
"#;
Self { editor: CodeEditor::new(code, "rust") }
}
}
impl MyApp {
fn update(&mut self, message: Message) -> Task<Message> {
match message {
Message::EditorEvent(event) => {
self.editor.update(&event).map(Message::EditorEvent)
}
}
}
fn view(&self) -> Element<'_, Message> {
container(self.editor.view().map(Message::EditorEvent))
.padding(20)
.into()
}
}
fn main() -> iced::Result {
iced::run(MyApp::update, MyApp::view)
}§Themes
The editor supports all native Iced themes with automatic color adaptation:
use iced_code_editor::{CodeEditor, theme};
// Create an editor (defaults to Tokyo Night Storm theme)
let mut editor = CodeEditor::new("fn main() {}", "rs");
// Switch to any Iced theme
editor.set_theme(theme::from_iced_theme(&iced::Theme::Dracula));
editor.set_theme(theme::from_iced_theme(&iced::Theme::CatppuccinMocha));
editor.set_theme(theme::from_iced_theme(&iced::Theme::Nord));§Vim Mode
Vim behavior is disabled by default and configured per editor instance:
use iced_code_editor::{CodeEditor, VimMode};
let mut editor =
CodeEditor::new("fn main() {}", "rs").with_vim_enabled(true);
assert!(editor.vim_enabled());
assert_eq!(editor.vim_mode(), Some(VimMode::Normal));
editor.set_vim_enabled(false);
assert_eq!(editor.vim_mode(), None);A focused editor can toggle Vim behavior with Ctrl+Alt+V, or
Command+Alt+V on macOS. Regular Ctrl/Command+V system paste is
unchanged. Counts support both target lines (5G/5gg jump to logical
line 5) and line operators (5yy, 5dd, and 5cc affect five lines).
A fixed bottom status line shows the current mode, pending keys, and active
/pattern or :N input. Submit with Enter, edit with Backspace, or cancel
with Escape. n and N repeat the last search forward or backward.
The Vim MVP provides Normal, Insert, Visual, and Visual Line modes; counts;
h/j/k/l, word and line motions; d/c/y, x, p/P, and u/Ctrl+R;
/ search with n/N, :N line jumps; and a per-editor unnamed register.
It is single-cursor and does not include general Ex commands, regex/search
history, text objects, macros, named registers, marks, . repeat, or
configurable mappings. Platform clipboard shortcuts continue to use the
system clipboard instead of the Vim register.
§Keyboard Shortcuts
The editor supports a comprehensive set of keyboard shortcuts:
§Navigation
| Shortcut | Action |
|---|---|
| Arrow Keys (Up, Down, Left, Right) | Move cursor |
| Shift + Arrows | Move cursor with selection |
| Ctrl + Left / Ctrl + Right | Move one word left/right (Alt on macOS) |
| Shift + Ctrl + Left/Right | Select one word left/right |
| Home | Jump to the first non-blank character; press again for column 0 |
| End | Jump to end of line |
| Shift + Home / Shift + End | Select to start/end of line |
| Ctrl + Home / Ctrl + End | Jump to start/end of document |
| Page Up / Page Down | Scroll one page up/down |
§Editing
| Shortcut | Action |
|---|---|
| Backspace | Delete character before cursor (or delete selection if text is selected) |
| Delete | Delete character after cursor (or delete selection if text is selected) |
| Ctrl + Backspace | Delete the word before the cursor (Alt on macOS) |
| Ctrl + Delete | Delete the word after the cursor (Alt on macOS) |
| Shift + Delete | Delete selected text (same as Delete when selection exists) |
| Enter | Insert new line |
§Clipboard
| Shortcut | Action |
|---|---|
| Ctrl + C or Ctrl + Insert | Copy selected text |
| Ctrl + V or Shift + Insert | Paste from clipboard |
§Supported Languages
The editor supports syntax highlighting through the syntect crate:
- Python (
"py"or"python") - Lua (
"lua") - Rust (
"rs"or"rust") - JavaScript (
"js"or"javascript") - And many more…
For a complete list, refer to the syntect crate documentation.
§Command History Management
The CommandHistory type provides fine-grained control over undo/redo operations.
While the editor handles history automatically, you can access it directly for
advanced use cases:
§Monitoring History State
use iced_code_editor::CommandHistory;
let history = CommandHistory::new(100);
// Check how many operations are available
println!("Undo operations: {}", history.undo_count());
println!("Redo operations: {}", history.redo_count());
// Check if operations are possible
if history.can_undo() {
println!("Can undo!");
}§Adjusting History Size
You can dynamically adjust the maximum number of operations kept in history:
use iced_code_editor::CommandHistory;
let history = CommandHistory::new(100);
// Get current maximum
assert_eq!(history.max_size(), 100);
// Increase limit for memory-rich environments
history.set_max_size(500);
// Or decrease for constrained environments
history.set_max_size(50);§Clearing History
You can reset the entire history when needed:
use iced_code_editor::CommandHistory;
let history = CommandHistory::new(100);
// Clear all undo/redo operations
history.clear();
assert_eq!(history.undo_count(), 0);
assert_eq!(history.redo_count(), 0);§Save Point Tracking
Track whether the document has been modified since the last save:
use iced_code_editor::CommandHistory;
let history = CommandHistory::new(100);
// After loading or saving a file
history.mark_saved();
// Check if there are unsaved changes
if history.is_modified() {
println!("Document has unsaved changes!");
}Re-exports§
pub use i18n::Language;pub use i18n::Translations;pub use theme::Catalog;pub use theme::Style;pub use theme::StyleFn;pub use theme::TokenColors;pub use theme::from_iced_theme;
Modules§
- i18n
- Internationalization support for the code editor.
- theme
- Theming support for the code editor widget.
Structs§
- Code
Editor - Canvas-based high-performance text editor.
- Command
History - Manages command history for undo/redo operations.
- Context
Menu Item - An actionable entry in the editor context menu.
- Diagnostic
- LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. A diagnostic range to underline in the editor.
- Fold
Region - A region of the buffer that can be collapsed into a single header line.
- LspCommand
- Resolved command to execute an LSP server.
- LspDocument
- LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. Metadata describing the currently edited document.
- LspFormatting
Options - LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. Formatting options sent with a document-formatting request.
- LspLanguage
- Represents a language supported by an LSP server.
- LspOverlay
State - State for the LSP overlay display (hover tooltips and completion menus).
- LspPosition
- LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. A zero-based position in an LSP document.
- LspProcess
Client - Client for communicating with an LSP server process.
- LspRange
- LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. A text range in an LSP document.
- LspServer
Config - Configuration for an LSP server.
- LspText
Change - LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. A text change described by a range replacement.
- Range
Style - The color of one range of a line, see
CodeEditor::set_range_styles. - Virtual
Text - One piece of virtual text, see
CodeEditor::set_virtual_text.
Enums§
- Arrow
Direction - Arrow key directions
- Context
Menu Entry - A custom editor context-menu entry.
- Diagnostic
Severity - LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. Severity of a diagnostic underline.
- Fold
Marker Position - Which side of the line numbers the fold-marker column sits on.
- Fold
Markers - When the gutter draws fold markers (the
▼/▶chevrons). - Indent
Style - Indentation style used when pressing the Tab key.
- LspEvent
- Events that can be sent from the LSP client to the application.
- LspOverlay
Message - Messages produced by LSP overlay UI interactions.
- Message
- Messages emitted by the code editor
- Syntax
Registration Error - Error returned by
register_syntaxes. - VimMode
- The active editing mode when Vim behavior is enabled.
Constants§
- LSP_
EVENT_ QUEUE_ CAPACITY - Recommended capacity for the
LspEventchannel.
Traits§
- LspClient
- LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. LSP client hooks invoked by the editor.
Functions§
- compute_
text_ change - LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. LSP integration types and traits for editor clients. Computes a minimal text change between two snapshots.
- ensure_
rust_ analyzer_ config - No-op on non-macOS platforms.
- lsp_
language_ for_ extension - Looks up the LSP language configuration for a file extension.
- lsp_
language_ for_ path - Looks up the LSP language configuration for a file path.
- lsp_
server_ config - Retrieves the server configuration for a given server key.
- register_
syntaxes - Adds syntax definitions, in Sublime Text’s
.sublime-syntaxformat, to the set every editor highlights with. - resolve_
lsp_ command - Resolves the command to execute an LSP server.
- view_
lsp_ overlay - Renders LSP overlay elements (hover tooltip and completion menu) on top of a
CodeEditor.