Skip to content

Commit 0790356

Browse files
committed
Auto merge of rust-lang#18300 - krobelus:clamp-position-character-2, r=Veykril
Clamp Position::character to line length 2/2 Completes rust-lang/rust-analyzer#18243 I don't think I have permissions to target this on the other PR, so we'll need to rebase manually
2 parents 8305c19 + 00318ba commit 0790356

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/tools/rust-analyzer/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ test-fixture = { path = "./crates/test-fixture" }
9696
test-utils = { path = "./crates/test-utils" }
9797

9898
# In-tree crates that are published separately and follow semver. See lib/README.md
99-
line-index = { version = "0.1.1" }
99+
line-index = { version = "0.1.2" }
100100
la-arena = { version = "0.3.1" }
101101
lsp-server = { version = "0.7.6" }
102102

src/tools/rust-analyzer/crates/rust-analyzer/src/lsp/from_proto.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,18 @@ pub(crate) fn offset(
3535
.ok_or_else(|| format_err!("Invalid wide col offset"))?
3636
}
3737
};
38-
let text_size = line_index.index.offset(line_col).ok_or_else(|| {
38+
let line_range = line_index.index.line(line_col.line).ok_or_else(|| {
3939
format_err!("Invalid offset {line_col:?} (line index length: {:?})", line_index.index.len())
4040
})?;
41-
Ok(text_size)
41+
let col = TextSize::from(line_col.col);
42+
let clamped_len = col.min(line_range.len());
43+
if clamped_len < col {
44+
tracing::error!(
45+
"Position {line_col:?} column exceeds line length {}, clamping it",
46+
u32::from(line_range.len()),
47+
);
48+
}
49+
Ok(line_range.start() + clamped_len)
4250
}
4351

4452
pub(crate) fn text_range(

0 commit comments

Comments
 (0)