branch: master
commit 230ae1a018c166addb258e20c51150b9d42edcaa
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>
Implement unique index for alist completion
The uniqueness assumption is that the completion system is passed a list
of /unique/ strings, of which one (or more) are selected.
Unlike plain string completion, alists may require violating the
uniqueness assumption: there may be two elements with the same `car' but
different `cdr'. Example: C function declaration and definition for tag
completion.
Until now, whenever two equal strings were sent to `ivy-read', only the
first one could be selected. Now, each alist car gets an integer index
assigned to it as a text property 'idx. So it's possible to
differentiate two alist items with the same key.
* ivy.el (ivy-call): Possibly locate the collection item using the 'idx
property.
(ivy-read): Remove the added 'idx text property.
(ivy--reset-state): Add 'idx text property to alist type collection.
* ivy-test.el: Update test.
---
ivy-test.el | 2 +-
ivy.el | 13 +++++++++++--
2 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/ivy-test.el b/ivy-test.el
index 121e63f..09b341a 100644
--- a/ivy-test.el
+++ b/ivy-test.el
@@ -88,7 +88,7 @@
(ivy-read "test" '(("foo" . "bar"))
:action (lambda (x) (prin1 x))))
"f C-m")
- "(\"foo\" . \"bar\")"))
+ "(#(\"foo\" 0 1 (idx 0)) . \"bar\")"))
(should (equal
(ivy-with
'(with-output-to-string
diff --git a/ivy.el b/ivy.el
index 1ba266a..0cea3d9 100644
--- a/ivy.el
+++ b/ivy.el
@@ -946,7 +946,10 @@ Example use:
(consp (car collection))
;; Previously, the cdr of the selected candidate
would be returned.
;; Now, the whole candidate is returned.
- (assoc ivy--current collection)))
+ (let (idx)
+ (if (setq idx (get-text-property 0 'idx
ivy--current))
+ (nth idx collection)
+ (assoc ivy--current collection)))))
((equal ivy--current "")
ivy-text)
(t
@@ -1435,6 +1438,8 @@ customizations apply to the current completion session."
(set hist (cons (propertize item 'ivy-index ivy--index)
(delete item
(cdr (symbol-value hist))))))))
+ (when (> (length ivy--current) 0)
+ (remove-text-properties 0 1 '(idx) ivy--current))
ivy--current))
(remove-hook 'post-command-hook #'ivy--exhibit)
(when (setq unwind (ivy-state-unwind ivy-last))
@@ -1539,7 +1544,11 @@ This is useful for recursive `ivy-read'."
(cl-sort
(copy-sequence collection)
sort-fn))))
- (setq coll (all-completions "" collection predicate))))
+ (setq coll (all-completions "" collection predicate)))
+ (let ((i 0))
+ (dolist (cm coll)
+ (add-text-properties 0 1 `(idx ,i) cm)
+ (cl-incf i))))
((or (functionp collection)
(byte-code-function-p collection)
(vectorp collection)