Skip to content

Commit

Permalink
Refactor lsp server into cli subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell committed Nov 2, 2023
1 parent a48914e commit 82a27ee
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 105 deletions.
1 change: 1 addition & 0 deletions editors/vscode/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const startServer = async () => {
const server: Executable = {
command,
options,
args: ["serve"],
};

// Create language server & client config
Expand Down
68 changes: 16 additions & 52 deletions src/cli/mod.rs
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,
}
}
}
45 changes: 45 additions & 0 deletions src/cli/serve.rs
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;
}
}
7 changes: 4 additions & 3 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use tower_lsp::{Client, LspService, Server as LspServer};

use crate::clients::*;
use crate::tools::*;
use crate::util::*;

mod document;
mod initialize;
mod language_server;
mod requests;
mod transport;

pub use document::*;
pub use transport::*;

pub struct ServerArguments {
pub transport: Transport,
Expand Down Expand Up @@ -54,11 +55,11 @@ impl Server {

match args.transport {
Transport::Socket(port) => {
let (read, write) = create_socket(port).await;
let (read, write) = Transport::create_socket(port).await;
LspServer::new(read, write, socket).serve(service).await;
}
Transport::Stdio => {
let (stdin, stdout) = create_stdio();
let (stdin, stdout) = Transport::create_stdio();
LspServer::new(stdin, stdout, socket).serve(service).await;
}
}
Expand Down
50 changes: 50 additions & 0 deletions src/server/transport.rs
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})"),
}
}
}
2 changes: 0 additions & 2 deletions src/util/mod.rs
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::*;
48 changes: 0 additions & 48 deletions src/util/transport.rs

This file was deleted.

0 comments on commit 82a27ee

Please sign in to comment.