summaryrefslogtreecommitdiffstats
path: root/src/command
diff options
context:
space:
mode:
authorCanop <cano.petrole@gmail.com>2022-06-30 20:39:34 +0200
committerCanop <cano.petrole@gmail.com>2022-06-30 20:39:34 +0200
commit60c29ed5571ab3def5869af31e9410e58863f76f (patch)
tree204a80e9f946d8c06abda029b66dc83e0b60ae92 /src/command
parent03f5270cd3bf6a9a126d57e3dce1d1dcc20589d6 (diff)
add the set_syntax_theme verb
which isn't useful right now as the theme isn't persisted. It's an addition for a future use.
Diffstat (limited to 'src/command')
-rw-r--r--src/command/command.rs10
-rw-r--r--src/command/completion.rs53
2 files changed, 58 insertions, 5 deletions
diff --git a/src/command/command.rs b/src/command/command.rs
index 176165b..3785ef6 100644
--- a/src/command/command.rs
+++ b/src/command/command.rs
@@ -59,6 +59,16 @@ impl Command {
Command::None
}
+ pub fn as_verb_invocation(&self) -> Option<&VerbInvocation> {
+ match self {
+ Self::VerbEdit(vi) => Some(vi),
+ Self::VerbInvocate(vi) => Some(vi),
+ Self::Internal { input_invocation, .. } => input_invocation.as_ref(),
+ Self::VerbTrigger { input_invocation, .. } => input_invocation.as_ref(),
+ _ => None,
+ }
+ }
+
/// build a command from the parsed string representation
///
/// The command being finished is the difference between
diff --git a/src/command/completion.rs b/src/command/completion.rs
index 11bdcee..0c2b91c 100644
--- a/src/command/completion.rs
+++ b/src/command/completion.rs
@@ -6,7 +6,11 @@ use {
SelInfo,
},
path::{self, PathAnchor},
- verb::PrefixSearchResult,
+ syntactic::SYNTAX_THEMES,
+ verb::{
+ ArgDef,
+ PrefixSearchResult,
+ },
},
lazy_regex::regex_captures,
std::{
@@ -85,7 +89,7 @@ impl Completions {
con: &AppContext,
sel_info: SelInfo<'_>,
) -> Self {
- match con.verb_store.search_sel_info(start, &sel_info) {
+ match con.verb_store.search_sel_info(start, sel_info) {
PrefixSearchResult::NoMatch => Self::None,
PrefixSearchResult::Match(name, _) => {
if start.len() >= name.len() {
@@ -106,7 +110,7 @@ impl Completions {
verb_name: &str,
arg: &str,
path: &Path,
- sel_info: &SelInfo<'_>,
+ sel_info: SelInfo<'_>,
con: &AppContext,
) -> io::Result<Vec<String>> {
let anchor = match con.verb_store.search_sel_info(verb_name, sel_info) {
@@ -138,12 +142,50 @@ impl Completions {
Ok(children)
}
+
+ /// we have a verb, we try to complete one of the args
fn for_arg(
verb_name: &str,
arg: &str,
con: &AppContext,
sel_info: SelInfo<'_>,
) -> Self {
+ if arg.contains(' ') {
+ return Self::None;
+ }
+ // we try to get the type of argument
+ let arg_def = con
+ .verb_store
+ .search_sel_info_unique(verb_name, sel_info)
+ .and_then(|verb| verb.invocation_parser.as_ref())
+ .and_then(|invocation_parser| invocation_parser.get_unique_arg_def());
+ if matches!(arg_def, Some(ArgDef::Theme)) {
+ Self::for_theme_arg(arg)
+ } else {
+ Self::for_path_arg(verb_name, arg, con, sel_info)
+ }
+ }
+
+ /// we have a verb and it asks for a theme
+ fn for_theme_arg(
+ arg: &str,
+ ) -> Self {
+ let arg = arg.to_lowercase();
+ let completions: Vec<String> = SYNTAX_THEMES
+ .iter()
+ .map(|st| st.name().to_lowercase())
+ .filter_map(|name| name.strip_prefix(&arg).map(|s| s.to_string()))
+ .collect();
+ Self::from_list(completions)
+ }
+
+ /// we have a verb and it asks for a path
+ fn for_path_arg(
+ verb_name: &str,
+ arg: &str,
+ con: &AppContext,
+ sel_info: SelInfo<'_>,
+ ) -> Self {
// in the future we might offer completion of other types
// of arguments, maybe user supplied, but there's no use case
// now so we'll just assume the user wants to complete a path.
@@ -153,7 +195,7 @@ impl Completions {
match &sel_info {
SelInfo::None => Self::None,
SelInfo::One(sel) => {
- match Self::list_for_path(verb_name, arg, sel.path, &sel_info, con) {
+ match Self::list_for_path(verb_name, arg, sel.path, sel_info, con) {
Ok(list) => Self::from_list(list),
Err(e) => {
warn!("Error while trying to complete path: {:?}", e);
@@ -171,7 +213,7 @@ impl Completions {
verb_name,
arg,
path,
- &sel_info,
+ sel_info,
con
).ok()
});
@@ -199,6 +241,7 @@ impl Completions {
con: &AppContext,
sel_info: SelInfo<'_>,
) -> Self {
+ info!("Looking for completions");
match &parts.verb_invocation {
Some(invocation) if !invocation.is_empty() => {
match &invocation.args {