Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make consult-flycheck a separate package with correct dependencies #53

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ completions with richer information (e.g. `M-x`, `describe-face`,
* `consult-outline`: Jump to a heading of the outline. Supports live preview
and recursive editing.
* `consult-flycheck`: Jump to flycheck error. Supports live preview
and recursive editing.
and recursive editing. Flycheck users should install the separate
`consult-flycheck` package to obtain this functionality.
* `consult-imenu`: Jump to imenu item. Supports live preview
and recursive editing.

Expand Down
91 changes: 91 additions & 0 deletions consult-flycheck.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
;;; consult-flycheck.el --- Jump to flycheck errors with consult -*- lexical-binding: t; -*-

;; Author: Daniel Mendler, Consult and Selectrum contributors
;; Maintainer: Daniel Mendler
;; Created: 2020
;; License: GPL-3.0-or-later
;; Version: 0.1
;; Package-Requires: ((consult "0.1") (flycheck "31") (emacs "26.1"))
;; Homepage: https://github.com/minad/consult

;; This file is not part of GNU Emacs.

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Provides the command `consult-flycheck'.

;;; Code:

(require 'consult)
(require 'flycheck)

;;;; Customization

(defcustom consult-flycheck-preview t
"Enable flycheck preview during selection."
:type 'boolean
:group 'consult)

(defun consult-flycheck--candidates ()
"Return flycheck errors as alist."
(consult--forbid-minibuffer)
(unless (bound-and-true-p flycheck-current-errors)
(user-error "No flycheck errors (Status: %s)"
(if (boundp 'flycheck-last-status-change)
flycheck-last-status-change
'not-installed)))
(let* ((errors (mapcar
(lambda (err)
(list (file-name-nondirectory (flycheck-error-filename err))
(number-to-string (flycheck-error-line err))
err))
(seq-sort #'flycheck-error-level-< flycheck-current-errors)))
(file-width (apply #'max (mapcar (lambda (x) (length (car x))) errors)))
(line-width (apply #'max (mapcar (lambda (x) (length (cadr x))) errors)))
(fmt (format "%%-%ds %%-%ds %%-7s %%s (%%s)" file-width line-width)))
(mapcar
(pcase-lambda (`(,file ,line ,err))
(flycheck-jump-to-error err)
(cons
(format fmt
(propertize file 'face 'flycheck-error-list-filename)
(propertize line 'face 'flycheck-error-list-line-number)
(let ((level (flycheck-error-level err)))
(propertize (symbol-name level) 'face (flycheck-error-level-error-list-face level)))
(propertize (flycheck-error-message err)
'face 'flycheck-error-list-error-message)
(propertize (symbol-name (flycheck-error-checker err))
'face 'flycheck-error-list-checker-name))
(point-marker)))
errors)))

;;;###autoload
(defun consult-flycheck ()
"Jump to flycheck error."
(interactive)
(consult--goto
(save-excursion
(consult--read "Flycheck error: "
(consult--with-increased-gc (consult-flycheck--candidates))
:category 'flycheck-error
:require-match t
:sort nil
:lookup #'consult--lookup-list
:preview (and consult-flycheck-preview #'consult--preview-position)))))


(provide 'consult-flycheck)
;;; consult-flycheck.el ends here
63 changes: 0 additions & 63 deletions consult.el
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,6 @@ The histories can be rings or lists."
:type 'boolean
:group 'consult)

(defcustom consult-preview-flycheck t
"Enable flycheck preview during selection."
:type 'boolean
:group 'consult)

(defcustom consult-preview-imenu t
"Enable imenu item preview during selection."
:type 'boolean
Expand Down Expand Up @@ -237,17 +232,6 @@ nil shows all `custom-available-themes'."
(declare-function selectrum-read "selectrum")
(declare-function selectrum-get-current-candidate "selectrum")

(defvar flycheck-current-errors)
(declare-function flycheck-error-buffer "flycheck")
(declare-function flycheck-error-checker "flycheck")
(declare-function flycheck-error-filename "flycheck")
(declare-function flycheck-error-level "flycheck")
(declare-function flycheck-error-level-< "flycheck")
(declare-function flycheck-error-level-error-list-face "flycheck")
(declare-function flycheck-error-line "flycheck")
(declare-function flycheck-error-message "flycheck")
(declare-function flycheck-jump-to-error "flycheck")

;;;; Helper functions

(defun consult--lookup-list (alist key)
Expand Down Expand Up @@ -503,53 +487,6 @@ See `multi-occur' for the meaning of the arguments BUFS, REGEXP and NLINES."
:history 'consult-outline-history
:preview (and consult-preview-outline #'consult--preview-position)))))

(defun consult--flycheck-candidates ()
"Return flycheck errors as alist."
(consult--forbid-minibuffer)
(unless (bound-and-true-p flycheck-current-errors)
(user-error "No flycheck errors (Status: %s)"
(if (boundp 'flycheck-last-status-change)
flycheck-last-status-change
'not-installed)))
(let* ((errors (mapcar
(lambda (err)
(list (file-name-nondirectory (flycheck-error-filename err))
(number-to-string (flycheck-error-line err))
err))
(seq-sort #'flycheck-error-level-< flycheck-current-errors)))
(file-width (apply #'max (mapcar (lambda (x) (length (car x))) errors)))
(line-width (apply #'max (mapcar (lambda (x) (length (cadr x))) errors)))
(fmt (format "%%-%ds %%-%ds %%-7s %%s (%%s)" file-width line-width)))
(mapcar
(pcase-lambda (`(,file ,line ,err))
(flycheck-jump-to-error err)
(cons
(format fmt
(propertize file 'face 'flycheck-error-list-filename)
(propertize line 'face 'flycheck-error-list-line-number)
(let ((level (flycheck-error-level err)))
(propertize (symbol-name level) 'face (flycheck-error-level-error-list-face level)))
(propertize (flycheck-error-message err)
'face 'flycheck-error-list-error-message)
(propertize (symbol-name (flycheck-error-checker err))
'face 'flycheck-error-list-checker-name))
(point-marker)))
errors)))

;;;###autoload
(defun consult-flycheck ()
"Jump to flycheck error."
(interactive)
(consult--goto
(save-excursion
(consult--read "Flycheck error: "
(consult--with-increased-gc (consult--flycheck-candidates))
:category 'flycheck-error
:require-match t
:sort nil
:lookup #'consult--lookup-list
:preview (and consult-preview-flycheck #'consult--preview-position)))))

(defun consult--mark-candidates ()
"Return alist of lines containing markers.
The alist contains (string . position) pairs."
Expand Down