(feat): Improve interactive format for ref completions (#906)

This commit is contained in:
Leo Vivier
2020-07-10 05:47:07 +02:00
committed by GitHub
parent ca4a7421bc
commit b8aa5c1f23

View File

@@ -859,41 +859,49 @@ whose title is 'Index'."
;;;; org-roam-find-ref ;;;; org-roam-find-ref
(defun org-roam--get-ref-path-completions (&optional interactive filter) (defun org-roam--get-ref-path-completions (&optional interactive filter)
"Return an alist of refs to absolute path of Org-roam files. "Return an alist of refs to absolute path of Org-roam files.
When `org-roam-include-type-in-ref-path-completions' and When called interactively (i.e. when INTERACTIVE is non-nil),
INTERACTIVE are non-nil, format the car of the format the car of the completion-candidates with extra
completion-candidates as 'type:ref'. information: title, tags, and type if
FILTER can either be a string or a function: `org-roam-include-type-in-ref-path-completions' is non-nil.
- If it is a string, it should be the type of refs to include as FILTER can either be a string or a function: - If it is a string,
candidates (e.g. \"cite\" ,\"website\" ,etc.) it should be the type of refs to include as candidates (e.g.
- If it is a function, it should be the name of a function that \"cite\" ,\"website\" ,etc.) - If it is a function, it should be
takes three arguments: the type, the ref, and the file of the the name of a function that takes three arguments: the type, the
current candidate. It should return t if that candidate is to be ref, and the file of the current candidate. It should return t if
included as a candidate." that candidate is to be included as a candidate."
(let ((rows (org-roam-db-query [:select [refs:type refs:ref refs:file ] :from refs (let ((rows (org-roam-db-query
:left :join files [:select [refs:type refs:ref refs:file titles:titles tags:tags]
:on (= refs:file files:file)])) :from titles
(include-type (and interactive :left :join tags
org-roam-include-type-in-ref-path-completions)) :on (= titles:file tags:file)
:left :join refs :on (= titles:file refs:file)
:where refs:file :is :not :null]))
completions) completions)
(seq-sort-by (lambda (x) (seq-sort-by (lambda (x)
(plist-get (nth 3 x) :mtime)) (plist-get (nth 3 x) :mtime))
#'time-less-p #'time-less-p
rows) rows)
(dolist (row rows completions) (dolist (row rows completions)
(pcase-let ((`(,type ,ref ,file-path) row)) (pcase-let ((`(,type ,ref ,file-path ,titles ,tags) row))
(when (pcase filter (let ((titles (or titles (list (org-roam--path-to-slug file-path)))))
('nil t) (when (pcase filter
((pred stringp) (string= type filter)) ('nil t)
((pred functionp) (funcall filter type ref file-path)) ((pred stringp) (string= type filter))
(wrong-type (signal 'wrong-type-argument ((pred functionp) (funcall filter type ref file-path))
`((stringp functionp) (wrong-type (signal 'wrong-type-argument
,wrong-type)))) `((stringp functionp)
(let ((k (concat ,wrong-type))))
(when include-type (dolist (title titles)
(format "(%s) " type)) (let ((k (if interactive
ref)) (concat
(v (list :path file-path :type type :ref ref))) (when org-roam-include-type-in-ref-path-completions
(push (cons k v) completions))))))) (format "{%s} " type))
(when tags
(format "(%s) " (s-join org-roam-tag-separator tags)))
(format "%s (%s)" title ref))
ref))
(v (list :path file-path :type type :ref ref)))
(push (cons k v) completions)))))))))
(defun org-roam--find-file (file) (defun org-roam--find-file (file)
"Open FILE using `org-roam-find-file-function' or `find-file'." "Open FILE using `org-roam-find-file-function' or `find-file'."