From 8894fac8c46905fab563f67d2bc80762a4e34af8 Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Wed, 24 Jul 2024 18:52:53 +0200 Subject: [PATCH] (kubed-command-line-completion-table): Also complete file names --- lisp/net/kubed.el | 122 ++++++++++++++++++++++++++-------------------- 1 file changed, 70 insertions(+), 52 deletions(-) diff --git a/lisp/net/kubed.el b/lisp/net/kubed.el index 5289b9bff90..280979b6f42 100644 --- a/lisp/net/kubed.el +++ b/lisp/net/kubed.el @@ -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'.") -- 2.39.2