Skip to content

Commit 1ce1560

Browse files
Address the feedback from pascalkuthe
* Use Base64 to minify the hash representation in the JSON data * Do hash checks only for items with similar labels
1 parent f94953d commit 1ce1560

File tree

6 files changed

+21
-6
lines changed

6 files changed

+21
-6
lines changed

src/tools/rust-analyzer/Cargo.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ dependencies = [
8484
"vfs",
8585
]
8686

87+
[[package]]
88+
name = "base64"
89+
version = "0.22.1"
90+
source = "registry+https://github.com/rust-lang/crates.io-index"
91+
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
92+
8793
[[package]]
8894
name = "bitflags"
8995
version = "1.3.2"
@@ -1649,6 +1655,7 @@ version = "0.0.0"
16491655
dependencies = [
16501656
"always-assert",
16511657
"anyhow",
1658+
"base64",
16521659
"cargo_metadata",
16531660
"cfg",
16541661
"crossbeam-channel",

src/tools/rust-analyzer/crates/rust-analyzer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ path = "src/bin/main.rs"
2121

2222
[dependencies]
2323
anyhow.workspace = true
24+
base64 = "0.22"
2425
crossbeam-channel.workspace = true
2526
dirs = "5.0.1"
2627
dissimilar.workspace = true

src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/request.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::{
1010

1111
use anyhow::Context;
1212

13+
use base64::{prelude::BASE64_STANDARD, Engine};
1314
use ide::{
1415
AnnotationConfig, AssistKind, AssistResolveStrategy, Cancellable, CompletionFieldsToResolve,
1516
FilePosition, FileRange, HoverAction, HoverGotoTypeData, InlayFieldsToResolve, Query,
@@ -1136,10 +1137,15 @@ pub(crate) fn handle_completion_resolve(
11361137
else {
11371138
return Ok(original_completion);
11381139
};
1140+
let Ok(resolve_data_hash) = BASE64_STANDARD.decode(resolve_data.hash) else {
1141+
return Ok(original_completion);
1142+
};
11391143

11401144
let Some(corresponding_completion) = completions.into_iter().find(|completion_item| {
1141-
let hash = completion_item_hash(completion_item, resolve_data.for_ref);
1142-
hash == resolve_data.hash
1145+
// Avoid computing hashes for items that obviously do not match
1146+
// r-a might append a detail-based suffix to the label, so we cannot check for equality
1147+
original_completion.label.starts_with(completion_item.label.as_str())
1148+
&& resolve_data_hash == completion_item_hash(completion_item, resolve_data.for_ref)
11431149
}) else {
11441150
return Ok(original_completion);
11451151
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ pub struct CompletionResolveData {
827827
pub version: Option<i32>,
828828
pub trigger_character: Option<char>,
829829
pub for_ref: bool,
830-
pub hash: [u8; 20],
830+
pub hash: String,
831831
}
832832

833833
#[derive(Debug, Serialize, Deserialize)]

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{
55
sync::atomic::{AtomicU32, Ordering},
66
};
77

8+
use base64::{prelude::BASE64_STANDARD, Engine};
89
use ide::{
910
Annotation, AnnotationKind, Assist, AssistKind, Cancellable, CompletionFieldsToResolve,
1011
CompletionItem, CompletionItemKind, CompletionRelevance, Documentation, FileId, FileRange,
@@ -402,7 +403,7 @@ fn completion_item(
402403
version,
403404
trigger_character: completion_trigger_character,
404405
for_ref: true,
405-
hash: completion_item_hash(&item, true),
406+
hash: BASE64_STANDARD.encode(completion_item_hash(&item, true)),
406407
};
407408
Some(to_value(ref_resolve_data).unwrap())
408409
} else {
@@ -414,7 +415,7 @@ fn completion_item(
414415
version,
415416
trigger_character: completion_trigger_character,
416417
for_ref: false,
417-
hash: completion_item_hash(&item, false),
418+
hash: BASE64_STANDARD.encode(completion_item_hash(&item, false)),
418419
};
419420
(ref_resolve_data, Some(to_value(resolve_data).unwrap()))
420421
} else {

src/tools/rust-analyzer/docs/dev/lsp-extensions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!---
2-
lsp/ext.rs hash: c4c37ab0bcf7ccb0
2+
lsp/ext.rs hash: 14b7fb1309f5bb00
33
44
If you need to change the above hash to make the test pass, please check if you
55
need to adjust this doc as well and ping this issue:

0 commit comments

Comments
 (0)