Skip to content

Commit eda4046

Browse files
committed
Introduce postfix item types
1 parent 41d8369 commit eda4046

File tree

5 files changed

+50
-33
lines changed

5 files changed

+50
-33
lines changed

crates/ide_completion/src/completions/postfix.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ use syntax::{
1212
use text_edit::TextEdit;
1313

1414
use crate::{
15-
completions::postfix::format_like::add_format_like_completions, context::CompletionContext,
16-
item::Builder, patterns::ImmediateLocation, CompletionItem, CompletionItemKind,
17-
CompletionRelevance, Completions, SnippetScope,
15+
completions::postfix::format_like::add_format_like_completions,
16+
context::CompletionContext,
17+
item::{Builder, CompletionRelevancePostfixMatch},
18+
patterns::ImmediateLocation,
19+
CompletionItem, CompletionItemKind, CompletionRelevance, Completions, SnippetScope,
1820
};
1921

2022
pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
@@ -240,11 +242,12 @@ fn build_postfix_snippet_builder<'ctx>(
240242
let mut item =
241243
CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), label);
242244
item.detail(detail).snippet_edit(cap, edit);
243-
let relevance = if ctx.original_token.text() == label {
244-
CompletionRelevance { exact_postfix_snippet_match: true, ..Default::default() }
245+
let postfix_match = if ctx.original_token.text() == label {
246+
Some(CompletionRelevancePostfixMatch::Exact)
245247
} else {
246-
CompletionRelevance { is_postfix: true, ..Default::default() }
248+
Some(CompletionRelevancePostfixMatch::NonExact)
247249
};
250+
let relevance = CompletionRelevance { postfix_match, ..Default::default() };
248251
item.set_relevance(relevance);
249252
item
250253
}

crates/ide_completion/src/completions/record.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use syntax::{ast::Expr, T};
44

55
use crate::{
66
patterns::ImmediateLocation, CompletionContext, CompletionItem, CompletionItemKind,
7-
CompletionRelevance, Completions,
7+
CompletionRelevance, CompletionRelevancePostfixMatch, Completions,
88
};
99

1010
pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
@@ -45,7 +45,7 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
4545
let completion_text =
4646
completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text);
4747
item.insert_text(completion_text).set_relevance(CompletionRelevance {
48-
exact_postfix_snippet_match: true,
48+
postfix_match: Some(CompletionRelevancePostfixMatch::Exact),
4949
..Default::default()
5050
});
5151
item.add_to(acc);

crates/ide_completion/src/item.rs

+27-16
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,8 @@ pub struct CompletionRelevance {
143143
pub is_op_method: bool,
144144
/// Set for item completions that are private but in the workspace.
145145
pub is_private_editable: bool,
146-
/// This is set in cases like these:
147-
///
148-
/// ```
149-
/// (a > b).not$0
150-
/// ```
151-
///
152-
/// Basically, we want to guarantee that postfix snippets always takes
153-
/// precedence over everything else.
154-
pub exact_postfix_snippet_match: bool,
155-
/// Set in cases when item is postfix, but not exact
156-
pub is_postfix: bool,
146+
/// Set for postfix snippet item completions
147+
pub postfix_match: Option<CompletionRelevancePostfixMatch>,
157148
}
158149

159150
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
@@ -180,6 +171,21 @@ pub enum CompletionRelevanceTypeMatch {
180171
Exact,
181172
}
182173

174+
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
175+
pub enum CompletionRelevancePostfixMatch {
176+
/// Set in cases when item is postfix, but not exact
177+
NonExact,
178+
/// This is set in cases like these:
179+
///
180+
/// ```
181+
/// (a > b).not$0
182+
/// ```
183+
///
184+
/// Basically, we want to guarantee that postfix snippets always takes
185+
/// precedence over everything else.
186+
Exact,
187+
}
188+
183189
impl CompletionRelevance {
184190
const BASE_LINE: u32 = 3;
185191
/// Provides a relevance score. Higher values are more relevant.
@@ -201,7 +207,7 @@ impl CompletionRelevance {
201207
if self.is_private_editable {
202208
score -= 1;
203209
}
204-
if self.is_postfix {
210+
if self.postfix_match.is_some() {
205211
score -= 3;
206212
}
207213

@@ -217,7 +223,7 @@ impl CompletionRelevance {
217223
if self.is_local {
218224
score += 1;
219225
}
220-
if self.exact_postfix_snippet_match {
226+
if self.postfix_match == Some(CompletionRelevancePostfixMatch::Exact) {
221227
score += 100;
222228
}
223229

@@ -536,7 +542,9 @@ mod tests {
536542
use itertools::Itertools;
537543
use test_utils::assert_eq_text;
538544

539-
use super::{CompletionRelevance, CompletionRelevanceTypeMatch};
545+
use super::{
546+
CompletionRelevance, CompletionRelevancePostfixMatch, CompletionRelevanceTypeMatch,
547+
};
540548

541549
/// Check that these are CompletionRelevance are sorted in ascending order
542550
/// by their relevance score.
@@ -579,7 +587,10 @@ mod tests {
579587
// This test asserts that the relevance score for these items is ascending, and
580588
// that any items in the same vec have the same score.
581589
let expected_relevance_order = vec![
582-
vec![CompletionRelevance { is_postfix: true, ..CompletionRelevance::default() }],
590+
vec![CompletionRelevance {
591+
postfix_match: Some(CompletionRelevancePostfixMatch::NonExact),
592+
..CompletionRelevance::default()
593+
}],
583594
vec![CompletionRelevance {
584595
is_op_method: true,
585596
is_private_editable: true,
@@ -619,7 +630,7 @@ mod tests {
619630
..CompletionRelevance::default()
620631
}],
621632
vec![CompletionRelevance {
622-
exact_postfix_snippet_match: true,
633+
postfix_match: Some(CompletionRelevancePostfixMatch::Exact),
623634
..CompletionRelevance::default()
624635
}],
625636
];

crates/ide_completion/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ use crate::{completions::Completions, context::CompletionContext};
2828

2929
pub use crate::{
3030
config::CompletionConfig,
31-
item::{CompletionItem, CompletionItemKind, CompletionRelevance, ImportEdit},
31+
item::{
32+
CompletionItem, CompletionItemKind, CompletionRelevance, CompletionRelevancePostfixMatch,
33+
ImportEdit,
34+
},
3235
snippet::{Snippet, SnippetScope},
3336
};
3437

crates/ide_completion/src/render.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ mod tests {
365365
use crate::{
366366
item::CompletionRelevanceTypeMatch,
367367
tests::{check_edit, do_completion, get_all_items, TEST_CONFIG},
368-
CompletionItem, CompletionItemKind, CompletionRelevance,
368+
CompletionItem, CompletionItemKind, CompletionRelevance, CompletionRelevancePostfixMatch,
369369
};
370370

371371
#[track_caller]
@@ -432,7 +432,10 @@ mod tests {
432432
),
433433
(relevance.exact_name_match, "name"),
434434
(relevance.is_local, "local"),
435-
(relevance.exact_postfix_snippet_match, "snippet"),
435+
(
436+
relevance.postfix_match == Some(CompletionRelevancePostfixMatch::Exact),
437+
"snippet",
438+
),
436439
(relevance.is_op_method, "op_method"),
437440
]
438441
.into_iter()
@@ -614,8 +617,7 @@ fn main() { let _: m::Spam = S$0 }
614617
is_local: false,
615618
is_op_method: false,
616619
is_private_editable: false,
617-
exact_postfix_snippet_match: false,
618-
is_postfix: false,
620+
postfix_match: None,
619621
},
620622
},
621623
CompletionItem {
@@ -636,8 +638,7 @@ fn main() { let _: m::Spam = S$0 }
636638
is_local: false,
637639
is_op_method: false,
638640
is_private_editable: false,
639-
exact_postfix_snippet_match: false,
640-
is_postfix: false,
641+
postfix_match: None,
641642
},
642643
},
643644
]
@@ -724,8 +725,7 @@ fn foo() { A { the$0 } }
724725
is_local: false,
725726
is_op_method: false,
726727
is_private_editable: false,
727-
exact_postfix_snippet_match: false,
728-
is_postfix: false,
728+
postfix_match: None,
729729
},
730730
},
731731
]

0 commit comments

Comments
 (0)