summaryrefslogtreecommitdiff
path: root/src/state/editor/mod.rs
blob: 36392264c5e67cc79472b5a224cfc68d368ec16f (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use crate::cursor::CursorMove;

use self::{bar::EditorBarState, buffer::Buffer, cursorshape::CursorShape};

use super::{window::Window, GlobalState, DUMMY_STATE};
use mlua::{Lua, UserData};

pub mod bar;
pub mod buffer;
pub mod cursorshape;

#[derive(Default, Debug)]
pub struct EditorState {
    pub visible: bool,
    pub buffer: Buffer,
    pub bar: EditorBarState,
    pub cursor_shape: CursorShape,
}

macro_rules! cfg {
    () => {
        GlobalState::instance().editor
    };
}

macro_rules! cfg_mut {
    () => {
        GlobalState::instance_mut().editor
    };
}

impl EditorState {
    pub const fn new() -> Self {
        Self {
            visible: false,
            buffer: Buffer::new(),
            bar: EditorBarState::new(),
            cursor_shape: CursorShape::Bar,
        }
    }

    pub fn run(lua: &Lua) {
        let script = { cfg!().buffer.as_string() };
        if let Err(_error) = lua.load(script).exec() {
            // TODO: add error handling
        }
    }
}

impl UserData for EditorState {
    fn add_fields<'lua, F: mlua::prelude::LuaUserDataFields<'lua, Self>>(fields: &mut F) {
        fields.add_field_function_get("visible", |_, _| Ok(cfg!().visible));
        fields.add_field_function_set("visible", |_, _, visible: bool| {
            let mut state = GlobalState::instance_mut();
            state.editor.visible = visible;

            if let Window::Editor = state.active_window {
                if !visible {
                    state.set_focus(Window::View);
                }
            }

            Ok(())
        });

        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("cursor_shape", |_, _| Ok(cfg!().cursor_shape));
        fields.add_field_function_set("cursor_shape", |_, _, shape: CursorShape| {
            cfg_mut!().cursor_shape = shape;
            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: String| {
            cfg_mut!().buffer.set_lines_from_string(content);
            Ok(())
        });

        fields.add_field_function_get("bar", |_, _| Ok(DUMMY_STATE.editor.bar));
    }

    fn add_methods<'lua, M: mlua::prelude::LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
        methods.add_function("run", |lua, _: ()| {
            EditorState::run(lua);
            Ok(())
        });

        methods.add_function("move_cursor", |_, m: CursorMove| {
            cfg_mut!().buffer.move_cursor(m);
            Ok(())
        })
    }
}