about summary refs log tree commit diff
path: root/src/tools/rust-analyzer/lib
diff options
context:
space:
mode:
authorLaurențiu Nicola <lnicola@dend.ro>2024-03-10 08:47:38 +0200
committerLaurențiu Nicola <lnicola@dend.ro>2024-03-10 08:47:38 +0200
commit56493e4cbd6262adae03e73aefb8a9e618a9fc2d (patch)
tree404d4a38aff53e1c880c2708478fdcaf0b2f6e61 /src/tools/rust-analyzer/lib
parent5bc7b9ac8ace5312e1d2cdc2722715cf58d4f926 (diff)
parent574e23ec508064613783cba3d1833a95fd9a5080 (diff)
downloadrust-56493e4cbd6262adae03e73aefb8a9e618a9fc2d.tar.gz
rust-56493e4cbd6262adae03e73aefb8a9e618a9fc2d.zip
Merge commit '574e23ec508064613783cba3d1833a95fd9a5080' into sync-from-ra
Diffstat (limited to 'src/tools/rust-analyzer/lib')
-rw-r--r--src/tools/rust-analyzer/lib/line-index/Cargo.toml5
-rw-r--r--src/tools/rust-analyzer/lib/line-index/src/tests.rs53
-rw-r--r--src/tools/rust-analyzer/lib/lsp-server/src/req_queue.rs6
3 files changed, 60 insertions, 4 deletions
diff --git a/src/tools/rust-analyzer/lib/line-index/Cargo.toml b/src/tools/rust-analyzer/lib/line-index/Cargo.toml
index 77e187de1ed..8ae4954dd0d 100644
--- a/src/tools/rust-analyzer/lib/line-index/Cargo.toml
+++ b/src/tools/rust-analyzer/lib/line-index/Cargo.toml
@@ -10,5 +10,8 @@ edition = "2021"
 text-size = "1.1.1"
 nohash-hasher = "0.2.0"
 
+[dev-dependencies]
+oorandom = "11.1.3"
+
 [lints]
-workspace = true
\ No newline at end of file
+workspace = true
diff --git a/src/tools/rust-analyzer/lib/line-index/src/tests.rs b/src/tools/rust-analyzer/lib/line-index/src/tests.rs
index 981008e346b..57fad1dfc05 100644
--- a/src/tools/rust-analyzer/lib/line-index/src/tests.rs
+++ b/src/tools/rust-analyzer/lib/line-index/src/tests.rs
@@ -142,3 +142,56 @@ fn test_to_wide() {
     let wide_line_col = line_index.to_wide(WideEncoding::Utf16, line_col.unwrap());
     assert_eq!(wide_line_col, Some(WideLineCol { line: 5, col: 4 }));
 }
+
+#[test]
+fn test_every_chars() {
+    let text: String = {
+        let mut chars: Vec<char> = ((0 as char)..char::MAX).collect(); // Neat!
+        chars.extend("\n".repeat(chars.len() / 16).chars());
+        let seed = std::hash::Hasher::finish(&std::hash::BuildHasher::build_hasher(
+            #[allow(clippy::disallowed_types)]
+            &std::collections::hash_map::RandomState::new(),
+        ));
+        let mut rng = oorandom::Rand32::new(seed);
+        let mut rand_index = |i| rng.rand_range(0..i as u32) as usize;
+        let mut remaining = chars.len() - 1;
+        while remaining > 0 {
+            let index = rand_index(remaining);
+            chars.swap(remaining, index);
+            remaining -= 1;
+        }
+        chars.into_iter().collect()
+    };
+    assert!(text.contains('💩')); // Sanity check.
+
+    let line_index = LineIndex::new(&text);
+
+    let mut lin_col = LineCol { line: 0, col: 0 };
+    let mut col_utf16 = 0;
+    let mut col_utf32 = 0;
+    for (offset, c) in text.char_indices() {
+        let got_offset = line_index.offset(lin_col).unwrap();
+        assert_eq!(usize::from(got_offset), offset);
+
+        let got_lin_col = line_index.line_col(got_offset);
+        assert_eq!(got_lin_col, lin_col);
+
+        for (enc, col) in [(WideEncoding::Utf16, col_utf16), (WideEncoding::Utf32, col_utf32)] {
+            let wide_lin_col = line_index.to_wide(enc, lin_col).unwrap();
+            let got_lin_col = line_index.to_utf8(enc, wide_lin_col).unwrap();
+            assert_eq!(got_lin_col, lin_col);
+            assert_eq!(wide_lin_col.col, col)
+        }
+
+        if c == '\n' {
+            lin_col.line += 1;
+            lin_col.col = 0;
+            col_utf16 = 0;
+            col_utf32 = 0;
+        } else {
+            lin_col.col += c.len_utf8() as u32;
+            col_utf16 += c.len_utf16() as u32;
+            col_utf32 += 1;
+        }
+    }
+}
diff --git a/src/tools/rust-analyzer/lib/lsp-server/src/req_queue.rs b/src/tools/rust-analyzer/lib/lsp-server/src/req_queue.rs
index 7b47f5388b5..347a9fb6fb9 100644
--- a/src/tools/rust-analyzer/lib/lsp-server/src/req_queue.rs
+++ b/src/tools/rust-analyzer/lib/lsp-server/src/req_queue.rs
@@ -37,7 +37,7 @@ impl<I> Incoming<I> {
     }
 
     pub fn cancel(&mut self, id: RequestId) -> Option<Response> {
-        let _data = self.complete(id.clone())?;
+        let _data = self.complete(&id)?;
         let error = ResponseError {
             code: ErrorCode::RequestCanceled as i32,
             message: "canceled by client".to_owned(),
@@ -46,8 +46,8 @@ impl<I> Incoming<I> {
         Some(Response { id, result: None, error: Some(error) })
     }
 
-    pub fn complete(&mut self, id: RequestId) -> Option<I> {
-        self.pending.remove(&id)
+    pub fn complete(&mut self, id: &RequestId) -> Option<I> {
+        self.pending.remove(id)
     }
 
     pub fn is_completed(&self, id: &RequestId) -> bool {