Skip to content

Commit 227bc0b

Browse files
committed
add try_conv_with_to_vec
1 parent 5ce2b48 commit 227bc0b

File tree

2 files changed

+54
-40
lines changed

2 files changed

+54
-40
lines changed

crates/ra_lsp_server/src/conv.rs

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -384,27 +384,32 @@ impl TryConvWith for &NavigationTarget {
384384
}
385385
}
386386

387-
pub fn to_location_link(
388-
target: &RangeInfo<NavigationTarget>,
389-
world: &WorldSnapshot,
390-
// line index for original range file
391-
line_index: &LineIndex,
392-
) -> Result<LocationLink> {
393-
let target_uri = target.info.file_id().try_conv_with(world)?;
394-
let tgt_line_index = world.analysis().file_line_index(target.info.file_id());
395-
396-
let target_range = target.info.full_range().conv_with(&tgt_line_index);
397-
398-
let target_selection_range =
399-
target.info.focus_range().map(|it| it.conv_with(&tgt_line_index)).unwrap_or(target_range);
400-
401-
let res = LocationLink {
402-
origin_selection_range: Some(target.range.conv_with(line_index)),
403-
target_uri,
404-
target_range,
405-
target_selection_range,
406-
};
407-
Ok(res)
387+
impl TryConvWith for (FileId, RangeInfo<NavigationTarget>) {
388+
type Ctx = WorldSnapshot;
389+
type Output = LocationLink;
390+
fn try_conv_with(self, world: &WorldSnapshot) -> Result<LocationLink> {
391+
let (src_file_id, target) = self;
392+
393+
let target_uri = target.info.file_id().try_conv_with(world)?;
394+
let src_line_index = world.analysis().file_line_index(src_file_id);
395+
let tgt_line_index = world.analysis().file_line_index(target.info.file_id());
396+
397+
let target_range = target.info.full_range().conv_with(&tgt_line_index);
398+
399+
let target_selection_range = target
400+
.info
401+
.focus_range()
402+
.map(|it| it.conv_with(&tgt_line_index))
403+
.unwrap_or(target_range);
404+
405+
let res = LocationLink {
406+
origin_selection_range: Some(target.range.conv_with(&src_line_index)),
407+
target_uri,
408+
target_range,
409+
target_selection_range,
410+
};
411+
Ok(res)
412+
}
408413
}
409414

410415
pub fn to_location(
@@ -452,3 +457,23 @@ where
452457
self.iter.next().map(|item| item.conv_with(self.ctx))
453458
}
454459
}
460+
461+
pub trait TryConvWithToVec<'a>: Sized + 'a {
462+
type Ctx;
463+
type Output;
464+
465+
fn try_conv_with_to_vec(self, ctx: &'a Self::Ctx) -> Result<Vec<Self::Output>>;
466+
}
467+
468+
impl<'a, I> TryConvWithToVec<'a> for I
469+
where
470+
I: Iterator + 'a,
471+
I::Item: TryConvWith,
472+
{
473+
type Ctx = <I::Item as TryConvWith>::Ctx;
474+
type Output = <I::Item as TryConvWith>::Output;
475+
476+
fn try_conv_with_to_vec(self, ctx: &'a Self::Ctx) -> Result<Vec<Self::Output>> {
477+
self.map(|it| it.try_conv_with(ctx)).collect()
478+
}
479+
}

crates/ra_lsp_server/src/main_loop/handlers.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use url_serde::Ser;
2121

2222
use crate::{
2323
cargo_target_spec::{runnable_args, CargoTargetSpec},
24-
conv::{to_location, to_location_link, Conv, ConvWith, MapConvWith, TryConvWith},
24+
conv::{to_location, Conv, ConvWith, MapConvWith, TryConvWith, TryConvWithToVec},
2525
req::{self, Decoration},
2626
world::WorldSnapshot,
2727
LspError, Result,
@@ -263,7 +263,6 @@ pub fn handle_goto_definition(
263263
params: req::TextDocumentPositionParams,
264264
) -> Result<Option<req::GotoDefinitionResponse>> {
265265
let position = params.try_conv_with(&world)?;
266-
let line_index = world.analysis().file_line_index(position.file_id);
267266
let nav_info = match world.analysis().goto_definition(position)? {
268267
None => return Ok(None),
269268
Some(it) => it,
@@ -272,9 +271,8 @@ pub fn handle_goto_definition(
272271
let res = nav_info
273272
.info
274273
.into_iter()
275-
.map(|nav| RangeInfo::new(nav_range, nav))
276-
.map(|nav| to_location_link(&nav, &world, &line_index))
277-
.collect::<Result<Vec<_>>>()?;
274+
.map(|nav| (position.file_id, RangeInfo::new(nav_range, nav)))
275+
.try_conv_with_to_vec(&world)?;
278276
Ok(Some(res.into()))
279277
}
280278

@@ -283,7 +281,6 @@ pub fn handle_goto_implementation(
283281
params: req::TextDocumentPositionParams,
284282
) -> Result<Option<req::GotoImplementationResponse>> {
285283
let position = params.try_conv_with(&world)?;
286-
let line_index = world.analysis().file_line_index(position.file_id);
287284
let nav_info = match world.analysis().goto_implementation(position)? {
288285
None => return Ok(None),
289286
Some(it) => it,
@@ -292,9 +289,8 @@ pub fn handle_goto_implementation(
292289
let res = nav_info
293290
.info
294291
.into_iter()
295-
.map(|nav| RangeInfo::new(nav_range, nav))
296-
.map(|nav| to_location_link(&nav, &world, &line_index))
297-
.collect::<Result<Vec<_>>>()?;
292+
.map(|nav| (position.file_id, RangeInfo::new(nav_range, nav)))
293+
.try_conv_with_to_vec(&world)?;
298294
Ok(Some(res.into()))
299295
}
300296

@@ -303,7 +299,6 @@ pub fn handle_goto_type_definition(
303299
params: req::TextDocumentPositionParams,
304300
) -> Result<Option<req::GotoTypeDefinitionResponse>> {
305301
let position = params.try_conv_with(&world)?;
306-
let line_index = world.analysis().file_line_index(position.file_id);
307302
let nav_info = match world.analysis().goto_type_definition(position)? {
308303
None => return Ok(None),
309304
Some(it) => it,
@@ -312,9 +307,8 @@ pub fn handle_goto_type_definition(
312307
let res = nav_info
313308
.info
314309
.into_iter()
315-
.map(|nav| RangeInfo::new(nav_range, nav))
316-
.map(|nav| to_location_link(&nav, &world, &line_index))
317-
.collect::<Result<Vec<_>>>()?;
310+
.map(|nav| (position.file_id, RangeInfo::new(nav_range, nav)))
311+
.try_conv_with_to_vec(&world)?;
318312
Ok(Some(res.into()))
319313
}
320314

@@ -323,12 +317,7 @@ pub fn handle_parent_module(
323317
params: req::TextDocumentPositionParams,
324318
) -> Result<Vec<Location>> {
325319
let position = params.try_conv_with(&world)?;
326-
world
327-
.analysis()
328-
.parent_module(position)?
329-
.into_iter()
330-
.map(|nav| nav.try_conv_with(&world))
331-
.collect::<Result<Vec<_>>>()
320+
world.analysis().parent_module(position)?.iter().try_conv_with_to_vec(&world)
332321
}
333322

334323
pub fn handle_runnables(

0 commit comments

Comments
 (0)