Skip to content
Merged
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
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

# Development version

- New `air generate-shell-completion <SHELL>` hidden command that emits a script to stdout that generates shell completions. Supports bash, zsh, fish, powershell, and elvish (#477, @salim-b).

For zsh, run the following to add to your `.zshrc`:

```bash
echo 'eval "$(air generate-shell-completion zsh)"' >> ~/.zshrc
```

For powershell, run the following to add to your profile:

```powershell
if (!(Test-Path -Path $PROFILE)) {
New-Item -ItemType File -Path $PROFILE -Force
}
Add-Content -Path $PROFILE -Value '(& air generate-shell-completion powershell) | Out-String | Invoke-Expression'
```

Then restart your shell and type `air <tab>` to see completions.

# 0.8.2

Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ bytes = "1.8.0"
cargo_metadata = "0.20.0"
case = "1.0.0"
clap = { version = "4.5.20", features = ["derive"] }
clap_complete = "4.5.66"
colored = "3.0.0"
comments = { path = "./crates/comments" }
crates = { path = "./crates/crates" }
Expand Down
1 change: 1 addition & 0 deletions crates/air/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ publish = true
[dependencies]
anyhow = { workspace = true }
clap = { workspace = true, features = ["wrap_help"] }
clap_complete = { workspace = true }
colored = { workspace = true }
crates = { workspace = true }
fs = { workspace = true }
Expand Down
13 changes: 11 additions & 2 deletions crates/air/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ pub(crate) enum Command {

/// Start a language server
LanguageServer(LanguageServerCommand),

/// Generate shell completion scripts
#[clap(hide = true)]
GenerateShellCompletion(GenerateShellCompletionCommand),
Comment thread
salim-b marked this conversation as resolved.
}

#[derive(Clone, Debug, Parser)]
Expand All @@ -55,12 +59,17 @@ pub(crate) struct FormatCommand {
#[derive(Clone, Debug, Parser)]
pub(crate) struct LanguageServerCommand {}

#[derive(Clone, Debug, Parser)]
pub(crate) struct GenerateShellCompletionCommand {
/// The shell for which to generate the completion script
pub shell: clap_complete::Shell,
}

/// All configuration options that can be passed "globally"
#[derive(Debug, Default, clap::Args)]
#[command(next_help_heading = "Global options")]
pub(crate) struct GlobalOptions {
/// The log level. One of: `error`, `warn`, `info`, `debug`, or `trace`. Defaults
/// to `warn`.
/// The log level [default: warn]
Comment thread
salim-b marked this conversation as resolved.
#[arg(long, global = true)]
pub(crate) log_level: Option<logging::LogLevel>,

Expand Down
1 change: 1 addition & 0 deletions crates/air/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub(crate) mod format;
pub(crate) mod generate_shell_completion;
pub(crate) mod language_server;
15 changes: 15 additions & 0 deletions crates/air/src/commands/generate_shell_completion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::args::{Args, GenerateShellCompletionCommand};
use crate::status::ExitStatus;
use clap::CommandFactory;

pub(crate) fn generate_shell_completion(
command: GenerateShellCompletionCommand,
) -> anyhow::Result<ExitStatus> {
clap_complete::generate(
command.shell,
&mut Args::command(),
"air",
&mut std::io::stdout(),
);
Ok(ExitStatus::Success)
}
3 changes: 3 additions & 0 deletions crates/air/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub fn run(args: Args) -> anyhow::Result<ExitStatus> {
}

match args.command {
Command::GenerateShellCompletion(command) => {
commands::generate_shell_completion::generate_shell_completion(command)
}
Command::Format(command) => commands::format::format(command),
Command::LanguageServer(command) => commands::language_server::language_server(command),
}
Expand Down
2 changes: 1 addition & 1 deletion crates/air/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn turn_off_colors(
layer.with_ansi(false)
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum)]
pub(crate) enum LogLevel {
Error,
#[default]
Expand Down
70 changes: 70 additions & 0 deletions crates/air/tests/integration/generate_shell_completion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use std::process::Command;

use crate::helpers::CommandExt;
use crate::helpers::binary_path;

#[test]
fn test_completions_help() {
insta::assert_snapshot!(
Command::new(binary_path())
.arg("generate-shell-completion")
.arg("--help")
.run()
.normalize_os_executable_name()
);
}

#[test]
fn test_completions_bash() {
insta::assert_snapshot!(
Command::new(binary_path())
.arg("generate-shell-completion")
.arg("bash")
.run()
.normalize_os_executable_name()
);
}

#[test]
fn test_completions_elvish() {
insta::assert_snapshot!(
Command::new(binary_path())
.arg("generate-shell-completion")
.arg("elvish")
.run()
.normalize_os_executable_name()
);
}

#[test]
fn test_completions_fish() {
insta::assert_snapshot!(
Command::new(binary_path())
.arg("generate-shell-completion")
.arg("fish")
.run()
.normalize_os_executable_name()
);
}

#[test]
fn test_completions_powershell() {
insta::assert_snapshot!(
Command::new(binary_path())
.arg("generate-shell-completion")
.arg("powershell")
.run()
.normalize_os_executable_name()
);
}

#[test]
fn test_completions_zsh() {
insta::assert_snapshot!(
Command::new(binary_path())
.arg("generate-shell-completion")
.arg("zsh")
.run()
.normalize_os_executable_name()
);
}
1 change: 1 addition & 0 deletions crates/air/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
/// - Compilation times, by only having 1 integration test binary
/// - Dead code analysis of integration test helpers https://github.com/rust-lang/rust/issues/46379
mod format;
mod generate_shell_completion;
mod help;
mod helpers;
Loading
Loading