Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate shell completion scripts #36

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ pest_meta = "2.5"
text-utils = "0.2"
toml = "0.5"
clap = { version = "4.4.15", features = ["derive"] }
clap_complete = "4.5.42"

[dev-dependencies]
criterion = "0.4.0"
indoc = "2"
pretty_assertions = "1.3.0"
quote = "1.0"

[build-dependencies]
clap = { version = "4.4.15", features = ["derive"] }
clap_complete = "4.5.42"

[[bin]]
name = "pestfmt"
path = "src/main.rs"
Expand Down
17 changes: 17 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
include!("src/cli.rs");

fn main() {
use std::{env, fs};

use clap::{CommandFactory, ValueEnum};
use clap_complete::{Shell, generate_to};

let out_dir = env::var("SHELL_COMPLETIONS_DIR").or_else(|_| env::var("OUT_DIR")).unwrap();
ice1000 marked this conversation as resolved.
Show resolved Hide resolved

fs::create_dir_all(&out_dir).unwrap();
ice1000 marked this conversation as resolved.
Show resolved Hide resolved

let mut cmd = Args::command();
for &shell in Shell::value_variants() {
generate_to(shell, &mut cmd, "pestfmt", &out_dir).unwrap();
}
}
23 changes: 23 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use clap::{CommandFactory, Parser};
use clap_complete::{Generator, Shell, generate};

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
/// The file or path to format
#[arg(default_value = ".")]
pub file: Vec<String>,

#[clap(long, short, default_value = "false")]
pub stdin: bool,

/// Generate shell completion script
#[arg(long, value_enum)]
pub gen_completion: Option<Shell>,
}

pub fn print_completions<G: Generator>(gen: G) {
let mut cmd = Args::command();

generate(gen, &mut cmd, "pestfmt", &mut std::io::stdout());
}
20 changes: 7 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
use ignore::{overrides::OverrideBuilder, WalkBuilder};
mod cli;

use ignore::{WalkBuilder, overrides::OverrideBuilder};
use pest_fmt::{Formatter, PestResult};
use std::{error::Error, fs, io::Read, path::Path};
use toml::Value;

use clap::Parser;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Cli {
/// The file or path to format
#[arg(default_value = ".")]
file: Vec<String>,
#[clap(long, short, default_value = "false")]
stdin: bool,
}

fn main() -> Result<(), Box<dyn Error>> {
let cli = Cli::parse();
let cli = cli::Args::parse();

if cli.stdin {
if let Some(generator) = cli.gen_completion {
cli::print_completions(generator);
} else if cli.stdin {
Comment on lines +11 to +15
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure consistent error handling for completion generation.

The completion generation branch doesn't propagate potential I/O errors, unlike other branches that use the ? operator. This could lead to silent failures when generating completions.

     let cli = cli::Args::parse();
 
     if let Some(generator) = cli.gen_completion {
-        cli::print_completions(generator);
+        cli::print_completions(generator)?;
     } else if cli.stdin {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let cli = cli::Args::parse();
if cli.stdin {
if let Some(generator) = cli.gen_completion {
cli::print_completions(generator);
} else if cli.stdin {
let cli = cli::Args::parse();
if let Some(generator) = cli.gen_completion {
cli::print_completions(generator)?;
} else if cli.stdin {

let mut source = String::new();
std::io::stdin().read_to_string(&mut source).expect("failed read source from stdin");
println!("{}", format(&source).unwrap_or_else(|e| panic!("failed to format: {:?}", e)));
Expand Down