-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
text_editor::Edit::Paste doesn't trigger syntax highlight like other Edit variants in case of custom highlight rules #2946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You really should try it on master even if there isn't a related issue. |
The problem in the code is probably here: iced/graphics/src/text/editor.rs Lines 371 to 385 in 5de7bc8
See how the Paste is running different code than the rest. And the The fix should be as easy as: [Edit: this doesn't seem to be solve the problem... but doing something here should work..] , Edit::Paste(text) => {
editor.insert_string(&text, None);
+ editor.set_redraw(true);
} Maybe more complicated if you want to test things before calling redraw. But for completeness, I tested it in master, the README seems to be out of date, that's why I couldn't run it following that example. This no longer works.
Because of this: Lines 693 to 697 in 5de7bc8
Anyway, here is the code for master: [package]
name = "ice-issue"
version = "0.1.0"
edition = "2024"
[dependencies]
iced = { git = "https://github.com/iced-rs/iced.git" , features = ["highlighter"] }
iced_core = {git = "https://github.com/iced-rs/iced.git"} use core::ops::Range;
use iced::widget::text_editor;
use iced::Color;
use iced::{Element, Fill, Font};
use iced_core::text::highlighter::{Format, Highlighter};
fn main() -> iced::Result {
iced::run(Editor::update, Editor::view)
}
#[derive(Default)]
struct Editor {
content: text_editor::Content,
}
#[derive(Clone, Debug)]
enum Message {
Editor(text_editor::Action),
}
impl Editor {
fn update(&mut self, message: Message) {
match message {
Message::Editor(a) => self.content.perform(a),
}
}
fn view(&self) -> Element<'_, Message> {
text_editor(&self.content)
.height(Fill)
.on_action(Message::Editor)
.font(Font::MONOSPACE)
.highlight_with::<MyHighlighter>((), Highlight::to_format)
.into()
}
}
enum Highlight {
First,
Second,
None,
}
impl Highlight {
fn get(i: usize) -> Self {
match i {
0 => Self::First,
1 => Self::Second,
_ => Self::None,
}
}
fn to_format(&self, _theme: &iced::Theme) -> Format<Font> {
let color = match self {
Self::First => Some(Color::from_rgb(0.0, 0.0, 0.1)),
Self::Second => Some(Color::from_rgb(1.0, 0.0, 0.0)),
Self::None => None,
};
Format { color, font: None }
}
}
fn highlight_line(line: &str) -> Vec<(Range<usize>, Highlight)> {
let mut offset = 0;
line.split(" ")
.enumerate()
.map(|(i, l)| {
let start = offset;
offset += l.len() + 1;
(start..offset, Highlight::get(i))
})
.collect()
}
struct MyHighlighter {
curr_line: usize,
}
impl Highlighter for MyHighlighter {
type Settings = ();
type Highlight = Highlight;
type Iterator<'a> = Box<dyn Iterator<Item = (Range<usize>, Self::Highlight)>>;
fn new(_: &Self::Settings) -> Self {
Self { curr_line: 0 }
}
fn update(&mut self, _: &Self::Settings) {
self.change_line(0);
}
fn change_line(&mut self, line: usize) {
self.curr_line = line;
}
fn highlight_line(&mut self, line: &str) -> Self::Iterator<'_> {
Box::new(highlight_line(line).into_iter())
}
fn current_line(&self) -> usize {
self.curr_line
}
} |
Thanks for the notice! |
Fixes iced-rs#2946 for multiline text
Seems to be fixed on my local run now. I didn't realize the top changed line was overwritten at the end based on the cursor position. I think the assumption was for single line edits only. |
Fixes iced-rs#2946 for multiline text
Uh oh!
There was an error while loading. Please reload this page.
Is your issue REALLY a bug?
Is there an existing issue for this?
Is this issue related to iced?
What happened?
When I copy and paste multiline strings in TextEditor only the last line gets syntax highlight. I have a custom syntax highlight rules, I cannot see any way to trigger the highlight, or to call the
change_line
programmatically while changing the editor Content.You can see in the picture below that the pasted block only has the last line highlighted. When I go and edit something above them, then it gets highlighted.
I didn't test it in master because I didn't find any issues related to this, and it seems to work fine with the default language based highlighter. (And I couldn't compile it in master because the
iced_core::Color::new
) seems to be private now, and other changes)Here is a minimal reproducible example, you can try typing some texts and then copy multiple lines:
src/main.rs
What is the expected behavior?
The syntax highlight is triggered from the first line of the pasted string, instead of from the last line.
Version
0.13.1
Operating System
Arch Linux
Do you have any log output?
No
The text was updated successfully, but these errors were encountered: