Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clap-complete): implemented Generator for Clink autocompletions
- Loading branch information
Showing
6 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use crate::{generator::utils, Generator}; | ||
use clap::*; | ||
use std::fmt::Write; | ||
|
||
/// Generate clink completion lua script | ||
#[derive(Copy, Clone, PartialEq, Eq, Debug)] | ||
pub struct Clink; | ||
|
||
impl Generator for Clink { | ||
fn file_name(&self, name: &str) -> String { | ||
format!("{name}.lua") | ||
} | ||
|
||
fn generate(&self, cmd: &clap::Command, buf: &mut dyn std::io::prelude::Write) { | ||
let bin_name = cmd | ||
.get_bin_name() | ||
.expect("crate::generate should have set the bin_name"); | ||
|
||
let result = format!( | ||
r#"clink.argmatcher("{bin_name}") | ||
{}"#, | ||
generate_inner(cmd, 0) | ||
); | ||
w!(buf, result.as_bytes()); | ||
} | ||
} | ||
|
||
fn generate_inner(p: &Command, depth: usize) -> String { | ||
let mut b = String::new(); | ||
let indent = " ".repeat(depth * 4); | ||
|
||
for opt in p.get_opts() { | ||
writeln!(b, "{}:addarg(\"--{}\")", indent, opt.get_id()).unwrap(); | ||
if let Some(help) = opt.get_help() { | ||
writeln!( | ||
b, | ||
"{}:adddescriptions({{\"--{}\", description = \"{}\"}})", | ||
indent, | ||
opt.get_id(), | ||
help | ||
) | ||
.unwrap(); | ||
if let Some(short) = opt.get_short() { | ||
writeln!( | ||
b, | ||
"{}:adddescriptions({{\"-{}\", description = \"{}\"}})", | ||
indent, short, help | ||
) | ||
.unwrap() | ||
} | ||
} | ||
} | ||
|
||
for flag in utils::flags(p) { | ||
if let Some(shorts) = flag.get_short_and_visible_aliases() { | ||
for short in shorts { | ||
writeln!(b, "{}:addflags(\"-{}\")", indent, short).unwrap(); | ||
if let Some(help) = flag.get_help() { | ||
writeln!( | ||
b, | ||
"{}:adddescriptions({{\"-{}\", description = \"{}\"}})", | ||
indent, short, help | ||
) | ||
.unwrap(); | ||
} | ||
} | ||
} | ||
if let Some(longs) = flag.get_long_and_visible_aliases() { | ||
for long in longs { | ||
writeln!(b, "{}:addflags(\"--{}\")", indent, long).unwrap(); | ||
if let Some(help) = flag.get_help() { | ||
writeln!( | ||
b, | ||
"{}:adddescriptions({{\"--{}\", description = \"{}\"}})", | ||
indent, long, help | ||
) | ||
.unwrap(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
for sub_cmd in p.get_subcommands() { | ||
writeln!( | ||
b, | ||
"{}:addarg({{ \"{}\" .. clink.argmatcher()\n{}{}}})", | ||
indent, | ||
sub_cmd.get_name(), | ||
generate_inner(sub_cmd, depth + 1), | ||
indent | ||
) | ||
.unwrap(); | ||
} | ||
|
||
b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
clink.argmatcher("my-app") | ||
:addflags("-c") | ||
:addflags("-v") | ||
:addflags("-h") | ||
:adddescriptions({"-h", description = "Print help"}) | ||
:addflags("--help") | ||
:adddescriptions({"--help", description = "Print help"}) | ||
:addarg({ "test" .. clink.argmatcher() | ||
:addflags("-d") | ||
:addflags("-c") | ||
:addflags("-h") | ||
:adddescriptions({"-h", description = "Print help"}) | ||
:addflags("--help") | ||
:adddescriptions({"--help", description = "Print help"}) | ||
}) | ||
:addarg({ "help" .. clink.argmatcher() | ||
:addarg({ "test" .. clink.argmatcher() | ||
}) | ||
:addarg({ "help" .. clink.argmatcher() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use crate::common; | ||
|
||
#[test] | ||
fn basic() { | ||
let name = "my-app"; | ||
let cmd = common::basic_command(name); | ||
common::assert_matches( | ||
snapbox::file!["../snapshots/basic.lua"], | ||
clap_complete::shells::Clink, | ||
cmd, | ||
name, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ mod fish; | |
mod general; | ||
mod powershell; | ||
mod zsh; | ||
mod clink; |