From d19f3ac83441420365bff5e4ce21d1d2175bd8c2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 13 Aug 2018 15:35:53 +0300 Subject: workspace symbols --- crates/server/src/caps.rs | 2 +- crates/server/src/dispatch.rs | 15 +++++++++------ crates/server/src/main.rs | 4 +--- crates/server/src/main_loop/handlers.rs | 29 +++++++++++++++++++++++++++-- crates/server/src/main_loop/mod.rs | 4 ++++ crates/server/src/req.rs | 1 + 6 files changed, 43 insertions(+), 12 deletions(-) (limited to 'crates/server/src') diff --git a/crates/server/src/caps.rs b/crates/server/src/caps.rs index d06a43a82..4fd28b7c8 100644 --- a/crates/server/src/caps.rs +++ b/crates/server/src/caps.rs @@ -26,7 +26,7 @@ pub fn server_capabilities() -> ServerCapabilities { references_provider: None, document_highlight_provider: None, document_symbol_provider: Some(true), - workspace_symbol_provider: None, + workspace_symbol_provider: Some(true), code_action_provider: Some(true), code_lens_provider: None, document_formatting_provider: None, diff --git a/crates/server/src/dispatch.rs b/crates/server/src/dispatch.rs index 3a3ee74bb..d8cca48d0 100644 --- a/crates/server/src/dispatch.rs +++ b/crates/server/src/dispatch.rs @@ -30,8 +30,12 @@ impl Responder { error: serde_json::Value::Null, } } - Err(_) => { - error_response(self.id, ErrorCode::InternalError, "internal error")? + Err(e) => { + error_response( + self.id, + ErrorCode::InternalError, + format!("internal error: {}", e), + )? } }; Ok(res) @@ -115,21 +119,20 @@ pub fn send_notification(params: N::Params) -> RawNotification pub fn unknown_method(id: u64) -> Result { error_response(id, ErrorCode::MethodNotFound, "unknown method") - } -fn error_response(id: u64, code: ErrorCode, message: &'static str) -> Result { +fn error_response(id: u64, code: ErrorCode, message: impl Into) -> Result { #[derive(Serialize)] struct Error { code: i32, - message: &'static str, + message: String, } let resp = RawResponse { id, result: serde_json::Value::Null, error: serde_json::to_value(Error { code: code as i32, - message, + message: message.into(), })?, }; Ok(resp) diff --git a/crates/server/src/main.rs b/crates/server/src/main.rs index 8dca32183..9c044d5a9 100644 --- a/crates/server/src/main.rs +++ b/crates/server/src/main.rs @@ -27,8 +27,6 @@ mod conv; mod main_loop; mod vfs; -use std::path::PathBuf; - use threadpool::ThreadPool; use crossbeam_channel::bounded; use flexi_logger::{Logger, Duplicate}; @@ -120,7 +118,7 @@ fn initialized(io: &mut Io) -> Result<()> { let mut pool = ThreadPool::new(4); let (task_sender, task_receiver) = bounded::(16); let (fs_events_receiver, watcher) = vfs::watch(vec![ - PathBuf::from("./") + ::std::env::current_dir()?, ]); info!("lifecycle: handshake finished, server ready to serve requests"); let res = main_loop::main_loop( diff --git a/crates/server/src/main_loop/handlers.rs b/crates/server/src/main_loop/handlers.rs index 14dcafc38..bd0e6825b 100644 --- a/crates/server/src/main_loop/handlers.rs +++ b/crates/server/src/main_loop/handlers.rs @@ -2,9 +2,10 @@ use std::collections::HashMap; use languageserver_types::{ Diagnostic, DiagnosticSeverity, Url, DocumentSymbol, - Command, TextDocumentIdentifier, WorkspaceEdit + Command, TextDocumentIdentifier, WorkspaceEdit, + SymbolInformation, Location, }; -use libanalysis::World; +use libanalysis::{World}; use libeditor; use libsyntax2::TextUnit; use serde_json::{to_value, from_value}; @@ -94,6 +95,30 @@ pub fn handle_code_action( Ok(ret) } +pub fn handle_workspace_symbol( + world: World, + params: req::WorkspaceSymbolParams, +) -> Result>> { + let mut acc = Vec::new(); + for (path, symbol) in world.world_symbols(¶ms.query).take(128) { + let line_index = world.file_line_index(path)?; + + let info = SymbolInformation { + name: symbol.name.to_string(), + kind: symbol.kind.conv(), + location: Location::new( + Url::from_file_path(path) + .map_err(|()| format_err!("invalid url"))?, + symbol.node_range.conv_with(&line_index), + ), + container_name: None, + }; + acc.push(info); + }; + + Ok(Some(acc)) +} + pub fn handle_execute_command( world: World, mut params: req::ExecuteCommandParams, diff --git a/crates/server/src/main_loop/mod.rs b/crates/server/src/main_loop/mod.rs index f954e632c..e8b24355c 100644 --- a/crates/server/src/main_loop/mod.rs +++ b/crates/server/src/main_loop/mod.rs @@ -25,6 +25,7 @@ use { handle_document_symbol, handle_code_action, handle_execute_command, + handle_workspace_symbol, }, }; @@ -148,6 +149,9 @@ fn on_request( handle_request_on_threadpool::( &mut req, pool, world, sender, handle_code_action, )?; + handle_request_on_threadpool::( + &mut req, pool, world, sender, handle_workspace_symbol, + )?; dispatch::handle_request::(&mut req, |params, resp| { io.send(RawMsg::Response(resp.into_response(Ok(None))?)); diff --git a/crates/server/src/req.rs b/crates/server/src/req.rs index a22ba4bc3..a8cc9b537 100644 --- a/crates/server/src/req.rs +++ b/crates/server/src/req.rs @@ -8,6 +8,7 @@ pub use languageserver_types::{ DocumentSymbolParams, DocumentSymbolResponse, CodeActionParams, ApplyWorkspaceEditParams, ExecuteCommandParams, + WorkspaceSymbolParams, }; -- cgit v1.2.3