Skip to content

Commit 4169926

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 2529e9e commit 4169926

File tree

6 files changed

+21
-6
lines changed

6 files changed

+21
-6
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

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
};

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)]

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 {

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)