summaryrefslogtreecommitdiffstats
path: root/src/verb
diff options
context:
space:
mode:
authorCanop <cano.petrole@gmail.com>2022-06-19 20:14:12 +0200
committerCanop <cano.petrole@gmail.com>2022-06-19 20:14:12 +0200
commit8ef4ed54cea893b8e77b5e41a349ba77ca56937a (patch)
tree03dbae01285b1ba4272f68a4c09085396f573d0c /src/verb
parent0a34abdf7e1fa4aaaf2da641b3083dd0b015f2da (diff)
[WIP] introduce verb arg definition
Diffstat (limited to 'src/verb')
-rw-r--r--src/verb/arg_def.rs18
-rw-r--r--src/verb/builtin.rs3
-rw-r--r--src/verb/internal.rs2
-rw-r--r--src/verb/invocation_parser.rs61
-rw-r--r--src/verb/mod.rs2
-rw-r--r--src/verb/verb.rs18
6 files changed, 75 insertions, 29 deletions
diff --git a/src/verb/arg_def.rs b/src/verb/arg_def.rs
new file mode 100644
index 0000000..e51d24a
--- /dev/null
+++ b/src/verb/arg_def.rs
@@ -0,0 +1,18 @@
+use {
+ crate::{
+ app::SelectionType,
+ path::PathAnchor,
+ },
+};
+
+/// The definition of an argument given to a verb
+/// as understood from the invocation pattern
+#[derive(Debug, Clone, Copy)]
+pub enum ArgDef {
+ Path {
+ anchor: PathAnchor,
+ selection_type: SelectionType,
+ },
+ Theme,
+ Unspecified,
+}
diff --git a/src/verb/builtin.rs b/src/verb/builtin.rs
index 889c17d..bd28209 100644
--- a/src/verb/builtin.rs
+++ b/src/verb/builtin.rs
@@ -72,6 +72,9 @@ pub fn builtin_verbs() -> Vec<Verb> {
internal(line_down).with_key(key!(down)).with_key(key!('j')),
internal(line_up).with_key(key!(up)).with_key(key!('k')),
+ //
+ internal(set_syntax_theme),
+
// those two operations are mapped on ALT-ENTER, one
// for directories and the other one for the other files
internal(open_leave) // calls the system open
diff --git a/src/verb/internal.rs b/src/verb/internal.rs
index cfbfdf6..7d70e44 100644
--- a/src/verb/internal.rs
+++ b/src/verb/internal.rs
@@ -113,6 +113,7 @@ Internals! {
//restore_pattern: "restore a pattern which was just removed" false,
select_first: "select the first item" false,
select_last: "select the last item" false,
+ set_syntax_theme: "set the theme of code preview" false,
sort_by_count: "sort by count" false,
sort_by_date: "sort by date" false,
sort_by_size: "sort by size" false,
@@ -152,6 +153,7 @@ impl Internal {
Internal::line_up => r"line_up (?P<count>\d*)?",
Internal::line_down_no_cycle => r"line_down_no_cycle (?P<count>\d*)?",
Internal::line_up_no_cycle => r"line_up_no_cycle (?P<count>\d*)?",
+ Internal::set_syntax_theme => r"set_syntax_theme (?P<theme>\S*)?",
_ => self.name(),
}
}
diff --git a/src/verb/invocation_parser.rs b/src/verb/invocation_parser.rs
index 8129c70..b850a63 100644
--- a/src/verb/invocation_parser.rs
+++ b/src/verb/invocation_parser.rs
@@ -28,15 +28,17 @@ pub struct InvocationParser {
/// invocation name's characters are [_0-9a-zA-Z.\[\]])
args_parser: Option<Regex>,
- /// whether the path, when non absolute, should be interpreted
- /// as relative to the closest directory (which may be the selection)
- /// or to the parent of the selection
- pub arg_anchor: PathAnchor,
+ // //// whether the path, when non absolute, should be interpreted
+ // //// as relative to the closest directory (which may be the selection)
+ // //// or to the parent of the selection
+ // /pub arg_anchor: PathAnchor,
- /// contain the type of selection in case there's only one arg
- /// and it's a path (when it's not None, the user can type ctrl-P
- /// to select the argument in another panel)
- pub arg_selection_type: Option<SelectionType>,
+ // //// contain the type of selection in case there's only one arg
+ // //// and it's a path (when it's not None, the user can type ctrl-P
+ // //// to select the argument in another panel)
+ // /pub arg_selection_type: Option<SelectionType>,
+
+ pub arg_defs: Vec<ArgDef>,
}
@@ -47,8 +49,7 @@ impl InvocationParser {
) -> Result<Self, ConfError> {
let invocation_pattern = VerbInvocation::from(invocation_str);
let mut args_parser = None;
- let mut arg_selection_type = None;
- let mut arg_anchor = PathAnchor::Unspecified;
+ let mut arg_defs = Vec::new();
if let Some(args) = &invocation_pattern.args {
let spec = GROUP.replace_all(args, r"(?P<$1>.+)");
let spec = format!("^{}$", spec);
@@ -58,24 +59,36 @@ impl InvocationParser {
return Err(ConfError::InvalidVerbInvocation { invocation: spec });
}
};
- if let Some(group) = GROUP.find(args) {
- if group.start() == 0 && group.end() == args.len() {
- // there's one group, covering the whole args
- arg_selection_type = Some(SelectionType::Any);
- let group_str = group.as_str();
+ for group in GROUP.find_iter(args) {
+ let group_str = group.as_str();
+ arg_defs.push(
if group_str.ends_with("path-from-parent}") {
- arg_anchor = PathAnchor::Parent;
+ ArgDef::Path {
+ anchor: PathAnchor::Parent,
+ selection_type: SelectionType::Any,
+ }
} else if group_str.ends_with("path-from-directory}") {
- arg_anchor = PathAnchor::Directory;
+ ArgDef::Path {
+ anchor: PathAnchor::Directory,
+ selection_type: SelectionType::Any,
+ }
+ } else if group_str.ends_with("path}") {
+ ArgDef::Path {
+ anchor: PathAnchor::Unspecified,
+ selection_type: SelectionType::Any,
+ }
+ } else if group_str.ends_with("theme}") {
+ ArgDef::Theme
+ } else {
+ ArgDef::Unspecified // still probably a path
}
- }
+ );
}
}
Ok(Self {
invocation_pattern,
args_parser,
- arg_anchor,
- arg_selection_type,
+ arg_defs,
})
}
@@ -83,6 +96,14 @@ impl InvocationParser {
&self.invocation_pattern.name
}
+ pub fn get_unique_arg_anchor(&self) -> PathAnchor {
+ if self.arg_defs.len() == 1 {
+ if let ArgDef::Path { anchor, .. } = self.arg_defs[0] {
+ return anchor;
+ }
+ }
+ PathAnchor::Unspecified
+ }
/// Assuming the verb has been matched, check whether the arguments
/// are OK according to the regex. Return none when there's no problem
/// and return the error to display if arguments don't match
diff --git a/src/verb/mod.rs b/src/verb/mod.rs
index a0fe0ba..3a835de 100644
--- a/src/verb/mod.rs
+++ b/src/verb/mod.rs
@@ -1,3 +1,4 @@
+mod arg_def;
mod builtin;
mod exec_pattern;
mod execution_builder;
@@ -15,6 +16,7 @@ mod verb_invocation;
mod verb_store;
pub use {
+ arg_def::*,
exec_pattern::*,
execution_builder::ExecutionStringBuilder,
external_execution::ExternalExecution,
diff --git a/src/verb/verb.rs b/src/verb/verb.rs
index d94c293..7d297c6 100644
--- a/src/verb/verb.rs
+++ b/src/verb/verb.rs
@@ -260,18 +260,18 @@ impl Verb {
}
}
- /// in case the verb take only one argument of type path, return
- /// the selection type of this unique argument
- pub fn get_arg_selection_type(&self) -> Option<SelectionType> {
- self.invocation_parser
- .as_ref()
- .and_then(|parser| parser.arg_selection_type)
- }
+ // /// in case the verb take only one argument of type path, return
+ // /// the selection type of this unique argument
+ // pub fn get_arg_selection_type(&self) -> Option<SelectionType> {
+ // self.invocation_parser
+ // .as_ref()
+ // .and_then(|parser| parser.arg_selection_type)
+ // }
- pub fn get_arg_anchor(&self) -> PathAnchor {
+ pub fn get_unique_arg_anchor(&self) -> PathAnchor {
self.invocation_parser
.as_ref()
- .map_or(PathAnchor::Unspecified, |parser| parser.arg_anchor)
+ .map_or(PathAnchor::Unspecified, InvocationParser::get_unique_arg_anchor)
}
pub fn get_internal(&self) -> Option<Internal> {