Skip to content

Commit

Permalink
feat: show menu name in bar.
Browse files Browse the repository at this point in the history
- Disable cursor highlighting in Aux&Metadata docs, for realsies.
- Construct "String" docs that consist of just a single string.
- Add a menu name doc and show it in the pane notation.
- Use later version of ppp (needed for PaneNotation::style).
  • Loading branch information
justinpombrio committed Jun 2, 2024
1 parent 7289b3e commit b0998fa
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ron = "0.8.1"
[dependencies.partial-pretty-printer]
#path = "../ppp/"
git = "https://github.com/justinpombrio/partial-pretty-printer"
version = "0.8.0"
version = "0.9.0"
features = ["serialization"]
[dependencies.no-nonsense-flamegraphs]
version = "0.2.*"
Expand Down
26 changes: 26 additions & 0 deletions data/string_lang.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
LanguageSpec(
name: "string",
file_extensions: [],
grammar: GrammarSpec(
constructs: [
ConstructSpec(
name: "Root",
arity: Fixed([SortSpec(["String"])]),
),
ConstructSpec(
name: "String",
arity: Texty,
),
],
sorts: [],
root_construct: "Root",
),
default_display_notation: "DefaultDisplay",
default_source_notation: None,
notations: [
NotationSetSpec(
name: "DefaultDisplay",
notations: [("Root", Child(0)), ("String", Text)],
)
]
)
4 changes: 3 additions & 1 deletion scripts/init.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fn make_char_node_selection_keymap(language_name) {
}

fn open_file_menu(dir) {
let dir = s::canonicalize_path(dir);
let contents = s::list_files_and_dirs(dir);
let keymap = new_keymap();
for file in contents.files {
Expand All @@ -52,12 +53,13 @@ fn open_file_menu(dir) {
open_file_menu(dir + "/..");
});

s::open_menu("file_selection", keymap);
s::open_menu("file_selection", `Open file in ${dir}`, keymap);
}

s::load_language("data/keyhints_lang.ron");
s::load_language("data/selection_lang.ron");
s::load_language("data/json_lang.ron");
s::load_language("data/string_lang.ron");

s::open_doc("data/pokemon.json");

Expand Down
18 changes: 14 additions & 4 deletions src/engine/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,22 @@ impl Doc {
})
}

pub fn doc_ref_source<'d>(&self, s: &'d Storage) -> DocRef<'d> {
DocRef::new_source(s, self.cursor, self.cursor.root_node(s))
pub fn doc_ref_source<'d>(&self, s: &'d Storage, highlight_cursor: bool) -> DocRef<'d> {
let opt_cursor = if highlight_cursor {
Some(self.cursor)
} else {
None
};
DocRef::new_source(s, opt_cursor, self.cursor.root_node(s))
}

pub fn doc_ref_display<'d>(&self, s: &'d Storage) -> DocRef<'d> {
DocRef::new_display(s, self.cursor, self.cursor.root_node(s))
pub fn doc_ref_display<'d>(&self, s: &'d Storage, highlight_cursor: bool) -> DocRef<'d> {
let opt_cursor = if highlight_cursor {
Some(self.cursor)
} else {
None
};
DocRef::new_display(s, opt_cursor, self.cursor.root_node(s))
}

pub fn cursor(&self) -> Location {
Expand Down
10 changes: 5 additions & 5 deletions src/engine/doc_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl DocSet {
set_focus: false,
};

let (doc, opts) = match label {
let (doc, opts, highlight_cursor) = match label {
DocDisplayLabel::Visible => {
let doc = self.get_doc(self.visible_doc_name()?)?;
let (focus_path, focus_target) = doc.cursor().path_from_root(s);
Expand All @@ -158,17 +158,17 @@ impl DocSet {
width_strategy: pane::WidthStrategy::NoMoreThan(settings.max_display_width),
set_focus: doc.cursor().node(s).is_none(),
};
(doc, options)
(doc, options, true)
}
DocDisplayLabel::Metadata(name) => {
let doc = self.get_doc(&DocName::Metadata(name))?;
(doc, meta_and_aux_options)
(doc, meta_and_aux_options, false)
}
DocDisplayLabel::Auxilliary(name) => {
let doc = self.get_doc(&DocName::Auxilliary(name))?;
(doc, meta_and_aux_options)
(doc, meta_and_aux_options, false)
}
};
Some((doc.doc_ref_display(s), opts))
Some((doc.doc_ref_display(s, highlight_cursor), opts))
}
}
15 changes: 14 additions & 1 deletion src/engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use std::collections::HashMap;
use std::error::Error;
use std::path::Path;

const STRING_LANGUAGE_NAME: &str = "string";

#[derive(thiserror::Error, Debug)]
pub enum DocError {
#[error("Did not find doc named '{0}'")]
Expand Down Expand Up @@ -243,7 +245,7 @@ impl Engine {
.doc_set
.get_doc(doc_name)
.ok_or_else(|| DocError::DocNotFound(doc_name.to_owned()))?;
let doc_ref = doc.doc_ref_source(&self.storage);
let doc_ref = doc.doc_ref_source(&self.storage, false);
let source = ppp::pretty_print_to_string(doc_ref, self.settings.max_source_width)?;
Ok(source)
}
Expand All @@ -253,6 +255,17 @@ impl Engine {
.get_content(&self.storage, label, &self.settings)
}

pub fn make_string_doc(&mut self, string: String) -> Node {
let lang = self
.storage
.language(STRING_LANGUAGE_NAME)
.bug_msg("Missing String lang");
let c_root = lang.root_construct(&self.storage);
let c_string = lang.construct(&self.storage, "String").bug();
let string_node = Node::with_text(&mut self.storage, c_string, string).bug();
Node::with_children(&mut self.storage, c_root, [string_node]).bug()
}

/***********
* Editing *
***********/
Expand Down
11 changes: 8 additions & 3 deletions src/keymap/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,20 @@ impl LayerManager {
&mut self,
doc_name: Option<&DocName>,
menu_name: String,
description: String,
dynamic_keymap: Option<Keymap>,
) -> Result<(), SynlessError> {
let composite_layer = self.composite_layer(doc_name);
let label = KeymapLabel::Menu(menu_name.clone());
let menu = match (dynamic_keymap, composite_layer.keymaps.get(&label)) {
(None, None) => return Err(error!(Keymap, "No keymap for menu '{menu_name}'")),
(Some(keymap), None) => Menu::new(menu_name, keymap),
(Some(keymap), None) => Menu::new(menu_name, description, keymap),
(Some(dyn_keymap), Some(composite_keymap)) => {
let mut keymap = composite_keymap.to_owned();
keymap.append(dyn_keymap);
Menu::new(menu_name, keymap)
Menu::new(menu_name, description, keymap)
}
(None, Some(keymap)) => Menu::new(menu_name, keymap.to_owned()),
(None, Some(keymap)) => Menu::new(menu_name, description, keymap.to_owned()),
};
self.active_menu = Some(menu);
Ok(())
Expand Down Expand Up @@ -244,6 +245,10 @@ impl LayerManager {
self.active_menu.is_some()
}

pub fn menu_description(&self) -> Option<&str> {
self.active_menu.as_ref().map(|menu| menu.description())
}

/*********
* Input *
*********/
Expand Down
8 changes: 7 additions & 1 deletion src/keymap/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub enum MenuSelectionCmd {
/// An open menu. Keeps track of the state of its candidate selection.
pub struct Menu {
name: MenuName,
description: String,
keymap: Keymap,
selection: Option<MenuSelection>,
}
Expand Down Expand Up @@ -136,14 +137,19 @@ impl MenuSelection {
}

impl Menu {
pub fn new(name: MenuName, keymap: Keymap) -> Menu {
pub fn new(name: MenuName, description: String, keymap: Keymap) -> Menu {
Menu {
name,
description,
selection: MenuSelection::new(&keymap),
keymap,
}
}

pub fn description(&self) -> &str {
&self.description
}

#[must_use]
pub fn execute(&mut self, cmd: MenuSelectionCmd) -> bool {
if let Some(selection) = &mut self.selection {
Expand Down
28 changes: 20 additions & 8 deletions src/pretty_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ pub enum PrettyDocError {
#[derive(Clone, Copy)]
pub struct DocRef<'d> {
storage: &'d Storage,
cursor_loc: Location,
cursor_loc: Option<Location>,
node: Node,
use_source_notation: bool,
}

impl<'d> DocRef<'d> {
pub fn new_display(storage: &'d Storage, cursor_loc: Location, node: Node) -> DocRef<'d> {
pub fn new_display(
storage: &'d Storage,
cursor_loc: Option<Location>,
node: Node,
) -> DocRef<'d> {
DocRef {
storage,
cursor_loc,
Expand All @@ -31,7 +35,11 @@ impl<'d> DocRef<'d> {
}
}

pub fn new_source(storage: &'d Storage, cursor_loc: Location, node: Node) -> DocRef<'d> {
pub fn new_source(
storage: &'d Storage,
cursor_loc: Option<Location>,
node: Node,
) -> DocRef<'d> {
DocRef {
storage,
cursor_loc,
Expand Down Expand Up @@ -91,10 +99,14 @@ impl<'d> ppp::PrettyDoc<'d> for DocRef<'d> {
Ok(match style_label {
StyleLabel::Hole => HOLE_STYLE,
StyleLabel::Open => {
let parent = self.cursor_loc.parent_node(self.storage);
let node_at_cursor = self.cursor_loc.node(self.storage);
if parent == Some(self.node) && node_at_cursor.is_none() {
OPEN_STYLE
if let Some(cursor_loc) = self.cursor_loc {
let parent = cursor_loc.parent_node(self.storage);
let node_at_cursor = cursor_loc.node(self.storage);
if parent == Some(self.node) && node_at_cursor.is_none() {
OPEN_STYLE
} else {
Style::default()
}
} else {
Style::default()
}
Expand All @@ -118,7 +130,7 @@ impl<'d> ppp::PrettyDoc<'d> for DocRef<'d> {
}

fn node_style(self) -> Result<Style, Self::Error> {
let style = if self.cursor_loc.node(self.storage) == Some(self.node) {
let style = if self.cursor_loc.and_then(|loc| loc.node(self.storage)) == Some(self.node) {
CURSOR_STYLE
} else {
Style::default()
Expand Down
Loading

0 comments on commit b0998fa

Please sign in to comment.