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

feat: generate shell completion #525

Merged
merged 2 commits into from
Aug 12, 2024
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: 14 additions & 4 deletions crates/tinymist/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ pub struct CliArguments {

#[derive(Debug, Clone, clap::Subcommand)]
pub enum Commands {
/// Run Language Server
/// Generates completion script to stdout
Completion(ShellCompletionArgs),
/// Runs language server
Lsp(LspArgs),
/// Run Language Server for tracing some typst program.
/// Runs language server for tracing some typst program.
#[clap(hide(true))]
TraceLsp(CompileArgs),
/// Run Preview Server
/// Runs preview server
#[cfg(feature = "preview")]
Preview(tinymist::tool::preview::PreviewCliArgs),
/// Probe
/// Probes existence (Nop run)
Probe,
}

Expand All @@ -31,6 +33,14 @@ impl Default for Commands {
}
}

#[derive(Debug, Clone, clap::Parser)]
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>,
}

#[derive(Debug, Clone, Default, clap::Parser)]
pub struct CompileArgs {
#[clap(long, default_value = "false")]
Expand Down
21 changes: 18 additions & 3 deletions crates/tinymist/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

mod args;

use std::{path::PathBuf, sync::Arc};
use std::{io, path::PathBuf, sync::Arc};

use anyhow::bail;
use clap::Parser;
use clap_builder::CommandFactory;
use clap_complete::{generate, Shell};
use comemo::Prehashed;
use futures::future::MaybeDone;
use lsp_server::RequestId;
Expand All @@ -20,7 +22,7 @@ use typst::{eval::Tracer, foundations::IntoValue, syntax::Span};
use typst_ts_compiler::{CompileEnv, Compiler, TaskInputs};
use typst_ts_core::{typst::prelude::EcoVec, TypstDict};

use crate::args::{CliArguments, Commands, CompileArgs, LspArgs};
use crate::args::*;

#[cfg(feature = "dhat-heap")]
#[global_allocator]
Expand Down Expand Up @@ -67,6 +69,7 @@ fn main() -> anyhow::Result<()> {
let args = CliArguments::parse();

match args.command.unwrap_or_default() {
Commands::Completion(args) => completion(args),
Commands::Lsp(args) => lsp_main(args),
Commands::TraceLsp(args) => trace_main(args),
#[cfg(feature = "preview")]
Expand All @@ -80,6 +83,18 @@ fn main() -> anyhow::Result<()> {
}
}

/// Generates completion script to stdout.
pub fn completion(args: ShellCompletionArgs) -> anyhow::Result<()> {
let Some(shell) = args.shell.or_else(Shell::from_env) else {
anyhow::bail!("could not infer shell");
};

let mut cmd = CliArguments::command();
generate(shell, &mut cmd, "tinymist", &mut io::stdout());

Ok(())
}

/// The main entry point for the LSP server.
pub fn lsp_main(args: LspArgs) -> anyhow::Result<()> {
log::info!("starting LSP server: {:#?}", args);
Expand Down Expand Up @@ -107,7 +122,7 @@ pub fn lsp_main(args: LspArgs) -> anyhow::Result<()> {
pub fn trace_main(args: CompileArgs) -> anyhow::Result<()> {
let mut input = PathBuf::from(match args.compile.input {
Some(value) => value,
None => return Err(anyhow::Error::msg("Provide a valid path")),
None => return Err(anyhow::anyhow!("provide a valid path")),
});

let mut root_path = args.compile.root.unwrap_or(PathBuf::from("."));
Expand Down
Loading