Skip to content

Commit e835f51

Browse files
kinto0meta-codesync[bot]
authored andcommitted
Stabilize sort_text format when MRU is inactive
Summary: Change assign_sort_text to take Option<Option<usize>> so that "no MRU system" (None, e.g. the wasm path) preserves the compact sort_text format ("0", "1", etc.), while "MRU active but item not found" (Some(None)) uses the extended format with rank 9999. Previously all items got the extended format whenever the MRU system was active, even items not in the MRU, which was a surface-area change to all non-wasm completion responses. Reviewed By: jvansch1 Differential Revision: D93248048 fbshipit-source-id: 296df656d0e96efae6b750d4d86ecbd975f92884
1 parent 57d60c7 commit e835f51

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

pyrefly/lib/lsp/wasm/completion.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,15 @@ impl RankedCompletion {
8484

8585
/// All completion ranking logic lives here. Assigns `sort_text` to each item
8686
/// based on its source classification, name prefix, compatibility, and MRU rank.
87-
fn assign_sort_text(ranked: &mut RankedCompletion, mru_rank: Option<usize>) {
87+
///
88+
/// `mru_rank` uses two levels of `Option`:
89+
/// - `None` means the MRU system is not active (e.g. wasm path). sort_text uses
90+
/// the compact base format (`"0"`, `"1"`, etc.).
91+
/// - `Some(None)` means the MRU system is active but the item was not found in
92+
/// the MRU list. sort_text uses the extended format with rank 9999 so it sorts
93+
/// after MRU-matched items.
94+
/// - `Some(Some(rank))` means the item was found at position `rank` in the MRU.
95+
fn assign_sort_text(ranked: &mut RankedCompletion, mru_rank: Option<Option<usize>>) {
8896
let is_deprecated = ranked
8997
.item
9098
.tags
@@ -115,12 +123,18 @@ fn assign_sort_text(ranked: &mut RankedCompletion, mru_rank: Option<usize>) {
115123
}
116124
};
117125

118-
if let Some(rank) = mru_rank {
119-
let rank = rank.min(9999);
120-
ranked.item.sort_text = Some(format!("{base}.{rank:04}.{}", ranked.item.label));
121-
ranked.item.preselect = if rank == 0 { Some(true) } else { None };
122-
} else {
123-
ranked.item.sort_text = Some(base);
126+
match mru_rank {
127+
Some(Some(rank)) => {
128+
let rank = rank.min(9999);
129+
ranked.item.sort_text = Some(format!("{base}.{rank:04}.{}", ranked.item.label));
130+
ranked.item.preselect = if rank == 0 { Some(true) } else { None };
131+
}
132+
Some(None) => {
133+
ranked.item.sort_text = Some(format!("{base}.9999.{}", ranked.item.label));
134+
}
135+
None => {
136+
ranked.item.sort_text = Some(base);
137+
}
124138
}
125139
}
126140

@@ -950,9 +964,7 @@ impl Transaction<'_> {
950964
Self::add_function_call_parens(&mut result, supports_snippet_completions);
951965
}
952966
for ranked in &mut result {
953-
let mru_rank = mru_index
954-
.as_mut()
955-
.map(|index| (*index)(&ranked.item).unwrap_or(9999).min(9999));
967+
let mru_rank = mru_index.as_mut().map(|index| (*index)(&ranked.item));
956968
assign_sort_text(ranked, mru_rank);
957969
}
958970
(result.into_iter().map(|r| r.item).collect(), is_incomplete)

0 commit comments

Comments
 (0)