emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/bash-completion 432c2d15c9 142/313: Add a space after the


From: ELPA Syncer
Subject: [nongnu] elpa/bash-completion 432c2d15c9 142/313: Add a space after the completion candidate for single completion.
Date: Sat, 3 Dec 2022 10:59:24 -0500 (EST)

branch: elpa/bash-completion
commit 432c2d15c98cb79f582e40c17db6e46d978a2455
Author: Stephane Zermatten <szermatt@gmx.net>
Commit: Stephane Zermatten <szermatt@gmx.net>

    Add a space after the completion candidate for single completion.
    
    This commit adds a space after a single completion only for
    default (filename, not command, not custom) completion. Still rely on
    the programmable completion to add space - or not - for custom
    completion.
---
 bash-completion-test.el |  83 ++++++++++++++++++++++++++++++--------
 bash-completion.el      | 105 ++++++++++++++++++++++++++++++------------------
 2 files changed, 131 insertions(+), 57 deletions(-)

diff --git a/bash-completion-test.el b/bash-completion-test.el
index 9454793fa4..a602cc08e6 100644
--- a/bash-completion-test.el
+++ b/bash-completion-test.el
@@ -457,39 +457,41 @@ garbage
 (ert-deftest bash-completion-fix-test ()
   ;; escape rest
   (should (equal "a\\ bc\\ d\\ e"
-                (bash-completion-fix "a\\ bc d e" "a\\ b" "a\\ b" nil nil)))
+                (bash-completion-fix "a\\ bc d e" "a\\ b" "a\\ b" nil nil 
nil)))
 
   ;; recover original escaping
   (should (equal "a' 'bc\\ d\\ e"
-                (bash-completion-fix "a\\ bc d e" "a\\ b" "a' 'b" nil nil)))
+                (bash-completion-fix "a\\ bc d e" "a\\ b" "a' 'b" nil nil 
nil)))
 
   ;; do not escape final space
   (should (equal "ab "
                 (let ((bash-completion-nospace nil))
-                  (bash-completion-fix "ab " "a" "a" nil nil))))
+                  (bash-completion-fix "ab " "a" "a" nil nil nil))))
   
   ;; remove final space
   (should (equal "ab"
                 (let ((bash-completion-nospace t))
-                  (bash-completion-fix "ab " "a" "a" nil nil))))
+                  (bash-completion-fix "ab " "a" "a" nil nil nil))))
 
   ;; unexpand home and escape
   (should (equal "~/a/hello\\ world"
                 (bash-completion-fix (expand-file-name "~/a/hello world")
-                                     "~/a/he" "~/a/he" nil nil)))
+                                     "~/a/he" "~/a/he" nil nil nil)))
 
   ;; match after wordbreak and escape
   (should (equal "a:b:c:hello\\ world"
-                (bash-completion-fix "hello world" "a:b:c:he" "a:b:c:he" nil 
nil)))
+                (bash-completion-fix "hello world" "a:b:c:he" "a:b:c:he"
+                                      nil nil nil)))
 
   ;; just append
   (should (equal "hello\\ world"
-                (bash-completion-fix " world" "hello" "hello" nil nil)))
+                (bash-completion-fix " world" "hello" "hello" nil nil nil)))
 
   ;; append / for home
   (should (equal "~/"
-                 (bash-completion-fix (expand-file-name "~")  "~" "~" nil 
nil)))
-  
+                 (bash-completion-fix (expand-file-name "~")
+                                      "~" "~" nil 'default)))
+
   (cl-letf (((symbol-function 'file-accessible-directory-p)
              (lambda (d)
                (message "check: %s" d)
@@ -497,22 +499,41 @@ garbage
     (let ((default-directory "/tmp/"))
       ;; append / for directory
       (should (equal "somedir/"
-                     (bash-completion-fix "somedir" "some" "some" nil nil)))
+                     (bash-completion-fix "somedir" "some" "some"
+                                          nil 'default nil)))
       ;; append / for initial command that is a directory
       (should (equal "somedir/"
-                     (bash-completion-fix "somedir" "some" "some" nil t)))))
+                     (bash-completion-fix "somedir" "some" "some"
+                                          nil 'command nil)))))
 
   ;; append a space for initial command that is not a directory
   (should (let ((bash-completion-nospace nil))
-            (equal "somecmd " (bash-completion-fix "somecmd" "some" "some" nil 
t))))
-  
+            (equal "somecmd "
+                   (bash-completion-fix "somecmd" "some" "some"
+                                        nil 'command nil))))
+
   ;; ... but not if nospace is t.
   (should (let ((bash-completion-nospace t))
-            (equal "somecmd" (bash-completion-fix "somecmd" "some" "some" nil 
t))))
+            (equal "somecmd"
+                   (bash-completion-fix "somecmd" "some" "some"
+                                        nil 'command nil))))
+
+  ;; append a space for a single default completion
+  (should (let ((bash-completion-nospace nil))
+            (equal "somecmd "
+                   (bash-completion-fix "somecmd" "some" "some"
+                                        nil 'default 'single))))
+
+  ;; but only for a single completion
+  (should (let ((bash-completion-nospace nil))
+            (equal "somecmd"
+                   (bash-completion-fix "somecmd" "some" "some"
+                                        nil 'default nil))))
 
   ;; subset of the prefix"
   (should (equal "Dexter"
-                (bash-completion-fix "Dexter" "Dexter'" "Dexter'" nil nil))))
+                (bash-completion-fix "Dexter" "Dexter'" "Dexter'"
+                                      nil nil nil))))
 
 (ert-deftest bash-completion-extract-candidates-test ()
   (should (equal 
@@ -721,8 +742,35 @@ before calling `bash-completion-dynamic-complete-nocomint'.
    (should (equal "cd >/dev/null 2>&1 /tmp/test ; compgen -o default he 
2>/dev/null"
                   (pop --captured-commands)))))
 
-(ert-deftest bash-completion-trailing ()
+(ert-deftest bash-completion-single-completion-test ()
+  (--with-fake-bash-completion-send
+   (push "hello\n" --send-results)
+   (insert "$ cat he")
+   (should (equal
+            '("hello ")
+            (nth 2 (bash-completion-dynamic-complete-nocomint 3 (point)))))))
+
+(ert-deftest bash-completion-trailing-default-completion ()
+  (--with-fake-bash-completion-send
+   (push "without space\nwith space \nwith slash/\n" --send-results)
+   (insert "$ ls with")
+   (let ((bash-completion-nospace nil))
+     (should (equal
+              '("without\\ space" "with\\ space " "with\\ slash/")
+              (nth 2 (bash-completion-dynamic-complete-nocomint 3 
(point))))))))
+
+(ert-deftest bash-completion-trailing-default-completion-nospace ()
+  (--with-fake-bash-completion-send
+   (push "without space\nwith space \nwith slash/\n" --send-results)
+   (insert "$ ls with")
+   (let ((bash-completion-nospace t))
+     (should (equal
+              '("without\\ space" "with\\ space" "with\\ slash/")
+              (nth 2 (bash-completion-dynamic-complete-nocomint 3 
(point))))))))
+
+(ert-deftest bash-completion-trailing-custom-completion ()
   (--with-fake-bash-completion-send
+   (setq bash-completion-alist '(("ls" "compgen" "args")))
    (push "without space\nwith space \nwith slash/\n" --send-results)
    (insert "$ ls with")
    (let ((bash-completion-nospace nil))
@@ -730,8 +778,9 @@ before calling `bash-completion-dynamic-complete-nocomint'.
               '("without\\ space" "with\\ space " "with\\ slash/")
               (nth 2 (bash-completion-dynamic-complete-nocomint 3 
(point))))))))
 
-(ert-deftest bash-completion-trailing-nospace ()
+(ert-deftest bash-completion-trailing-custom-completion-nospace ()
   (--with-fake-bash-completion-send
+   (setq bash-completion-alist '(("ls" "compgen" "args")))
    (push "without space\nwith space \nwith slash/\n" --send-results)
    (insert "$ ls with")
    (let ((bash-completion-nospace t))
diff --git a/bash-completion.el b/bash-completion.el
index a0e8850ded..d032e4dd57 100644
--- a/bash-completion.el
+++ b/bash-completion.el
@@ -389,7 +389,7 @@ This function is not meant to be called outside of
                             (bash-completion-quote after-wordbreak)))
       (let ((completions
             (bash-completion-extract-candidates
-              after-wordbreak unparsed-after-wordbreak open-quote nil)))
+              after-wordbreak unparsed-after-wordbreak open-quote 'default)))
        (list (+ stub-start separator-pos-in-unparsed)
               pos
              completions)))))
@@ -682,10 +682,11 @@ The result is a list of candidates, which might be empty."
   ;; start process now, to make sure bash-completion-alist is
   ;; set before we run bash-completion-generate-line
   
-  (let* ((process (bash-completion-require-process))
-        (completion-status
-         (bash-completion-send
-          (bash-completion-generate-line line pos words cword t))))
+  (let ((process (bash-completion-require-process))
+        (cmdline)
+        (completion-status))
+    (setq cmdline (bash-completion-generate-line line pos words cword t))
+    (setq completion-status (bash-completion-send (cdr cmdline)))
     (when (eq 124 completion-status)
       ;; Special 'retry-completion' exit status, typically returned by
       ;; functions bound by complete -D. Presumably, the function has
@@ -693,14 +694,14 @@ The result is a list of candidates, which might be empty."
       ;; us to retry once with the new configuration. 
       (bash-completion-send "complete -p" process)
       (bash-completion-build-alist (process-buffer process))
-      (setq completion-status
-           (bash-completion-send
-            (bash-completion-generate-line line pos words cword nil))))
+      (setq cmdline (bash-completion-generate-line line pos words cword nil))
+      (setq completion-status (bash-completion-send (cdr cmdline))))
     (when (eq 0 completion-status)
       (bash-completion-extract-candidates
-       (nth cword words) unparsed-stub open-quote (eq cword 0)))))
+       (nth cword words) unparsed-stub open-quote (car cmdline)))))
 
-(defun bash-completion-extract-candidates (parsed-stub unparsed-stub 
open-quote is-command)
+(defun bash-completion-extract-candidates
+    (parsed-stub unparsed-stub open-quote completion-type)
   "Extract the completion candidates from the process buffer for PARSED-STUB.
 
 This command takes the content of the completion process buffer,
@@ -718,15 +719,21 @@ If IS-COMMAND is t, it is passed down to 
`bash-completion-suffix'
 Post-processing includes escaping special characters, adding a /
 to directory names, replacing STUB with UNPARSED-STUB in the
 result. See `bash-completion-fix' for more details."
-  (let ((result (list)))
-    (dolist (completion (with-current-buffer (bash-completion-buffer)
-                          (split-string (buffer-string) "\n" t)))
-      (push (bash-completion-fix
-             completion parsed-stub unparsed-stub open-quote is-command)
-            result))
-    (nreverse result)))
-
-(defun bash-completion-fix (str parsed-prefix unparsed-prefix open-quote 
is-command)
+  (let ((candidates) (result (list)))
+    (setq candidates (with-current-buffer (bash-completion-buffer)
+                       (split-string (buffer-string) "\n" t)))
+    (if (eq 1 (length candidates))
+        (list (bash-completion-fix
+               (car candidates) parsed-stub unparsed-stub
+               open-quote completion-type t))
+      (dolist (completion candidates)
+        (push (bash-completion-fix
+               completion parsed-stub unparsed-stub open-quote completion-type 
nil)
+              result))
+      (nreverse result))))
+
+(defun bash-completion-fix
+    (str parsed-prefix unparsed-prefix open-quote completion-type single)
   "Fix completion candidate in STR if PREFIX is the current prefix.
 
 STR is the completion candidate to modify.
@@ -743,8 +750,11 @@ character (' or \") or nil.  If it is nil, the value of
 `bash-completion-open-quote' is used.  This allows
 calling this function from `mapcar'.
 
-If IS-COMMAND is t and `bash-completion-nospace' isn't set,
-guarantee the result return with a known suffix or space.
+COMPLETION-TYPE describes the type of completion that was executed,
+as generated by `bash-completion-generate-line'. It is used to choose
+whether to add a space and detect directories.
+
+If SINGLE is non-nil, this is the single completion candidate.
 
 Return a modified version of the completion candidate.
 
@@ -802,12 +812,17 @@ for directory name detection to work."
        ((or (memq last-char bash-completion-wordbreaks)
             (eq ?/ last-char))
         (setq suffix ""))
-       ((file-accessible-directory-p
+       ((and
+         (memq completion-type '(command default))
+         (file-accessible-directory-p
          (expand-file-name (bash-completion-unescape
                             open-quote (concat parsed-prefix rest))
-                           default-directory))
+                           default-directory)))
         (setq suffix "/"))
-       (is-command
+       ((and (not open-quote)
+             (or (eq completion-type 'command)
+                 (and (eq completion-type 'default)
+                      single)))
         (setq suffix (if bash-completion-nospace "" " ")))))
 
     ;; put everything back together
@@ -1068,23 +1083,26 @@ arguments will be passed to this function or command as:
  COMP_WORDS, taken from WORDS (a bash array)
  COMP_CWORD, taken for CWORD
 
-Return a bash command-line that calls compgen to get the completion
-candidates."
-  (concat
-   (bash-completion-cd-command-prefix)
-   (let* ( (command-name (file-name-nondirectory (car words)))
-          (compgen-args
-           (or (cdr (assoc command-name bash-completion-alist))
-               (and allowdefault (cdr (assoc nil bash-completion-alist)))))
-          (stub (nth cword words)) )
-     (cond
+Return a cons containing the completion type (command default or
+custom) and a bash command-line that calls compgen to get the
+completion candidates."
+  (let* ( (command-name (file-name-nondirectory (car words)))
+          (compgen-args
+           (or (cdr (assoc command-name bash-completion-alist))
+               (and allowdefault (cdr (assoc nil bash-completion-alist)))))
+          (stub (nth cword words))
+          (completion-type)
+          (commandline) )
+    (cond
       ((= cword 0)
        ;; a command. let bash expand builtins, aliases and functions
-       (concat "compgen -b -c -a -A function " stub))
+       (setq completion-type 'command)
+       (setq commandline (concat "compgen -b -c -a -A function " stub)))
 
       ((not compgen-args)
        ;; no completion configured for this command
-       (bash-completion-join (list "compgen" "-o" "default" stub)))
+       (setq completion-type 'default)
+       (setq commandline (bash-completion-join (list "compgen" "-o" "default" 
stub))))
 
       ((or (member "-F" compgen-args) (member "-C" compgen-args))
        ;; custom completion with a function of command
@@ -1093,7 +1111,9 @@ candidates."
              (function-name (car (cdr function))) )
         (setcar function "-F")
         (setcar (cdr function) "__bash_complete_wrapper")
-        (format "__BASH_COMPLETE_WRAPPER=%s compgen %s -- %s"
+         (setq completion-type 'custom)
+        (setq commandline
+               (format "__BASH_COMPLETE_WRAPPER=%s compgen %s -- %s"
                 (bash-completion-quote
                  (format "COMP_LINE=%s; COMP_POINT=%s; COMP_CWORD=%s; 
COMP_WORDS=( %s ); %s \"${COMP_WORDS[@]}\""
                          (bash-completion-quote line)
@@ -1102,11 +1122,16 @@ candidates."
                          (bash-completion-join words)
                          (bash-completion-quote function-name)))
                 (bash-completion-join args)
-                (bash-completion-quote stub))))
+                (bash-completion-quote stub)))))
       (t
        ;; simple custom completion
-       (format "compgen %s -- %s" (bash-completion-join compgen-args) stub))))
-   " 2>/dev/null"))
+       (setq completion-type 'custom)
+       (setq commandline (format "compgen %s -- %s" (bash-completion-join 
compgen-args) stub))))
+    (cons completion-type
+          (concat
+           (bash-completion-cd-command-prefix)
+           commandline
+           " 2>/dev/null"))))
 
 ;;;###autoload
 (defun bash-completion-reset ()



reply via email to

[Prev in Thread] Current Thread [Next in Thread]