1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! The editors state
pub mod mode;
mod search;
pub mod selection;
mod undo;
mod view;
use self::search::SearchState;
use self::view::ViewState;
use self::{mode::EditorMode, selection::Selection, undo::Stack};
use crate::clipboard::{Clipboard, ClipboardTrait};
use crate::{Index2, Lines};
/// Represents the state of an editor.
#[derive(Clone)]
pub struct EditorState {
/// The text in the editor.
pub lines: Lines,
/// The current cursor position in the editor.
pub cursor: Index2,
/// The mode of the editor (insert, visual or normal mode).
pub mode: EditorMode,
/// Represents the selection in the editor, if any.
pub selection: Option<Selection>,
/// Internal view state of the editor.
pub(crate) view: ViewState,
/// State holding the search results in search mode.
pub(crate) search: SearchState,
/// Stack for undo operations.
pub(crate) undo: Stack,
/// Stack for redo operations.
pub(crate) redo: Stack,
/// Clipboard for yank and paste operations.
pub(crate) clip: Clipboard,
}
impl Default for EditorState {
/// Creates a default `EditorState` with no text.
fn default() -> Self {
EditorState::new(Lines::default())
}
}
impl EditorState {
/// Creates a new editor state.
///
/// # Example
///
/// ```
/// use edtui::{EditorState, Lines};
///
/// let state = EditorState::new(Lines::from("First line\nSecond Line"));
/// ```
#[must_use]
pub fn new(lines: Lines) -> EditorState {
EditorState {
lines,
cursor: Index2::new(0, 0),
mode: EditorMode::Normal,
selection: None,
view: ViewState::default(),
search: SearchState::default(),
undo: Stack::new(),
redo: Stack::new(),
clip: Clipboard::default(),
}
}
/// Set a custom clipboard.
pub fn set_clipboard(&mut self, clipboard: impl ClipboardTrait + 'static) {
self.clip = Clipboard::new(clipboard);
}
/// Returns the current search pattern.
#[must_use]
pub fn search_pattern(&self) -> String {
self.search.pattern.clone()
}
}