(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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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]
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)
"Return all files reachable from/connected to FILE, including the file itself.
If the file does not have any connections, nil is returned."

View file

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