Skip to content

fix(cd): generate only cdable_vars containing valid directory paths #1361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 9, 2025

Conversation

akinomyoga
Copy link
Collaborator

When shopt -s cdable_vars is set, all the variable names are generated as completions. For example, in my environment,

$ shopt -s cdable_vars
$ cd [TAB][TAB]
Display all 177 possibilities? (y or n)
_                               bleopt_connect_tty              CPLUS_INCLUDE_PATH              GUESTFISH_PS1                   MAIL                            prefix                          test/
_append                         BLE_SESSION_ID                  _cur                            GUESTFISH_RESTORE               MAILCHECK                       prev                            test2/
archive/                        _ble_util_fdlist_cloexec        cur                             _has_ifs                        MANPATH                         PS1                             TEXMFHOME
autom4te.cache/                 _ble_util_fdvars_export         CVS_RSH                         helpers/                        MODULEPATH                      PS2                             TIME_STYLE
_backup_glob                    CARGO_HOME                      cword                           HISTCMD                         MODULES_CMD                     PS4                             UID
base                            CARGO_PROFILE_RELEASE_LTO       DBUS_SESSION_BUS_ADDRESS        HISTCONTROL                     MODULESHOME                     PWD                             _upvars
BASH                            C_INCLUDE_PATH                  DEBUGINFOD_IMA_CERT_PATH        HISTFILE                        __MODULES_LMINIT                .pytest_cache/                  USER
BASH_ALIASES                    COLUMNS                         DEBUGINFOD_URLS                 HISTFILESIZE                    MODULES_RUN_QUARANTINE          RANDOM                          _var
BASH_ARGC                       comp_args                       _dir                            HISTSIZE                        __MODULES_SHARE_MANPATH         READLINE_LINE                   venv/
BASH_ARGV                       compatdir                       DIRSTACK                        HOME                            MOTD_SHOWN                      READLINE_POINT                  VIRTUAL_ENV
BASH_ARGV0                      _comp_backup_glob               DISPLAY                         HOSTNAME                        MOZ_GMP_PATH                    _result                         VIRTUAL_ENV_PROMPT
BASH_CMDS                       _comp__base_directory           doc/                            HOSTTYPE                        MWGDIR                          .ruff_cache/                    wiki/
BASH_COMMAND                    COMP_CWORD                      EPOCHREALTIME                   _icmd                           MWG_LOGINTERM                   SECONDS                         WINDOW
BASH_COMPLETION_COMPAT_DIR      _comp_dequote__regex_safe_word  EPOCHSECONDS                    _ifs                            .mypy_cache/                    SHELL                           words
bash_completion.d/              COMP_FILEDIR_FALLBACK           EUID                            IFS                             old/                            SHELLOPTS                       work/
BASH_COMPLETION_VERSINFO        COMP_KEY                        ext/                            KDEDIRS                         _old_nocasematch                SHLVL                           _xcmd
BASH_LINENO                     COMP_KNOWN_HOSTS_WITH_AVAHI     FUNCNAME                        LANG                            OLDPWD                          SOURCE_HIGHLIGHT_DATADIR        XDG_DATA_DIRS
BASH_LOADABLES_PATH             COMP_KNOWN_HOSTS_WITH_HOSTFILE  GCC_COLORS                      LD_LIBRARY_PATH                 _opt                            SRANDOM                         XDG_RUNTIME_DIR
BASH_MONOSECONDS                completions/                    .git/                           LESSOPEN                        OPTERR                          SSH_ASKPASS                     XDG_SESSION_CLASS
BASHOPTS                        COMP_LINE                       .git.20220224/                  LIBRARY_PATH                    OPTIND                          SSH_CLIENT                      XDG_SESSION_ID
BASHPID                         COMP_POINT                      .github/                        LINENO                          OSTYPE                          SSH_CONNECTION                  XDG_SESSION_TYPE
BASH_REMATCH                    COMPREPLY                       GOPATH                          LINES                           patches/                        SSH_TTY
BASH_SOURCE                     COMP_TYPE                       GPG_TTY                         LOADEDMODULES                   PATH                            STY
BASH_SUBSHELL                   COMP_WORDBREAKS                 GROUPS                          LOGNAME                         PIPESTATUS                      t/
BASH_VERSINFO                   COMP_WORDS                      GUESTFISH_INIT                  LS_COLORS                       PKG_CONFIG_PATH                 TERM
BASH_VERSION                    _comp_xspecs                    GUESTFISH_OUTPUT                MACHTYPE                        PPID                            TERMCAP

However, most of them do not contain valid directory names, so they do not work even if they are specified to the argument of cd. In this PR, I try to filter only the variables that contain a valid directory path. After this change, the above result is changed to the following:

$ cd
archive/                    CARGO_HOME                  doc/                        GOPATH                      MOZ_GMP_PATH                patches/                    t/                          VIRTUAL_ENV
autom4te.cache/             compatdir                   ext/                        helpers/                    MWGDIR                      PWD                         test/                       VIRTUAL_ENV_PROMPT
base                        _comp__base_directory       .git/                       HOME                        .mypy_cache/                .pytest_cache/              test2/                      wiki/
BASH_COMPLETION_COMPAT_DIR  completions/                .git.20220224/              KDEDIRS                     old/                        .ruff_cache/                TEXMFHOME                   work/
bash_completion.d/          DIRSTACK                    .github/                    MODULESHOME                 OLDPWD                      SOURCE_HIGHLIGHT_DATADIR    venv/                       XDG_RUNTIME_DIR

I also include other minor fixes and refactoring in separate commits.

This code appears to try to suffix "/" when the unique completion of a
directory name does not end with "/".  However, the test compares the
completion with the quoted `"*/"`, which does not work as expected.
If the generated directory name already ends with "/" (though it is
unclear whether it may happen except for "/"), this code would result
in double slashes such as "dir//".

This double-quoted pattern is present from the beginning when this
code was introduced in commit a1969bd.

This patch removes the double quoting so that the directory name is
correctly matched with the pattern `*/`.
To apply the adjustment of suffixing "/" only to the completions
generated based on CDPATH, we isolate the CDPATH completion code in a
separate generator.  This is needed because we want to generate
cdable_vars completions into COMPREPLY, but we do not want to apply
sufixing to the cdable_vars completions.
@akinomyoga akinomyoga force-pushed the cdable_vars-filter branch from d070a04 to 5e9f8d4 Compare April 9, 2025 10:36
@akinomyoga akinomyoga force-pushed the cdable_vars-filter branch from 5e9f8d4 to 355eb4d Compare April 9, 2025 10:39
@scop scop merged commit b1374fc into scop:main Apr 9, 2025
7 checks passed
@akinomyoga akinomyoga deleted the cdable_vars-filter branch April 9, 2025 15:05
@akinomyoga
Copy link
Collaborator Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants