(feat): tag completion via capf (#1017)

This commit is contained in:
Jethro Kuan
2020-08-09 21:49:06 +08:00
committed by GitHub
parent 5d02e6407b
commit 0ed9057a87
2 changed files with 49 additions and 26 deletions

View File

@ -314,6 +314,16 @@ Insertions can fail if the key is already in the database."
:limit 1] :limit 1]
file))) file)))
(defun org-roam-db--get-tags ()
"Return all distinct tags from the cache."
(let ((rows (org-roam-db-query [:select :distinct [tags] :from tags]))
acc)
(dolist (row rows)
(dolist (tag (car row))
(unless (member tag acc)
(push tag acc))))
acc))
(defun org-roam-db--connected-component (file) (defun org-roam-db--connected-component (file)
"Return all files reachable from/connected to FILE, including the file itself. "Return all files reachable from/connected to FILE, including the file itself.
If the file does not have any connections, nil is returned." If the file does not have any connections, nil is returned."

View File

@ -1098,33 +1098,45 @@ This function hooks into `org-open-at-point' via
(defun org-roam-complete-at-point () (defun org-roam-complete-at-point ()
"Do appropriate completion for the thing at point." "Do appropriate completion for the thing at point."
(let ((end (point)) (let ((end (point))
start (start (point))
(exit-fn (lambda (&rest _) nil))
collection) collection)
(cond (;; In an open bracket (cond
(looking-back (concat "^.*" org-roam-open-bracket-regexp) (line-beginning-position)) (;; completing roam_tags
(setq start (match-beginning 1) (looking-back "^#\\+roam_tags:.*" (line-beginning-position))
end (match-end 1)) (when (looking-at "\\>")
(save-match-data (setq start (save-excursion (skip-syntax-backward "w")
(save-excursion (point))
(goto-char start) end (point)))
(when (looking-at org-roam-title-headline-split-regexp) (setq collection #'org-roam-db--get-tags
(let ((title (match-string-no-properties 1)) exit-fn (lambda (str _status)
(has-headline-p (not (string-empty-p (match-string-no-properties 2)))) (delete-char (- (length str)))
(headline-start (match-beginning 3))) (insert "\"" str "\""))))
(cond (;; title and headline present (;; In an open bracket
(and (not (string-empty-p title)) (looking-back (concat "^.*" org-roam-open-bracket-regexp) (line-beginning-position))
has-headline-p) (setq start (match-beginning 1)
(when-let ((file (org-roam--get-file-from-title title t))) end (match-end 1))
(setq collection (apply-partially #'org-roam--get-headlines file)) (save-match-data
(setq start headline-start))) (save-excursion
(;; Only title (goto-char start)
(not has-headline-p) (when (looking-at (concat org-roam-title-headline-split-regexp "\]\]"))
(setq collection #'org-roam--get-titles)) (let ((title (match-string-no-properties 1))
(;; Only headline (has-headline-p (not (string-empty-p (match-string-no-properties 2))))
(string-empty-p title) (headline-start (match-beginning 3)))
has-headline-p (cond (;; title and headline present
(setq collection #'org-roam--get-headlines) (and (not (string-empty-p title))
(setq start headline-start))))))))) has-headline-p)
(when-let ((file (org-roam--get-file-from-title title t)))
(setq collection (apply-partially #'org-roam--get-headlines file))
(setq start headline-start)))
(;; Only title
(not has-headline-p)
(setq collection #'org-roam--get-titles))
(;; Only headline
(string-empty-p title)
has-headline-p
(setq collection #'org-roam--get-headlines)
(setq start headline-start)))))))))
(when collection (when collection
(let ((prefix (buffer-substring-no-properties start end))) (let ((prefix (buffer-substring-no-properties start end)))
(list start end (list start end
@ -1133,6 +1145,7 @@ This function hooks into `org-open-at-point' via
(lambda (_) (lambda (_)
(cl-remove-if (apply-partially 'string= prefix) (funcall collection)))) (cl-remove-if (apply-partially 'string= prefix) (funcall collection))))
collection) collection)
:exit-function exit-fn
'ignore))))) 'ignore)))))
;;; Fuzzy Links ;;; Fuzzy Links