improvements and fixes to the tcsh completion logic#213
improvements and fixes to the tcsh completion logic#213ThomasWaldmann wants to merge 1 commit intoiterative:mainfrom
Conversation
Issues:
1. Subcommand Positional Completion: shtab lacks native support for auto-completing positional
arguments that belong to subcommands in tcsh (e.g., `borg help <topic>`). This builds a
conditional evaluation structure (`if ( $#cmd >= X && ... )`) to support them.
2. Subshell Array Indexing Fix: `tcsh` aggressively evaluates array indices like `$cmd[2]` even
if the array is smaller than the requested index, causing "if: Empty if." errors. Added
explicit bounds checking (`$#cmd >= max_idx`).
3. Nested Subshell Safety: Standard shtab nests subshells using backticks which causes recursive
parsing crashes in tcsh. Replaced with safe `eval` usage.
Fix:
It now adds completions for optional arguments that take parameters (`nargs != 0`) by appending a `=` version (e.g., `--opt=`).
```python
if optional.nargs != 0:
specials.extend(get_specials(optional, 'c', optional_str + '='))
```
The logic for recursing into sub-parsers was broadened. Previously, it only recursed if there were no "requirements" (positional arguments already met). Now, it recurses as long as the choices are a dictionary, enabling better support for complex nested sub-commands.
The completion script generation for sub-commands was significantly rewritten:
- **Condition-based logic**: It replaced an "ugly hack" using shell `||` checks with explicit `if` conditions in `tcsh`.
- **Command line parsing**: It now calculates conditions based on the length and content of the command line (`$cmd`).
- **Safety Padding**: It adds a padding of empty strings (`"" "" ...`) to the `$COMMAND_LINE` before assigning it to `$cmd`. This prevents "Subscript out of range" errors when `tcsh` tries to access an index that doesn't exist yet.
- **Custom `complete` Support**: It now handles arguments with a `complete` attribute (custom completion logic), converting them to `tcsh` patterns and using `eval` for function calls.
Finally, it ensures the `specials` list (which stores the completion rules) contains no duplicate rules while maintaining their original order:
```python
specials = list(dict.fromkeys(specials))
```
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
Note:
This needs review by someone with a clue about tcsh and shtab (that's not me). |
|
Maybe @simaoafonso-pwt could help here. I set "allow edits by maintainers" - @simaoafonso-pwt if you are not maintainer here, feel free to copy whatever you like from here to an own PR. |
This seems like a good feature, but it doesn't really work. I see it produces: The For the rest of the changes, I don't have so many complex commands to test, if you say that borg works better, I'm good. |
Issues:
Subcommand Positional Completion: shtab lacks native support for auto-completing positional arguments that belong to subcommands in tcsh (e.g.,
borg help <topic>). This builds a conditional evaluation structure (if ( $#cmd >= X && ... )) to support them.Subshell Array Indexing Fix:
tcshaggressively evaluates array indices like$cmd[2]even if the array is smaller than the requested index, causing "if: Empty if." errors. Added explicit bounds checking ($#cmd >= max_idx).Nested Subshell Safety: Standard shtab nests subshells using backticks which causes recursive parsing crashes in tcsh. Replaced with safe
evalusage.Fix:
It now adds completions for optional arguments that take parameters (
nargs != 0) by appending a=version (e.g.,--opt=).The logic for recursing into sub-parsers was broadened. Previously, it only recursed if there were no "requirements" (positional arguments already met). Now, it recurses as long as the choices are a dictionary, enabling better support for complex nested sub-commands.
The completion script generation for sub-commands was significantly rewritten:
Condition-based logic: It replaced an "ugly hack" using shell
||checks with explicitifconditions intcsh.Command line parsing: It now calculates conditions based on the length and content of the command line (
$cmd).Safety Padding: It adds a padding of empty strings (
"" "" ...) to the$COMMAND_LINEbefore assigning it to$cmd. This prevents "Subscript out of range" errors whentcshtries to access an index that doesn't exist yet.Custom
completeSupport: It now handles arguments with acompleteattribute (custom completion logic), converting them totcshpatterns and usingevalfor function calls.Finally, it ensures the
specialslist (which stores the completion rules) contains no duplicate rules while maintaining their original order: