abo-abo / swiper

Ivy - a generic completion frontend for Emacs, Swiper - isearch with an overview, and more. Oh, man!
https://oremacs.com/swiper/
2.27k stars 337 forks source link

Tramp completion returns irrelevant entries #3013

Open ywwry66 opened 1 year ago

ywwry66 commented 1 year ago

With ivy mode on, typing in /ssh: in dired mode followed by C-j will return irrelevant completion entries. For example, it returns the local hostname; it also returns a nil entry.

After looking at tramp.el, I figured the following possible fix. In the function tramp-completion-handle-file-name-all-completions which tramp.el uses to handle completion, variables tramp-default-user tramp-default-user-alist tramp-default-host tramp-default-host-alist are set to nil within the function so that tramp-get-completion-function won't output things like local hostname. As for the nil entry, it can be removed manually.

(defun ivy--tramp-candidates ()
   (let ((method (match-string 1 ivy-text))
         (user (match-string 2 ivy-text))
         (rest (match-string 3 ivy-text))
+    (tramp-default-method
+     (and (string-empty-p tramp-postfix-method-format)
+          tramp-default-method))
+    (tramp-default-method-alist
+     (and (string-empty-p tramp-postfix-method-format)
+          tramp-default-method-alist))
+    tramp-default-user tramp-default-user-alist
+    tramp-default-host tramp-default-host-alist
         res)
     (dolist (x (tramp-get-completion-function method))
       (setq res (append res (funcall (car x) (cadr x)))))
     (setq res (delq nil res))
     (when user
       (dolist (x res)
         (setcar x user)))
     (setq res (delete-dups res))
+    (setq res (delete '(nil nil) res))
     (let* ((old-ivy-last ivy-last)
            (enable-recursive-minibuffers t)
            (host (let ((ivy-auto-select-single-candidate nil))
                   (ivy-read "user@host: "
                             (mapcar #'ivy-build-tramp-name res)
                             :initial-input rest))))
      (setq ivy-last old-ivy-last)
      (when host
        (setq ivy--directory "/")
        (ivy--cd (concat "/" method ":" host ":/"))))))