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/editor/mod.rs | |
| parent | eafde55afcdf9dc4c17c6c97c1db472fc9ff9957 (diff) | |
implement into_luamulti for cursor move
Diffstat (limited to 'src/state/editor/mod.rs')
| -rw-r--r-- | src/state/editor/mod.rs | 31 |
1 files changed, 29 insertions, 2 deletions
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) { |