Skip to content

Commit

Permalink
add GeneralArgs support to lsp command (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdmistman committed Mar 28, 2024
1 parent 7cf4044 commit f7c5279
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
13 changes: 11 additions & 2 deletions crates/taplo-cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -108,8 +108,17 @@ pub struct FormatCommand {
pub stdin_filepath: Option<String>,
}

#[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.
Expand Down
20 changes: 12 additions & 8 deletions crates/taplo-cli/src/commands/lsp.rs
Original file line number Diff line number Diff line change
@@ -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<E: Environment> Taplo<E> {
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
Expand Down

0 comments on commit f7c5279

Please sign in to comment.