diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-03 10:30:14 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-03 10:30:14 +0200 |
| commit | cd907dd59a48c2aa9d602aa3fb2f24563994420e (patch) | |
| tree | 33a1d783dc98a09b09fdcaf039bdcee9912dadd5 /src/state | |
| parent | eafde55afcdf9dc4c17c6c97c1db472fc9ff9957 (diff) | |
implement into_luamulti for cursor move
Diffstat (limited to 'src/state')
| -rw-r--r-- | src/state/editor/buffer.rs | 10 | ||||
| -rw-r--r-- | src/state/editor/mod.rs | 31 | ||||
| -rw-r--r-- | src/state/view/mod.rs | 12 |
3 files changed, 42 insertions, 11 deletions
diff --git a/src/state/editor/buffer.rs b/src/state/editor/buffer.rs index 49c27b9..6e210ab 100644 --- a/src/state/editor/buffer.rs +++ b/src/state/editor/buffer.rs @@ -52,6 +52,10 @@ impl Buffer { } pub fn insert(&mut self, c: char) { + if self.lines.is_empty() { + return; + } + match c { '\n' => { let (a, b) = { @@ -96,6 +100,10 @@ impl Buffer { } pub fn delete(&mut self) { + if self.lines.is_empty() { + return; + } + if self.cursor.is_at_start() && !self.cursor.is_at_top() { let old_line = self.current_line_mut().clone(); self.lines.remove(self.cursor.y()); @@ -108,7 +116,7 @@ impl Buffer { .move_checked(CursorMove::Jump((new_x, self.cursor.y()))); self.refresh_buffer_max() - } else if !self.cursor.is_at_top() { + } else if !self.cursor.is_at_start() { let x = self.cursor.x() - 1; self.current_line_mut().remove(x); self.refresh_line_max(); diff --git a/src/state/editor/mod.rs b/src/state/editor/mod.rs index 279a164..058bbf6 100644 --- a/src/state/editor/mod.rs +++ b/src/state/editor/mod.rs @@ -1,10 +1,10 @@ use self::{bar::EditorBarState, buffer::Buffer}; use super::{GlobalState, DUMMY_STATE}; -use mlua::{Lua, UserData}; +use mlua::{Lua, Table, UserData}; -pub mod buffer; pub mod bar; +pub mod buffer; #[derive(Default, Debug)] pub struct EditorState { @@ -51,6 +51,33 @@ impl UserData for EditorState { }); fields.add_field_function_get("bar", |_, _| Ok(DUMMY_STATE.editor.bar)); + + fields.add_field_function_get("content", |_, _| Ok(cfg!().buffer.as_string())); + fields.add_field_function_set("content", |_, _, content: String| { + cfg_mut!().buffer.set_lines_from_string(content); + Ok(()) + }); + + fields.add_field_function_get("lines", |lua, _| { + let table = lua.create_table()?; + + for (i, line) in cfg!().buffer.lines().iter().enumerate() { + table.set::<usize, &str>(i + 1, line.as_ref())? + } + + Ok(table) + }); + fields.add_field_function_set("lines", |_, _, content: Table| { + let mut vec = Vec::new(); + + for r in content.pairs::<usize, String>() { + let (_, line) = r?; + vec.push(line); + } + + cfg_mut!().buffer.set_lines(vec); + Ok(()) + }); } fn add_methods<'lua, M: mlua::prelude::LuaUserDataMethods<'lua, Self>>(methods: &mut M) { diff --git a/src/state/view/mod.rs b/src/state/view/mod.rs index b142227..961c4ca 100644 --- a/src/state/view/mod.rs +++ b/src/state/view/mod.rs @@ -14,8 +14,8 @@ use crate::{ }, }; -pub mod mode; pub mod bar; +pub mod mode; #[derive(Default, Debug)] pub struct SheetViewState { @@ -172,16 +172,12 @@ impl UserData for SheetViewState { Ok(()) }); - fields.add_field_function_get("bar", |_, _| { - Ok(DUMMY_STATE.view.bar) - }) + fields.add_field_function_get("bar", |_, _| Ok(DUMMY_STATE.view.bar)) } fn add_methods<'lua, M: mlua::prelude::LuaUserDataMethods<'lua, Self>>(methods: &mut M) { - methods.add_function("move_cursor", |_, (row, column): (usize, usize)| { - cfg_mut!() - .cursor - .move_checked(CursorMove::Jump((column, row))); + methods.add_function("move_cursor", |_, m: CursorMove| { + cfg_mut!().cursor.move_checked(m); Ok(()) }); |