Add basic autocomplete

This commit is contained in:
Austen Adler 2022-07-06 22:11:41 -04:00
parent df610c7e03
commit 296ac24d81
4 changed files with 67 additions and 1 deletions

30
Cargo.lock generated
View File

@ -144,6 +144,8 @@ dependencies = [
"rand", "rand",
"regex", "regex",
"shellwords", "shellwords",
"strum",
"strum_macros",
] ]
[[package]] [[package]]
@ -283,6 +285,12 @@ version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "rustversion"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0a5f7c728f5d284929a1cccb5bc19884422bfe6ef4d6c409da2c41838983fcf"
[[package]] [[package]]
name = "shellwords" name = "shellwords"
version = "1.1.0" version = "1.1.0"
@ -299,6 +307,28 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "strum"
version = "0.24.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f"
dependencies = [
"strum_macros",
]
[[package]]
name = "strum_macros"
version = "0.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4faebde00e8ff94316c01800f9054fd2ba77d30d9e922541913051d1d978918b"
dependencies = [
"heck",
"proc-macro2",
"quote",
"rustversion",
"syn",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.86" version = "1.0.86"

View File

@ -41,6 +41,8 @@ evalexpr = "7"
kakplugin = {path = "./kakplugin/"} kakplugin = {path = "./kakplugin/"}
linked-hash-map = "0.5.4" linked-hash-map = "0.5.4"
linked_hash_set = "0.1.4" linked_hash_set = "0.1.4"
strum_macros = "0.24"
strum = { version = "0.24", features = ["derive"] }
[profile.release] [profile.release]
lto = true lto = true

View File

@ -212,3 +212,24 @@ pub fn get_var(var_name: &str) -> Result<String, KakError> {
} }
}) })
} }
/// Prints a list of shell script candidates for kakoune to ingest
pub fn generate_shell_script_candidates<S>(variants: &[S]) -> Result<(), KakError>
where
S: AsRef<str>,
{
let token_to_complete = get_var("kak_token_to_complete")?.parse::<u8>()?;
match token_to_complete {
0 => {
for v in variants {
println!("{}", v.as_ref());
}
}
1_u8..=u8::MAX => {
// We can't see which command was selected, so none of these will do anything
}
}
Ok(())
}

View File

@ -21,6 +21,8 @@ mod utils;
// mod xargs; // mod xargs;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use kakplugin::{display_message, get_var, KakError}; use kakplugin::{display_message, get_var, KakError};
use std::env;
use strum::VariantNames;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[clap(about, version, author)] #[clap(about, version, author)]
@ -34,7 +36,8 @@ struct Cli {
// kak_response_fifo_name: PathBuf, // kak_response_fifo_name: PathBuf,
} }
#[derive(Subcommand, Debug)] #[derive(Subcommand, Debug, strum::EnumVariantNames)]
#[strum(serialize_all = "kebab_case")]
enum Commands { enum Commands {
#[clap(about = "Sorts selections based on content or content regex match")] #[clap(about = "Sorts selections based on content or content regex match")]
Sort(sort::Options), Sort(sort::Options),
@ -57,6 +60,16 @@ enum Commands {
} }
fn main() { fn main() {
// First, check if we are just getting candidates to run the program. kak_command_fifo is not needed for this
let args = env::args().collect::<Vec<_>>();
if args.len() == 2 && args[1] == "shell-script-candidates" {
match kakplugin::generate_shell_script_candidates(Commands::VARIANTS) {
Err(e) => eprintln!("{e:?}"),
Ok(()) => {}
}
return;
}
if get_var("kak_command_fifo") if get_var("kak_command_fifo")
.and(get_var("kak_response_fifo")) .and(get_var("kak_response_fifo"))
.is_err() .is_err()