feat: add shell completions script for Fig and Nushell (#578)

This commit is contained in:
Tim Voßhenrich 2024-09-06 01:18:33 +02:00 committed by GitHub
parent b8d933615d
commit 5b1d5ce331
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 87 additions and 2 deletions

View file

@ -40,6 +40,7 @@ clap.workspace = true
clap_builder.workspace = true
clap_complete.workspace = true
clap_complete_fig.workspace = true
clap_complete_nushell.workspace = true
clap_mangen.workspace = true
typst.workspace = true

View file

@ -1,3 +1,5 @@
use std::path::Path;
use once_cell::sync::Lazy;
use sync_lsp::transport::MirrorArgs;
@ -38,7 +40,77 @@ pub struct ShellCompletionArgs {
/// The shell to generate the completion script for. If not provided, it
/// will be inferred from the environment.
#[clap(value_enum)]
pub shell: Option<clap_complete::Shell>,
pub shell: Option<Shell>,
}
#[allow(clippy::enum_variant_names)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, clap::ValueEnum)]
#[clap(rename_all = "lowercase")]
pub enum Shell {
Bash,
Elvish,
Fig,
Fish,
PowerShell,
Zsh,
Nushell,
}
impl Shell {
pub fn from_env() -> Option<Self> {
if let Some(env_shell) = std::env::var_os("SHELL") {
let name = Path::new(&env_shell).file_stem()?.to_str()?;
match name {
"bash" => Some(Shell::Bash),
"zsh" => Some(Shell::Zsh),
"fig" => Some(Shell::Fig),
"fish" => Some(Shell::Fish),
"elvish" => Some(Shell::Elvish),
"powershell" | "powershell_ise" => Some(Shell::PowerShell),
"nushell" => Some(Shell::Nushell),
_ => None,
}
} else if cfg!(windows) {
Some(Shell::PowerShell)
} else {
None
}
}
}
impl clap_complete::Generator for Shell {
fn file_name(&self, name: &str) -> String {
use clap_complete::shells::{Bash, Elvish, Fish, PowerShell, Zsh};
use clap_complete_fig::Fig;
use clap_complete_nushell::Nushell;
match self {
Shell::Bash => Bash.file_name(name),
Shell::Elvish => Elvish.file_name(name),
Shell::Fig => Fig.file_name(name),
Shell::Fish => Fish.file_name(name),
Shell::PowerShell => PowerShell.file_name(name),
Shell::Zsh => Zsh.file_name(name),
Shell::Nushell => Nushell.file_name(name),
}
}
fn generate(&self, cmd: &clap::Command, buf: &mut dyn std::io::Write) {
use clap_complete::shells::{Bash, Elvish, Fish, PowerShell, Zsh};
use clap_complete_fig::Fig;
use clap_complete_nushell::Nushell;
match self {
Shell::Bash => Bash.generate(cmd, buf),
Shell::Elvish => Elvish.generate(cmd, buf),
Shell::Fig => Fig.generate(cmd, buf),
Shell::Fish => Fish.generate(cmd, buf),
Shell::PowerShell => PowerShell.generate(cmd, buf),
Shell::Zsh => Zsh.generate(cmd, buf),
Shell::Nushell => Nushell.generate(cmd, buf),
}
}
}
#[derive(Debug, Clone, Default, clap::Parser)]

View file

@ -7,7 +7,7 @@ use std::{io, path::PathBuf, sync::Arc};
use anyhow::bail;
use clap::Parser;
use clap_builder::CommandFactory;
use clap_complete::{generate, Shell};
use clap_complete::generate;
use comemo::Prehashed;
use futures::future::MaybeDone;
use lsp_server::RequestId;