mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 13:48:50 +00:00
Prepare gen_lsp_server for publishing
This commit is contained in:
parent
c9798c0e6d
commit
239213a3db
4 changed files with 88 additions and 17 deletions
|
@ -2,11 +2,13 @@
|
||||||
name = "gen_lsp_server"
|
name = "gen_lsp_server"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
|
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
|
||||||
|
repository = "https://github.com/rust-analyzer/rust-analyzer"
|
||||||
|
license = "MIT OR Apache-2.0"
|
||||||
|
description = "Generic LSP server scaffold."
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
languageserver-types = "0.51.0"
|
languageserver-types = "0.51.0"
|
||||||
log = "0.4.3"
|
log = "0.4.3"
|
||||||
|
|
||||||
failure = "0.1.2"
|
failure = "0.1.2"
|
||||||
serde_json = "1.0.24"
|
serde_json = "1.0.24"
|
||||||
serde = "1.0.71"
|
serde = "1.0.71"
|
||||||
|
|
|
@ -1,3 +1,65 @@
|
||||||
|
//! A language server scaffold, exposing synchroneous crossbeam-channel based API.
|
||||||
|
//! This crate handles protocol handshaking and parsing messages, while you
|
||||||
|
//! control the message dispatch loop yourself.
|
||||||
|
//!
|
||||||
|
//! Run with `RUST_LOG=sync_lsp_server=debug` to see all the messages.
|
||||||
|
//!
|
||||||
|
//! ```no_run
|
||||||
|
//! extern crate gen_lsp_server;
|
||||||
|
//! extern crate languageserver_types;
|
||||||
|
//! extern crate failure;
|
||||||
|
//! extern crate crossbeam_channel;
|
||||||
|
//!
|
||||||
|
//! use crossbeam_channel::{Sender, Receiver};
|
||||||
|
//! use languageserver_types::{ServerCapabilities, InitializeParams, request::{GotoDefinition, GotoDefinitionResponse}};
|
||||||
|
//! use gen_lsp_server::{run_server, stdio_transport, handle_shutdown, RawMessage, RawResponse};
|
||||||
|
//!
|
||||||
|
//! fn main() -> Result<(), failure::Error> {
|
||||||
|
//! let (receiver, sender, io_threads) = stdio_transport();
|
||||||
|
//! gen_lsp_server::run_server(
|
||||||
|
//! ServerCapabilities::default(),
|
||||||
|
//! receiver,
|
||||||
|
//! sender,
|
||||||
|
//! main_loop,
|
||||||
|
//! )?;
|
||||||
|
//! io_threads.join()?;
|
||||||
|
//! Ok(())
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! fn main_loop(
|
||||||
|
//! _params: InitializeParams,
|
||||||
|
//! receiver: &Receiver<RawMessage>,
|
||||||
|
//! sender: &Sender<RawMessage>,
|
||||||
|
//! ) -> Result<(), failure::Error> {
|
||||||
|
//! for msg in receiver {
|
||||||
|
//! match msg {
|
||||||
|
//! RawMessage::Request(req) => {
|
||||||
|
//! let req = match handle_shutdown(req, sender) {
|
||||||
|
//! None => return Ok(()),
|
||||||
|
//! Some(req) => req,
|
||||||
|
//! };
|
||||||
|
//! let req = match req.cast::<GotoDefinition>() {
|
||||||
|
//! Ok((id, _params)) => {
|
||||||
|
//! let resp = RawResponse::ok::<GotoDefinition>(
|
||||||
|
//! id,
|
||||||
|
//! &Some(GotoDefinitionResponse::Array(Vec::new())),
|
||||||
|
//! );
|
||||||
|
//! sender.send(RawMessage::Response(resp));
|
||||||
|
//! continue;
|
||||||
|
//! },
|
||||||
|
//! Err(req) => req,
|
||||||
|
//! };
|
||||||
|
//! // ...
|
||||||
|
//! }
|
||||||
|
//! RawMessage::Response(_resp) => (),
|
||||||
|
//! RawMessage::Notification(_not) => (),
|
||||||
|
//! }
|
||||||
|
//! }
|
||||||
|
//! Ok(())
|
||||||
|
//! }
|
||||||
|
//! ```
|
||||||
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate failure;
|
extern crate failure;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
@ -25,20 +87,26 @@ pub use {
|
||||||
stdio::{stdio_transport, Threads},
|
stdio::{stdio_transport, Threads},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Main entry point: runs the server from initialization to shutdown.
|
||||||
|
/// To attach server to standard input/output streams, use `stdio_transport`
|
||||||
|
/// function to create corresponding `sender` and `receiver` pair.
|
||||||
|
///
|
||||||
|
///`server` should use `handle_shutdown` function to handle the `Shutdown`
|
||||||
|
/// request.
|
||||||
pub fn run_server(
|
pub fn run_server(
|
||||||
caps: ServerCapabilities,
|
caps: ServerCapabilities,
|
||||||
|
receiver: Receiver<RawMessage>,
|
||||||
|
sender: Sender<RawMessage>,
|
||||||
server: impl FnOnce(
|
server: impl FnOnce(
|
||||||
InitializeParams,
|
InitializeParams,
|
||||||
&mut Receiver<RawMessage>,
|
&Receiver<RawMessage>,
|
||||||
&mut Sender<RawMessage>,
|
&Sender<RawMessage>,
|
||||||
) -> Result<()>,
|
) -> Result<()>,
|
||||||
mut receiver: Receiver<RawMessage>,
|
|
||||||
mut sender: Sender<RawMessage>,
|
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
info!("lsp server initializes");
|
info!("lsp server initializes");
|
||||||
let params = initialize(&mut receiver, &mut sender, caps)?;
|
let params = initialize(&receiver, &sender, caps)?;
|
||||||
info!("lsp server initialized, serving requests");
|
info!("lsp server initialized, serving requests");
|
||||||
server(params, &mut receiver, &mut sender)?;
|
server(params, &receiver, &sender)?;
|
||||||
info!("lsp server waiting for exit notification");
|
info!("lsp server waiting for exit notification");
|
||||||
match receiver.recv() {
|
match receiver.recv() {
|
||||||
Some(RawMessage::Notification(n)) => {
|
Some(RawMessage::Notification(n)) => {
|
||||||
|
@ -52,6 +120,7 @@ pub fn run_server(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// if `req` is `Shutdown`, respond to it and return `None`, otherwise return `Some(req)`
|
||||||
pub fn handle_shutdown(req: RawRequest, sender: &Sender<RawMessage>) -> Option<RawRequest> {
|
pub fn handle_shutdown(req: RawRequest, sender: &Sender<RawMessage>) -> Option<RawRequest> {
|
||||||
match req.cast::<Shutdown>() {
|
match req.cast::<Shutdown>() {
|
||||||
Ok((id, ())) => {
|
Ok((id, ())) => {
|
||||||
|
@ -64,8 +133,8 @@ pub fn handle_shutdown(req: RawRequest, sender: &Sender<RawMessage>) -> Option<R
|
||||||
}
|
}
|
||||||
|
|
||||||
fn initialize(
|
fn initialize(
|
||||||
receiver: &mut Receiver<RawMessage>,
|
receiver: &Receiver<RawMessage>,
|
||||||
sender: &mut Sender<RawMessage>,
|
sender: &Sender<RawMessage>,
|
||||||
caps: ServerCapabilities,
|
caps: ServerCapabilities,
|
||||||
) -> Result<InitializeParams> {
|
) -> Result<InitializeParams> {
|
||||||
let (id, params) = match receiver.recv() {
|
let (id, params) = match receiver.recv() {
|
||||||
|
|
|
@ -35,14 +35,14 @@ fn main_inner() -> Result<()> {
|
||||||
let cwd = ::std::env::current_dir()?;
|
let cwd = ::std::env::current_dir()?;
|
||||||
run_server(
|
run_server(
|
||||||
ra_lsp_server::server_capabilities(),
|
ra_lsp_server::server_capabilities(),
|
||||||
|
receiver,
|
||||||
|
sender,
|
||||||
|params, r, s| {
|
|params, r, s| {
|
||||||
let root = params.root_uri
|
let root = params.root_uri
|
||||||
.and_then(|it| it.to_file_path().ok())
|
.and_then(|it| it.to_file_path().ok())
|
||||||
.unwrap_or(cwd);
|
.unwrap_or(cwd);
|
||||||
ra_lsp_server::main_loop(false, root, r, s)
|
ra_lsp_server::main_loop(false, root, r, s)
|
||||||
},
|
},
|
||||||
receiver,
|
|
||||||
sender,
|
|
||||||
)?;
|
)?;
|
||||||
info!("shutting down IO...");
|
info!("shutting down IO...");
|
||||||
threads.join()?;
|
threads.join()?;
|
||||||
|
|
|
@ -35,8 +35,8 @@ enum Task {
|
||||||
pub fn main_loop(
|
pub fn main_loop(
|
||||||
internal_mode: bool,
|
internal_mode: bool,
|
||||||
root: PathBuf,
|
root: PathBuf,
|
||||||
msg_receriver: &mut Receiver<RawMessage>,
|
msg_receriver: &Receiver<RawMessage>,
|
||||||
msg_sender: &mut Sender<RawMessage>,
|
msg_sender: &Sender<RawMessage>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let pool = rayon::ThreadPoolBuilder::new()
|
let pool = rayon::ThreadPoolBuilder::new()
|
||||||
.num_threads(4)
|
.num_threads(4)
|
||||||
|
@ -88,8 +88,8 @@ fn main_loop_inner(
|
||||||
internal_mode: bool,
|
internal_mode: bool,
|
||||||
ws_root: PathBuf,
|
ws_root: PathBuf,
|
||||||
pool: &ThreadPool,
|
pool: &ThreadPool,
|
||||||
msg_sender: &mut Sender<RawMessage>,
|
msg_sender: &Sender<RawMessage>,
|
||||||
msg_receiver: &mut Receiver<RawMessage>,
|
msg_receiver: &Receiver<RawMessage>,
|
||||||
task_sender: Sender<Task>,
|
task_sender: Sender<Task>,
|
||||||
task_receiver: Receiver<Task>,
|
task_receiver: Receiver<Task>,
|
||||||
fs_worker: Worker<PathBuf, (PathBuf, Vec<FileEvent>)>,
|
fs_worker: Worker<PathBuf, (PathBuf, Vec<FileEvent>)>,
|
||||||
|
@ -212,7 +212,7 @@ fn main_loop_inner(
|
||||||
|
|
||||||
fn on_task(
|
fn on_task(
|
||||||
task: Task,
|
task: Task,
|
||||||
msg_sender: &mut Sender<RawMessage>,
|
msg_sender: &Sender<RawMessage>,
|
||||||
pending_requests: &mut HashMap<u64, JobHandle>,
|
pending_requests: &mut HashMap<u64, JobHandle>,
|
||||||
) {
|
) {
|
||||||
match task {
|
match task {
|
||||||
|
@ -266,7 +266,7 @@ fn on_request(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_notification(
|
fn on_notification(
|
||||||
msg_sender: &mut Sender<RawMessage>,
|
msg_sender: &Sender<RawMessage>,
|
||||||
state: &mut ServerWorldState,
|
state: &mut ServerWorldState,
|
||||||
pending_requests: &mut HashMap<u64, JobHandle>,
|
pending_requests: &mut HashMap<u64, JobHandle>,
|
||||||
subs: &mut Subscriptions,
|
subs: &mut Subscriptions,
|
||||||
|
|
Loading…
Reference in a new issue