Skip to content

Commit

Permalink
fixed editor API not buffering file properly
Browse files Browse the repository at this point in the history
  • Loading branch information
curlpipe committed Jul 22, 2024
1 parent 0ecfd7b commit a195ab5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .todo.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
- [ ] Plugin refresh - 0.4.2
- [ ] Investigate panics in plug-ins
- [ ] Update documentation
- [ ] New API
- [ ] User defined syntax highlighting
Expand Down Expand Up @@ -28,6 +27,7 @@
- [X] file type
- [X] length of current document
- [X] Custom syntax highlighting syntax sugar
- [X] Investigate panics in plug-ins

- [ ] Editing improvements - 0.4.3
- [ ] Better editor navigation (see github issues)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ox"
version = "0.4.1"
version = "0.4.2"
edition = "2021"
authors = ["Curlpipe <[email protected]>"]
description = "A Rust powered text editor."
Expand Down
3 changes: 0 additions & 3 deletions config/.oxrc
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ event_mapping = {
["ctrl_k"] = function()
editor:open_command_line()
end,
["ctrl_h"] = function()
editor:display_info(editor.document_length)
end,
}

-- Allow user-defined commands
Expand Down
40 changes: 38 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,12 @@ pub fn key_to_string(modifiers: KMod, key: KCode) -> String {
return result
}

fn update_highlighter(editor: &mut Editor) {
if let Err(err) = editor.update_highlighter() {
editor.feedback = Feedback::Error(err.to_string());
}
}

impl LuaUserData for Editor {
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("cursor", |_, editor| {
Expand Down Expand Up @@ -784,78 +790,95 @@ impl LuaUserData for Editor {
editor.feedback = Feedback::Error(err.to_string());
}
}
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("remove", |_, editor, ()| {
if let Err(err) = editor.backspace() {
editor.feedback = Feedback::Error(err.to_string());
}
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("insert_line", |_, editor, ()| {
if let Err(err) = editor.enter() {
editor.feedback = Feedback::Error(err.to_string());
}
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("remove_line", |_, editor, ()| {
if let Err(err) = editor.delete_line() {
editor.feedback = Feedback::Error(err.to_string());
}
update_highlighter(editor);
Ok(())
});
// Cursor moving
methods.add_method_mut("move_to", |_, editor, (x, y): (usize, usize)| {
let y = y.saturating_sub(1);
editor.doc_mut().goto(&Loc{ x, y });
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("move_up", |_, editor, ()| {
editor.up();
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("move_down", |_, editor, ()| {
editor.down();
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("move_left", |_, editor, ()| {
editor.left();
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("move_right", |_, editor, ()| {
editor.right();
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("move_home", |_, editor, ()| {
editor.doc_mut().move_home();
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("move_end", |_, editor, ()| {
editor.doc_mut().move_end();
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("move_page_up", |_, editor, ()| {
editor.doc_mut().move_page_up();
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("move_page_down", |_, editor, ()| {
editor.doc_mut().move_page_down();
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("move_top", |_, editor, ()| {
editor.doc_mut().move_top();
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("move_bottom", |_, editor, ()| {
editor.doc_mut().move_bottom();
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("move_previous_word", |_, editor, ()| {
editor.prev_word();
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("move_next_word", |_, editor, ()| {
editor.next_word();
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("insert_at", |_, editor, (text, x, y): (String, usize, usize)| {
Expand All @@ -868,6 +891,7 @@ impl LuaUserData for Editor {
}
}
editor.doc_mut().goto(&location);
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("remove_at", |_, editor, (x, y): (usize, usize)| {
Expand All @@ -878,6 +902,7 @@ impl LuaUserData for Editor {
editor.feedback = Feedback::Error(err.to_string());
}
editor.doc_mut().goto(&location);
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("insert_line_at", |_, editor, (text, y): (String, usize)| {
Expand All @@ -902,6 +927,7 @@ impl LuaUserData for Editor {
}
}
editor.doc_mut().goto(&location);
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("remove_line_at", |_, editor, y: usize| {
Expand All @@ -912,6 +938,7 @@ impl LuaUserData for Editor {
editor.feedback = Feedback::Error(err.to_string());
}
editor.doc_mut().goto(&location);
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("open_command_line", |_, editor, ()| {
Expand Down Expand Up @@ -973,24 +1000,28 @@ impl LuaUserData for Editor {
if let Err(err) = editor.doc_mut().undo() {
editor.feedback = Feedback::Error(err.to_string());
}
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("redo", |_, editor, ()| {
if let Err(err) = editor.doc_mut().redo() {
editor.feedback = Feedback::Error(err.to_string());
}
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("search", |_, editor, ()| {
if let Err(err) = editor.search() {
editor.feedback = Feedback::Error(err.to_string());
}
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("replace", |_, editor, ()| {
if let Err(err) = editor.replace() {
editor.feedback = Feedback::Error(err.to_string());
}
update_highlighter(editor);
Ok(())
});
methods.add_method("get_character", |_, editor, ()| {
Expand All @@ -1004,7 +1035,8 @@ impl LuaUserData for Editor {
.unwrap_or_else(|| "".to_string());
Ok(ch)
});
methods.add_method("get_character_at", |_, editor, (x, y): (usize, usize)| {
methods.add_method_mut("get_character_at", |_, editor, (x, y): (usize, usize)| {
editor.doc_mut().load_to(y);
let y = y.saturating_sub(1);
let ch = editor.doc()
.line(y)
Expand All @@ -1013,6 +1045,7 @@ impl LuaUserData for Editor {
.nth(x)
.and_then(|ch| Some(ch.to_string()))
.unwrap_or_else(|| "".to_string());
update_highlighter(editor);
Ok(ch)
});
methods.add_method("get_line", |_, editor, ()| {
Expand All @@ -1022,11 +1055,13 @@ impl LuaUserData for Editor {
.unwrap_or_else(|| "".to_string());
Ok(line)
});
methods.add_method("get_line_at", |_, editor, y: usize| {
methods.add_method_mut("get_line_at", |_, editor, y: usize| {
editor.doc_mut().load_to(y);
let y = y.saturating_sub(1);
let line = editor.doc()
.line(y)
.unwrap_or_else(|| "".to_string());
update_highlighter(editor);
Ok(line)
});
methods.add_method_mut("move_to_document", |_, editor, id: usize| {
Expand All @@ -1035,6 +1070,7 @@ impl LuaUserData for Editor {
});
methods.add_method_mut("move_previous_match", |_, editor, query: String| {
editor.prev_match(&query);
update_highlighter(editor);
Ok(())
});
methods.add_method_mut("hide_help_message", |_, editor, ()| {
Expand Down

0 comments on commit a195ab5

Please sign in to comment.