Disambiguate skill mentions from shell parameters (#33232)

## Why

Skill mentions and shell parameters both use a `$` prefix. Numeric and special
shell parameters should not open or displace completion popups, while loaded
skills can legitimately have digit- or hyphen-leading names.

## What changed

- Classify `$` queries as skill candidates, shell variables, definite shell
  parameters, or ambiguous shell-like names.
- Offer completion for an ambiguous name only when it fuzzy-matches a bindable
  loaded mention, reusing the filtered catalog when the popup opens.
- Keep definite shell forms such as `$1`, `$12`, `$-`, and `$_` out of mention
  completion and preserve completion targets elsewhere in the composer.

## Testing

Added unit and snapshot coverage for positional and special parameters,
digit- and hyphen-leading skills, uppercase shell variables, unbindable
qualified skills, and competing completion targets.

GitOrigin-RevId: 8bb64cb5ca0fa8233c7fadb11a53678d57eb814c
This commit is contained in:
Charlie Marsh authored and copyberry committed 2026-07-15 04:04:17 +00:00
1 parent 35b33e4304
commit b6f1cfa3f3
10 files changed
+460 -35

No files matched your search

@@ -72,6 +72,22 @@ pub(super) fn current_prefixed_token_range(
textarea: &TextArea,
prefix: char,
allow_empty: bool,
) -> Option<(Range<usize>, String)> {
current_prefixed_token_range_with_dollar_predicate(
textarea,
prefix,
allow_empty,
dollar_query_is_completable,
)
}
/// Extracts a prefixed token while using the provided dollar-query predicate for separator
/// arbitration.
pub(super) fn current_prefixed_token_range_with_dollar_predicate(
textarea: &TextArea,
prefix: char,
allow_empty: bool,
dollar_query_is_completable: impl Fn(&str) -> bool,
) -> Option<(Range<usize>, String)> {
let cursor_offset = textarea.cursor();
let text = textarea.text();
@@ -279,9 +295,30 @@ pub(super) fn prefixed_token_range_is_editable(
/// 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`.
/// Uppercase common environment-variable spellings and tokens beginning with a positional or
/// special shell parameter 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 {
matches!(dollar_query_kind(query), DollarQueryKind::Completable)
}
/// Classifies text after `$` for arbitration between shell syntax and mention completion.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum DollarQueryKind {
Completable,
ShellVariable,
DefiniteShellParameter,
/// A digit-leading query containing a non-digit, or a `-` query with a suffix.
AmbiguousShellParameter,
Invalid,
}
/// Conservatively classifies a dollar query before the mention catalog is consulted.
///
/// Numeric-only and exact special-parameter forms remain definite shell syntax. A digit-leading
/// query containing a non-digit, or a `-` query with a suffix, is ambiguous because loaded
/// mentions may legally use those spellings.
pub(super) fn dollar_query_kind(query: &str) -> DollarQueryKind {
let name_end = query
.as_bytes()
.iter()
@@ -290,7 +327,24 @@ pub(super) fn dollar_query_is_completable(query: &str) -> bool {
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
let is_shell_parameter = name
.as_bytes()
.first()
.is_some_and(|byte| *byte == b'-' || byte.is_ascii_digit());
let is_numeric_parameter = !name.is_empty() && name.bytes().all(|byte| byte.is_ascii_digit());
if query.is_empty() {
DollarQueryKind::Completable
} else if name_end == 0 {
DollarQueryKind::Invalid
} else if is_shell_var {
DollarQueryKind::ShellVariable
} else if is_numeric_parameter || matches!(name, "-" | "_") {
DollarQueryKind::DefiniteShellParameter
} else if is_shell_parameter {
DollarQueryKind::AmbiguousShellParameter
} else {
DollarQueryKind::Completable
}
}
#[cfg(test)]
@@ -184,4 +184,18 @@ fn dollar_query_classifies_shell_and_skill_syntax() {
assert!(dollar_query_is_completable("home:search"));
assert!(dollar_query_is_completable("home"));
assert!(!dollar_query_is_completable("HOME"));
for query in ["0", "1", "12", "-", "_"] {
assert!(!dollar_query_is_completable(query));
assert_eq!(
dollar_query_kind(query),
DollarQueryKind::DefiniteShellParameter
);
}
for query in ["1_suffix", "1foo", "-x"] {
assert!(!dollar_query_is_completable(query));
assert_eq!(
dollar_query_kind(query),
DollarQueryKind::AmbiguousShellParameter
);
}
}