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

add GeneralArgs support to lsp command #510

Merged
merged 2 commits into from
Mar 28, 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
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
Loading