Skip to content

Commit

Permalink
Simplify highlight method for text_editor widget
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Jul 28, 2024
1 parent 1aa0a8f commit 16212ea
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
21 changes: 8 additions & 13 deletions examples/editor/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use iced::highlighter::{self, Highlighter};
use iced::highlighter;
use iced::keyboard;
use iced::widget::{
button, column, container, horizontal_space, pick_list, row, text,
Expand Down Expand Up @@ -186,18 +186,13 @@ impl Editor {
text_editor(&self.content)
.height(Fill)
.on_action(Message::ActionPerformed)
.highlight::<Highlighter>(
highlighter::Settings {
theme: self.theme,
token: self
.file
.as_deref()
.and_then(Path::extension)
.and_then(ffi::OsStr::to_str)
.map(str::to_string)
.unwrap_or(String::from("rs")),
},
|highlight, _theme| highlight.to_format()
.highlight(
self.file
.as_deref()
.and_then(Path::extension)
.and_then(ffi::OsStr::to_str)
.unwrap_or("rs"),
self.theme,
),
status,
]
Expand Down
10 changes: 2 additions & 8 deletions examples/markdown/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use iced::highlighter::{self, Highlighter};
use iced::highlighter;
use iced::widget::{self, markdown, row, scrollable, text_editor};
use iced::{Element, Fill, Font, Task, Theme};

Expand Down Expand Up @@ -65,13 +65,7 @@ impl Markdown {
.height(Fill)
.padding(10)
.font(Font::MONOSPACE)
.highlight::<Highlighter>(
highlighter::Settings {
theme: highlighter::Theme::Base16Ocean,
token: "markdown".to_owned(),
},
|highlight, _theme| highlight.to_format(),
);
.highlight("markdown", highlighter::Theme::Base16Ocean);

let preview = markdown(&self.items, markdown::Settings::default())
.map(Message::LinkClicked);
Expand Down
23 changes: 21 additions & 2 deletions widget/src/text_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::core::text::{self, LineHeight, Text};
use crate::core::widget::operation;
use crate::core::widget::{self, Widget};
use crate::core::{
Background, Border, Color, Element, Length, Padding, Pixels, Point,
self, Background, Border, Color, Element, Length, Padding, Pixels, Point,

Check warning on line 16 in widget/src/text_editor.rs

View workflow job for this annotation

GitHub Actions / todos_linux

unused import: `self`

Check warning on line 16 in widget/src/text_editor.rs

View workflow job for this annotation

GitHub Actions / wasm

unused import: `self`

Check warning on line 16 in widget/src/text_editor.rs

View workflow job for this annotation

GitHub Actions / wasm

unused import: `self`

Check warning on line 16 in widget/src/text_editor.rs

View workflow job for this annotation

GitHub Actions / wasm

unused import: `self`

Check warning on line 16 in widget/src/text_editor.rs

View workflow job for this annotation

GitHub Actions / todos_raspberry

unused import: `self`

Check warning on line 16 in widget/src/text_editor.rs

View workflow job for this annotation

GitHub Actions / todos_raspberry

unused import: `self`

Check warning on line 16 in widget/src/text_editor.rs

View workflow job for this annotation

GitHub Actions / widget

unused import: `self`

Check warning on line 16 in widget/src/text_editor.rs

View workflow job for this annotation

GitHub Actions / todos_windows

unused import: `self`

Check warning on line 16 in widget/src/text_editor.rs

View workflow job for this annotation

GitHub Actions / todos_macos

unused import: `self`
Rectangle, Shell, Size, SmolStr, Theme, Vector,
};

Expand Down Expand Up @@ -146,9 +146,28 @@ where
self
}

/// Highlights the [`TextEditor`] using the given syntax and theme.
#[cfg(feature = "highlighter")]
pub fn highlight(
self,
syntax: &str,
theme: iced_highlighter::Theme,
) -> TextEditor<'a, iced_highlighter::Highlighter, Message, Theme, Renderer>
where
Renderer: text::Renderer<Font = core::Font>,
{
self.highlight_with::<iced_highlighter::Highlighter>(
iced_highlighter::Settings {
theme,
token: syntax.to_owned(),
},
|highlight, _theme| highlight.to_format(),
)
}

/// Highlights the [`TextEditor`] with the given [`Highlighter`] and
/// a strategy to turn its highlights into some text format.
pub fn highlight<H: text::Highlighter>(
pub fn highlight_with<H: text::Highlighter>(
self,
settings: H::Settings,
to_format: fn(
Expand Down

0 comments on commit 16212ea

Please sign in to comment.