mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Improve composer completion target resolution (#32628)
## What changed - Resolve `@` and `$` completion targets on either side of the cursor while treating atomic text elements and line breaks as boundaries. - Prefer the nearest editable mention when file, skill, and plugin candidates compete, and avoid treating common uppercase environment variables as skill queries. - Insert a separator before completions adjacent to atomic elements and keep dismissed popup state scoped to the matching editable token occurrence. ## Testing - Add unit and snapshot coverage for adjacent and partially bound mentions, whitespace boundaries, shell variables, popup dismissal, and file, image, skill, and plugin insertion. GitOrigin-RevId: 352a42c7bcdbf4f6cd35368ced25f71926fdbc1b
This commit is contained in:
1 parent
9e552e9d15
commit
c888e8e75a
13 files changed
+1273
-196
No files matched your search
@@ -0,0 +1,298 @@
|
||||
//! Cursor-neighborhood resolution for sigil-prefixed composer completions.
|
||||
|
||||
use super::ends_plaintext_at_mention;
|
||||
use super::is_mention_name_char;
|
||||
use crate::bottom_pane::textarea::TextArea;
|
||||
use crate::mention_codec::is_common_env_var;
|
||||
use std::ops::Range;
|
||||
|
||||
/// Narrows one whitespace-delimited candidate to the editable segment around `anchor`.
|
||||
///
|
||||
/// Atomic elements form hard boundaries. If the anchor is inside an element, the raw candidate is
|
||||
/// retained only when it is needed to identify that the candidate is already bound.
|
||||
fn prefixed_candidate_range(
|
||||
textarea: &TextArea,
|
||||
range: Range<usize>,
|
||||
anchor: usize,
|
||||
prefix: char,
|
||||
allow_empty: bool,
|
||||
) -> Option<(Range<usize>, String)> {
|
||||
let text = textarea.text();
|
||||
let prefix_len = prefix.len_utf8();
|
||||
let raw_prefixed = text
|
||||
.get(range.clone())
|
||||
.filter(|token| token.starts_with(prefix))
|
||||
.map(|token| (range.clone(), token[prefix_len..].to_string()));
|
||||
let mut segment_start = range.start;
|
||||
let mut segment_end = range.end;
|
||||
let mut has_element_boundary = false;
|
||||
let anchor = anchor.clamp(range.start, range.end);
|
||||
|
||||
for element in textarea.text_element_ranges_overlapping(range) {
|
||||
has_element_boundary = true;
|
||||
if element.end <= anchor {
|
||||
segment_start = element.end;
|
||||
continue;
|
||||
}
|
||||
if anchor <= element.start {
|
||||
segment_end = element.start;
|
||||
} else {
|
||||
segment_start = anchor;
|
||||
segment_end = anchor;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if !has_element_boundary {
|
||||
return raw_prefixed;
|
||||
}
|
||||
|
||||
let segmented_prefixed = text
|
||||
.get(segment_start..segment_end)
|
||||
.and_then(|token| token.strip_prefix(prefix))
|
||||
.filter(|query| allow_empty || !query.is_empty())
|
||||
.map(|query| (segment_start..segment_end, query.to_string()));
|
||||
if segmented_prefixed.is_some()
|
||||
|| raw_prefixed.as_ref().is_none_or(|(range, query)| {
|
||||
prefixed_token_range_is_editable(textarea, prefix, range, query)
|
||||
})
|
||||
{
|
||||
segmented_prefixed
|
||||
} else {
|
||||
raw_prefixed
|
||||
}
|
||||
}
|
||||
|
||||
/// Extracts a token prefixed with `prefix` under the cursor, if any.
|
||||
///
|
||||
/// The returned string does not include the prefix. Resolution considers the tokens on both sides
|
||||
/// of same-line separator whitespace and preserves their ranges for cross-sigil arbitration. It
|
||||
/// stops editable candidates at atomic text elements, and line breaks never provide affinity.
|
||||
pub(super) fn current_prefixed_token_range(
|
||||
textarea: &TextArea,
|
||||
prefix: char,
|
||||
allow_empty: bool,
|
||||
) -> Option<(Range<usize>, String)> {
|
||||
let cursor_offset = textarea.cursor();
|
||||
let text = textarea.text();
|
||||
|
||||
// Adjust the provided byte offset to the nearest valid char boundary at or before it.
|
||||
let mut safe_cursor = cursor_offset.min(text.len());
|
||||
if safe_cursor < text.len() && !text.is_char_boundary(safe_cursor) {
|
||||
safe_cursor = text
|
||||
.char_indices()
|
||||
.map(|(i, _)| i)
|
||||
.take_while(|&i| i <= cursor_offset)
|
||||
.last()
|
||||
.unwrap_or(0);
|
||||
}
|
||||
|
||||
let before_cursor = &text[..safe_cursor];
|
||||
let after_cursor = &text[safe_cursor..];
|
||||
let is_horizontal_whitespace = |c: char| {
|
||||
c.is_whitespace()
|
||||
&& !matches!(
|
||||
c,
|
||||
'\n' | '\r' | '\u{000B}' | '\u{000C}' | '\u{0085}' | '\u{2028}' | '\u{2029}'
|
||||
)
|
||||
};
|
||||
|
||||
let at_whitespace = after_cursor.chars().next().is_some_and(char::is_whitespace);
|
||||
let after_horizontal_whitespace = before_cursor
|
||||
.chars()
|
||||
.next_back()
|
||||
.is_some_and(is_horizontal_whitespace);
|
||||
let cursor_starts_token = after_horizontal_whitespace && !at_whitespace;
|
||||
let next_non_separator = after_cursor.chars().find(|c| !is_horizontal_whitespace(*c));
|
||||
let separator_precedes_token = next_non_separator.is_some_and(|c| !c.is_whitespace());
|
||||
let separator_precedes_completion = next_non_separator.is_some_and(|c| matches!(c, '$' | '@'));
|
||||
let at_separator = (at_whitespace || after_horizontal_whitespace) && separator_precedes_token;
|
||||
|
||||
let end_left = if at_separator {
|
||||
before_cursor
|
||||
.trim_end_matches(is_horizontal_whitespace)
|
||||
.len()
|
||||
} else {
|
||||
let end_left_rel = after_cursor
|
||||
.char_indices()
|
||||
.find(|(_, c)| c.is_whitespace())
|
||||
.map(|(idx, _)| idx)
|
||||
.unwrap_or(after_cursor.len());
|
||||
safe_cursor + end_left_rel
|
||||
};
|
||||
let start_left = text[..end_left]
|
||||
.char_indices()
|
||||
.rfind(|(_, c)| c.is_whitespace())
|
||||
.map(|(idx, c)| idx + c.len_utf8())
|
||||
.unwrap_or(0);
|
||||
|
||||
let ws_len_right: usize = after_cursor
|
||||
.chars()
|
||||
.take_while(|c| is_horizontal_whitespace(*c))
|
||||
.map(char::len_utf8)
|
||||
.sum();
|
||||
let start_right = safe_cursor + ws_len_right;
|
||||
let end_right_rel = text[start_right..]
|
||||
.char_indices()
|
||||
.find(|(_, c)| c.is_whitespace())
|
||||
.map(|(idx, _)| idx)
|
||||
.unwrap_or(text.len() - start_right);
|
||||
let end_right = start_right + end_right_rel;
|
||||
let token_right = if start_right < end_right {
|
||||
Some(&text[start_right..end_right])
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let left_prefixed = prefixed_candidate_range(
|
||||
textarea,
|
||||
start_left..end_left,
|
||||
safe_cursor,
|
||||
prefix,
|
||||
allow_empty,
|
||||
);
|
||||
let right_prefixed = prefixed_candidate_range(
|
||||
textarea,
|
||||
start_right..end_right,
|
||||
start_right,
|
||||
prefix,
|
||||
allow_empty,
|
||||
);
|
||||
|
||||
if cursor_starts_token {
|
||||
let right_is_bound = token_right
|
||||
.and_then(|token| {
|
||||
let prefix = token.chars().next()?;
|
||||
matches!(prefix, '$' | '@').then_some((prefix, &token[prefix.len_utf8()..]))
|
||||
})
|
||||
.is_some_and(|(prefix, query)| {
|
||||
!prefixed_token_range_is_editable(
|
||||
textarea,
|
||||
prefix,
|
||||
&(start_right..end_right),
|
||||
query,
|
||||
)
|
||||
});
|
||||
return if right_is_bound {
|
||||
left_prefixed
|
||||
} else {
|
||||
right_prefixed
|
||||
};
|
||||
}
|
||||
|
||||
if allow_empty && after_cursor.starts_with(prefix) {
|
||||
let left_fragment = &text[start_left..safe_cursor];
|
||||
if let Some(left_token) = left_fragment.strip_prefix(prefix)
|
||||
&& left_token
|
||||
.as_bytes()
|
||||
.iter()
|
||||
.all(|byte| is_mention_name_char(*byte))
|
||||
{
|
||||
let left_range = start_left..safe_cursor;
|
||||
let left_is_editable =
|
||||
prefixed_token_range_is_editable(textarea, prefix, &left_range, left_token);
|
||||
if left_token.is_empty() || prefix == '$' && left_is_editable {
|
||||
return Some((left_range, left_token.to_string()));
|
||||
}
|
||||
if !left_is_editable {
|
||||
return left_prefixed.or(right_prefixed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if at_separator {
|
||||
if after_horizontal_whitespace && !separator_precedes_completion {
|
||||
return right_prefixed;
|
||||
}
|
||||
if prefix == '$'
|
||||
&& left_prefixed
|
||||
.as_ref()
|
||||
.is_some_and(|(_, query)| !dollar_query_is_completable(query))
|
||||
&& right_prefixed
|
||||
.as_ref()
|
||||
.is_some_and(|(_, query)| dollar_query_is_completable(query))
|
||||
{
|
||||
return right_prefixed;
|
||||
}
|
||||
if prefix == '@'
|
||||
&& right_prefixed.as_ref().is_some_and(|(range, query)| {
|
||||
prefixed_token_range_is_editable(textarea, prefix, range, query)
|
||||
})
|
||||
{
|
||||
return right_prefixed;
|
||||
}
|
||||
if left_prefixed.as_ref().is_some_and(|(range, token)| {
|
||||
!prefixed_token_range_is_editable(textarea, prefix, range, token)
|
||||
}) {
|
||||
return right_prefixed.or(left_prefixed);
|
||||
}
|
||||
if left_prefixed
|
||||
.as_ref()
|
||||
.is_some_and(|(_, token)| token.is_empty())
|
||||
&& !allow_empty
|
||||
{
|
||||
return right_prefixed;
|
||||
}
|
||||
return left_prefixed.or(right_prefixed);
|
||||
}
|
||||
if after_cursor.starts_with(prefix) {
|
||||
let prefix_starts_token = before_cursor
|
||||
.chars()
|
||||
.next_back()
|
||||
.is_none_or(char::is_whitespace);
|
||||
return if prefix_starts_token {
|
||||
right_prefixed.or(left_prefixed)
|
||||
} else {
|
||||
left_prefixed
|
||||
};
|
||||
}
|
||||
left_prefixed.or(right_prefixed)
|
||||
}
|
||||
|
||||
/// Returns whether a token candidate's sigil and mention name are editable plaintext.
|
||||
///
|
||||
/// A candidate is bound when either its entire range is atomic or its mention-name prefix is
|
||||
/// atomic and the remaining text begins with punctuation that terminates the mention.
|
||||
pub(super) fn prefixed_token_range_is_editable(
|
||||
textarea: &TextArea,
|
||||
prefix: char,
|
||||
range: &Range<usize>,
|
||||
token: &str,
|
||||
) -> bool {
|
||||
if textarea.element_id_for_exact_range(range.clone()).is_some() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let name_len = token
|
||||
.as_bytes()
|
||||
.iter()
|
||||
.take_while(|byte| is_mention_name_char(**byte))
|
||||
.count();
|
||||
let mention_end = range.start + prefix.len_utf8() + name_len;
|
||||
!(name_len > 0
|
||||
&& mention_end < range.end
|
||||
&& ends_plaintext_at_mention(textarea.text().as_bytes(), mention_end)
|
||||
&& textarea
|
||||
.element_id_for_exact_range(range.start..mention_end)
|
||||
.is_some())
|
||||
}
|
||||
|
||||
/// Rejects shell-like dollar syntax while preserving lowercase and plugin-qualified skill queries.
|
||||
///
|
||||
/// Only uppercase common environment-variable spellings are excluded. The check uses the complete
|
||||
/// colon-qualified name so a skill such as `home` or `home:search` is not mistaken for `$HOME`.
|
||||
pub(super) fn dollar_query_is_completable(query: &str) -> bool {
|
||||
let name_end = query
|
||||
.as_bytes()
|
||||
.iter()
|
||||
.take_while(|byte| is_mention_name_char(**byte) || **byte == b':')
|
||||
.count();
|
||||
let name = &query[..name_end];
|
||||
let is_shell_var =
|
||||
name.bytes().all(|byte| !byte.is_ascii_lowercase()) && is_common_env_var(name);
|
||||
query.is_empty() || name_end > 0 && !is_shell_var
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "completion_target_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,187 @@
|
||||
use super::*;
|
||||
use crate::bottom_pane::textarea::TextArea;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn current_prefixed_token_affinity_does_not_cross_line_break() {
|
||||
for (text, prefix, allow_empty) in [
|
||||
("@file\n continue", '@', false),
|
||||
("continue \n@file", '@', false),
|
||||
("$skill\n continue", '$', true),
|
||||
("continue \n$skill", '$', true),
|
||||
] {
|
||||
let mut textarea = TextArea::new();
|
||||
textarea.insert_str(text);
|
||||
textarea.set_cursor(text.find(" ").expect("indentation present") + 1);
|
||||
|
||||
assert_eq!(
|
||||
current_prefixed_token_range(&textarea, prefix, allow_empty),
|
||||
None
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn current_prefixed_token_affinity_does_not_cross_separator_before_plain_text() {
|
||||
for (text, prefix, allow_empty) in [("@old word", '@', false), ("$old word", '$', true)] {
|
||||
let mut textarea = TextArea::new();
|
||||
textarea.insert_str(text);
|
||||
textarea.set_cursor(text.find(" ").expect("separator present") + 1);
|
||||
|
||||
assert_eq!(
|
||||
current_prefixed_token_range(&textarea, prefix, allow_empty),
|
||||
None
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn current_prefixed_token_prefers_right_after_adjacent_target() {
|
||||
let bound = "@bound";
|
||||
let left = "@bound@old";
|
||||
let text = "@bound@old @new";
|
||||
let right_start = left.len() + 2;
|
||||
let mut textarea = TextArea::new();
|
||||
textarea.insert_str(text);
|
||||
textarea.add_element_range(0..bound.len());
|
||||
textarea.set_cursor(left.len());
|
||||
|
||||
assert_eq!(
|
||||
current_prefixed_token_range(&textarea, '@', /*allow_empty*/ false),
|
||||
Some((right_start..text.len(), "new".to_string()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn current_prefixed_token_prefers_right_at_token_start() {
|
||||
let text = "$old $new";
|
||||
let right_start = "$old ".len();
|
||||
let mut textarea = TextArea::new();
|
||||
textarea.insert_str(text);
|
||||
textarea.set_cursor(right_start);
|
||||
|
||||
assert_eq!(
|
||||
current_prefixed_token_range(&textarea, '$', /*allow_empty*/ true),
|
||||
Some((right_start..text.len(), "new".to_string()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn current_prefixed_token_falls_back_from_bound_at_token_on_right() {
|
||||
let text = "@left @bound";
|
||||
let bound_start = text.find("@bound").expect("bound mention");
|
||||
let mut textarea = TextArea::new();
|
||||
textarea.insert_str(text);
|
||||
textarea.add_element_range(bound_start..text.len());
|
||||
textarea.set_cursor("@left ".len());
|
||||
|
||||
assert_eq!(
|
||||
current_prefixed_token_range(&textarea, '@', /*allow_empty*/ true),
|
||||
Some((0.."@left".len(), "left".to_string()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn current_prefixed_token_targets_later_segment_between_bound_elements() {
|
||||
let text = "$bound1$one$bound2$two$bound3";
|
||||
let bound2_start = text.find("$bound2").expect("second bound mention");
|
||||
let two_start = text.find("$two").expect("later plaintext target");
|
||||
let bound3_start = text.find("$bound3").expect("third bound mention");
|
||||
let mut textarea = TextArea::new();
|
||||
textarea.insert_str(text);
|
||||
textarea.add_element_range(0.."$bound1".len());
|
||||
textarea.add_element_range(bound2_start..two_start);
|
||||
textarea.add_element_range(bound3_start..text.len());
|
||||
textarea.set_cursor(two_start + "$t".len());
|
||||
|
||||
assert_eq!(
|
||||
current_prefixed_token_range(&textarea, '$', /*allow_empty*/ true),
|
||||
Some((two_start..bound3_start, "two".to_string()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn current_prefixed_token_bounds_right_candidate_before_bound_element() {
|
||||
let text = "continue @other@bound";
|
||||
let other_start = text.find("@other").expect("editable mention");
|
||||
let bound_start = text.find("@bound").expect("bound mention");
|
||||
let mut textarea = TextArea::new();
|
||||
textarea.insert_str(text);
|
||||
textarea.add_element_range(bound_start..text.len());
|
||||
textarea.set_cursor(other_start);
|
||||
|
||||
assert_eq!(
|
||||
current_prefixed_token_range(&textarea, '@', /*allow_empty*/ false),
|
||||
Some((other_start..bound_start, "other".to_string()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn current_prefixed_token_stops_before_overlapping_element() {
|
||||
let editable = "$other";
|
||||
let placeholder = "[Pasted Content 1001 chars]";
|
||||
let text = format!("{editable}{placeholder}");
|
||||
let mut textarea = TextArea::new();
|
||||
textarea.insert_str(&text);
|
||||
textarea.add_element_range(editable.len()..text.len());
|
||||
textarea.set_cursor(editable.len());
|
||||
|
||||
assert_eq!(
|
||||
current_prefixed_token_range(&textarea, '$', /*allow_empty*/ true),
|
||||
Some((0..editable.len(), "other".to_string()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn current_prefixed_token_stops_at_trailing_space_before_line_break() {
|
||||
let text = "$figma \nnext";
|
||||
let mut textarea = TextArea::new();
|
||||
textarea.insert_str(text);
|
||||
textarea.set_cursor("$figma ".len());
|
||||
|
||||
assert_eq!(
|
||||
current_prefixed_token_range(&textarea, '$', /*allow_empty*/ true),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn current_prefixed_token_keeps_nested_dollar_prefix_in_same_token() {
|
||||
let text = "$HOME/$USER";
|
||||
let mut textarea = TextArea::new();
|
||||
textarea.insert_str(text);
|
||||
textarea.set_cursor(text.find("$USER").expect("nested prefix present"));
|
||||
|
||||
assert_eq!(
|
||||
current_prefixed_token_range(&textarea, '$', /*allow_empty*/ true),
|
||||
Some((0..text.len(), "HOME/$USER".to_string()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typed_qualified_suffix_preserves_bound_plain_mention() {
|
||||
// Qualified skill bindings and history persistence are intentionally deferred. Typing a
|
||||
// suffix after a bound plain mention must not reinterpret it as an editable qualified one.
|
||||
let bound = "$google-calendar";
|
||||
let text = "$google-calendar:availability";
|
||||
let mut textarea = TextArea::new();
|
||||
textarea.insert_str(text);
|
||||
textarea.add_element_range(0..bound.len());
|
||||
textarea.set_cursor(text.len());
|
||||
|
||||
let (range, token) = current_prefixed_token_range(&textarea, '$', /*allow_empty*/ true)
|
||||
.expect("dollar token should be present");
|
||||
assert_eq!((range.clone(), token.as_str()), (0..text.len(), &text[1..]));
|
||||
assert!(!prefixed_token_range_is_editable(
|
||||
&textarea, '$', &range, &token,
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dollar_query_classifies_shell_and_skill_syntax() {
|
||||
assert!(!dollar_query_is_completable("{CODEX_HOME}/config.toml"));
|
||||
assert!(!dollar_query_is_completable("(pwd)"));
|
||||
assert!(dollar_query_is_completable("home:search"));
|
||||
assert!(dollar_query_is_completable("home"));
|
||||
assert!(!dollar_query_is_completable("HOME"));
|
||||
}
|
||||
@@ -5,6 +5,7 @@ use crate::bottom_pane::command_popup::CommandPopup;
|
||||
use crate::bottom_pane::file_search_popup::FileSearchPopup;
|
||||
use crate::bottom_pane::mentions_v2::MentionV2Popup;
|
||||
use crate::bottom_pane::skill_popup::SkillPopup;
|
||||
use crate::bottom_pane::textarea::TextArea;
|
||||
use std::ops::Range;
|
||||
|
||||
/// One token occurrence whose autocomplete popup should remain hidden.
|
||||
@@ -19,9 +20,10 @@ pub(super) struct DismissedToken {
|
||||
|
||||
impl DismissedToken {
|
||||
/// Captures the stable identity of the token at `range`.
|
||||
pub(super) fn new(text: &str, range: Range<usize>, query: String) -> Self {
|
||||
pub(super) fn new(textarea: &TextArea, range: Range<usize>, query: String) -> Self {
|
||||
let text = textarea.text();
|
||||
let token = text[range.clone()].to_string();
|
||||
let occurrence = complete_token_occurrences_before(text, &token, range.start);
|
||||
let occurrence = complete_token_occurrences_before(textarea, &token, range.start);
|
||||
Self {
|
||||
query,
|
||||
token,
|
||||
@@ -33,18 +35,54 @@ impl DismissedToken {
|
||||
///
|
||||
/// Byte offsets may shift under offset-only edits, while the token text and its ordinal keep
|
||||
/// later identical occurrences distinct.
|
||||
pub(super) fn matches(&self, text: &str, range: &Range<usize>, query: &str) -> bool {
|
||||
pub(super) fn matches(&self, textarea: &TextArea, range: &Range<usize>, query: &str) -> bool {
|
||||
let text = textarea.text();
|
||||
if self.query != query || text.get(range.clone()) != Some(self.token.as_str()) {
|
||||
return false;
|
||||
}
|
||||
complete_token_occurrences_before(text, &self.token, range.start) == self.occurrence
|
||||
complete_token_occurrences_before(textarea, &self.token, range.start) == self.occurrence
|
||||
}
|
||||
}
|
||||
|
||||
fn complete_token_occurrences_before(text: &str, token: &str, before: usize) -> usize {
|
||||
/// Counts complete editable-token occurrences before `before` in a single ordered pass.
|
||||
///
|
||||
/// Whitespace and atomic element edges delimit occurrences; bare nested sigils do not. This keeps
|
||||
/// dismissal identity aligned with the completion resolver without rescanning every element for
|
||||
/// each match.
|
||||
fn complete_token_occurrences_before(textarea: &TextArea, token: &str, before: usize) -> usize {
|
||||
let text = textarea.text();
|
||||
let mut elements_ending_before_matches = textarea.text_element_ranges().peekable();
|
||||
let mut elements_starting_after_matches = textarea.text_element_ranges().peekable();
|
||||
text[..before]
|
||||
.split_whitespace()
|
||||
.filter(|candidate| *candidate == token)
|
||||
.match_indices(token)
|
||||
.filter(|(start, _)| {
|
||||
let end = start + token.len();
|
||||
while elements_ending_before_matches
|
||||
.peek()
|
||||
.is_some_and(|element| element.end < *start)
|
||||
{
|
||||
elements_ending_before_matches.next();
|
||||
}
|
||||
while elements_starting_after_matches
|
||||
.peek()
|
||||
.is_some_and(|element| element.start < end)
|
||||
{
|
||||
elements_starting_after_matches.next();
|
||||
}
|
||||
let ends_at_boundary = text[end..].chars().next().is_none_or(char::is_whitespace)
|
||||
|| elements_starting_after_matches
|
||||
.peek()
|
||||
.is_some_and(|element| element.start == end);
|
||||
let starts_at_boundary = *start == 0
|
||||
|| text[..*start]
|
||||
.chars()
|
||||
.next_back()
|
||||
.is_some_and(char::is_whitespace)
|
||||
|| elements_ending_before_matches
|
||||
.peek()
|
||||
.is_some_and(|element| element.end == *start);
|
||||
starts_at_boundary && ends_at_boundary
|
||||
})
|
||||
.count()
|
||||
}
|
||||
|
||||
@@ -72,3 +110,7 @@ pub(super) enum ActivePopup {
|
||||
Skill(SkillPopup),
|
||||
MentionV2(MentionV2Popup),
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "popup_state_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,60 @@
|
||||
use super::*;
|
||||
use crate::bottom_pane::textarea::TextArea;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn dismissed_tokens_after_adjacent_elements_are_occurrence_scoped() {
|
||||
let first_bound = "$bound1";
|
||||
let second_bound = "$bound2";
|
||||
let token = "$other";
|
||||
let text = format!("{first_bound}{token} {second_bound}{token}");
|
||||
let first_start = first_bound.len();
|
||||
let second_start = text.rfind(token).expect("second token");
|
||||
let second_bound_start = text.find(second_bound).expect("second bound mention");
|
||||
let mut textarea = TextArea::new();
|
||||
textarea.insert_str(&text);
|
||||
textarea.add_element_range(0..first_start);
|
||||
textarea.add_element_range(second_bound_start..second_bound_start + second_bound.len());
|
||||
let dismissed = DismissedToken::new(
|
||||
&textarea,
|
||||
first_start..first_start + token.len(),
|
||||
"other".to_string(),
|
||||
);
|
||||
|
||||
assert!(!dismissed.matches(
|
||||
&textarea,
|
||||
&(second_start..second_start + token.len()),
|
||||
"other",
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_at_query_does_not_count_as_complete_dismissed_token() {
|
||||
let text = "@ma@latest @ma";
|
||||
let second_start = text.rfind("@ma").expect("second token");
|
||||
let mut textarea = TextArea::new();
|
||||
textarea.insert_str(text);
|
||||
|
||||
assert_eq!(
|
||||
complete_token_occurrences_before(&textarea, "@ma", second_start),
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_sigil_elements_delimit_dismissed_token_occurrences() {
|
||||
let token = "$other";
|
||||
let first_element = "[one]";
|
||||
let text = format!("{token}{first_element} {token}");
|
||||
let second_start = text.rfind(token).expect("second token");
|
||||
let mut textarea = TextArea::new();
|
||||
textarea.insert_str(&text);
|
||||
textarea.add_element_range(token.len()..token.len() + first_element.len());
|
||||
let dismissed = DismissedToken::new(&textarea, 0..token.len(), "other".to_string());
|
||||
|
||||
assert!(!dismissed.matches(
|
||||
&textarea,
|
||||
&(second_start..second_start + token.len()),
|
||||
"other",
|
||||
));
|
||||
}
|
||||
Reference in new issue
Block a user