diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-08-03 18:01:56 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-08-03 18:01:56 +0800 |
| commit | ffd2925b5fb619a91cd66f0e35a518c9077838d4 (patch) | |
| tree | f518d2bfc7caad7d985bddcd2b4bd5c859838257 /mingling_core/src/comp/suggest.rs | |
| parent | 4ef4dfa8a36610cb2bb626794dc1d35d4584ee03 (diff) | |
feat(comp): add prefix and suffix methods to Suggest
Diffstat (limited to 'mingling_core/src/comp/suggest.rs')
| -rw-r--r-- | mingling_core/src/comp/suggest.rs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/mingling_core/src/comp/suggest.rs b/mingling_core/src/comp/suggest.rs index dda5026..804d622 100644 --- a/mingling_core/src/comp/suggest.rs +++ b/mingling_core/src/comp/suggest.rs @@ -92,6 +92,72 @@ impl Suggest { self.insert(SuggestItem::WithDescription(item, desc_str.clone())); } } + + /// Adds a prefix to every suggestion in the `Suggest` set. + /// + /// This method takes the current `Suggest` value and prepends the given + /// prefix to the suggestion text of each item. If the `Suggest` value is + /// [`Suggest::FileCompletion`], it is returned unchanged. + /// + /// # Arguments + /// + /// * `prefix` — The string to prepend to each suggestion. Must implement + /// `Into<String>`. + /// + /// # Returns + /// + /// A new `Suggest` value where each item's suggestion text is prefixed + /// with the given string. For example, `["foo", "bar"]` with prefix `"--"` + /// becomes `["--foo", "--bar"]`. + pub fn add_prefix(self, prefix: impl Into<String>) -> Suggest { + let suggest = match self { + Suggest::Suggest(s) => s, + Suggest::FileCompletion => return Suggest::FileCompletion, + }; + let prefix = prefix.into(); + let prefixed = suggest + .into_iter() + .map(|item| { + let mut new_item = item; + new_item.set_suggest(format!("{}{}", prefix, new_item.suggest())); + new_item + }) + .collect(); + Suggest::Suggest(prefixed) + } + + /// Appends a suffix to every suggestion in the `Suggest` set. + /// + /// This method takes the current `Suggest` value and appends the given + /// suffix to the suggestion text of each item. If the `Suggest` value is + /// [`Suggest::FileCompletion`], it is returned unchanged. + /// + /// # Arguments + /// + /// * `suffix` — The string to append to each suggestion. Must implement + /// `Into<String>`. + /// + /// # Returns + /// + /// A new `Suggest` value where each item's suggestion text is suffixed + /// with the given string. For example, `["foo", "bar"]` with suffix `"="` + /// becomes `["foo=", "bar="]`. + pub fn add_suffix(self, suffix: impl Into<String>) -> Suggest { + let suggest = match self { + Suggest::Suggest(s) => s, + Suggest::FileCompletion => return Suggest::FileCompletion, + }; + let suffix = suffix.into(); + let suffixed = suggest + .into_iter() + .map(|item| { + let mut new_item = item; + new_item.set_suggest(format!("{}{}", new_item.suggest(), suffix)); + new_item + }) + .collect(); + Suggest::Suggest(suffixed) + } } impl<T> From<T> for Suggest |
