]> git.lizzy.rs Git - rust.git/commitdiff
Merge #151
authorbors[bot] <bors[bot]@users.noreply.github.com>
Tue, 23 Oct 2018 12:28:57 +0000 (12:28 +0000)
committerbors[bot] <bors[bot]@users.noreply.github.com>
Tue, 23 Oct 2018 12:28:57 +0000 (12:28 +0000)
151: Add LspError to explicity return errors from LSP handlers r=matklad a=kjeremy

Fixes #145

Co-authored-by: Jeremy A. Kolb <jkolb@ara.com>
1  2 
crates/ra_lsp_server/src/main_loop/handlers.rs
crates/ra_lsp_server/src/main_loop/mod.rs

index 11f34eb9354543c0077173055e56a3198183602c,baf82f3fcde5359b3bd55c6cded614839a5d9f6b..b27b5d007950d82e83dcd8850e3a4c5a5c564d10
@@@ -7,6 -7,7 +7,7 @@@ use languageserver_types::
      InsertTextFormat, Location, Position, SymbolInformation, TextDocumentIdentifier, TextEdit,
      RenameParams, WorkspaceEdit, PrepareRenameResponse
  };
+ use gen_lsp_server::ErrorCode;
  use ra_analysis::{FileId, FoldKind, Query, RunnableKind};
  use ra_syntax::text_utils::contains_offset_nonstrict;
  use serde_json::to_value;
@@@ -16,7 -17,7 +17,7 @@@ use crate::
      project_model::TargetKind,
      req::{self, Decoration},
      server_world::ServerWorld,
-     Result,
+     Result, LspError
  };
  
  pub fn handle_syntax_tree(
@@@ -154,8 -155,8 +155,8 @@@ pub fn handle_workspace_symbol
      world: ServerWorld,
      params: req::WorkspaceSymbolParams,
  ) -> Result<Option<Vec<SymbolInformation>>> {
 -    let all_symbols = params.query.contains("#");
 -    let libs = params.query.contains("*");
 +    let all_symbols = params.query.contains('#');
 +    let libs = params.query.contains('*');
      let query = {
          let query: String = params
              .query
@@@ -279,8 -280,8 +280,8 @@@ pub fn handle_runnables
                  .filter_map(|ws| {
                      let tgt = ws.target_by_root(path)?;
                      Some((
 -                        tgt.package(ws).name(ws).clone(),
 -                        tgt.name(ws).clone(),
 +                        tgt.package(ws).name(ws),
 +                        tgt.name(ws),
                          tgt.kind(ws),
                      ))
                  })
@@@ -476,7 -477,7 +477,7 @@@ pub fn handle_rename
      let offset = params.position.conv_with(&line_index);
  
      if params.new_name.is_empty() {
-         return Ok(None);
+         return Err(LspError::new(ErrorCode::InvalidParams as i32, "New Name cannot be empty".into()).into());
      }
  
      let refs = world.analysis().find_all_refs(file_id, offset)?;
index 69dcc7e579c8f5759f0f8b1d76b7f5c7f1a79943,aa3cf1dbd273cd18b3bba78dabb4553d0dcd2ff1..e681062ca09ceb0940c39ae58a55f908e309d533
@@@ -23,6 -23,19 +23,19 @@@ use crate::
      Result,
  };
  
+ #[derive(Debug, Fail)]
+ #[fail(display = "Language Server request failed with {}. ({})", code, message)]
+ pub struct LspError {
+     pub code: i32,
+     pub message: String,
+ }
+ impl LspError {
+     pub fn new(code: i32, message: String) -> LspError {
+         LspError {code, message}
+     }
+ }
  #[derive(Debug)]
  enum Task {
      Respond(RawResponse),
@@@ -32,7 -45,7 +45,7 @@@
  pub fn main_loop(
      internal_mode: bool,
      root: PathBuf,
 -    msg_receriver: &Receiver<RawMessage>,
 +    msg_receiver: &Receiver<RawMessage>,
      msg_sender: &Sender<RawMessage>,
  ) -> Result<()> {
      let pool = rayon::ThreadPoolBuilder::new()
@@@ -54,7 -67,7 +67,7 @@@
          root,
          &pool,
          msg_sender,
 -        msg_receriver,
 +        msg_receiver,
          task_sender,
          task_receiver.clone(),
          fs_worker,
@@@ -361,7 -374,10 +374,10 @@@ impl<'a> PoolDispatcher<'a> 
                      let resp = match f(world, params) {
                          Ok(resp) => RawResponse::ok::<R>(id, &resp),
                          Err(e) => {
-                             RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string())
+                             match e.downcast::<LspError>() {
+                                 Ok(lsp_error) => RawResponse::err(id, lsp_error.code, lsp_error.message),
+                                 Err(e) => RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string())
+                             }
                          }
                      };
                      let task = Task::Respond(resp);