Skip to content

Commit

Permalink
feat(lsp): handle CLI args for different communication channels
Browse files Browse the repository at this point in the history
Implement handling of the common CLI arguments that define how to
establish the client/server communication channel.

The options are defined, but not all are supported as of yet.
  • Loading branch information
dnaka91 committed Dec 4, 2023
1 parent 5962776 commit 670d9d1
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ all = "deny"
pedantic = "warn"

[workspace.dependencies]
clap = { version = "4.4.10", features = ["derive", "wrap_help"] }
color-eyre = { version = "0.6.2", default-features = false }
glob = "0.3.1"
indoc = "2.0.4"
Expand Down
2 changes: 1 addition & 1 deletion crates/stef-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ name = "stef"
path = "src/main.rs"

[dependencies]
clap = { version = "4.4.10", features = ["derive", "wrap_help"] }
clap.workspace = true
color-eyre.workspace = true
glob.workspace = true
miette = { workspace = true, features = ["fancy-no-backtrace"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/stef-go/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository.workspace = true
license.workspace = true

[dependencies]
clap = { version = "4.4.10", features = ["derive"] }
clap.workspace = true
heck = "0.4.1"
miette = { workspace = true, features = ["fancy-no-backtrace"] }
stef-compiler = { path = "../stef-compiler" }
Expand Down
1 change: 1 addition & 0 deletions crates/stef-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repository.workspace = true
license.workspace = true

[dependencies]
clap.workspace = true
directories = "5.0.1"
ouroboros = "0.18.0"
stef-compiler = { path = "../stef-compiler" }
Expand Down
46 changes: 46 additions & 0 deletions crates/stef-lsp/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::path::PathBuf;

use clap::{Parser, ValueHint};

#[derive(Parser)]
#[command(about, author, version)]
pub struct Cli {
/// Use standard I/O as the communication channel.
#[arg(long, conflicts_with_all = ["pipe", "socket"])]
pub stdio: bool,
/// Use pipes (Windows) or socket files (Linux, Mac) as the communication channel.
#[arg(
long,
conflicts_with_all = ["socket"],
value_name = "FILE",
value_hint = ValueHint::FilePath,
)]
pub pipe: Option<PathBuf>,
/// Use a socket as the communication channel.
#[arg(long, exclusive = true, value_name = "PORT")]
pub socket: Option<u16>,
/// Process ID of the editor that started this server.
///
/// To support the case that the editor starting a server crashes an editor should also pass
/// its process id to the server. This allows the server to monitor the editor process and to
/// shutdown itself if the editor process dies.
#[arg(long = "clientProcessId", value_name = "ID")]
pub client_process_id: Option<i32>,
}

impl Cli {
pub fn parse() -> Self {
<Self as Parser>::parse()
}
}

#[cfg(test)]
mod tests {
use super::Cli;

#[test]
fn verify_cli() {
use clap::CommandFactory;
Cli::command().debug_assert();
}
}
22 changes: 20 additions & 2 deletions crates/stef-lsp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ use tower_lsp::{
use tracing::{debug, Level};
use tracing_subscriber::{filter::Targets, fmt::MakeWriter, prelude::*};

use self::cli::Cli;

mod cli;
mod compile;
mod utf16;

Expand Down Expand Up @@ -120,6 +123,8 @@ impl LanguageServer for Backend {

#[tokio::main]
async fn main() {
let cli = Cli::parse();

let dirs = ProjectDirs::from("rocks", "dnaka91", env!("CARGO_PKG_NAME")).unwrap();

let file_appender = tracing_appender::rolling::daily(dirs.cache_dir(), "log");
Expand Down Expand Up @@ -147,8 +152,21 @@ async fn main() {
}
});

let (stdin, stdout) = (tokio::io::stdin(), tokio::io::stdout());
Server::new(stdin, stdout, socket).serve(service).await;
if cli.stdio {
let (stdin, stdout) = (tokio::io::stdin(), tokio::io::stdout());
Server::new(stdin, stdout, socket).serve(service).await;
} else if let Some(file) = cli.pipe {
let file = tokio::fs::File::options()
.read(true)
.write(true)
.open(file)
.await
.expect("failed to open provided pipe/socket");
let (read, write) = tokio::io::split(file);
Server::new(read, write, socket).serve(service).await;
} else if let Some(port) = cli.socket {
unimplemented!("open TCP connection on port {port}");
}
}

struct ClientLogWriter {
Expand Down

0 comments on commit 670d9d1

Please sign in to comment.