use crate::autocomplete::FuzzyMatcher;
use crate::{Component, Event};
pub trait EditorComponent: Component + Send {
fn get_text(&self) -> String;
fn set_text(&mut self, text: &str);
fn handle_input(&mut self, event: &Event);
fn on_submit(&mut self, _text: &str) {}
fn on_change(&mut self, _text: &str) {}
fn add_to_history(&mut self, _text: &str) {}
fn insert_text_at_cursor(&mut self, _text: &str) {}
fn get_expanded_text(&self) -> String {
self.get_text()
}
fn set_autocomplete_provider(&mut self, _matcher: FuzzyMatcher) {}
fn set_padding_x(&mut self, _padding: usize) {}
fn set_autocomplete_max_visible(&mut self, _max_visible: usize) {}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{Rect, Size, Surface};
struct TestEditor {
content: String,
dirty: bool,
}
impl TestEditor {
fn new() -> Self {
Self {
content: String::new(),
dirty: true,
}
}
}
impl Component for TestEditor {
fn request_render(&mut self) {
self.dirty = true;
}
fn is_dirty(&self) -> bool {
self.dirty
}
fn clear_dirty(&mut self) {
self.dirty = false;
}
fn handle_event(&mut self, _event: &Event) -> bool {
false
}
fn render(&mut self, _surface: &mut Surface, _area: Rect) {}
fn min_size(&self) -> Size {
Size { width: 10, height: 1 }
}
}
impl EditorComponent for TestEditor {
fn get_text(&self) -> String {
self.content.clone()
}
fn set_text(&mut self, text: &str) {
self.content = text.to_string();
}
fn handle_input(&mut self, _event: &Event) {}
}
#[test]
fn test_editor_component_trait() {
let mut editor = TestEditor::new();
assert_eq!(editor.get_text(), "");
editor.set_text("hello");
assert_eq!(editor.get_text(), "hello");
}
#[test]
fn test_expanded_text_fallback() {
let editor = TestEditor::new();
assert_eq!(editor.get_expanded_text(), "");
}
#[test]
fn test_boxed_editor_component() {
let editor = TestEditor::new();
let boxed: Box<dyn EditorComponent> = Box::new(editor);
drop(boxed);
}
}