-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor lsp server into cli subcommand
- Loading branch information
1 parent
a48914e
commit 82a27ee
Showing
7 changed files
with
116 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,33 @@ | ||
use ::tracing::debug; | ||
use clap::Parser; | ||
|
||
use crate::server::{Server, ServerArguments}; | ||
use crate::util::Transport; | ||
use clap::{Parser, Subcommand}; | ||
|
||
mod serve; | ||
mod tracing; | ||
|
||
#[derive(Parser)] | ||
struct Inner { | ||
#[arg(long, alias = "port")] | ||
pub socket: Option<u16>, | ||
#[arg(long)] | ||
pub stdio: bool, | ||
#[arg(long, env)] | ||
pub github_token: Option<String>, | ||
} | ||
use serve::*; | ||
use tracing::*; | ||
|
||
impl Inner { | ||
pub fn transport(&self) -> Option<Transport> { | ||
if let Some(port) = self.socket { | ||
Some(Transport::Socket(port)) | ||
} else if self.stdio { | ||
Some(Transport::Stdio) | ||
} else { | ||
None | ||
} | ||
} | ||
#[derive(Debug, Clone, Subcommand)] | ||
pub enum CliSubcommand { | ||
Serve(ServeCommand), | ||
} | ||
|
||
#[derive(Debug, Clone, Parser)] | ||
#[command(author, version, about, long_about = None)] | ||
pub struct Cli { | ||
pub transport: Transport, | ||
pub github_token: Option<String>, | ||
#[clap(subcommand)] | ||
subcommand: CliSubcommand, | ||
} | ||
|
||
impl Cli { | ||
pub fn new() -> Self { | ||
let arguments = Inner::parse(); | ||
|
||
let this = Self { | ||
transport: arguments.transport().unwrap_or_default(), | ||
github_token: arguments.github_token, | ||
}; | ||
|
||
debug!( | ||
"Parsed arguments\n\ttransport: {}\n\tgithub_token: {}", | ||
this.transport, | ||
if this.github_token.is_some() { | ||
"Some(_)" | ||
} else { | ||
"None" | ||
}, | ||
); | ||
|
||
this | ||
Self::parse() | ||
} | ||
|
||
pub async fn run(self) { | ||
// Set up tracing | ||
tracing::setup_tracing(); | ||
setup_tracing(); | ||
|
||
// Create and run our language server | ||
Server::serve(&ServerArguments { | ||
transport: self.transport, | ||
github_token: self.github_token, | ||
}) | ||
.await; | ||
match self.subcommand { | ||
CliSubcommand::Serve(cmd) => cmd.run().await, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use clap::Parser; | ||
use tracing::debug; | ||
|
||
use crate::server::{Server, ServerArguments, Transport}; | ||
|
||
#[derive(Debug, Clone, Parser)] | ||
pub struct ServeCommand { | ||
#[arg(long, alias = "port")] | ||
pub socket: Option<u16>, | ||
#[arg(long)] | ||
pub stdio: bool, | ||
#[arg(long, env)] | ||
pub github_token: Option<String>, | ||
} | ||
|
||
impl ServeCommand { | ||
fn transport(&self) -> Option<Transport> { | ||
if let Some(port) = self.socket { | ||
Some(Transport::Socket(port)) | ||
} else if self.stdio { | ||
Some(Transport::Stdio) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub async fn run(self) { | ||
let args = ServerArguments { | ||
transport: self.transport().unwrap_or_default(), | ||
github_token: self.github_token, | ||
}; | ||
|
||
debug!( | ||
"Parsed arguments\n\ttransport: {}\n\tgithub_token: {}", | ||
args.transport, | ||
if args.github_token.is_some() { | ||
"Some(_)" | ||
} else { | ||
"None" | ||
}, | ||
); | ||
|
||
Server::serve(&args).await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use std::{fmt, net::SocketAddr}; | ||
|
||
use tokio::{ | ||
io::{AsyncRead, AsyncWrite}, | ||
net::TcpStream, | ||
}; | ||
|
||
/** | ||
Transport implementation for sockets and stdio. | ||
*/ | ||
#[derive(Debug, Default, Clone, Copy)] | ||
#[non_exhaustive] | ||
pub enum Transport { | ||
Socket(u16), | ||
#[default] | ||
Stdio, | ||
} | ||
|
||
impl Transport { | ||
/** | ||
Creates a socket listener. | ||
*/ | ||
pub async fn create_socket(port: u16) -> (impl AsyncRead, impl AsyncWrite) { | ||
let addr = SocketAddr::try_from(([127, 0, 0, 1], port)).unwrap(); | ||
|
||
let stream = TcpStream::connect(addr) | ||
.await | ||
.expect("Failed to connect to socket"); | ||
|
||
stream.into_split() | ||
} | ||
|
||
/** | ||
Get handles to standard input and output streams. | ||
*/ | ||
pub fn create_stdio() -> (impl AsyncRead, impl AsyncWrite) { | ||
let stdin = tokio::io::stdin(); | ||
let stdout = tokio::io::stdout(); | ||
(stdin, stdout) | ||
} | ||
} | ||
|
||
impl fmt::Display for Transport { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Self::Stdio => write!(f, "Stdio"), | ||
Self::Socket(p) => write!(f, "Socket({p})"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
mod requests; | ||
mod transport; | ||
mod uri; | ||
|
||
pub use requests::*; | ||
pub use transport::*; | ||
pub use uri::*; |
This file was deleted.
Oops, something went wrong.