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

feat: Add option to get diagnostics for the whole line #8

Merged
merged 2 commits into from
Mar 9, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
* Add face text property according to error level (793f7dbf5f9bb1899d741394a48605f1aa4a5ade)
* Add option to display checker name (1f2f82d4383718a8dd2aff40cffafce4a8d0aca1)
* feat: Add custom variable for max lines (#3)
* feat: Add option to get diagnostics for the whole line (#5)

## 0.1.0
> Released Jun 14, 2022
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

#### 🧪 Variables

- `sideline-flycheck-inhibit-functions` - Functions to inhibit display of sideline flycheck.
- `sideline-flycheck-display-mode` - Method type to when sideline will display flycheck's errors.
- `sideline-flycheck-show-checker-name` - If non-nil, show the checker's name at the back.
- `sideline-flycheck-max-lines` - Maximum number of lines to show.

Expand Down
48 changes: 33 additions & 15 deletions sideline-flycheck.el
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@
:group 'tool
:link '(url-link :tag "Repository" "https://github.com/emacs-sideline/sideline-flycheck"))

(defcustom sideline-flycheck-inhibit-functions nil
"Functions to inhibit display of sideline flycheck."
:type 'hook
(defcustom sideline-flycheck-display-mode 'point
"Method type to when sideline will display flycheck's errors."
:type '(choice (const line)
(const point))
:group 'sideline-flycheck)

(defcustom sideline-flycheck-show-checker-name nil
Expand All @@ -66,12 +67,12 @@
:type 'integer
:group 'sideline-flycheck)

(defvar-local sideline-flycheck--old-display-function nil
"The former value of `flycheck-display-errors-function'.")

(defvar-local sideline-flycheck--callback nil
"Callback to display errors with sideline.")

(defvar-local sideline-flycheck--timer nil
"Timer to display diagnostics.")

(defvar-local sideline-flycheck--errors (ht-create)
"Store error messages as key.")

Expand All @@ -85,11 +86,21 @@ Argument COMMAND is required in sideline backend."
(lambda (callback &rest _)
(setq sideline-flycheck--callback callback))))))

(defun sideline-flycheck--show (errors)
"Display ERRORS, using sideline.el library."
(when (and errors
(not (run-hook-with-args-until-success 'sideline-flycheck-inhibit-functions))
sideline-flycheck--callback)
(defun sideline-flycheck--get-errors ()
"Return flycheck errors."
(cl-case sideline-flycheck-display-mode
(`point (flycheck-overlay-errors-at (point)))
(`line (flycheck-overlay-errors-in (line-beginning-position) (1+ (line-end-position))))
(t (user-error "Invalid value of `sideline-flycheck-display-mode': %s"
sideline-flycheck-display-mode))))

(defun sideline-flycheck--show (&optional buffer)
"Display ERRORS in BUFFER, using sideline library."
(when-let ((sideline-mode)
(buffer (or buffer (current-buffer)))
((eq buffer (current-buffer)))
(errors (sideline-flycheck--get-errors))
(sideline-flycheck--callback))
(let (msgs)
(dolist (err errors)
(let* ((level (flycheck-error-level err))
Expand All @@ -107,6 +118,13 @@ Argument COMMAND is required in sideline backend."
(push msg msgs))))
(funcall sideline-flycheck--callback msgs))))

(defun sideline-flycheck--post-command ()
"Display error message at point with a delay, unless already displayed."
(when (timerp sideline-flycheck--timer) (cancel-timer sideline-flycheck--timer))
(setq sideline-flycheck--timer
(run-at-time flycheck-display-errors-delay nil
#'sideline-flycheck--show (current-buffer))))

(defun sideline-flycheck--reset ()
"After sideline is reset."
(ht-clear sideline-flycheck--errors))
Expand All @@ -116,12 +134,12 @@ Argument COMMAND is required in sideline backend."
"Setup for `flycheck-mode'."
(cond
(flycheck-mode
(setq sideline-flycheck--old-display-function flycheck-display-errors-function)
(setq-local flycheck-display-errors-function #'sideline-flycheck--show)
(add-hook 'flycheck-after-syntax-check-hook #'sideline-flycheck--show nil t)
(add-hook 'post-command-hook #'sideline-flycheck--post-command nil t)
(add-hook 'sideline-reset-hook #'sideline-flycheck--reset nil t))
(t
(setq-local flycheck-display-errors-function sideline-flycheck--old-display-function)
(setq sideline-flycheck--old-display-function nil)
(remove-hook 'flycheck-after-syntax-check-hook #'sideline-flycheck--show t)
(remove-hook 'post-command-hook #'sideline-flycheck--post-command t)
(remove-hook 'sideline-reset-hook #'sideline-flycheck--reset t)
(sideline-render)))) ; update sideline once

Expand Down
Loading