From f7c5279af987c2ef02c8931618295bb3e5b52dec Mon Sep 17 00:00:00 2001 From: Colton Donnelly Date: Wed, 27 Mar 2024 20:50:59 -0400 Subject: [PATCH] add GeneralArgs support to lsp command (#510) --- crates/taplo-cli/src/args.rs | 13 +++++++++++-- crates/taplo-cli/src/commands/lsp.rs | 20 ++++++++++++-------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/crates/taplo-cli/src/args.rs b/crates/taplo-cli/src/args.rs index 9a1c5ae8b..d563bdbec 100644 --- a/crates/taplo-cli/src/args.rs +++ b/crates/taplo-cli/src/args.rs @@ -56,7 +56,7 @@ pub enum TaploCommand { Format(FormatCommand), /// Language server operations. Lsp { - #[clap(subcommand)] + #[clap(flatten)] cmd: LspCommand, }, /// Operations with the Taplo config file. @@ -108,8 +108,17 @@ pub struct FormatCommand { pub stdin_filepath: Option, } +#[derive(Clone, Args)] +pub struct LspCommand { + #[clap(flatten)] + pub general: GeneralArgs, + + #[clap(subcommand)] + pub io: LspCommandIo, +} + #[derive(Clone, Subcommand)] -pub enum LspCommand { +pub enum LspCommandIo { /// Run the language server and listen on a TCP address. Tcp { /// The address to listen on. diff --git a/crates/taplo-cli/src/commands/lsp.rs b/crates/taplo-cli/src/commands/lsp.rs index f981f49de..1b7d8f38c 100644 --- a/crates/taplo-cli/src/commands/lsp.rs +++ b/crates/taplo-cli/src/commands/lsp.rs @@ -1,22 +1,26 @@ -use std::sync::Arc; - use taplo_common::environment::{native::NativeEnvironment, Environment}; -use crate::{args::LspCommand, default_config, Taplo}; +use crate::{args::{LspCommand, LspCommandIo}, Taplo}; impl Taplo { - pub async fn execute_lsp(&self, cmd: LspCommand) -> Result<(), anyhow::Error> { + pub async fn execute_lsp(&mut self, cmd: LspCommand) -> Result<(), anyhow::Error> { + self.schemas + .cache() + .set_cache_path(cmd.general.cache_path.clone()); + + let config = self.load_config(&cmd.general).await?; + let server = taplo_lsp::create_server(); let world = taplo_lsp::create_world(NativeEnvironment::new()); - world.set_default_config(Arc::new(default_config())); + world.set_default_config(config); - match cmd { - LspCommand::Tcp { address } => { + match cmd.io { + LspCommandIo::Tcp { address } => { server .listen_tcp(world, &address, async_ctrlc::CtrlC::new().unwrap()) .await } - LspCommand::Stdio {} => { + LspCommandIo::Stdio {} => { server .listen_stdio(world, async_ctrlc::CtrlC::new().unwrap()) .await