]> git.eshelyaron.com Git - emacs.git/commitdiff
(kubed-command-line-completion-table): Also complete file names
authorEshel Yaron <me@eshelyaron.com>
Wed, 24 Jul 2024 16:52:53 +0000 (18:52 +0200)
committerEshel Yaron <me@eshelyaron.com>
Wed, 24 Jul 2024 16:52:53 +0000 (18:52 +0200)
lisp/net/kubed.el

index 5289b9bff903716398050bd1c01cea01e1ff1466..280979b6f424c417c642b2bf055e9289ca82f73a 100644 (file)
@@ -1271,59 +1271,77 @@ Optional argument DEFAULT is the minibuffer default argument."
 
 Perform completion action A on string S with predicate P."
   (let ((start 0))
-    (while (string-match "[[:space:]]" s start)
+    (while (string-match "[[:space:]=]" s start)
       (setq start (match-end 0)))
-    (cond
-     ((eq (car-safe a) 'boundaries)
-      `(boundaries ,start . ,(and (string-match "[[:space:]]" (cdr a))
-                                  (match-beginning 0))))
-     ((eq a 'metadata)
-      `(metadata
-        (category . kubed-command-line)
-        (affixation-function
-         . ,(lambda (cands)
-              (let ((max (seq-max
-                          (cons 0 (mapcar #'string-width cands)))))
-                (mapcar
-                 (lambda (cand)
-                   (list
-                    cand ""
-                    (if-let
-                        ((desc (get-text-property
-                                0 'kubed-command-line-argument-description
-                                cand)))
-                        (concat
-                         (make-string (1+ (- max (string-width cand))) ?\s)
-                         (propertize desc 'face 'completions-annotations))
-                      "")))
-                 cands))))))
-     (t (let ((table
-               (let* ((lines
-                       (apply #'process-lines-ignore-status
-                              kubed-kubectl-executable "__complete"
-                              (let ((args (cdr (split-string-and-unquote s))))
-                                (if (string-suffix-p " " s)
-                                    (nconc args '(""))
-                                  args))))
-                      (code nil)
-                      (comps (seq-take-while
-                              (lambda (line)
-                                (not (and (string-match "^:\\([0-9]+\\)$" line)
-                                          (setq code (string-to-number
-                                                      (match-string 1 line))))))
-                              lines)))
-                 (when (and code (zerop (logand #b1001 code)))
-                   (mapcar
-                    (lambda (comp)
-                      (pcase (split-string comp "\t" t)
-                        (`(,c ,a . ,_)
-                         (propertize c 'kubed-command-line-argument-description
-                                     (car (split-string a "\\." t))))
-                        (`(,c . ,_)  c)))
-                    comps)))))
-          (if a (complete-with-action a table (substring s start) p)
-            (let ((comp (complete-with-action a table (substring s start) p)))
-              (if (stringp comp) (concat (substring s 0 start) comp) comp))))))))
+    (if (eq a 'metadata)
+        `(metadata
+          (category . kubed-command-line)
+          (affixation-function
+           . ,(lambda (cands)
+                (let ((max (seq-max
+                            (cons 0 (mapcar #'string-width cands)))))
+                  (mapcar
+                   (lambda (cand)
+                     (list
+                      cand ""
+                      (if-let
+                          ((desc (get-text-property
+                                  0 'kubed-command-line-argument-description
+                                  cand)))
+                          (concat
+                           (make-string (1+ (- max (string-width cand))) ?\s)
+                           (propertize desc 'face 'completions-annotations))
+                        "")))
+                   cands)))))
+      (let* ((lines
+              (apply #'process-lines-ignore-status
+                     kubed-kubectl-executable "__complete"
+                     (let ((args (cdr (split-string-and-unquote s))))
+                       (if (string-suffix-p " " s)
+                           (nconc args '(""))
+                         args))))
+             (code nil)
+             (comps (seq-take-while
+                     (lambda (line)
+                       (not (and (string-match "^:\\([0-9]+\\)$" line)
+                                 (setq code (string-to-number
+                                             (match-string 1 line))))))
+                     lines)))
+        (when (and code (not (zerop code)))
+          (if (zerop (logand #b1001 code))
+              (if (eq (car-safe a) 'boundaries)
+                  `(boundaries ,start . ,(and (string-match "[[:space:]=]" (cdr a))
+                                              (match-beginning 0)))
+                (let ((table (mapcar
+                              (lambda (comp)
+                                (pcase (split-string comp "\t" t)
+                                  (`(,c ,a . ,_)
+                                   (propertize c 'kubed-command-line-argument-description
+                                               (car (split-string a "\\." t))))
+                                  (`(,c . ,_)  c)))
+                              comps)))
+                  (if a (complete-with-action a table (substring s start) p)
+                    ;; `try-completion'.
+                    (let ((comp (complete-with-action a table (substring s start) p)))
+                      (if (stringp comp) (concat (substring s 0 start) comp) comp)))))
+            ;; File name completion.  `comps' are valid extensions.
+            (setq p (lambda (f)
+                      (or (file-directory-p f)
+                          (when (string-match "\\.[^.]*\\'" f)
+                            (member (substring f (1+ (match-beginning 0)))
+                                    comps)))))
+            (if (eq (car-safe a) 'boundaries)
+                ;; Find nested boundaries.
+                (let* ((suf (cdr a))
+                       (bounds (completion-boundaries
+                                (substring s start) #'completion-file-name-table p
+                                (substring suf 0 (string-match "[[:space:]=]" suf)))))
+                  `(boundaries ,(+ (car bounds) start) . ,(cdr bounds)))
+              (if a (complete-with-action a #'completion-file-name-table
+                                          (substring s start) p)
+                (let ((comp (complete-with-action a #'completion-file-name-table
+                                                  (substring s start) p)))
+                  (if (stringp comp) (concat (substring s 0 start) comp) comp))))))))))
 
 (defvar kubed-kubectl-command-history nil
   "Minibuffer history for `kubed-kubectl-command'.")