use correct workdir for the server

This commit is contained in:
Aleksey Kladov 2018-09-05 21:38:43 +03:00
parent 529ebd5840
commit 47e8b80e9b
2 changed files with 19 additions and 10 deletions

View file

@ -14,7 +14,7 @@ mod stdio;
use crossbeam_channel::{Sender, Receiver}; use crossbeam_channel::{Sender, Receiver};
use languageserver_types::{ use languageserver_types::{
ServerCapabilities, InitializeResult, ServerCapabilities, InitializeResult, InitializeParams,
request::{Initialize, Shutdown}, request::{Initialize, Shutdown},
notification::{Initialized, Exit}, notification::{Initialized, Exit},
}; };
@ -27,14 +27,18 @@ pub use {
pub fn run_server( pub fn run_server(
caps: ServerCapabilities, caps: ServerCapabilities,
server: impl FnOnce(&mut Receiver<RawMessage>, &mut Sender<RawMessage>) -> Result<()>, server: impl FnOnce(
InitializeParams,
&mut Receiver<RawMessage>,
&mut Sender<RawMessage>,
) -> Result<()>,
mut receiver: Receiver<RawMessage>, mut receiver: Receiver<RawMessage>,
mut sender: Sender<RawMessage>, mut sender: Sender<RawMessage>,
) -> Result<()> { ) -> Result<()> {
info!("lsp server initializes"); info!("lsp server initializes");
initialize(&mut receiver, &mut sender, caps)?; let params = initialize(&mut receiver, &mut sender, caps)?;
info!("lsp server initialized, serving requests"); info!("lsp server initialized, serving requests");
server(&mut receiver, &mut sender)?; server(params, &mut receiver, &mut 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)) => {
@ -63,11 +67,11 @@ fn initialize(
receiver: &mut Receiver<RawMessage>, receiver: &mut Receiver<RawMessage>,
sender: &mut Sender<RawMessage>, sender: &mut Sender<RawMessage>,
caps: ServerCapabilities, caps: ServerCapabilities,
) -> Result<()> { ) -> Result<InitializeParams> {
let id = match receiver.recv() { let (id, params) = match receiver.recv() {
Some(RawMessage::Request(req)) => match req.cast::<Initialize>() { Some(RawMessage::Request(req)) => match req.cast::<Initialize>() {
Err(req) => bail!("expected initialize request, got {:?}", req), Err(req) => bail!("expected initialize request, got {:?}", req),
Ok(req) => req.0, Ok(req) => req,
} }
msg => msg =>
bail!("expected initialize request, got {:?}", msg), bail!("expected initialize request, got {:?}", msg),
@ -82,5 +86,5 @@ fn initialize(
} }
_ => bail!("expected initialized notification"), _ => bail!("expected initialized notification"),
} }
Ok(()) Ok(params)
} }

View file

@ -31,10 +31,15 @@ fn main() -> Result<()> {
fn main_inner() -> Result<()> { fn main_inner() -> Result<()> {
let (receiver, sender, threads) = stdio_transport(); let (receiver, sender, threads) = stdio_transport();
let root = ::std::env::current_dir()?; let cwd = ::std::env::current_dir()?;
run_server( run_server(
m::server_capabilities(), m::server_capabilities(),
|r, s| m::main_loop(false, root, r, s), |params, r, s| {
let root = params.root_uri
.and_then(|it| it.to_file_path().ok())
.unwrap_or(cwd);
m::main_loop(false, root, r, s)
},
receiver, receiver,
sender, sender,
)?; )?;