Simplify highlight method for text_editor widget

This commit is contained in:
Héctor Ramón Jiménez 2024-07-28 23:59:51 +02:00
parent 1aa0a8fa0d
commit 16212eaf52
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
3 changed files with 31 additions and 23 deletions

View file

@ -1,4 +1,4 @@
use iced::highlighter::{self, Highlighter}; use iced::highlighter;
use iced::keyboard; use iced::keyboard;
use iced::widget::{ use iced::widget::{
button, column, container, horizontal_space, pick_list, row, text, button, column, container, horizontal_space, pick_list, row, text,
@ -186,18 +186,13 @@ impl Editor {
text_editor(&self.content) text_editor(&self.content)
.height(Fill) .height(Fill)
.on_action(Message::ActionPerformed) .on_action(Message::ActionPerformed)
.highlight::<Highlighter>( .highlight(
highlighter::Settings { self.file
theme: self.theme, .as_deref()
token: self .and_then(Path::extension)
.file .and_then(ffi::OsStr::to_str)
.as_deref() .unwrap_or("rs"),
.and_then(Path::extension) self.theme,
.and_then(ffi::OsStr::to_str)
.map(str::to_string)
.unwrap_or(String::from("rs")),
},
|highlight, _theme| highlight.to_format()
), ),
status, status,
] ]

View file

@ -1,4 +1,4 @@
use iced::highlighter::{self, Highlighter}; use iced::highlighter;
use iced::widget::{self, markdown, row, scrollable, text_editor}; use iced::widget::{self, markdown, row, scrollable, text_editor};
use iced::{Element, Fill, Font, Task, Theme}; use iced::{Element, Fill, Font, Task, Theme};
@ -65,13 +65,7 @@ impl Markdown {
.height(Fill) .height(Fill)
.padding(10) .padding(10)
.font(Font::MONOSPACE) .font(Font::MONOSPACE)
.highlight::<Highlighter>( .highlight("markdown", highlighter::Theme::Base16Ocean);
highlighter::Settings {
theme: highlighter::Theme::Base16Ocean,
token: "markdown".to_owned(),
},
|highlight, _theme| highlight.to_format(),
);
let preview = markdown(&self.items, markdown::Settings::default()) let preview = markdown(&self.items, markdown::Settings::default())
.map(Message::LinkClicked); .map(Message::LinkClicked);

View file

@ -13,7 +13,7 @@ use crate::core::text::{self, LineHeight, Text};
use crate::core::widget::operation; use crate::core::widget::operation;
use crate::core::widget::{self, Widget}; use crate::core::widget::{self, Widget};
use crate::core::{ use crate::core::{
Background, Border, Color, Element, Length, Padding, Pixels, Point, self, Background, Border, Color, Element, Length, Padding, Pixels, Point,
Rectangle, Shell, Size, SmolStr, Theme, Vector, Rectangle, Shell, Size, SmolStr, Theme, Vector,
}; };
@ -146,9 +146,28 @@ where
self 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 /// Highlights the [`TextEditor`] with the given [`Highlighter`] and
/// a strategy to turn its highlights into some text format. /// a strategy to turn its highlights into some text format.
pub fn highlight<H: text::Highlighter>( pub fn highlight_with<H: text::Highlighter>(
self, self,
settings: H::Settings, settings: H::Settings,
to_format: fn( to_format: fn(