From ed7ae78c6fd9e508f6e959c6a164cf8481f6b377 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 17 Aug 2018 19:54:08 +0300 Subject: ServerWorld --- crates/server/src/main_loop/handlers.rs | 102 ++++++++++++++------------------ 1 file changed, 46 insertions(+), 56 deletions(-) (limited to 'crates/server/src/main_loop/handlers.rs') diff --git a/crates/server/src/main_loop/handlers.rs b/crates/server/src/main_loop/handlers.rs index f25c64c97..675f69bec 100644 --- a/crates/server/src/main_loop/handlers.rs +++ b/crates/server/src/main_loop/handlers.rs @@ -5,35 +5,33 @@ use languageserver_types::{ Command, TextDocumentIdentifier, WorkspaceEdit, SymbolInformation, Position, }; -use libanalysis::{World, Query}; +use libanalysis::{Query}; use libeditor::{self, CursorPosition}; use libsyntax2::TextUnit; use serde_json::{to_value, from_value}; use ::{ - PathMap, req::{self, Decoration}, Result, conv::{Conv, ConvWith, TryConvWith, MapConvWith, to_location}, + server_world::ServerWorld, }; pub fn handle_syntax_tree( - world: World, - path_map: PathMap, + world: ServerWorld, params: req::SyntaxTreeParams, ) -> Result { - let id = params.text_document.try_conv_with(&path_map)?; - let file = world.file_syntax(id)?; + let id = params.text_document.try_conv_with(&world)?; + let file = world.analysis().file_syntax(id)?; Ok(libeditor::syntax_tree(&file)) } pub fn handle_extend_selection( - world: World, - path_map: PathMap, + world: ServerWorld, params: req::ExtendSelectionParams, ) -> Result { - let file_id = params.text_document.try_conv_with(&path_map)?; - let file = world.file_syntax(file_id)?; - let line_index = world.file_line_index(file_id)?; + 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() .map_conv_with(&line_index) .map(|r| libeditor::extend_selection(&file, r).unwrap_or(r)) @@ -43,13 +41,12 @@ pub fn handle_extend_selection( } pub fn handle_find_matching_brace( - world: World, - path_map: PathMap, + world: ServerWorld, params: req::FindMatchingBraceParams, ) -> Result> { - let file_id = params.text_document.try_conv_with(&path_map)?; - let file = world.file_syntax(file_id)?; - let line_index = world.file_line_index(file_id)?; + 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 .into_iter() .map_conv_with(&line_index) @@ -62,13 +59,12 @@ pub fn handle_find_matching_brace( } pub fn handle_document_symbol( - world: World, - path_map: PathMap, + world: ServerWorld, params: req::DocumentSymbolParams, ) -> Result> { - let file_id = params.text_document.try_conv_with(&path_map)?; - let file = world.file_syntax(file_id)?; - let line_index = world.file_line_index(file_id)?; + 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 mut parents: Vec<(DocumentSymbol, Option)> = Vec::new(); @@ -102,13 +98,12 @@ pub fn handle_document_symbol( } pub fn handle_code_action( - world: World, - path_map: PathMap, + world: ServerWorld, params: req::CodeActionParams, ) -> Result>> { - let file_id = params.text_document.try_conv_with(&path_map)?; - let file = world.file_syntax(file_id)?; - let line_index = world.file_line_index(file_id)?; + 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 offset = params.range.conv_with(&line_index).start(); let mut ret = Vec::new(); @@ -127,8 +122,7 @@ pub fn handle_code_action( } pub fn handle_workspace_symbol( - world: World, - path_map: PathMap, + world: ServerWorld, params: req::WorkspaceSymbolParams, ) -> Result>> { let all_symbols = params.query.contains("#"); @@ -143,25 +137,25 @@ pub fn handle_workspace_symbol( q.limit(128); q }; - let mut res = exec_query(&world, &path_map, query)?; + let mut res = exec_query(&world, query)?; if res.is_empty() && !all_symbols { let mut query = Query::new(params.query); query.limit(128); - res = exec_query(&world, &path_map, query)?; + res = exec_query(&world, query)?; } return Ok(Some(res)); - fn exec_query(world: &World, path_map: &PathMap, query: Query) -> Result> { + fn exec_query(world: &ServerWorld, query: Query) -> Result> { let mut res = Vec::new(); - for (file_id, symbol) in world.world_symbols(query) { - let line_index = world.file_line_index(file_id)?; + for (file_id, symbol) in world.analysis().world_symbols(query) { + 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, - path_map, &line_index + world, &line_index )?, container_name: None, }; @@ -172,19 +166,18 @@ pub fn handle_workspace_symbol( } pub fn handle_goto_definition( - world: World, - path_map: PathMap, + world: ServerWorld, params: req::TextDocumentPositionParams, ) -> Result> { - let file_id = params.text_document.try_conv_with(&path_map)?; - let line_index = world.file_line_index(file_id)?; + let file_id = params.text_document.try_conv_with(&world)?; + 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.approximately_resolve_symbol(file_id, offset)? { - let line_index = world.file_line_index(file_id)?; + for (file_id, symbol) in world.analysis().approximately_resolve_symbol(file_id, offset)? { + let line_index = world.analysis().file_line_index(file_id)?; let location = to_location( file_id, symbol.node_range, - &path_map, &line_index, + &world, &line_index, )?; res.push(location) } @@ -192,8 +185,7 @@ pub fn handle_goto_definition( } pub fn handle_execute_command( - world: World, - path_map: PathMap, + world: ServerWorld, mut params: req::ExecuteCommandParams, ) -> Result<(req::ApplyWorkspaceEditParams, Option)> { if params.command.as_str() != "apply_code_action" { @@ -204,13 +196,13 @@ pub fn handle_execute_command( } let arg = params.arguments.pop().unwrap(); let arg: ActionRequest = from_value(arg)?; - let file_id = arg.text_document.try_conv_with(&path_map)?; - let file = world.file_syntax(file_id)?; + let file_id = arg.text_document.try_conv_with(&world)?; + let file = world.analysis().file_syntax(file_id)?; let action_result = match arg.id { ActionId::FlipComma => libeditor::flip_comma(&file, arg.offset).map(|f| f()), ActionId::AddDerive => libeditor::add_derive(&file, arg.offset).map(|f| f()), }.ok_or_else(|| format_err!("command not applicable"))?; - let line_index = world.file_line_index(file_id)?; + let line_index = world.analysis().file_line_index(file_id)?; let mut changes = HashMap::new(); changes.insert( arg.text_document.uri, @@ -265,13 +257,12 @@ impl ActionId { } pub fn publish_diagnostics( - world: World, - path_map: PathMap, + world: ServerWorld, uri: Url ) -> Result { - let file_id = uri.try_conv_with(&path_map)?; - let file = world.file_syntax(file_id)?; - let line_index = world.file_line_index(file_id)?; + let file_id = world.uri_to_file_id(&uri)?; + let file = world.analysis().file_syntax(file_id)?; + let line_index = world.analysis().file_line_index(file_id)?; let diagnostics = libeditor::diagnostics(&file) .into_iter() .map(|d| Diagnostic { @@ -286,13 +277,12 @@ pub fn publish_diagnostics( } pub fn publish_decorations( - world: World, - path_map: PathMap, + world: ServerWorld, uri: Url ) -> Result { - let file_id = uri.try_conv_with(&path_map)?; - let file = world.file_syntax(file_id)?; - let line_index = world.file_line_index(file_id)?; + let file_id = world.uri_to_file_id(&uri)?; + let file = world.analysis().file_syntax(file_id)?; + let line_index = world.analysis().file_line_index(file_id)?; let decorations = libeditor::highlight(&file) .into_iter() .map(|h| Decoration { -- cgit v1.2.3