Skip to content

Commit

Permalink
refactor: address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
e-matteson committed Apr 19, 2024
1 parent c53cb28 commit 9218e14
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 31 deletions.
2 changes: 1 addition & 1 deletion scripts/base_module.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn block() {
};
if keyprog.close_menu {
s::close_menu();
return keyprog.prog.call();
return call(keyprog.prog);
}
call(keyprog.prog);
}
Expand Down
10 changes: 7 additions & 3 deletions scripts/init.rhai
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// TODO split into user_init.rhai and system_init.rhai

fn open_file_menu(dir) {
let contents = s::list_files_and_dirs(dir);
let keymap = new_keymap();
Expand All @@ -11,6 +13,10 @@ fn open_file_menu(dir) {
};
keymap.bind_key_for_special_candidate("enter", child_dir, "View directory", prog, false);
}
keymap.bind_key_for_special_candidate("enter", "..", "View directory", || {
open_file_menu(dir + "/..");
} , false);

s::open_menu("file_selection", keymap);
}

Expand All @@ -28,9 +34,7 @@ keymap.bind_key("o", "Open", || {
}, true);

let file_selection_keymap = new_keymap();
file_selection_keymap.bind_key_for_regular_candidates("enter", "Open file", |file_path| {
s::open_doc(file_path);
}, true);
file_selection_keymap.bind_key_for_regular_candidates("enter", "Open file", |path| s::open_doc(path), true);

let layer = new_layer("default");
layer.add_menu_keymap("file_selection", file_selection_keymap);
Expand Down
7 changes: 2 additions & 5 deletions src/engine/doc_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl fmt::Display for DocName {

#[derive(Debug)]
pub struct DocSet {
// TODO: consider more efficient ways to store docs in DocSet
docs: HashMap<DocName, Doc>,
visible_doc: Option<DocName>,
}
Expand All @@ -98,11 +99,7 @@ impl DocSet {

#[must_use]
pub fn delete_doc(&mut self, doc_name: &DocName) -> bool {
if let Some(index) = self.docs.remove(doc_name) {
true
} else {
false
}
self.docs.remove(doc_name).is_some()
}

#[must_use]
Expand Down
2 changes: 1 addition & 1 deletion src/engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl Engine {
Ok(())
}

pub fn add_doc(&mut self, root_node: Node, doc_name: &DocName) -> Result<(), SynlessError> {
pub fn add_doc(&mut self, doc_name: &DocName, root_node: Node) -> Result<(), SynlessError> {
let doc = Doc::new(&self.storage, root_node).ok_or(DocError::InvalidRootNode)?;
if !self.doc_set.add_doc(doc_name.to_owned(), doc) {
Err(DocError::DocAlreadyOpen(doc_name.to_owned()))?;
Expand Down
7 changes: 1 addition & 6 deletions src/keymap/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,7 @@ impl rhai::CustomType for Keymap {
Ok(())
},
)
.with_fn(
"add_regular_candidate",
|keymap: &mut Keymap, display: String, value: rhai::Dynamic| {
keymap.add_regular_candidate(display, value)
},
)
.with_fn("add_regular_candidate", Keymap::add_regular_candidate)
.with_fn(
"add_regular_candidate",
|keymap: &mut Keymap, value: rhai::Dynamic| {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ fn display_error(error: Box<rhai::EvalAltResult>) {
log!(Error, "Uncaught error in main: {error}");
}

// TODO: catch panics and print them after dropping the terminal?
fn main() {
log!(Info, "Synless is starting");
if let Err(err) = run() {
Expand Down
36 changes: 21 additions & 15 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::frontends::{Event, Frontend, Key, MouseEvent};
use crate::keymap::{KeyProg, Keymap, Layer, LayerManager};
use crate::language::Construct;
use crate::style::Style;
use crate::tree::Mode;
use crate::tree::{Mode, Node};
use crate::util::{error, log, SynlessBug, SynlessError};
use partial_pretty_printer as ppp;
use partial_pretty_printer::pane;
Expand Down Expand Up @@ -34,6 +34,7 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
// Magic initialization
engine.add_parser("json", crate::parsing::JsonParser);

// TODO load pane notation from file
let pane_notation = pane::PaneNotation::Vert(vec![
(
pane::PaneSize::Proportional(1),
Expand Down Expand Up @@ -156,30 +157,35 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
}

fn update_auxilliary_docs(&mut self) {
// Candidate Selection
let selection_doc_name = DocName::Auxilliary(CANDIDATE_SELECTION_DOC_NAME.to_owned());
self.engine.delete_doc(&selection_doc_name);
for (name, node) in [self.make_keyhint_doc(), self.make_candidate_selection_doc()] {
let _ = self.engine.delete_doc(&name);
if let Some(node) = node {
self.engine.add_doc(&name, node).bug();
}
}
}

fn make_candidate_selection_doc(&mut self) -> (DocName, Option<Node>) {
let storage = self.engine.raw_storage_mut();
if let Some(node) = self.layers.make_candidate_selection_doc(storage) {
self.engine.add_doc(node, &selection_doc_name).bug();
};
let node = self.layers.make_candidate_selection_doc(storage);
(
DocName::Auxilliary(CANDIDATE_SELECTION_DOC_NAME.to_owned()),
node,
)
}

// Keyhints
let keyhints_doc_name = DocName::Auxilliary(KEYHINTS_DOC_NAME.to_owned());
self.engine.delete_doc(&keyhints_doc_name);
fn make_keyhint_doc(&mut self) -> (DocName, Option<Node>) {
let visible_doc_name = self.engine.visible_doc_name().cloned();
let mode = if let Some(doc) = self.engine.visible_doc() {
doc.mode()
} else {
Mode::Tree
};
let storage = self.engine.raw_storage_mut();
if let Some(node) = self
let node = self
.layers
.make_keyhint_doc(storage, mode, visible_doc_name.as_ref())
{
self.engine.add_doc(node, &keyhints_doc_name).bug();
};
.make_keyhint_doc(storage, mode, visible_doc_name.as_ref());
(DocName::Auxilliary(KEYHINTS_DOC_NAME.to_owned()), node)
}

/******************
Expand Down

0 comments on commit 9218e14

Please sign in to comment.