From 85a6bf342490f2a8be34ea53af9eb8fcdcad2b38 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Sun, 17 Feb 2019 13:38:32 +0200 Subject: Refactor find_all_refs to return ReferenceSearchResult --- crates/ra_lsp_server/src/conv.rs | 2 +- crates/ra_lsp_server/src/main_loop/handlers.rs | 38 +++++++++++++++++++------- 2 files changed, 29 insertions(+), 11 deletions(-) (limited to 'crates/ra_lsp_server/src') diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 20077a48a..c3192a1e5 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -333,7 +333,7 @@ impl TryConvWith for &NavigationTarget { type Output = Location; fn try_conv_with(self, world: &ServerWorld) -> Result { let line_index = world.analysis().file_line_index(self.file_id()); - let range = self.focus_range().unwrap_or(self.full_range()); + let range = self.range(); to_location(self.file_id(), range, &world, &line_index) } } diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 09d896c40..9208ee473 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -456,14 +456,16 @@ pub fn handle_prepare_rename( // We support renaming references like handle_rename does. // In the future we may want to reject the renaming of things like keywords here too. - let refs = world.analysis().find_all_refs(position)?; - let r = match refs.first() { - Some(r) => r, + let refs = match world.analysis().find_all_refs(position)? { None => return Ok(None), + Some(refs) => refs, }; + + // Refs should always have a declaration + let r = refs.declaration(); let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id); - let loc = to_location(r.0, r.1, &world, &line_index)?; + let loc = to_location(r.file_id(), r.range(), &world, &line_index)?; Ok(Some(PrepareRenameResponse::Range(loc.range))) } @@ -501,11 +503,24 @@ pub fn handle_references( let line_index = world.analysis().file_line_index(file_id); let offset = params.position.conv_with(&line_index); - let refs = world.analysis().find_all_refs(FilePosition { file_id, offset })?; + let refs = match world.analysis().find_all_refs(FilePosition { file_id, offset })? { + None => return Ok(None), + Some(refs) => refs, + }; - Ok(Some( - refs.into_iter().filter_map(|r| to_location(r.0, r.1, &world, &line_index).ok()).collect(), - )) + let locations = if params.context.include_declaration { + refs.into_iter() + .filter_map(|r| to_location(r.file_id, r.range, &world, &line_index).ok()) + .collect() + } else { + // Only iterate over the references if include_declaration was false + refs.references() + .iter() + .filter_map(|r| to_location(r.file_id, r.range, &world, &line_index).ok()) + .collect() + }; + + Ok(Some(locations)) } pub fn handle_formatting( @@ -712,11 +727,14 @@ pub fn handle_document_highlight( let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id); - let refs = world.analysis().find_all_refs(params.try_conv_with(&world)?)?; + let refs = match world.analysis().find_all_refs(params.try_conv_with(&world)?)? { + None => return Ok(None), + Some(refs) => refs, + }; Ok(Some( refs.into_iter() - .map(|r| DocumentHighlight { range: r.1.conv_with(&line_index), kind: None }) + .map(|r| DocumentHighlight { range: r.range.conv_with(&line_index), kind: None }) .collect(), )) } -- cgit v1.2.3