From 227bc0b6d478564c45f49cb47dac963d9c37a528 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 8 Jul 2019 13:39:16 +0300 Subject: add try_conv_with_to_vec --- crates/ra_lsp_server/src/conv.rs | 67 +++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 21 deletions(-) (limited to 'crates/ra_lsp_server/src/conv.rs') diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 32e67838e..fc01b1c0e 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -384,27 +384,32 @@ impl TryConvWith for &NavigationTarget { } } -pub fn to_location_link( - target: &RangeInfo, - world: &WorldSnapshot, - // line index for original range file - line_index: &LineIndex, -) -> Result { - let target_uri = target.info.file_id().try_conv_with(world)?; - let tgt_line_index = world.analysis().file_line_index(target.info.file_id()); - - let target_range = target.info.full_range().conv_with(&tgt_line_index); - - let target_selection_range = - target.info.focus_range().map(|it| it.conv_with(&tgt_line_index)).unwrap_or(target_range); - - let res = LocationLink { - origin_selection_range: Some(target.range.conv_with(line_index)), - target_uri, - target_range, - target_selection_range, - }; - Ok(res) +impl TryConvWith for (FileId, RangeInfo) { + type Ctx = WorldSnapshot; + type Output = LocationLink; + fn try_conv_with(self, world: &WorldSnapshot) -> Result { + let (src_file_id, target) = self; + + let target_uri = target.info.file_id().try_conv_with(world)?; + let src_line_index = world.analysis().file_line_index(src_file_id); + let tgt_line_index = world.analysis().file_line_index(target.info.file_id()); + + let target_range = target.info.full_range().conv_with(&tgt_line_index); + + let target_selection_range = target + .info + .focus_range() + .map(|it| it.conv_with(&tgt_line_index)) + .unwrap_or(target_range); + + let res = LocationLink { + origin_selection_range: Some(target.range.conv_with(&src_line_index)), + target_uri, + target_range, + target_selection_range, + }; + Ok(res) + } } pub fn to_location( @@ -452,3 +457,23 @@ where self.iter.next().map(|item| item.conv_with(self.ctx)) } } + +pub trait TryConvWithToVec<'a>: Sized + 'a { + type Ctx; + type Output; + + fn try_conv_with_to_vec(self, ctx: &'a Self::Ctx) -> Result>; +} + +impl<'a, I> TryConvWithToVec<'a> for I +where + I: Iterator + 'a, + I::Item: TryConvWith, +{ + type Ctx = ::Ctx; + type Output = ::Output; + + fn try_conv_with_to_vec(self, ctx: &'a Self::Ctx) -> Result> { + self.map(|it| it.try_conv_with(ctx)).collect() + } +} -- cgit v1.2.3