completion/ivy: fix +ivy/tasks, add :todo & +ivy:todo

This commit is contained in:
Henrik Lissner 2017-05-10 06:13:14 +02:00
parent c4e404b0bb
commit bfd79e78ce
4 changed files with 67 additions and 6 deletions

View file

@ -40,3 +40,9 @@ session."
"Invoke `swiper' with SEARCH, otherwise with the symbol at point." "Invoke `swiper' with SEARCH, otherwise with the symbol at point."
(interactive "<a>") (interactive "<a>")
(swiper search)) (swiper search))
;;;###autoload (autoload '+ivy:todo "completion/ivy/autoload/evil" nil t)
(evil-define-command +ivy:todo (&optional bang)
"An ex wrapper around `+ivy/tasks'."
(interactive "<!>")
(+ivy/tasks bang))

View file

@ -76,12 +76,62 @@ limit to buffers in the current workspace."
(string-match-p "\\`[\n[:blank:]]+\\'" it))) (string-match-p "\\`[\n[:blank:]]+\\'" it)))
(cl-remove-duplicates kill-ring :test 'equal)))) (cl-remove-duplicates kill-ring :test 'equal))))
(defun +ivy--tasks-candidates ()
"Generate a list of task tags (specified by `+ivy-task-tags') for
`+ivy/tasks'."
(let ((default-directory (if arg default-directory (doom-project-root)))
case-fold-search)
(mapcar (lambda (x)
(save-match-data
(string-match (concat "^\\([^:]+\\):\\([0-9]+\\):.+\\("
(string-join (mapcar #'car +ivy-task-tags) "\\|")
"\\):?\\s-*\\(.+\\)")
x)
(let* (case-fold-search
(file (match-string 1 x))
(line (match-string 2 x))
(type (match-string 3 x))
(desc (match-string 4 x)))
(format "%-5s %-90s %s:%s"
(propertize type 'face (cdr (assoc type +ivy-task-tags)))
(substring desc 0 (min 90 (length desc)))
(propertize file 'face 'font-lock-keyword-face)
(propertize line 'face 'font-lock-constant-face)))))
(split-string (shell-command-to-string
(format "rg -H -S --no-heading --line-number %s %s"
(concat " -- "
(shell-quote-argument (concat "\\s("
(string-join (mapcar #'car +ivy-task-tags) "|")
")([\\s:]|\([^)]+\):?)")))
(if arg buffer-file-name ".")))
"\n" t))))
(defun +ivy--tasks-open-action (x)
"Jump to the file and line of the current task."
(let* ((spec (split-string (substring x 97) ":"))
(type (car (split-string x " ")))
(file (car spec))
(line (string-to-number (cadr spec))))
(with-ivy-window
(find-file (expand-file-name file (doom-project-root)))
(goto-char (point-min))
(forward-line (1- line))
(search-forward type (line-end-position) t)
(backward-char (length type))
(recenter))))
;;;###autoload ;;;###autoload
(defun +ivy/tasks () (defun +ivy/tasks (&optional arg)
"Search through all TODO/FIXME tags in the current project using "Search through all TODO/FIXME tags in the current project. If ARG, only
`counsel-rg'." search current file. See `+ivy-task-tags' to customize what this searches for."
(interactive) (interactive "P")
(counsel-rg "\\(TODO|FIXME\\)\\s" (doom-project-root) "--case-sensitive -w")) (ivy-read (format "Tasks (%s): "
(if arg
(concat "in: " (file-relative-name buffer-file-name))
"project"))
(+ivy--tasks-candidates)
:action #'+ivy--tasks-open-action
:caller '+ivy/tasks))
;;;###autoload ;;;###autoload
(defun +ivy*counsel-ag-function (string base-cmd extra-ag-args) (defun +ivy*counsel-ag-function (string base-cmd extra-ag-args)

View file

@ -1,5 +1,10 @@
;;; completion/ivy/packages.el ;;; completion/ivy/packages.el
(defvar +ivy-task-tags '(("TODO" . warning)
("FIXME" . error))
"An alist of tags for `+ivy/tasks' to include in its search, whose CDR is the
face to render it with.")
(defmacro ivy-do-action! (action) (defmacro ivy-do-action! (action)
"A factory function that returns an interactive lamba that sets the current "A factory function that returns an interactive lamba that sets the current
ivy action and immediately runs it on the current candidate (ending the ivy ivy action and immediately runs it on the current candidate (ending the ivy

View file

@ -61,7 +61,7 @@
(ex! "build" '+eval/build) (ex! "build" '+eval/build)
(ex! "debug" '+debug/run) (ex! "debug" '+debug/run)
(ex! "er[rors]" 'flycheck-list-errors) (ex! "er[rors]" 'flycheck-list-errors)
(ex! "todo" '+ivy/tasks) (ex! "todo" '+ivy:todo)
;; File operations ;; File operations
(ex! "mv" '+evil:file-move) (ex! "mv" '+evil:file-move)