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 ++++++++++++++----------- 1 file changed, 114 insertions(+), 92 deletions(-) (limited to 'crates/ra_lsp_server/src/main_loop/handlers.rs') 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() } -- cgit v1.2.3