Skip to content

Commit 23fc596

Browse files
committed
Don't reconstruct ref match completion in to_proto manually
1 parent 026a8c9 commit 23fc596

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

crates/ide-completion/src/item.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use crate::{
1414
render::{render_path_resolution, RenderContext},
1515
};
1616

17-
/// `CompletionItem` describes a single completion variant in the editor pop-up.
18-
/// It is basically a POD with various properties. To construct a
19-
/// `CompletionItem`, use `new` method and the `Builder` struct.
17+
/// `CompletionItem` describes a single completion entity which expands to 1 or more entries in the
18+
/// editor pop-up. It is basically a POD with various properties. To construct a
19+
/// [`CompletionItem`], use [`Builder::new`] method and the [`Builder`] struct.
2020
#[derive(Clone)]
2121
pub struct CompletionItem {
2222
/// Label in the completion pop up which identifies completion.
@@ -396,14 +396,20 @@ impl CompletionItem {
396396
self.trigger_call_info
397397
}
398398

399-
pub fn ref_match(&self) -> Option<(Mutability, TextSize, CompletionRelevance)> {
399+
pub fn ref_match(&self) -> Option<(String, text_edit::Indel, CompletionRelevance)> {
400400
// Relevance of the ref match should be the same as the original
401401
// match, but with exact type match set because self.ref_match
402402
// is only set if there is an exact type match.
403403
let mut relevance = self.relevance;
404404
relevance.type_match = Some(CompletionRelevanceTypeMatch::Exact);
405405

406-
self.ref_match.map(|(mutability, offset)| (mutability, offset, relevance))
406+
self.ref_match.map(|(mutability, offset)| {
407+
(
408+
format!("&{}{}", mutability.as_keyword_for_ref(), self.label()),
409+
text_edit::Indel::insert(offset, format!("&{}", mutability.as_keyword_for_ref())),
410+
relevance,
411+
)
412+
})
407413
}
408414

409415
pub fn imports_to_add(&self) -> &[LocatedImport] {

crates/ide-completion/src/render.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,7 @@ mod tests {
529529
let relevance = display_relevance(it.relevance());
530530
items.push(format!("{tag} {} {relevance}\n", it.label()));
531531

532-
if let Some((mutability, _offset, relevance)) = it.ref_match() {
533-
let label = format!("&{}{}", mutability.as_keyword_for_ref(), it.label());
532+
if let Some((label, _indel, relevance)) = it.ref_match() {
534533
let relevance = display_relevance(relevance);
535534

536535
items.push(format!("{tag} {label} {relevance}\n"));

crates/rust-analyzer/src/to_proto.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,16 @@ fn completion_item(
242242
let text_edit = {
243243
let mut text_edit = None;
244244
let source_range = item.source_range();
245-
for indel in item.text_edit().iter() {
245+
for indel in item.text_edit() {
246246
if indel.delete.contains_range(source_range) {
247+
// Extract this indel as the main edit
247248
text_edit = Some(if indel.delete == source_range {
248249
self::completion_text_edit(line_index, insert_replace_support, indel.clone())
249250
} else {
250251
assert!(source_range.end() == indel.delete.end());
251252
let range1 = TextRange::new(indel.delete.start(), source_range.start());
252253
let range2 = source_range;
253-
let indel1 = Indel::replace(range1, String::new());
254+
let indel1 = Indel::delete(range1);
254255
let indel2 = Indel::replace(range2, indel.insert.clone());
255256
additional_text_edits.push(self::text_edit(line_index, indel1));
256257
self::completion_text_edit(line_index, insert_replace_support, indel2)
@@ -316,18 +317,13 @@ fn completion_item(
316317
}
317318
}
318319

319-
if let Some((mutability, offset, relevance)) = item.ref_match() {
320-
let mut lsp_item_with_ref = lsp_item.clone();
320+
if let Some((label, indel, relevance)) = item.ref_match() {
321+
let mut lsp_item_with_ref = lsp_types::CompletionItem { label, ..lsp_item.clone() };
322+
lsp_item_with_ref
323+
.additional_text_edits
324+
.get_or_insert_with(Default::default)
325+
.push(self::text_edit(line_index, indel));
321326
set_score(&mut lsp_item_with_ref, max_relevance, relevance);
322-
lsp_item_with_ref.label =
323-
format!("&{}{}", mutability.as_keyword_for_ref(), lsp_item_with_ref.label);
324-
lsp_item_with_ref.additional_text_edits.get_or_insert_with(Default::default).push(
325-
self::text_edit(
326-
line_index,
327-
Indel::insert(offset, format!("&{}", mutability.as_keyword_for_ref())),
328-
),
329-
);
330-
331327
acc.push(lsp_item_with_ref);
332328
};
333329

0 commit comments

Comments
 (0)