Unnamed repository; edit this file 'description' to name the repository.
Auto merge of #18300 - krobelus:clamp-position-character-2, r=Veykril
Clamp Position::character to line length 2/2 Completes https://github.com/rust-lang/rust-analyzer/pull/18243 I don't think I have permissions to target this on the other PR, so we'll need to rebase manually
bors 2024-10-18
parent 72b214f · parent 94a4c3a · commit 3ddfb0d
-rw-r--r--Cargo.toml2
-rw-r--r--crates/rust-analyzer/src/lsp/from_proto.rs12
2 files changed, 11 insertions, 3 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 8c099f324b..17cefe941e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -96,7 +96,7 @@ test-fixture = { path = "./crates/test-fixture" }
test-utils = { path = "./crates/test-utils" }
# In-tree crates that are published separately and follow semver. See lib/README.md
-line-index = { version = "0.1.1" }
+line-index = { version = "0.1.2" }
la-arena = { version = "0.3.1" }
lsp-server = { version = "0.7.6" }
diff --git a/crates/rust-analyzer/src/lsp/from_proto.rs b/crates/rust-analyzer/src/lsp/from_proto.rs
index 1f4544887f..47e9961cf1 100644
--- a/crates/rust-analyzer/src/lsp/from_proto.rs
+++ b/crates/rust-analyzer/src/lsp/from_proto.rs
@@ -35,10 +35,18 @@ pub(crate) fn offset(
.ok_or_else(|| format_err!("Invalid wide col offset"))?
}
};
- let text_size = line_index.index.offset(line_col).ok_or_else(|| {
+ let line_range = line_index.index.line(line_col.line).ok_or_else(|| {
format_err!("Invalid offset {line_col:?} (line index length: {:?})", line_index.index.len())
})?;
- Ok(text_size)
+ let col = TextSize::from(line_col.col);
+ let clamped_len = col.min(line_range.len());
+ if clamped_len < col {
+ tracing::error!(
+ "Position {line_col:?} column exceeds line length {}, clamping it",
+ u32::from(line_range.len()),
+ );
+ }
+ Ok(line_range.start() + clamped_len)
}
pub(crate) fn text_range(