aboutsummaryrefslogtreecommitdiff
path: root/crates/rust-analyzer/src/to_proto.rs
diff options
context:
space:
mode:
authorJosh Mcguigan <[email protected]>2021-03-12 03:11:14 +0000
committerJosh Mcguigan <[email protected]>2021-03-12 14:16:01 +0000
commit3679821eea94f30f2582ea7bca7569dad3ca31be (patch)
tree99edbd24d1d5da54af0e3f177cb18719b7880bda /crates/rust-analyzer/src/to_proto.rs
parentc0e9530fd095317563532c20f13959619515c9b2 (diff)
add completion relevance score
Diffstat (limited to 'crates/rust-analyzer/src/to_proto.rs')
-rw-r--r--crates/rust-analyzer/src/to_proto.rs34
1 files changed, 22 insertions, 12 deletions
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index a9846fa70..a467bc685 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -6,9 +6,10 @@ use std::{
6 6
7use ide::{ 7use ide::{
8 Annotation, AnnotationKind, Assist, AssistKind, CallInfo, CompletionItem, CompletionItemKind, 8 Annotation, AnnotationKind, Assist, AssistKind, CallInfo, CompletionItem, CompletionItemKind,
9 Documentation, FileId, FileRange, FileSystemEdit, Fold, FoldKind, Highlight, HlMod, HlPunct, 9 CompletionRelevance, Documentation, FileId, FileRange, FileSystemEdit, Fold, FoldKind,
10 HlRange, HlTag, Indel, InlayHint, InlayKind, InsertTextFormat, Markup, NavigationTarget, 10 Highlight, HlMod, HlPunct, HlRange, HlTag, Indel, InlayHint, InlayKind, InsertTextFormat,
11 ReferenceAccess, RenameError, Runnable, Severity, SourceChange, TextEdit, TextRange, TextSize, 11 Markup, NavigationTarget, ReferenceAccess, RenameError, Runnable, Severity, SourceChange,
12 TextEdit, TextRange, TextSize,
12}; 13};
13use ide_db::SymbolKind; 14use ide_db::SymbolKind;
14use itertools::Itertools; 15use itertools::Itertools;
@@ -213,12 +214,22 @@ pub(crate) fn completion_item(
213 ..Default::default() 214 ..Default::default()
214 }; 215 };
215 216
216 if item.relevance().is_relevant() { 217 fn set_score(res: &mut lsp_types::CompletionItem, relevance: CompletionRelevance) {
217 lsp_item.preselect = Some(true); 218 if relevance.is_relevant() {
218 // HACK: sort preselect items first 219 res.preselect = Some(true);
219 lsp_item.sort_text = Some(format!(" {}", item.label())); 220 }
221 // The relevance needs to be inverted to come up with a sort score
222 // because the client will sort ascending.
223 let sort_score = relevance.score() ^ 0xFF;
224 // Zero pad the string to ensure values are sorted numerically
225 // even though the client is sorting alphabetically. Three
226 // characters is enough to fit the largest u8, which is the
227 // type of the relevance score.
228 res.sort_text = Some(format!("{:03}", sort_score));
220 } 229 }
221 230
231 set_score(&mut lsp_item, item.relevance());
232
222 if item.deprecated() { 233 if item.deprecated() {
223 lsp_item.tags = Some(vec![lsp_types::CompletionItemTag::Deprecated]) 234 lsp_item.tags = Some(vec![lsp_types::CompletionItemTag::Deprecated])
224 } 235 }
@@ -228,10 +239,9 @@ pub(crate) fn completion_item(
228 } 239 }
229 240
230 let mut res = match item.ref_match() { 241 let mut res = match item.ref_match() {
231 Some(mutability) => { 242 Some((mutability, relevance)) => {
232 let mut lsp_item_with_ref = lsp_item.clone(); 243 let mut lsp_item_with_ref = lsp_item.clone();
233 lsp_item.preselect = Some(true); 244 set_score(&mut lsp_item_with_ref, relevance);
234 lsp_item.sort_text = Some(format!(" {}", item.label()));
235 lsp_item_with_ref.label = 245 lsp_item_with_ref.label =
236 format!("&{}{}", mutability.as_keyword_for_ref(), lsp_item_with_ref.label); 246 format!("&{}{}", mutability.as_keyword_for_ref(), lsp_item_with_ref.label);
237 if let Some(lsp_types::CompletionTextEdit::Edit(it)) = &mut lsp_item_with_ref.text_edit 247 if let Some(lsp_types::CompletionTextEdit::Edit(it)) = &mut lsp_item_with_ref.text_edit
@@ -1107,13 +1117,13 @@ mod tests {
1107 ( 1117 (
1108 "&arg", 1118 "&arg",
1109 Some( 1119 Some(
1110 " arg", 1120 "253",
1111 ), 1121 ),
1112 ), 1122 ),
1113 ( 1123 (
1114 "arg", 1124 "arg",
1115 Some( 1125 Some(
1116 " arg", 1126 "254",
1117 ), 1127 ),
1118 ), 1128 ),
1119 ] 1129 ]