diff options
| author | pml68 <contact@pml68.dev> | 2025-02-27 00:01:38 +0100 |
|---|---|---|
| committer | pml68 <contact@pml68.dev> | 2025-02-27 00:01:38 +0100 |
| commit | 1889f2fddddcce21790828c46a396e12c416919a (patch) | |
| tree | 9b1438127127a54896f9496fb4f2cd6f731507c3 | |
| parent | feat: switch to personal `iced` fork (should be temporary) (diff) | |
| download | iced_custom_highlighter-1889f2fddddcce21790828c46a396e12c416919a.tar.gz | |
docs: add README
| -rw-r--r-- | Cargo.lock | 12 | ||||
| -rw-r--r-- | README.md | 57 | ||||
| -rw-r--r-- | src/lib.rs | 62 |
3 files changed, 120 insertions, 11 deletions
@@ -94,9 +94,9 @@ checksum = "dd2e7510819d6fbf51a5545c8f922716ecfb14df168a3242f7d33e0239efe6a1" [[package]] name = "flate2" -version = "1.0.35" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c936bfdafb507ebbf50b8074c54fa31c5be9a1e7e5f467dd659697041407d07c" +checksum = "11faaf5a5236997af9848be0bef4db95824b1d534ebc64d0f0c6cf3e67bd38dc" dependencies = [ "crc32fast", "miniz_oxide", @@ -158,9 +158,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.169" +version = "0.2.170" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" +checksum = "875b3680cb2f8f71bdcf9a30f38d48282f5d3c95cbf9b3fa57269bb5d5c06828" [[package]] name = "lilt" @@ -185,9 +185,9 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "miniz_oxide" -version = "0.8.4" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3b1c9bd4fe1f0f8b387f6eb9eb3b4a1aa26185e5750efb9140301703f62cd1b" +checksum = "8e3e04debbb59698c15bacbb6d93584a8c0ca9cc3213cb423d31f760d8843ce5" dependencies = [ "adler2", ] diff --git a/README.md b/README.md new file mode 100644 index 0000000..d09cfcc --- /dev/null +++ b/README.md @@ -0,0 +1,57 @@ +# iced_custom_highlighter + +A custom syntax highlighter for iced. + +It uses the colors from your app's Theme, based on a styling method (like `default_style`) + +# Example + +```rust +use iced::widget::{Column, pick_list, text_editor}; +use iced::{Element, Theme}; +use iced_custom_highlighter::{Highlight, Highlighter, Settings}; + +#[derive(Default)] +struct State { + content: text_editor::Content, + theme: Theme, +} + +#[derive(Debug, Clone)] +enum Message { + Edit(text_editor::Action), + ChangeTheme(Theme), +} + +fn view(state: &State) -> Element<'_, Message> { +Column::new() + .push( + text_editor(&state.content) + .placeholder("Type something here...") + .rehighlight_on_redraw(true) + .highlight_with::<Highlighter>( + Settings::new(vec![], Highlight::default_style, "rs"), + Highlight::to_format, + ) + .on_action(Message::Edit), + ) + .push(pick_list( + Theme::ALL, + Some(state.theme), + Message::ChangeTheme, + )) + .into() +} + +fn update(state: &mut State, message: Message) { + match message { + Message::Edit(action) => { + state.content.perform(action); + } + + Message::ChangeTheme(theme) => { + state.theme = theme; + } + } +} +``` @@ -1,6 +1,60 @@ //! A custom syntax highlighter for iced. //! -//! It uses the colors from your app's Theme, based on the current [`Scope`] +//! It uses the colors from your app's Theme, based on a styling method (like [`default_style`]) +//! +//! # Example +//! +//! ```no_run +//! use iced::widget::{Column, pick_list, text_editor}; +//! use iced::{Element, Theme}; +//! use iced_custom_highlighter::{Highlight, Highlighter, Settings}; +//! +//! #[derive(Default)] +//! struct State { +//! content: text_editor::Content, +//! theme: Theme, +//! } +//! +//! #[derive(Debug, Clone)] +//! enum Message { +//! Edit(text_editor::Action), +//! ChangeTheme(Theme), +//! } +//! +//! fn view(state: &State) -> Element<'_, Message> { +//! Column::new() +//! .push( +//! text_editor(&state.content) +//! .placeholder("Type something here...") +//! .rehighlight_on_redraw(true) +//! .highlight_with::<Highlighter>( +//! Settings::new(vec![], Highlight::default_style, "rs"), +//! Highlight::to_format, +//! ) +//! .on_action(Message::Edit), +//! ) +//! .push(pick_list( +//! Theme::ALL, +//! Some(state.theme), +//! Message::ChangeTheme, +//! )) +//! .into() +//! } +//! +//! fn update(state: &mut State, message: Message) { +//! match message { +//! Message::Edit(action) => { +//! state.content.perform(action); +//! } +//! +//! Message::ChangeTheme(theme) => { +//! state.theme = theme; +//! } +//! } +//! } +//! ``` +//! +//! [`default_style`]: crate::Highlight::default_style use iced_core::font::Font; use iced_core::text::highlighter::{self, Format}; use iced_core::Theme; @@ -16,10 +70,8 @@ static SYNTAXES: LazyLock<parsing::SyntaxSet> = const LINES_PER_SNAPSHOT: usize = 50; -type ScopeSelectorsResult = core::result::Result< - highlighting::ScopeSelectors, - parsing::ParseScopeError, ->; +type ScopeSelectorsResult = + std::result::Result<highlighting::ScopeSelectors, parsing::ParseScopeError>; /// A syntax highlighter. #[derive(Debug)] |
