Skip to content

Prevent Flycheck from showing errors due to deleted temp files during editing #56

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

Merged
merged 1 commit into from
Apr 8, 2025
Merged
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
24 changes: 24 additions & 0 deletions flycheck-phpstan.el
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
;; Usually it is defined dynamically by flycheck
(defvar flycheck-phpstan-executable)
(defvar flycheck-phpstan--temp-buffer-name "*Flycheck PHPStan*")
(defvar flycheck-phpstan--output-filter-added nil)
(defconst flycheck-phpstan--nofiles-message (eval-when-compile (regexp-quote "[ERROR] No files found to analyse.")))

(defcustom flycheck-phpstan-ignore-metadata-list nil
"Set of metadata items to ignore in PHPStan messages for Flycheck."
Expand All @@ -57,6 +59,24 @@
:safe #'stringp
:group 'phpstan)

(defun flycheck-phpstan--suppress-no-files-error (next checker exit-status files output callback cwd)
"Suppress Flycheck errors if PHPStan reports no files in a modified buffer.

This function is intended to be used as an :around advice for
`flycheck-finish-checker-process'.

It prevents Flycheck from displaying an error when:
- CHECKER is `phpstan',
- the current buffer is modified,
- and OUTPUT contains the message `flycheck-phpstan--nofiles-message'.

NEXT, EXIT-STATUS, FILES, OUTPUT, CALLBACK, and CWD are the original arguments
passed to `flycheck-finish-checker-process'."
(unless (and (eq checker 'phpstan)
(buffer-modified-p)
(string-match-p flycheck-phpstan--nofiles-message output))
(funcall next checker exit-status files output callback cwd)))

(defun flycheck-phpstan--enabled-and-set-variable ()
"Return path to phpstan configure file, and set buffer execute in side effect."
(let ((enabled (phpstan-enabled)))
Expand All @@ -71,6 +91,10 @@
(and (stringp (car-safe phpstan-executable))
(listp (cdr-safe phpstan-executable)))
(null phpstan-executable)))
(unless flycheck-phpstan--output-filter-added
(advice-add 'flycheck-finish-checker-process
:around #'flycheck-phpstan--suppress-no-files-error)
(setq flycheck-phpstan--output-filter-added t))
(setq-local flycheck-phpstan-executable (car (phpstan-get-executable-and-args)))))))

(defun flycheck-phpstan-parse-output (output &optional _checker _buffer)
Expand Down