aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-08-03 17:22:44 +0800
committer魏曹先生 <1992414357@qq.com>2026-08-03 17:22:44 +0800
commit61c42889b4bd4b91fe8660a3f17d3ad0dedf247b (patch)
tree19cc4124525e9800cef0ea39b6b5eaf81ca72a5d
parentc31aefaf46c33dea092b357d6311fac7819a3cea (diff)
feat(comps): improve bash completion script word tracking
Rework bash completion script to fix word-index tracking when the cursor is in the middle of a word, compute the current word from COMP_LINE truncated at COMP_POINT, and use space-separated option arguments. Also trim colon prefixes from completion items when COMP_WORDBREAKS includes colons.
-rw-r--r--CHANGELOG.md2
-rw-r--r--mingling_core/tmpls/comps/bash.sh43
2 files changed, 33 insertions, 12 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 69bfe43..590d566 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -59,6 +59,8 @@ None
1. **[`comps:zsh`]** Fixed zsh completion script to properly escape colons in completion descriptions. The zsh completion script generated for the `zsh` output format now escapes colon characters in completion items (`${item//:/\\:}`) and description parts (`${match[1]//:/\\:}`) so that descriptions containing colons don't break the `_describe` command's parsing. Additionally, fixed the simple-completions branch to iterate over the original `completions` array (with the colon-escaped format matching) rather than the already-parsed `parsed_completions` array, correctly extracting the completion item when no description is present.
+2. **[`comps:bash`]** Reworked the bash completion script template to fix word-index tracking when the cursor is in the middle of a word and to add colon-ltrimming for completion descriptions. The script now computes the current word (`cur`) using `COMP_LINE` truncated to `COMP_POINT` (taking the last whitespace-delimited token) rather than relying on `COMP_WORDS[COMP_CWORD]`, which fails when the cursor is in the middle of a word. The word index is derived from the count of words preceding the current cursor position (`before_words`), and `prev` is the last word in that preceding set. The option flags to the underlying completion engine are now passed as `-f value`/`-C value`/etc. (space-separated) instead of `-f=value`/`-C=value`/etc. (equals-separated), since the engine expects space-delimited argument pairs. Additionally, when the current word contains a colon and `COMP_WORDBREAKS` includes `:`, the completion items are trimmed of the colon prefix before being inserted into `COMPREPLY` — this prevents completions like `foo:bar` from being double-prefixed with the literal `foo:bar` when bash would otherwise append the full word.
+
#### Optimizations:
1. **[`pathf`]** Added `is_module` field to `AnalyzeItem` and a new constructor `AnalyzeItem::local_module(module, item_name)` which sets `is_module: true`. The `type_mapping_builder` now tracks whether an item is a module: when generating `type_using.rs`, module items produce `use path::to::module::*;` (glob import) instead of the standard `use path::to::TypeName;` direct import. Non-module items continue to use direct imports as before. The internal data structure changed from `Vec<(String, String)>` to `Vec<(String, String, bool)>` to carry the `is_module` flag through the pipeline.
diff --git a/mingling_core/tmpls/comps/bash.sh b/mingling_core/tmpls/comps/bash.sh
index 1af4f6c..edec28d 100644
--- a/mingling_core/tmpls/comps/bash.sh
+++ b/mingling_core/tmpls/comps/bash.sh
@@ -1,22 +1,31 @@
#!/usr/bin/env bash
_<<<bin_name>>>_bash_completion() {
- local cur="${COMP_WORDS[COMP_CWORD]}"
+ local line="${COMP_LINE:0:COMP_POINT}"
+ local cur="${line##* }"
local prev=""
- [ $COMP_CWORD -gt 0 ] && prev="${COMP_WORDS[COMP_CWORD-1]}"
+ local word_index=1
- local word_index=$((COMP_CWORD + 1))
+ local before="${line:0:$(( ${#line} - ${#cur} ))}"
+ local -a before_words
+ if [[ -n "$before" ]]; then
+ read -ra before_words <<< "$before"
+ word_index=$(( ${#before_words[@]} + 1 ))
+ if [[ $word_index -gt 1 ]]; then
+ prev="${before_words[${#before_words[@]}-1]}"
+ fi
+ fi
local args=()
- args+=(-f="${COMP_LINE//-/^}")
- args+=(-C="$COMP_POINT")
- args+=(-w="${cur//-/^}")
- args+=(-p="${prev//-/^}")
- args+=(-c="${COMP_WORDS[0]//-/^}")
- args+=(-i="$word_index")
- args+=(-F="bash")
+ args+=(-f "${COMP_LINE//-/^}")
+ args+=(-C "$COMP_POINT")
+ args+=(-w "${cur//-/^}")
+ args+=(-p "${prev//-/^}")
+ args+=(-c "${COMP_WORDS[0]//-/^}")
+ args+=(-i "$word_index")
+ args+=(-F "bash")
for word in "${COMP_WORDS[@]}"; do
- args+=(-a="${word//-/^}")
+ args+=(-a "${word//-/^}")
done
local suggestions
@@ -36,7 +45,17 @@ _<<<bin_name>>>_bash_completion() {
[ -z "$cur" ] || [[ "$suggestion" == "$cur"* ]] && filtered+=("$suggestion")
done
- [ ${#filtered[@]} -gt 0 ] && COMPREPLY=("${filtered[@]}")
+ if [ ${#filtered[@]} -gt 0 ]; then
+ COMPREPLY=("${filtered[@]}")
+ if [[ "$cur" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then
+ local colon_prefix="${cur%"${cur##*:}"}"
+ local -a ltrimmed=()
+ for suggestion in "${COMPREPLY[@]}"; do
+ ltrimmed+=("${suggestion#"$colon_prefix"}")
+ done
+ COMPREPLY=("${ltrimmed[@]}")
+ fi
+ fi
return
fi
fi