Skip to content

Tab completion: quote each match individually instead of the whole argument #1821

Open

Description

@tleonhardt

Follow-up from the review of #1812.

Background

When a completion needs quotes, cmd2 adds one opening quote to the argument being completed before a match has been chosen. Cmd._perform_completion() sets Completions._add_opening_quote and Completions._quote_char for the whole set of matches, and Cmd2Completer.get_completions() in cmd2/pt_utils.py inserts that quote into the input buffer and starts a new completion cycle.

This has always been how a match containing a space is handled. #1812 added more triggers in the two places where cmd2 knows a shell is the consumer (arguments of shell/! and everything after a pipe), which makes the following limits easier to hit.

Problems

1. Choosing a match from the menu leaves the quote unclosed

The closing quote is only added when there is a single match which is finalized. With sp ace.txt and spare.txt in the current directory:

(Cmd) edit sp<TAB>          -> edit "sp
(Cmd) edit "sp<TAB><TAB>    -> menu opens, first entry selected: edit "sp ace.txt
<ENTER>                     -> Invalid syntax: No closing quotation

The same happens for !cat fi with file(1).txt and file.txt. Reproduced in a real terminal (tmux, macOS) for both cases.

2. One match needing quotes puts quotes on all of them

In a directory with [id].tsx and index.tsx, completing a blank argument of shell cat quotes both. An executable named [ exists on most POSIX systems (and Homebrew coreutils installs g[), so completing !g quotes every match.

3. One quote character is chosen for all the matches

If any match contains a single quote, double quotes are used for all of them. With it's.txt and invoice$5.txt both offered in a shell context, invoice$5.txt ends up in double quotes and the shell expands $5.

4. Macros which run a shell command are not treated as shell contexts

After macro create del !rm {1}, del my* + Tab completes to an unquoted my*file.txt, which the shell then expands. macro_arg_complete() completes paths without knowing what the macro runs. An alias for shell is covered, because the alias is resolved before completion.

5. A second Tab is needed after the opening quote is inserted

The first Tab inserts only the opening quote; a second Tab is needed to complete the name. A comment in get_completions() says modifying the buffer triggers a new completion cycle, which is not what happens under tmux.

Possible direction

Decide quoting per match: each Completion yielded to prompt-toolkit carries its own opening and closing quote, chosen for that match alone. That would resolve 1, 2, 3 and 5.

The reason for the current approach is noted in get_completions(): when a completion changes text before the cursor, prompt-toolkit does not insert a common prefix. A per-match design needs an answer for that, for example only inserting the opening quote early when every match needs the same one.

Problem 4 is separate and could be handled by checking whether the macro value resolves to the shell command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions