Skip to content

Commit 6cc3393

Browse files
MichaReiserGankra
andauthored
[ty] Make range/position conversions fallible (#21297)
Co-authored-by: Aria Desires <aria.desires@gmail.com>
1 parent c7ff982 commit 6cc3393

19 files changed

+237
-223
lines changed

crates/ty_server/src/document/location.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub(crate) trait ToLink {
2020
impl ToLink for NavigationTarget {
2121
fn to_location(&self, db: &dyn Db, encoding: PositionEncoding) -> Option<Location> {
2222
FileRange::new(self.file(), self.focus_range())
23-
.as_lsp_range(db, encoding)
23+
.to_lsp_range(db, encoding)?
2424
.to_location()
2525
}
2626

@@ -35,17 +35,17 @@ impl ToLink for NavigationTarget {
3535
// Get target_range and URI together to ensure they're consistent (same cell for notebooks)
3636
let target_location = self
3737
.full_range()
38-
.as_lsp_range(db, file, encoding)
39-
.to_location()?;
38+
.to_lsp_range(db, file, encoding)?
39+
.into_location()?;
4040
let target_range = target_location.range;
4141

4242
// For selection_range, we can use as_local_range since we know it's in the same document/cell
4343
let selection_range = self
4444
.focus_range()
45-
.as_lsp_range(db, file, encoding)
46-
.to_local_range();
45+
.to_lsp_range(db, file, encoding)?
46+
.local_range();
4747

48-
let src = src.map(|src| src.as_lsp_range(db, encoding).to_local_range());
48+
let src = src.and_then(|src| Some(src.to_lsp_range(db, encoding)?.local_range()));
4949

5050
Some(lsp_types::LocationLink {
5151
target_uri: target_location.uri,
@@ -58,7 +58,9 @@ impl ToLink for NavigationTarget {
5858

5959
impl ToLink for ReferenceTarget {
6060
fn to_location(&self, db: &dyn Db, encoding: PositionEncoding) -> Option<Location> {
61-
self.file_range().as_lsp_range(db, encoding).to_location()
61+
self.file_range()
62+
.to_lsp_range(db, encoding)?
63+
.into_location()
6264
}
6365

6466
fn to_link(
@@ -70,12 +72,12 @@ impl ToLink for ReferenceTarget {
7072
// Get target_range and URI together to ensure they're consistent (same cell for notebooks)
7173
let target_location = self
7274
.range()
73-
.as_lsp_range(db, self.file(), encoding)
74-
.to_location()?;
75+
.to_lsp_range(db, self.file(), encoding)?
76+
.into_location()?;
7577
let target_range = target_location.range;
7678
let selection_range = target_range;
7779

78-
let src = src.map(|src| src.as_lsp_range(db, encoding).to_local_range());
80+
let src = src.and_then(|src| Some(src.to_lsp_range(db, encoding)?.local_range()));
7981

8082
Some(lsp_types::LocationLink {
8183
target_uri: target_location.uri,

0 commit comments

Comments
 (0)