Skip to main content

Crate iced_code_editor

Crate iced_code_editor 

Source
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

§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));

§Keyboard Shortcuts

The editor supports a comprehensive set of keyboard shortcuts:

ShortcutAction
Arrow Keys (Up, Down, Left, Right)Move cursor
Shift + ArrowsMove cursor with selection
Home / EndJump to start/end of line
Shift + Home / Shift + EndSelect to start/end of line
Ctrl + Home / Ctrl + EndJump to start/end of document
Page Up / Page DownScroll one page up/down

§Editing

ShortcutAction
BackspaceDelete character before cursor (or delete selection if text is selected)
DeleteDelete character after cursor (or delete selection if text is selected)
Shift + DeleteDelete selected text (same as Delete when selection exists)
EnterInsert new line

§Clipboard

ShortcutAction
Ctrl + C or Ctrl + InsertCopy selected text
Ctrl + V or Shift + InsertPaste 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::from_iced_theme;

Modules§

i18n
Internationalization support for the code editor.
theme

Structs§

CodeEditor
Canvas-based high-performance text editor.
CommandHistory
Manages command history for undo/redo operations.
LspCommand
Resolved command to execute an LSP server.
LspDocument
LSP integration types and traits for editor clients. Metadata describing the currently edited document.
LspLanguage
Represents a language supported by an LSP server.
LspOverlayState
State for the LSP overlay display (hover tooltips and completion menus).
LspPosition
LSP integration types and traits for editor clients. A zero-based position in an LSP document.
LspProcessClient
Client for communicating with an LSP server process.
LspRange
LSP integration types and traits for editor clients. A text range in an LSP document.
LspServerConfig
Configuration for an LSP server.
LspTextChange
LSP integration types and traits for editor clients. A text change described by a range replacement.

Enums§

ArrowDirection
Arrow key directions
LspEvent
Events that can be sent from the LSP client to the application.
LspOverlayMessage
Messages produced by LSP overlay UI interactions.
Message
Messages emitted by the code editor

Traits§

LspClient
LSP integration types and traits for editor clients. LSP client hooks invoked by the editor.

Functions§

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.
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.