From 61f3a438d3a729a6be941bca1ff4c6a97a33f221 Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Mon, 15 Oct 2018 17:44:23 -0400 Subject: Cargo Format Run `cargo fmt` and ignore generated files --- crates/ra_lsp_server/src/main_loop/handlers.rs | 206 ++++++++++++--------- crates/ra_lsp_server/src/main_loop/mod.rs | 161 ++++++++-------- .../ra_lsp_server/src/main_loop/subscriptions.rs | 6 +- 3 files changed, 197 insertions(+), 176 deletions(-) (limited to 'crates/ra_lsp_server/src/main_loop') diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 5acb39b60..c25b63852 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -1,23 +1,20 @@ use rustc_hash::FxHashMap; use languageserver_types::{ - Diagnostic, DiagnosticSeverity, DocumentSymbol, - CodeActionResponse, Command, TextDocumentIdentifier, - SymbolInformation, Position, Location, TextEdit, - CompletionItem, InsertTextFormat, CompletionItemKind, - FoldingRange, FoldingRangeParams, FoldingRangeKind + CodeActionResponse, Command, CompletionItem, CompletionItemKind, Diagnostic, + DiagnosticSeverity, DocumentSymbol, FoldingRange, FoldingRangeKind, FoldingRangeParams, + InsertTextFormat, Location, Position, SymbolInformation, TextDocumentIdentifier, TextEdit, }; +use ra_analysis::{FileId, FoldKind, JobToken, Query, RunnableKind}; +use ra_syntax::text_utils::contains_offset_nonstrict; use serde_json::to_value; -use ra_analysis::{Query, FileId, RunnableKind, JobToken, FoldKind}; -use ra_syntax::{ - text_utils::contains_offset_nonstrict -}; use crate::{ - req::{self, Decoration}, Result, - conv::{Conv, ConvWith, TryConvWith, MapConvWith, to_location}, - server_world::ServerWorld, + conv::{to_location, Conv, ConvWith, MapConvWith, TryConvWith}, project_model::TargetKind, + req::{self, Decoration}, + server_world::ServerWorld, + Result, }; pub fn handle_syntax_tree( @@ -38,7 +35,9 @@ pub fn handle_extend_selection( let file_id = params.text_document.try_conv_with(&world)?; let file = world.analysis().file_syntax(file_id); let line_index = world.analysis().file_line_index(file_id); - let selections = params.selections.into_iter() + let selections = params + .selections + .into_iter() .map_conv_with(&line_index) .map(|r| world.analysis().extend_selection(&file, r)) .map_conv_with(&line_index) @@ -54,11 +53,15 @@ pub fn handle_find_matching_brace( let file_id = params.text_document.try_conv_with(&world)?; let file = world.analysis().file_syntax(file_id); let line_index = world.analysis().file_line_index(file_id); - let res = params.offsets + let res = params + .offsets .into_iter() .map_conv_with(&line_index) .map(|offset| { - world.analysis().matching_brace(&file, offset).unwrap_or(offset) + world + .analysis() + .matching_brace(&file, offset) + .unwrap_or(offset) }) .map_conv_with(&line_index) .collect(); @@ -73,7 +76,9 @@ pub fn handle_join_lines( let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id); let range = params.range.conv_with(&line_index); - world.analysis().join_lines(file_id, range) + world + .analysis() + .join_lines(file_id, range) .try_conv_with(&world) } @@ -87,7 +92,7 @@ pub fn handle_on_enter( let offset = params.position.conv_with(&line_index); match world.analysis().on_enter(file_id, offset) { None => Ok(None), - Some(edit) => Ok(Some(edit.try_conv_with(&world)?)) + Some(edit) => Ok(Some(edit.try_conv_with(&world)?)), } } @@ -158,7 +163,9 @@ pub fn handle_workspace_symbol( let all_symbols = params.query.contains("#"); let libs = params.query.contains("*"); let query = { - let query: String = params.query.chars() + let query: String = params + .query + .chars() .filter(|&c| c != '#' && c != '*') .collect(); let mut q = Query::new(query); @@ -180,22 +187,23 @@ pub fn handle_workspace_symbol( return Ok(Some(res)); - fn exec_query(world: &ServerWorld, query: Query, token: &JobToken) -> Result> { + fn exec_query( + world: &ServerWorld, + query: Query, + token: &JobToken, + ) -> Result> { let mut res = Vec::new(); for (file_id, symbol) in world.analysis().symbol_search(query, token) { let line_index = world.analysis().file_line_index(file_id); let info = SymbolInformation { name: symbol.name.to_string(), kind: symbol.kind.conv(), - location: to_location( - file_id, symbol.node_range, - world, &line_index - )?, + location: to_location(file_id, symbol.node_range, world, &line_index)?, container_name: None, deprecated: None, }; res.push(info); - }; + } Ok(res) } } @@ -209,12 +217,12 @@ pub fn handle_goto_definition( let line_index = world.analysis().file_line_index(file_id); let offset = params.position.conv_with(&line_index); let mut res = Vec::new(); - for (file_id, symbol) in world.analysis().approximately_resolve_symbol(file_id, offset, &token) { + for (file_id, symbol) in world + .analysis() + .approximately_resolve_symbol(file_id, offset, &token) + { let line_index = world.analysis().file_line_index(file_id); - let location = to_location( - file_id, symbol.node_range, - &world, &line_index, - )?; + let location = to_location(file_id, symbol.node_range, &world, &line_index)?; res.push(location) } Ok(Some(req::GotoDefinitionResponse::Array(res))) @@ -229,10 +237,7 @@ pub fn handle_parent_module( let mut res = Vec::new(); for (file_id, symbol) in world.analysis().parent_module(file_id) { let line_index = world.analysis().file_line_index(file_id); - let location = to_location( - file_id, symbol.node_range, - &world, &line_index - )?; + let location = to_location(file_id, symbol.node_range, &world, &line_index)?; res.push(location); } Ok(res) @@ -259,21 +264,16 @@ pub fn handle_runnables( let r = req::Runnable { range: runnable.range.conv_with(&line_index), label: match &runnable.kind { - RunnableKind::Test { name } => - format!("test {}", name), - RunnableKind::Bin => - "run binary".to_string(), + RunnableKind::Test { name } => format!("test {}", name), + RunnableKind::Bin => "run binary".to_string(), }, bin: "cargo".to_string(), args, env: { let mut m = FxHashMap::default(); - m.insert( - "RUST_BACKTRACE".to_string(), - "short".to_string(), - ); + m.insert("RUST_BACKTRACE".to_string(), "short".to_string()); m - } + }, }; res.push(r); } @@ -283,10 +283,16 @@ pub fn handle_runnables( let spec = if let Some(&crate_id) = world.analysis().crate_for(file_id).first() { let file_id = world.analysis().crate_root(crate_id); let path = world.path_map.get_path(file_id); - world.workspaces.iter() + world + .workspaces + .iter() .filter_map(|ws| { let tgt = ws.target_by_root(path)?; - Some((tgt.package(ws).name(ws).clone(), tgt.name(ws).clone(), tgt.kind(ws))) + Some(( + tgt.package(ws).name(ws).clone(), + tgt.name(ws).clone(), + tgt.kind(ws), + )) }) .next() } else { @@ -294,22 +300,22 @@ pub fn handle_runnables( }; let mut res = Vec::new(); match kind { - RunnableKind::Test { name } => { - res.push("test".to_string()); - if let Some((pkg_name, tgt_name, tgt_kind)) = spec { - spec_args(pkg_name, tgt_name, tgt_kind, &mut res); - } - res.push("--".to_string()); - res.push(name.to_string()); - res.push("--nocapture".to_string()); + RunnableKind::Test { name } => { + res.push("test".to_string()); + if let Some((pkg_name, tgt_name, tgt_kind)) = spec { + spec_args(pkg_name, tgt_name, tgt_kind, &mut res); } - RunnableKind::Bin => { - res.push("run".to_string()); - if let Some((pkg_name, tgt_name, tgt_kind)) = spec { - spec_args(pkg_name, tgt_name, tgt_kind, &mut res); - } + res.push("--".to_string()); + res.push(name.to_string()); + res.push("--nocapture".to_string()); + } + RunnableKind::Bin => { + res.push("run".to_string()); + if let Some((pkg_name, tgt_name, tgt_kind)) = spec { + spec_args(pkg_name, tgt_name, tgt_kind, &mut res); } } + } res } @@ -362,12 +368,13 @@ pub fn handle_completion( None => return Ok(None), Some(items) => items, }; - let items = items.into_iter() + let items = items + .into_iter() .map(|item| { let mut res = CompletionItem { label: item.label, filter_text: item.lookup, - .. Default::default() + ..Default::default() }; if let Some(snip) = item.snippet { res.insert_text = Some(snip); @@ -389,24 +396,27 @@ pub fn handle_folding_range( let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id); - let res = Some(world.analysis() - .folding_ranges(file_id) - .into_iter() - .map(|fold| { - let kind = match fold.kind { - FoldKind::Comment => FoldingRangeKind::Comment, - FoldKind::Imports => FoldingRangeKind::Imports - }; - let range = fold.range.conv_with(&line_index); - FoldingRange { - start_line: range.start.line, - start_character: Some(range.start.character), - end_line: range.end.line, - end_character: Some(range.start.character), - kind: Some(kind) - } - }) - .collect()); + let res = Some( + world + .analysis() + .folding_ranges(file_id) + .into_iter() + .map(|fold| { + let kind = match fold.kind { + FoldKind::Comment => FoldingRangeKind::Comment, + FoldKind::Imports => FoldingRangeKind::Imports, + }; + let range = fold.range.conv_with(&line_index); + FoldingRange { + start_line: range.start.line, + start_character: Some(range.start.character), + end_line: range.end.line, + end_character: Some(range.start.character), + kind: Some(kind), + } + }) + .collect(), + ); Ok(res) } @@ -422,25 +432,28 @@ pub fn handle_signature_help( let line_index = world.analysis().file_line_index(file_id); let offset = params.position.conv_with(&line_index); - if let Some((descriptor, active_param)) = world.analysis().resolve_callable(file_id, offset, &token) { - let parameters : Vec = - descriptor.params.iter().map(|param| - ParameterInformation { - label: param.clone(), - documentation: None - } - ).collect(); + if let Some((descriptor, active_param)) = + world.analysis().resolve_callable(file_id, offset, &token) + { + let parameters: Vec = descriptor + .params + .iter() + .map(|param| ParameterInformation { + label: param.clone(), + documentation: None, + }) + .collect(); let sig_info = SignatureInformation { label: descriptor.label, documentation: None, - parameters: Some(parameters) + parameters: Some(parameters), }; Ok(Some(req::SignatureHelp { signatures: vec![sig_info], active_signature: Some(0), - active_parameter: active_param.map(|a| a as u64) + active_parameter: active_param.map(|a| a as u64), })) } else { Ok(None) @@ -457,7 +470,10 @@ pub fn handle_code_action( let range = params.range.conv_with(&line_index); let assists = world.analysis().assists(file_id, range).into_iter(); - let fixes = world.analysis().diagnostics(file_id).into_iter() + let fixes = world + .analysis() + .diagnostics(file_id) + .into_iter() .filter_map(|d| Some((d.range, d.fix?))) .filter(|(range, _fix)| contains_offset_nonstrict(*range, range.start())) .map(|(_range, fix)| fix); @@ -483,7 +499,9 @@ pub fn publish_diagnostics( ) -> Result { let uri = world.file_id_to_uri(file_id)?; let line_index = world.analysis().file_line_index(file_id); - let diagnostics = world.analysis().diagnostics(file_id) + let diagnostics = world + .analysis() + .diagnostics(file_id) .into_iter() .map(|d| Diagnostic { range: d.range.conv_with(&line_index), @@ -492,7 +510,8 @@ pub fn publish_diagnostics( source: Some("rust-analyzer".to_string()), message: d.message, related_information: None, - }).collect(); + }) + .collect(); Ok(req::PublishDiagnosticsParams { uri, diagnostics }) } @@ -509,10 +528,13 @@ pub fn publish_decorations( fn highlight(world: &ServerWorld, file_id: FileId) -> Vec { let line_index = world.analysis().file_line_index(file_id); - world.analysis().highlight(file_id) + world + .analysis() + .highlight(file_id) .into_iter() .map(|h| Decoration { range: h.range.conv_with(&line_index), tag: h.tag, - }).collect() + }) + .collect() } diff --git a/crates/ra_lsp_server/src/main_loop/mod.rs b/crates/ra_lsp_server/src/main_loop/mod.rs index cf2477cb5..a11baf4aa 100644 --- a/crates/ra_lsp_server/src/main_loop/mod.rs +++ b/crates/ra_lsp_server/src/main_loop/mod.rs @@ -1,29 +1,26 @@ mod handlers; mod subscriptions; -use std::{ - path::PathBuf, -}; +use std::path::PathBuf; -use serde::{Serialize, de::DeserializeOwned}; -use crossbeam_channel::{unbounded, Sender, Receiver}; -use rayon::{self, ThreadPool}; -use languageserver_types::{NumberOrString}; -use ra_analysis::{FileId, JobHandle, JobToken, LibraryData}; +use crossbeam_channel::{unbounded, Receiver, Sender}; use gen_lsp_server::{ - RawRequest, RawNotification, RawMessage, RawResponse, ErrorCode, - handle_shutdown, + handle_shutdown, ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse, }; +use languageserver_types::NumberOrString; +use ra_analysis::{FileId, JobHandle, JobToken, LibraryData}; +use rayon::{self, ThreadPool}; use rustc_hash::FxHashMap; +use serde::{de::DeserializeOwned, Serialize}; use crate::{ + main_loop::subscriptions::Subscriptions, + project_model::{workspace_loader, CargoWorkspace}, req, - Result, - vfs::{self, FileEvent}, - server_world::{ServerWorldState, ServerWorld}, - main_loop::subscriptions::{Subscriptions}, - project_model::{CargoWorkspace, workspace_loader}, + server_world::{ServerWorld, ServerWorldState}, thread_watcher::Worker, + vfs::{self, FileEvent}, + Result, }; #[derive(Debug)] @@ -147,56 +144,50 @@ fn main_loop_inner( } state_changed = true; } - Event::Ws(ws) => { - match ws { - Ok(ws) => { - let workspaces = vec![ws]; - feedback(internal_mode, "workspace loaded", msg_sender); - for ws in workspaces.iter() { - for pkg in ws.packages().filter(|pkg| !pkg.is_member(ws)) { - debug!("sending root, {}", pkg.root(ws).to_path_buf().display()); - fs_worker.send(pkg.root(ws).to_path_buf()); - } + Event::Ws(ws) => match ws { + Ok(ws) => { + let workspaces = vec![ws]; + feedback(internal_mode, "workspace loaded", msg_sender); + for ws in workspaces.iter() { + for pkg in ws.packages().filter(|pkg| !pkg.is_member(ws)) { + debug!("sending root, {}", pkg.root(ws).to_path_buf().display()); + fs_worker.send(pkg.root(ws).to_path_buf()); } - state.set_workspaces(workspaces); - state_changed = true; } - Err(e) => warn!("loading workspace failed: {}", e), + state.set_workspaces(workspaces); + state_changed = true; } - } + Err(e) => warn!("loading workspace failed: {}", e), + }, Event::Lib(lib) => { feedback(internal_mode, "library loaded", msg_sender); state.add_lib(lib); } - Event::Msg(msg) => { - match msg { - RawMessage::Request(req) => { - let req = match handle_shutdown(req, msg_sender) { - Some(req) => req, - None => return Ok(()), - }; - match on_request(state, pending_requests, pool, &task_sender, req)? { - None => (), - Some(req) => { - error!("unknown request: {:?}", req); - let resp = RawResponse::err( - req.id, - ErrorCode::MethodNotFound as i32, - "unknown request".to_string(), - ); - msg_sender.send(RawMessage::Response(resp)) - } + Event::Msg(msg) => match msg { + RawMessage::Request(req) => { + let req = match handle_shutdown(req, msg_sender) { + Some(req) => req, + None => return Ok(()), + }; + match on_request(state, pending_requests, pool, &task_sender, req)? { + None => (), + Some(req) => { + error!("unknown request: {:?}", req); + let resp = RawResponse::err( + req.id, + ErrorCode::MethodNotFound as i32, + "unknown request".to_string(), + ); + msg_sender.send(RawMessage::Response(resp)) } } - RawMessage::Notification(not) => { - on_notification(msg_sender, state, pending_requests, subs, not)?; - state_changed = true; - } - RawMessage::Response(resp) => { - error!("unexpected response: {:?}", resp) - } } - } + RawMessage::Notification(not) => { + on_notification(msg_sender, state, pending_requests, subs, not)?; + state_changed = true; + } + RawMessage::Response(resp) => error!("unexpected response: {:?}", resp), + }, }; if state_changed { @@ -222,8 +213,7 @@ fn on_task( } msg_sender.send(RawMessage::Response(response)) } - Task::Notify(n) => - msg_sender.send(RawMessage::Notification(n)), + Task::Notify(n) => msg_sender.send(RawMessage::Notification(n)), } } @@ -237,7 +227,9 @@ fn on_request( let mut pool_dispatcher = PoolDispatcher { req: Some(req), res: None, - pool, world, sender + pool, + world, + sender, }; let req = pool_dispatcher .on::(handlers::handle_syntax_tree)? @@ -262,7 +254,7 @@ fn on_request( let inserted = pending_requests.insert(id, handle).is_none(); assert!(inserted, "duplicate request: {}", id); Ok(None) - }, + } Err(req) => Ok(Some(req)), } } @@ -285,45 +277,53 @@ fn on_notification( if let Some(handle) = pending_requests.remove(&id) { handle.cancel(); } - return Ok(()) + return Ok(()); } Err(not) => not, }; let not = match not.cast::() { Ok(params) => { let uri = params.text_document.uri; - let path = uri.to_file_path() + let path = uri + .to_file_path() .map_err(|()| format_err!("invalid uri: {}", uri))?; let file_id = state.add_mem_file(path, params.text_document.text); subs.add_sub(file_id); - return Ok(()) + return Ok(()); } Err(not) => not, }; let not = match not.cast::() { Ok(mut params) => { let uri = params.text_document.uri; - let path = uri.to_file_path() + let path = uri + .to_file_path() .map_err(|()| format_err!("invalid uri: {}", uri))?; - let text = params.content_changes.pop() + let text = params + .content_changes + .pop() .ok_or_else(|| format_err!("empty changes"))? .text; state.change_mem_file(path.as_path(), text)?; - return Ok(()) + return Ok(()); } Err(not) => not, }; let not = match not.cast::() { Ok(params) => { let uri = params.text_document.uri; - let path = uri.to_file_path() + let path = uri + .to_file_path() .map_err(|()| format_err!("invalid uri: {}", uri))?; let file_id = state.remove_mem_file(path.as_path())?; subs.remove_sub(file_id); - let params = req::PublishDiagnosticsParams { uri, diagnostics: Vec::new() }; + let params = req::PublishDiagnosticsParams { + uri, + diagnostics: Vec::new(), + }; let not = RawNotification::new::(¶ms); msg_sender.send(RawMessage::Notification(not)); - return Ok(()) + return Ok(()); } Err(not) => not, }; @@ -342,11 +342,12 @@ struct PoolDispatcher<'a> { impl<'a> PoolDispatcher<'a> { fn on<'b, R>( &'b mut self, - f: fn(ServerWorld, R::Params, JobToken) -> Result + f: fn(ServerWorld, R::Params, JobToken) -> Result, ) -> Result<&'b mut Self> - where R: req::Request, - R::Params: DeserializeOwned + Send + 'static, - R::Result: Serialize + 'static, + where + R: req::Request, + R::Params: DeserializeOwned + Send + 'static, + R::Result: Serialize + 'static, { let req = match self.req.take() { None => return Ok(self), @@ -360,16 +361,16 @@ impl<'a> PoolDispatcher<'a> { self.pool.spawn(move || { let resp = match f(world, params, token) { Ok(resp) => RawResponse::ok::(id, &resp), - Err(e) => RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string()), + Err(e) => { + RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string()) + } }; let task = Task::Respond(resp); sender.send(task); }); self.res = Some((id, handle)); } - Err(req) => { - self.req = Some(req) - } + Err(req) => self.req = Some(req), } Ok(self) } @@ -392,18 +393,14 @@ fn update_file_notifications_on_threadpool( pool.spawn(move || { for file_id in subscriptions { match handlers::publish_diagnostics(&world, file_id) { - Err(e) => { - error!("failed to compute diagnostics: {:?}", e) - } + Err(e) => error!("failed to compute diagnostics: {:?}", e), Ok(params) => { let not = RawNotification::new::(¶ms); sender.send(Task::Notify(not)); } } match handlers::publish_decorations(&world, file_id) { - Err(e) => { - error!("failed to compute decorations: {:?}", e) - } + Err(e) => error!("failed to compute decorations: {:?}", e), Ok(params) => { let not = RawNotification::new::(¶ms); sender.send(Task::Notify(not)) diff --git a/crates/ra_lsp_server/src/main_loop/subscriptions.rs b/crates/ra_lsp_server/src/main_loop/subscriptions.rs index 310153382..03f41e870 100644 --- a/crates/ra_lsp_server/src/main_loop/subscriptions.rs +++ b/crates/ra_lsp_server/src/main_loop/subscriptions.rs @@ -1,5 +1,5 @@ -use rustc_hash::FxHashSet; use ra_analysis::FileId; +use rustc_hash::FxHashSet; pub struct Subscriptions { subs: FxHashSet, @@ -7,7 +7,9 @@ pub struct Subscriptions { impl Subscriptions { pub fn new() -> Subscriptions { - Subscriptions { subs: FxHashSet::default() } + Subscriptions { + subs: FxHashSet::default(), + } } pub fn add_sub(&mut self, file_id: FileId) { self.subs.insert(file_id); -- cgit v1.2.3