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

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

[nongnu] elpa/bash-completion 685ed73df0 243/313: Allow completion to re


From: ELPA Syncer
Subject: [nongnu] elpa/bash-completion 685ed73df0 243/313: Allow completion to replace the word being completed.
Date: Sat, 3 Dec 2022 10:59:34 -0500 (EST)

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

    Allow completion to replace the word being completed.
    
    With this change, when bash-completion cannot find a link between the
    word being completed and the completion result, it'll return the
    completion result as a whole. This behavior is more consistent with
    what bash does and accept more complex completion logic.
---
 bash-completion.el                       | 61 +++++++++++++++++---------------
 test/bash-completion-integration-test.el | 28 +++++++++++++--
 test/bash-completion-test.el             | 10 +++---
 3 files changed, 64 insertions(+), 35 deletions(-)

diff --git a/bash-completion.el b/bash-completion.el
index 44ce408e9a..56a6126572 100644
--- a/bash-completion.el
+++ b/bash-completion.el
@@ -949,6 +949,7 @@ for directory name detection to work."
         (wordbreaks (bash-completion--wordbreaks comp))
         (suffix "")
         (rest) ; the part between the prefix and the suffix
+        (whole)
         (rebuilt))
 
     ;; build rest by removing parsed-prefix from str
@@ -976,34 +977,38 @@ for directory name detection to work."
        parsed-prefix)
       (setq rest (substring rebuilt (length parsed-prefix))))
 
-     ;; there is no meaningful link between the prefix and
-     ;; the string. just append the string to the prefix and
-     ;; hope for the best.
-     (t (setq rest str)))
-
-    ;; build suffix
-    (let ((last-char (bash-completion-last-char rest))
-          (close-quote-str (if open-quote (char-to-string open-quote) ""))
-          (final-space-str (if nospace "" " ")))
-      (cond
-       ((eq ?\  last-char)
-        (setq rest (substring rest 0 -1))
-        (setq suffix (concat close-quote-str final-space-str)))
-       ((or (bash-completion--find-last last-char wordbreaks)
-            (eq ?/ last-char))
-        (setq suffix ""))
-       ((file-accessible-directory-p
-         (bash-completion--expand-file-name (bash-completion-unescape
-                                             open-quote (concat parsed-prefix 
rest))))
-        (setq suffix "/"))
-       (single
-        (setq suffix (concat close-quote-str final-space-str)))
-       (t (setq suffix close-quote-str))))
-
-    ;; put everything back together
-    (concat unparsed-prefix
-            (bash-completion-escape-candidate rest open-quote)
-            suffix)))
+     ;; there is no meaningful link between the prefix and the string.
+     ;; Bypass the whole prefix/suffix logic and replace the string
+     ;; being completed with the string provided by the completion
+     ;; logic.
+     (t (setq whole str)))
+    
+    (if whole
+        (bash-completion-escape-candidate whole nil)
+      
+      ;; build suffix
+      (let ((last-char (bash-completion-last-char rest))
+            (close-quote-str (if open-quote (char-to-string open-quote) ""))
+            (final-space-str (if nospace "" " ")))
+        (cond
+         ((eq ?\  last-char)
+          (setq rest (substring rest 0 -1))
+          (setq suffix (concat close-quote-str final-space-str)))
+         ((or (bash-completion--find-last last-char wordbreaks)
+              (eq ?/ last-char))
+          (setq suffix ""))
+         ((file-accessible-directory-p
+           (bash-completion--expand-file-name (bash-completion-unescape
+                                               open-quote (concat 
parsed-prefix rest))))
+          (setq suffix "/"))
+         (single
+          (setq suffix (concat close-quote-str final-space-str)))
+         (t (setq suffix close-quote-str))))
+      
+      ;; put everything back together
+      (concat unparsed-prefix
+              (bash-completion-escape-candidate rest open-quote)
+              suffix))))
 
 (defun bash-completion-escape-candidate (completion-candidate open-quote)
   "Escapes COMPLETION-CANDIDATE.
diff --git a/test/bash-completion-integration-test.el 
b/test/bash-completion-integration-test.el
index 8cf504d946..2c62d494aa 100644
--- a/test/bash-completion-integration-test.el
+++ b/test/bash-completion-integration-test.el
@@ -87,7 +87,8 @@
              (bash-completion--wait-for-prompt (get-buffer-process 
shell-buffer)
                                                
(bash-completion--get-prompt-regexp)
                                                3.0)
-             (let ((comint-dynamic-complete-functions 
'(bash-completion-dynamic-complete)))
+             (let ((comint-dynamic-complete-functions 
'(bash-completion-dynamic-complete))
+                   (completion-at-point-functions '(comint-completion-at-point 
t)))
                (progn ,@body))))
        (when shell-buffer
          (when (and (buffer-live-p shell-buffer)
@@ -100,12 +101,20 @@
   (process-get (bash-completion--get-process) 'bash-major-version))
 
 (defun bash-completion_test-complete (complete-me)
+  "Complete COMPLETE-ME and returns the resulting string."
   (goto-char (point-max))
   (delete-region (line-beginning-position) (line-end-position))
   (insert complete-me)
   (completion-at-point)
   (buffer-substring-no-properties
-   (comint-line-beginning-position) (point)))
+   (line-beginning-position) (point)))
+
+(defun bash-completion_test-candidates (complete-me)
+  "Complete COMPLETE-ME and returns the candidates."
+  (goto-char (point-max))
+  (delete-region (line-beginning-position) (line-end-position))
+  (insert complete-me)
+  (nth 2 (bash-completion-dynamic-complete-nocomint)))
 
 (defun bash-completion_test-setup-env (bashrc)
   "Sets up a directory that contains a bashrc file other files
@@ -309,5 +318,20 @@ for testing completion."
                 "sometimes_not_nospace dummyo"
                 (bash-completion_test-complete "sometimes_not_nospace 
dum")))))))
 
+(ert-deftest bash-completion-integration-bash-4-complex-completion ()
+  (bash-completion_test-with-shell-harness
+   (concat ; .bashrc
+    "function _myprog {\n"
+    "  COMPREPLY=( \"ba${COMP_WORDS[$COMP_CWORD]}ta\" )\n"
+    "  COMPREPLY+=( \"ba${COMP_WORDS[$COMP_CWORD]}to\" )\n"
+    "}\n"
+    "complete -F _myprog myprog\n")
+   nil ; use-separate-process
+   ;; The default completion engine doesn't support replacing the word
+   ;; to complete with candidates and will ignore all candidates, but
+   ;; other completions engines do support it, so it's worth returning
+   ;; them - but we can't use bash-completion_test-complete.
+   (should (equal '("batitita" "batitito")
+                  (bash-completion_test-candidates "myprog blah titi")))))
 
 ;;; bash-completion-integration-test.el ends here
diff --git a/test/bash-completion-test.el b/test/bash-completion-test.el
index 4f0d8c06c9..c4516e434a 100644
--- a/test/bash-completion-test.el
+++ b/test/bash-completion-test.el
@@ -660,14 +660,14 @@ Return (const return-value new-buffer-content)"
                    :wordbreaks "@><=;|&(:")
                   nil)))
 
-  ;; just append
-  (should (equal "hello\\ world"
+  ;; just replace
+  (should (equal "something\\ else"
                 (bash-completion-fix
-                  " world"
+                  "something else"
                   (bash-completion--make
                    :cword 1
-                   :stub "hello"
-                   :unparsed-stub "hello"
+                   :stub "something"
+                   :unparsed-stub "something"
                    :wordbreaks "")
                   nil)))
 



reply via email to

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