aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server/src
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-06-15 10:45:55 +0100
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-06-15 10:45:55 +0100
commit257a15b93942a1e38e561576af471a6a1e63bcff (patch)
tree3586eda54dab85be6d24325e597a1d8f060e36b7 /crates/ra_lsp_server/src
parent9dbf985df5515f4b9b40a7dcf74f916fa8d57ee3 (diff)
parent408e173bb9737f9484ca773ee57cc791f5c57e16 (diff)
Merge #1404
1404: Fight down failures! r=matklad a=mominul issue #1400 Now only `ra_tools` crate depends on `failure`, should I also fight those? :grin: Co-authored-by: Muhammad Mominul Huque <[email protected]>
Diffstat (limited to 'crates/ra_lsp_server/src')
-rw-r--r--crates/ra_lsp_server/src/lib.rs2
-rw-r--r--crates/ra_lsp_server/src/main.rs2
-rw-r--r--crates/ra_lsp_server/src/main_loop.rs36
-rw-r--r--crates/ra_lsp_server/src/world.rs11
4 files changed, 25 insertions, 26 deletions
diff --git a/crates/ra_lsp_server/src/lib.rs b/crates/ra_lsp_server/src/lib.rs
index aabde420b..14cfa401f 100644
--- a/crates/ra_lsp_server/src/lib.rs
+++ b/crates/ra_lsp_server/src/lib.rs
@@ -9,5 +9,5 @@ pub mod req;
9pub mod init; 9pub mod init;
10mod world; 10mod world;
11 11
12pub type Result<T> = ::std::result::Result<T, ::failure::Error>; 12pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
13pub use crate::{caps::server_capabilities, main_loop::main_loop, main_loop::LspError, init::InitializationOptions}; 13pub use crate::{caps::server_capabilities, main_loop::main_loop, main_loop::LspError, init::InitializationOptions};
diff --git a/crates/ra_lsp_server/src/main.rs b/crates/ra_lsp_server/src/main.rs
index 3c3e8b5b0..7749d97d6 100644
--- a/crates/ra_lsp_server/src/main.rs
+++ b/crates/ra_lsp_server/src/main.rs
@@ -25,7 +25,7 @@ fn main() -> Result<()> {
25 } 25 }
26 Err(_) => { 26 Err(_) => {
27 log::error!("server panicked"); 27 log::error!("server panicked");
28 failure::bail!("server panicked") 28 Err("server panicked")?
29 } 29 }
30 } 30 }
31} 31}
diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs
index 0790ea472..aeb8a2299 100644
--- a/crates/ra_lsp_server/src/main_loop.rs
+++ b/crates/ra_lsp_server/src/main_loop.rs
@@ -2,11 +2,9 @@ mod handlers;
2mod subscriptions; 2mod subscriptions;
3pub(crate) mod pending_requests; 3pub(crate) mod pending_requests;
4 4
5use std::{fmt, path::PathBuf, sync::Arc, time::Instant}; 5use std::{fmt, path::PathBuf, sync::Arc, time::Instant, error::Error};
6 6
7use crossbeam_channel::{select, unbounded, Receiver, RecvError, Sender}; 7use crossbeam_channel::{select, unbounded, Receiver, RecvError, Sender};
8use failure::{bail, format_err};
9use failure_derive::Fail;
10use gen_lsp_server::{ 8use gen_lsp_server::{
11 handle_shutdown, ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse, 9 handle_shutdown, ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse,
12}; 10};
@@ -32,8 +30,7 @@ use crate::{
32const THREADPOOL_SIZE: usize = 8; 30const THREADPOOL_SIZE: usize = 8;
33const MAX_IN_FLIGHT_LIBS: usize = THREADPOOL_SIZE - 3; 31const MAX_IN_FLIGHT_LIBS: usize = THREADPOOL_SIZE - 3;
34 32
35#[derive(Debug, Fail)] 33#[derive(Debug)]
36#[fail(display = "Language Server request failed with {}. ({})", code, message)]
37pub struct LspError { 34pub struct LspError {
38 pub code: i32, 35 pub code: i32,
39 pub message: String, 36 pub message: String,
@@ -45,6 +42,14 @@ impl LspError {
45 } 42 }
46} 43}
47 44
45impl fmt::Display for LspError {
46 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47 write!(f, "Language Server request failed with {}. ({})", self.code, self.message)
48 }
49}
50
51impl Error for LspError {}
52
48pub fn main_loop( 53pub fn main_loop(
49 ws_roots: Vec<PathBuf>, 54 ws_roots: Vec<PathBuf>,
50 options: InitializationOptions, 55 options: InitializationOptions,
@@ -177,12 +182,12 @@ fn main_loop_inner(
177 let event = select! { 182 let event = select! {
178 recv(msg_receiver) -> msg => match msg { 183 recv(msg_receiver) -> msg => match msg {
179 Ok(msg) => Event::Msg(msg), 184 Ok(msg) => Event::Msg(msg),
180 Err(RecvError) => bail!("client exited without shutdown"), 185 Err(RecvError) => Err("client exited without shutdown")?,
181 }, 186 },
182 recv(task_receiver) -> task => Event::Task(task.unwrap()), 187 recv(task_receiver) -> task => Event::Task(task.unwrap()),
183 recv(state.vfs.read().task_receiver()) -> task => match task { 188 recv(state.vfs.read().task_receiver()) -> task => match task {
184 Ok(task) => Event::Vfs(task), 189 Ok(task) => Event::Vfs(task),
185 Err(RecvError) => bail!("vfs died"), 190 Err(RecvError) => Err("vfs died")?,
186 }, 191 },
187 recv(libdata_receiver) -> data => Event::Lib(data.unwrap()) 192 recv(libdata_receiver) -> data => Event::Lib(data.unwrap())
188 }; 193 };
@@ -380,7 +385,7 @@ fn on_notification(
380 let not = match not.cast::<req::DidOpenTextDocument>() { 385 let not = match not.cast::<req::DidOpenTextDocument>() {
381 Ok(params) => { 386 Ok(params) => {
382 let uri = params.text_document.uri; 387 let uri = params.text_document.uri;
383 let path = uri.to_file_path().map_err(|()| format_err!("invalid uri: {}", uri))?; 388 let path = uri.to_file_path().map_err(|()| format!("invalid uri: {}", uri))?;
384 if let Some(file_id) = 389 if let Some(file_id) =
385 state.vfs.write().add_file_overlay(&path, params.text_document.text) 390 state.vfs.write().add_file_overlay(&path, params.text_document.text)
386 { 391 {
@@ -393,9 +398,8 @@ fn on_notification(
393 let not = match not.cast::<req::DidChangeTextDocument>() { 398 let not = match not.cast::<req::DidChangeTextDocument>() {
394 Ok(mut params) => { 399 Ok(mut params) => {
395 let uri = params.text_document.uri; 400 let uri = params.text_document.uri;
396 let path = uri.to_file_path().map_err(|()| format_err!("invalid uri: {}", uri))?; 401 let path = uri.to_file_path().map_err(|()| format!("invalid uri: {}", uri))?;
397 let text = 402 let text = params.content_changes.pop().ok_or_else(|| format!("empty changes"))?.text;
398 params.content_changes.pop().ok_or_else(|| format_err!("empty changes"))?.text;
399 state.vfs.write().change_file_overlay(path.as_path(), text); 403 state.vfs.write().change_file_overlay(path.as_path(), text);
400 return Ok(()); 404 return Ok(());
401 } 405 }
@@ -404,7 +408,7 @@ fn on_notification(
404 let not = match not.cast::<req::DidCloseTextDocument>() { 408 let not = match not.cast::<req::DidCloseTextDocument>() {
405 Ok(params) => { 409 Ok(params) => {
406 let uri = params.text_document.uri; 410 let uri = params.text_document.uri;
407 let path = uri.to_file_path().map_err(|()| format_err!("invalid uri: {}", uri))?; 411 let path = uri.to_file_path().map_err(|()| format!("invalid uri: {}", uri))?;
408 if let Some(file_id) = state.vfs.write().remove_file_overlay(path.as_path()) { 412 if let Some(file_id) = state.vfs.write().remove_file_overlay(path.as_path()) {
409 subs.remove_sub(FileId(file_id.0)); 413 subs.remove_sub(FileId(file_id.0));
410 } 414 }
@@ -543,11 +547,7 @@ where
543 error: None, 547 error: None,
544 } 548 }
545 } else { 549 } else {
546 RawResponse::err( 550 RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string())
547 id,
548 ErrorCode::InternalError as i32,
549 format!("{}\n{}", e, e.backtrace()),
550 )
551 } 551 }
552 } 552 }
553 }, 553 },
@@ -599,6 +599,6 @@ fn show_message(typ: req::MessageType, message: impl Into<String>, sender: &Send
599 sender.send(not.into()).unwrap(); 599 sender.send(not.into()).unwrap();
600} 600}
601 601
602fn is_canceled(e: &failure::Error) -> bool { 602fn is_canceled(e: &Box<dyn std::error::Error + Send + Sync>) -> bool {
603 e.downcast_ref::<Canceled>().is_some() 603 e.downcast_ref::<Canceled>().is_some()
604} 604}
diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs
index f9ce570ca..7822e1c1c 100644
--- a/crates/ra_lsp_server/src/world.rs
+++ b/crates/ra_lsp_server/src/world.rs
@@ -11,7 +11,6 @@ use ra_ide_api::{
11use ra_vfs::{Vfs, VfsChange, VfsFile, VfsRoot}; 11use ra_vfs::{Vfs, VfsChange, VfsFile, VfsRoot};
12use relative_path::RelativePathBuf; 12use relative_path::RelativePathBuf;
13use parking_lot::RwLock; 13use parking_lot::RwLock;
14use failure::{Error, format_err};
15use gen_lsp_server::ErrorCode; 14use gen_lsp_server::ErrorCode;
16 15
17use crate::{ 16use crate::{
@@ -169,13 +168,13 @@ impl WorldSnapshot {
169 } 168 }
170 169
171 pub fn uri_to_file_id(&self, uri: &Url) -> Result<FileId> { 170 pub fn uri_to_file_id(&self, uri: &Url) -> Result<FileId> {
172 let path = uri.to_file_path().map_err(|()| format_err!("invalid uri: {}", uri))?; 171 let path = uri.to_file_path().map_err(|()| format!("invalid uri: {}", uri))?;
173 let file = self.vfs.read().path2file(&path).ok_or_else(|| { 172 let file = self.vfs.read().path2file(&path).ok_or_else(|| {
174 // Show warning as this file is outside current workspace 173 // Show warning as this file is outside current workspace
175 Error::from(LspError { 174 LspError {
176 code: ErrorCode::InvalidRequest as i32, 175 code: ErrorCode::InvalidRequest as i32,
177 message: "Rust file outside current workspace is not supported yet.".to_string(), 176 message: "Rust file outside current workspace is not supported yet.".to_string(),
178 }) 177 }
179 })?; 178 })?;
180 Ok(FileId(file.0)) 179 Ok(FileId(file.0))
181 } 180 }
@@ -183,7 +182,7 @@ impl WorldSnapshot {
183 pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> { 182 pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> {
184 let path = self.vfs.read().file2path(VfsFile(id.0)); 183 let path = self.vfs.read().file2path(VfsFile(id.0));
185 let url = Url::from_file_path(&path) 184 let url = Url::from_file_path(&path)
186 .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?; 185 .map_err(|_| format!("can't convert path to url: {}", path.display()))?;
187 Ok(url) 186 Ok(url)
188 } 187 }
189 188
@@ -191,7 +190,7 @@ impl WorldSnapshot {
191 let base = self.vfs.read().root2path(VfsRoot(root.0)); 190 let base = self.vfs.read().root2path(VfsRoot(root.0));
192 let path = path.to_path(base); 191 let path = path.to_path(base);
193 let url = Url::from_file_path(&path) 192 let url = Url::from_file_path(&path)
194 .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?; 193 .map_err(|_| format!("can't convert path to url: {}", path.display()))?;
195 Ok(url) 194 Ok(url)
196 } 195 }
197 196