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

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

[nongnu] elpa/bash-completion 2a937b3763 255/313: Don't let Emacs post-f


From: ELPA Syncer
Subject: [nongnu] elpa/bash-completion 2a937b3763 255/313: Don't let Emacs post-filter completions built by bash.
Date: Sat, 3 Dec 2022 10:59:35 -0500 (EST)

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

    Don't let Emacs post-filter completions built by bash.
    
    With this change, the completions built by bash are offered to the
    user even if they don't match the string to be completed.
    
    Before this change, the dynamic completion table would call
    try-completion/all-completion/test-completion, which would then filter
    the result according to the current completion style, which makes no
    sense as bash produces complete completions.
    
    Not allowing post-processing of the completion also means that there's
    no need to worry about the value of completion-ignore-case anymore on
    the Emacs side; bash-completion.el just serves whatever bash returns.
    
    Note that post-filtering is still possible when using
    bash-completion-dynamic-complete-nocomint with the argument
    dynamic-table left nil. In this scenario, the completion engine is
    free to process the results as it wants.
---
 bash-completion.el                       | 77 +++++++++++++++++---------------
 test/bash-completion-integration-test.el | 50 +++++++++++----------
 test/bash-completion-test.el             | 13 +-----
 3 files changed, 68 insertions(+), 72 deletions(-)

diff --git a/bash-completion.el b/bash-completion.el
index 9836051d3f..b6d6d20301 100644
--- a/bash-completion.el
+++ b/bash-completion.el
@@ -441,12 +441,6 @@ returned."
     (process-put process 'bash-major-version bash-major-version)
 
     (bash-completion-send "bind -v 2>/dev/null" process)
-    (process-put process 'completion-ignore-case 
-                 (with-current-buffer (bash-completion--get-buffer process)
-                   (save-excursion
-                     (goto-char (point-min))
-                     (search-forward "completion-ignore-case on" nil 
'noerror))))
-
     (process-put process 'setup-done t)))
 
 ;;; ---------- Inline functions
@@ -522,10 +516,6 @@ When doing completion outside of a comint buffer, call
         (if message-timer
             (cancel-timer message-timer)))))
 
-(defalias 'bash-completion--completion-table-with-cache
-  (if (fboundp 'completion-table-with-cache)
-      'completion-table-with-cache 'completion-table-dynamic))
-
 (defun bash-completion--complete (comp process)
   (condition-case err
       (bash-completion-comm comp process)
@@ -570,26 +560,16 @@ Returns (list stub-start stub-end completions) with
                     comp-start comp-pos
                     (process-get process 'wordbreaks)
                     (process-get process 'bash-major-version)))
-             (stub-start (bash-completion--stub-start comp))
-             (use-separate-processes bash-completion-use-separate-processes))
-
-        ;; This sets completion-ignore-case which matters for the
-        ;; caller, as it needs to know how to post-process the
-        ;; results.
-        (let ((ignore-case (process-get process 'completion-ignore-case)))
-          (unless (eq completion-ignore-case ignore-case)
-            (setq completion-ignore-case ignore-case)))
-        
+             (stub-start (bash-completion--stub-start comp)))
+
         (bash-completion--customize comp process)
         (list
          stub-start
          comp-pos
          (if dynamic-table
              (bash-completion--completion-table-with-cache
-              (lambda (_)
-                (let ((bash-completion-use-separate-processes
-                       use-separate-processes))
-                  (bash-completion--complete comp process))))
+              comp process
+              bash-completion-use-separate-processes)
            (bash-completion--complete comp process)))))))
 
 (defun bash-completion--find-last (elt array)
@@ -981,18 +961,6 @@ for directory name detection to work."
      ((bash-completion-starts-with str parsed-prefix)
       (setq rest (substring str (length parsed-prefix))))
 
-     ;; unexpand the home directory expanded by bash automatically
-     ((and (bash-completion-starts-with parsed-prefix "~")
-           (bash-completion-starts-with str (bash-completion--expand-file-name 
"~" t)))
-      (setq rest (substring (concat "~" (substring str (length 
(bash-completion--expand-file-name "~" t))))
-                            (length parsed-prefix))))
-
-     ((bash-completion-starts-with parsed-prefix str)
-      ;; completion is a substring of prefix something's gone
-      ;; wrong. Treat it as one (useless) candidate.
-      (setq unparsed-prefix "")
-      (setq rest str))
-
      ;; completion sometimes only applies to the last word, as
      ;; defined by COMP_WORDBREAKS. This detects and works around
      ;; this feature.
@@ -1005,6 +973,13 @@ for directory name detection to work."
      ;; Bypass the whole prefix/suffix logic and replace the string
      ;; being completed with the string provided by the completion
      ;; logic.
+     ((string-match "^~.*?\\($\\|/\\)" str)
+      (setq parsed-prefix (substring str 0 (match-end 0))
+            unparsed-prefix
+            (concat (substring str 0 (match-end 0))
+                    (if open-quote (char-to-string open-quote) ""))
+            rest (substring str (match-end 0))))
+     
      (t
       (setq parsed-prefix ""
             unparsed-prefix (if open-quote (char-to-string open-quote) "")
@@ -1563,6 +1538,36 @@ Return the parsed value, as a string or nil."
         (prog1 (match-string 1)
           (delete-region (match-beginning 0) (match-end 0)))))))
 
+(defun bash-completion--completion-table-with-cache (comp process 
use-separate-process)
+  "Build a dynamic completion table for COMP using PROCESS.
+
+The result is a function that works like one built by
+`completion-table-with-cache' with the difference that the
+completions, built by `bash-completion--complete' are complete
+and that completion style doesn't necessarily use substring
+completion."
+  (let ((last-str) (last-result))
+    (lambda (str predicate action)
+      (if (or (eq (car-safe action) 'boundaries)
+              (eq action 'metadata))
+          nil
+        (let ((result (if (equal str last-str)
+                          last-result
+                        (let ((bash-completion-use-separate-processes
+                               use-separate-process))
+                          (bash-completion--complete comp process)))))
+          (setq last-str str
+                last-result result)
+          ;; The below passes an empty string to try-completion,
+          ;; all-completions and test-completion to not let them do
+          ;; any further filtering.
+          (funcall
+           (cond
+            ((null action) 'try-completion)
+            ((eq action t) 'all-completions)
+            (t 'test-completion))
+           "" result predicate))))))
+
 (provide 'bash-completion)
 
 ;; Local Variables:
diff --git a/test/bash-completion-integration-test.el 
b/test/bash-completion-integration-test.el
index e2fe939ef7..97e0238590 100644
--- a/test/bash-completion-integration-test.el
+++ b/test/bash-completion-integration-test.el
@@ -65,12 +65,16 @@
        ;; Give Emacs time to process any input or process state
        ;; change from bash-completion-reset.
        (while (accept-process-output nil 0.1))
-       (unwind-protect
-           (progn ,@body)
-         (progn
-           (set explicit-args-var old-explicit-args)
-           (bash-completion_test-teardown-env test-env-dir)
-           (bash-completion-reset-all))))))
+       (let ((realhome (getenv "HOME")))
+         (unwind-protect
+             (progn
+               (setenv "HOME" test-env-dir)
+               ,@body)
+           (progn
+             (setenv "HOME" realhome)
+             (set explicit-args-var old-explicit-args)
+             (bash-completion_test-teardown-env test-env-dir)
+             (bash-completion-reset-all)))))))
 
 (defmacro bash-completion_test-with-shell-harness (bashrc use-separate-process 
&rest body)
   `(bash-completion_test-harness
@@ -341,11 +345,9 @@ for testing completion."
     "}\n"
     "complete -F _myprog myprog\n")
    t ; bash-completion-use-separate-processes
-   (let ((completion-in-region-function 'completion--in-region)
-         (completion-styles '(basic partial-completion substring emacs22)))
-     (should (equal
-              "myprog blah batitita "
-              (bash-completion_test-complete "myprog blah titi"))))))
+   (should (equal
+            "myprog blah batitita "
+            (bash-completion_test-complete "myprog blah titi")))))
 
 (ert-deftest bash-completion-integration-vioption-single-process-test ()
   (bash-completion_test--with-bash-option "set -o vi" nil))
@@ -409,7 +411,13 @@ for testing completion."
       (should (equal "ls \"Another Uppercase/" (bash-completion_test-complete 
"ls \"ano")))
       (should (equal "ls 'Another Uppercase/" (bash-completion_test-complete 
"ls 'Ano")))
       (should (equal "ls 'Another Uppercase/" (bash-completion_test-complete 
"ls 'ano")))
-      (should completion-ignore-case)))))
+
+      ;; When doing case-insensitive search, bash-completion.el cannot
+      ;; keep the exact same quotes, so it just puts the quote, if
+      ;; any, at the beginning, just after the tilde part.
+      (should (equal "ls \"Another Uppercase/" (bash-completion_test-complete 
"ls ano\"t")))
+      (should (equal "ls 'Another Uppercase/" (bash-completion_test-complete 
"ls ano't")))
+      (should (equal "ls ~/\"Another Uppercase/" 
(bash-completion_test-complete "ls ~/ano\"t")))))))      
 
 (ert-deftest bash-completion-integration-case-sensitive-test ()
   (bash-completion_test-harness
@@ -427,18 +435,12 @@ for testing completion."
     (should (not completion-ignore-case)))))
 
 (ert-deftest bash-completion-integration-tilde-test ()
-  (bash-completion_test-harness
-   "HOME=$PWD\n"
+  (bash-completion_test-with-shell-harness
+   ""
    nil ; use-separate-process
-   (let ((realhome (getenv "HOME")))
-     (unwind-protect
-         (progn
-           (setenv "HOME" test-env-dir)
-           (bash-completion_test-with-shell
-            (should (equal "ls some/" (bash-completion_test-complete "ls so")))
-            (should (equal "ls ~/some/" (bash-completion_test-complete "ls 
~/so")))
-            (should (equal "ls \"~/some/" (bash-completion_test-complete "ls 
\"~/so")))
-            (should (equal "ls '~/some/" (bash-completion_test-complete "ls 
'~/so")))))
-       (setenv "HOME" realhome)))))
+   (should (equal "ls some/" (bash-completion_test-complete "ls so")))
+   (should (equal "ls ~/some/" (bash-completion_test-complete "ls ~/so")))
+   (should (equal "ls \"~/some/" (bash-completion_test-complete "ls \"~/so")))
+   (should (equal "ls '~/some/" (bash-completion_test-complete "ls '~/so")))))
 
 ;;; bash-completion-integration-test.el ends here
diff --git a/test/bash-completion-test.el b/test/bash-completion-test.el
index 5c794525a2..84c2e2f30b 100644
--- a/test/bash-completion-test.el
+++ b/test/bash-completion-test.el
@@ -638,17 +638,6 @@ Return (const return-value new-buffer-content)"
                    :compgen-args '("-o" "nospace"))
                   nil)))
 
-  ;; unexpand home and escape
-  (should (equal "~/a/hello\\ world"
-                (bash-completion-fix
-                  (expand-file-name "~/a/hello world")
-                  (bash-completion--make
-                   :cword 1
-                   :stub "~/a/he"
-                   :unparsed-stub "~/a/he"
-                   :wordbreaks "")
-                  nil)))
-
   ;; match after wordbreak and escape
   (should (equal "a:b:c:hello\\ world"
                 (bash-completion-fix
@@ -674,7 +663,7 @@ Return (const return-value new-buffer-content)"
   ;; append / for home
   (should (equal "~/"
                  (bash-completion-fix
-                  (expand-file-name "~")
+                  "~"
                   (bash-completion--make
                    :cword 1
                    :stub "~"



reply via email to

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