Skip to content

Commit

Permalink
feat: node insertion!
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpombrio committed Apr 23, 2024
1 parent a8ce055 commit 34487aa
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 20 deletions.
39 changes: 33 additions & 6 deletions scripts/init.rhai
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
// TODO split into user_init.rhai and system_init.rhai

fn make_candidate_keymap() {
let keymap = new_keymap();
keymap.bind_key("esc", "Exit menu", || s::escape());
keymap.bind_key("up", "Up", || s::menu_selection_up(), false);
keymap.bind_key("down", "Down", || s::menu_selection_down(), false);
keymap.bind_key("bksp", "Backspace", || s::menu_selection_backspace(), false);
keymap
}

fn make_node_selection_keymap(language_name) {
let keymap = make_candidate_keymap();
for construct in s::language_constructs(s::get_language(language_name)) {
keymap.add_regular_candidate(s::construct_name(construct), construct);
}
keymap.bind_key_for_regular_candidates("enter", "Select", |construct| construct);
keymap
}

fn open_file_menu(dir) {
let contents = s::list_files_and_dirs(dir);
let keymap = new_keymap();
Expand Down Expand Up @@ -59,6 +77,12 @@ tree_keymap.bind_key("r", "Redo", || s::redo());
tree_keymap.bind_key("m", "Save bookmark", || s::save_bookmark('a'));
tree_keymap.bind_key("'", "Go to bookmark", || s::goto_bookmark('a'));

tree_keymap.bind_key("i", "Insert", || {
s::open_menu("node_selection");
let construct = s::block();
s::insert_node(construct);
});

// ~~~ Text Keymap ~~~

let text_keymap = new_keymap();
Expand All @@ -70,16 +94,19 @@ text_keymap.bind_key("del", "Delete", || s::text_ed_delete());

// ~~~ File Selection Keymap ~~~

let file_selection_keymap = new_keymap();
let file_selection_keymap = make_candidate_keymap();
file_selection_keymap.bind_key_for_regular_candidates("enter", "Open file", |path| s::open_doc(path));
file_selection_keymap.bind_key("esc", "Exit menu", || s::escape());
file_selection_keymap.bind_key("up", "Up", || s::menu_selection_up(), false);
file_selection_keymap.bind_key("down", "Down", || s::menu_selection_down(), false);
file_selection_keymap.bind_key("bksp", "Backspace", || s::menu_selection_backspace(), false);

// ~~~ Default Layer ~~~

let layer = new_layer("default");
layer.add_menu_keymap("file_selection", file_selection_keymap);

layer.add_mode_keymap("Tree", tree_keymap);
layer.add_mode_keymap("Text", text_keymap);

layer.add_menu_keymap("file_selection", file_selection_keymap);
// TODO: use local layers
layer.add_menu_keymap("node_selection", make_node_selection_keymap("json"));

s::register_layer(layer);
s::add_global_layer("default");
10 changes: 7 additions & 3 deletions src/engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::command::Command;
use super::doc::Doc;
use super::doc_set::{DocDisplayLabel, DocName, DocSet};
use super::Settings;
use crate::language::{LanguageSpec, NotationSetSpec, Storage};
use crate::language::{Language, LanguageSpec, NotationSetSpec, Storage};
use crate::parsing::{Parse, ParseError};
use crate::pretty_doc::DocRef;
use crate::tree::Node;
Expand Down Expand Up @@ -92,6 +92,10 @@ impl Engine {
Ok(notation_name)
}

pub fn get_language(&self, name: &str) -> Result<Language, SynlessError> {
Ok(self.storage.language(name)?)
}

pub fn set_display_notation(
&mut self,
language_name: &str,
Expand Down Expand Up @@ -253,12 +257,12 @@ impl Engine {
* Editing *
***********/

pub fn execute(&mut self, cmd: Command) -> Result<(), SynlessError> {
pub fn execute(&mut self, cmd: impl Into<Command>) -> Result<(), SynlessError> {
let doc = self
.doc_set
.visible_doc_mut()
.ok_or(DocError::NoVisibleDoc)?;
doc.execute(&mut self.storage, cmd, &mut self.clipboard)?;
doc.execute(&mut self.storage, cmd.into(), &mut self.clipboard)?;
Ok(())
}

Expand Down
20 changes: 20 additions & 0 deletions src/language/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ impl Language {
})
}

pub fn constructs(self, s: &Storage) -> impl Iterator<Item = Construct> {
let constructs = &grammar(s, self.language).constructs;
(&constructs).into_iter().map(move |id| Construct {
language: self.language,
construct: id,
})
}

pub fn root_construct(self, s: &Storage) -> Construct {
Construct {
language: self.language,
Expand Down Expand Up @@ -324,3 +332,15 @@ impl FixedSorts {
}
}
}

impl rhai::CustomType for Construct {
fn build(mut builder: rhai::TypeBuilder<Self>) {
builder.with_name("Construct");
}
}

impl rhai::CustomType for Language {
fn build(mut builder: rhai::TypeBuilder<Self>) {
builder.with_name("Language");
}
}
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ pub use engine::{DocName, Engine, Settings};
pub use frontends::Terminal;
pub use keymap::{KeyProg, Keymap, Layer};
pub use language::{
AritySpec, ConstructSpec, GrammarSpec, LanguageSpec, NotationSetSpec, SortSpec, Storage,
AritySpec, Construct, ConstructSpec, GrammarSpec, Language, LanguageSpec, NotationSetSpec,
SortSpec, Storage,
};
pub use pretty_doc::DocRef;
pub use runtime::Runtime;
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ fn make_engine() -> rhai::Engine {
engine.build_type::<synless::Layer>();
engine.build_type::<synless::KeyProg>();
engine.build_type::<synless::SynlessError>();
engine.build_type::<synless::Construct>();
engine.build_type::<synless::Language>();

println!("Signatures:");
engine
Expand All @@ -42,6 +44,7 @@ fn make_runtime() -> Rc<RefCell<Runtime<Terminal>>> {
}

fn run() -> Result<(), Box<rhai::EvalAltResult>> {
// TODO: Log which rhai script failed to compile (instead of simple ?s)
let mut engine = make_engine();

// Load internals_module.rhai
Expand Down
46 changes: 36 additions & 10 deletions src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::engine::{
BookmarkCommand, Command, DocDisplayLabel, DocName, Engine, Settings, TextEdCommand,
TextNavCommand, TreeEdCommand, TreeNavCommand,
BookmarkCommand, DocDisplayLabel, DocName, Engine, Settings, TextEdCommand, TextNavCommand,
TreeEdCommand, TreeNavCommand,
};
use crate::frontends::{Event, Frontend, Key};
use crate::keymap::{KeyLookupResult, KeyProg, Keymap, Layer, LayerManager, MenuSelectionCmd};
use crate::language::{Construct, Language};
use crate::style::Style;
use crate::tree::{Mode, Node};
use crate::util::{error, log, SynlessBug, SynlessError};
Expand Down Expand Up @@ -278,14 +279,25 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
self.engine.load_language_ron(Path::new(path), &ron_string)
}

pub fn get_language(&mut self, language_name: &str) -> Result<Language, SynlessError> {
self.engine.get_language(language_name)
}

pub fn language_constructs(&mut self, language: Language) -> Vec<rhai::Dynamic> {
language
.constructs(&self.engine.raw_storage())
.map(rhai::Dynamic::from)
.collect()
}

pub fn construct_name(&self, construct: Construct) -> String {
construct.name(self.engine.raw_storage()).to_owned()
}

/***********
* Editing *
***********/

pub fn execute(&mut self, cmd: Command) -> Result<(), SynlessError> {
self.engine.execute(cmd)
}

pub fn undo(&mut self) -> Result<(), SynlessError> {
self.engine.undo()
}
Expand All @@ -294,6 +306,11 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
self.engine.redo()
}

pub fn insert_node(&mut self, construct: Construct) -> Result<(), SynlessError> {
let node = Node::new(self.engine.raw_storage_mut(), construct);
self.engine.execute(TreeEdCommand::Insert(node))
}

/***********
* Private *
***********/
Expand Down Expand Up @@ -325,7 +342,7 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
Ok(None)
}
Some(KeyLookupResult::InsertChar(ch)) => {
self.execute(TextEdCommand::Insert(ch).into())?;
self.engine.execute(TextEdCommand::Insert(ch))?;
self.display()?;
Ok(None)
}
Expand Down Expand Up @@ -435,7 +452,7 @@ macro_rules! register {
($module:expr, $runtime:ident, $command:ident :: $variant:ident as $name:ident) => {
let rt = $runtime.clone();
let closure = move || {
rt.borrow_mut().execute($command::$variant.into())
rt.borrow_mut().engine.execute($command::$variant)
.map_err(|err| Box::<rhai::EvalAltResult>::from(err))
};
rhai::FuncRegistration::new(stringify!($name))
Expand All @@ -445,7 +462,7 @@ macro_rules! register {
($module:expr, $runtime:ident, $command:ident :: $variant:ident ($( $param:ident : $type:ty ),*) as $name:ident) => {
let rt = $runtime.clone();
let closure = move | $( $param : $type ),* | {
rt.borrow_mut().execute($command::$variant( $( $param ),* ).into())
rt.borrow_mut().engine.execute($command::$variant( $( $param ),* ))
.map_err(|err| Box::<rhai::EvalAltResult>::from(err))
};
rhai::FuncRegistration::new(stringify!($name))
Expand Down Expand Up @@ -486,8 +503,11 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {

// Languages
register!(module, rt.load_language(path: &str)?);
register!(module, rt.get_language(language_name: &str)?);
register!(module, rt.language_constructs(language: Language));
register!(module, rt.construct_name(construct: Construct));

// Editing
// Editing: Tree Nav
register!(module, rt, TreeNavCommand::Prev as tree_nav_prev);
register!(module, rt, TreeNavCommand::First as tree_nav_first);
register!(module, rt, TreeNavCommand::Next as tree_nav_next);
Expand All @@ -506,22 +526,28 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
register!(module, rt, TreeNavCommand::Parent as tree_nav_parent);
register!(module, rt, TreeNavCommand::EnterText as tree_nav_enter_text);

// Editing: Tree Ed
register!(module, rt, TreeEdCommand::Backspace as tree_ed_backspace);
register!(module, rt, TreeEdCommand::Delete as tree_ed_delete);
register!(module, rt.insert_node(construct: Construct)?);

// Editing: Text Nav
register!(module, rt, TextNavCommand::Left as text_nav_left);
register!(module, rt, TextNavCommand::Right as text_nav_right);
register!(module, rt, TextNavCommand::Beginning as text_nav_beginning);
register!(module, rt, TextNavCommand::End as text_nav_end);
register!(module, rt, TextNavCommand::ExitText as text_nav_exit);

// Editing: Text Ed
register!(module, rt, TextEdCommand::Backspace as text_ed_backspace);
register!(module, rt, TextEdCommand::Delete as text_ed_delete);
register!(module, rt, TextEdCommand::Insert(ch: char) as text_ed_insert);

// Editing: Bookmark
register!(module, rt, BookmarkCommand::Save(ch: char) as save_bookmark);
register!(module, rt, BookmarkCommand::Goto(ch: char) as goto_bookmark);

// Editing: Meta
register!(module, rt.undo()?);
register!(module, rt.redo()?);

Expand Down

0 comments on commit 34487aa

Please sign in to comment.