|
31 | 31 | ;; |
32 | 32 | ;; (add-hook 'php-mode-hook #'flymake-phpstan-turn-on) |
33 | 33 | ;; |
| 34 | +;; Like `flycheck-phpstan', this backend reads PHPStan's JSON output, so the |
| 35 | +;; identifier and tip of each message are shown, and `phpstan-insert-ignore' |
| 36 | +;; and `phpstan-copy-dumped-type' work from a Flymake session too. |
| 37 | +;; |
34 | 38 | ;; For Lisp maintainers: see [GNU Flymake manual - 2.2.2 An annotated example backend] |
35 | 39 | ;; https://www.gnu.org/software/emacs/manual/html_node/flymake/An-annotated-example-backend.html |
36 | 40 |
|
|
53 | 57 | :type 'boolean |
54 | 58 | :group 'flymake-phpstan) |
55 | 59 |
|
| 60 | +(defcustom flymake-phpstan-ignore-metadata-list nil |
| 61 | + "Set of metadata items to ignore in PHPStan messages for Flymake." |
| 62 | + :type '(set (const identifier) |
| 63 | + (const tip)) |
| 64 | + :group 'flymake-phpstan) |
| 65 | + |
| 66 | +(defcustom flymake-phpstan-metadata-separator "\n" |
| 67 | + "Separator of PHPStan message and metadata." |
| 68 | + :type 'string |
| 69 | + :safe #'stringp |
| 70 | + :group 'flymake-phpstan) |
| 71 | + |
| 72 | +(defconst flymake-phpstan--nofiles-message |
| 73 | + (eval-when-compile (regexp-quote "[ERROR] No files found to analyse."))) |
| 74 | + |
56 | 75 | (defvar-local flymake-phpstan--proc nil) |
57 | 76 |
|
| 77 | +(defun flymake-phpstan--build-message (message) |
| 78 | + "Build the diagnostic text for a PHPStan MESSAGE plist. |
| 79 | +
|
| 80 | +Append the identifier and tip, unless disabled by |
| 81 | +`flymake-phpstan-ignore-metadata-list', mirroring `flycheck-phpstan'." |
| 82 | + (let* ((msg (plist-get message :message)) |
| 83 | + (ignorable (plist-get message :ignorable)) |
| 84 | + (identifier (unless (memq 'identifier flymake-phpstan-ignore-metadata-list) |
| 85 | + (plist-get message :identifier))) |
| 86 | + (tip (unless (memq 'tip flymake-phpstan-ignore-metadata-list) |
| 87 | + (plist-get message :tip))) |
| 88 | + (lines (delq nil |
| 89 | + (list (when (and identifier ignorable) |
| 90 | + (concat phpstan-identifier-prefix identifier)) |
| 91 | + (when tip |
| 92 | + (concat phpstan-tip-message-prefix tip)))))) |
| 93 | + (if lines |
| 94 | + (concat msg flymake-phpstan-metadata-separator (string-join lines "\n")) |
| 95 | + msg))) |
| 96 | + |
| 97 | +(defun flymake-phpstan--build-diagnostics (errors source) |
| 98 | + "Build Flymake diagnostics for SOURCE from PHPStan ERRORS. |
| 99 | +
|
| 100 | +ERRORS is the alist produced by `phpstan--plist-to-alist' from the JSON |
| 101 | +`:files' object. Every message is attributed to SOURCE by its line, since |
| 102 | +editor mode analyzes the one file being edited." |
| 103 | + (cl-loop for (_file . entry) in errors |
| 104 | + append (cl-loop for message in (plist-get entry :messages) |
| 105 | + for text = (flymake-phpstan--build-message message) |
| 106 | + for (beg . end) = (flymake-diag-region |
| 107 | + source (plist-get message :line)) |
| 108 | + collect (flymake-make-diagnostic source beg end :error text)))) |
| 109 | + |
| 110 | +(defun flymake-phpstan--parse (output source) |
| 111 | + "Parse PHPStan OUTPUT and return Flymake diagnostics for SOURCE. |
| 112 | +
|
| 113 | +As a side effect, refresh `phpstan--ignorable-errors' and |
| 114 | +`phpstan--dumped-types' in SOURCE, so `phpstan-insert-ignore' and |
| 115 | +`phpstan-copy-dumped-type' work from Flymake too." |
| 116 | + ;; Look for a line starting with `{', the condition `phpstan--parse-json' |
| 117 | + ;; acts on: it skips anything before that line, so progress a container |
| 118 | + ;; runtime writes to STDERR (merged into STDOUT here) is ignored. |
| 119 | + (if (not (string-match-p "^{" output)) |
| 120 | + ;; No report. A modified buffer with nothing to analyse is expected and |
| 121 | + ;; stays silent; anything else is surfaced as a warning. |
| 122 | + (if (string-match-p flymake-phpstan--nofiles-message output) |
| 123 | + nil |
| 124 | + (list (flymake-make-diagnostic source (point-min) (point-max) |
| 125 | + :warning (string-trim output)))) |
| 126 | + (with-temp-buffer |
| 127 | + (insert output) |
| 128 | + (let ((errors (phpstan--plist-to-alist |
| 129 | + (plist-get (phpstan--parse-json (current-buffer)) :files)))) |
| 130 | + (with-current-buffer source |
| 131 | + (unless phpstan-disable-buffer-errors |
| 132 | + (phpstan-update-ignorebale-errors-from-json-buffer errors)) |
| 133 | + (phpstan-update-dumped-types errors)) |
| 134 | + (flymake-phpstan--build-diagnostics errors source))))) |
| 135 | + |
58 | 136 | (defun flymake-phpstan-make-process (root command-args report-fn source) |
59 | 137 | "Make PHPStan process by ROOT, COMMAND-ARGS, REPORT-FN and SOURCE." |
60 | 138 | (let ((default-directory root)) |
|
64 | 142 | :command command-args |
65 | 143 | :sentinel |
66 | 144 | (lambda (proc _event) |
67 | | - (pcase (process-status proc) |
68 | | - (`exit |
69 | | - (unwind-protect |
70 | | - (when (with-current-buffer source (eq proc flymake-phpstan--proc)) |
71 | | - (with-current-buffer (process-buffer proc) |
72 | | - (goto-char (point-min)) |
73 | | - (cl-loop |
74 | | - while (search-forward-regexp |
75 | | - (eval-when-compile |
76 | | - (rx line-start (1+ (not (any ":"))) ":" |
77 | | - (group-n 1 (one-or-more (not (any ":")))) ":" |
78 | | - (group-n 2 (one-or-more not-newline)) line-end)) |
79 | | - nil t) |
80 | | - for msg = (match-string 2) |
81 | | - for (beg . end) = (flymake-diag-region |
82 | | - source |
83 | | - (string-to-number (match-string 1))) |
84 | | - for type = :warning |
85 | | - collect (flymake-make-diagnostic source beg end type msg) |
86 | | - into diags |
87 | | - finally (funcall report-fn diags))) |
88 | | - (flymake-log :warning "Canceling obsolete check %s" proc)) |
89 | | - (kill-buffer (process-buffer proc)))) |
90 | | - (code (user-error "PHPStan error (exit status: %s)" code))))))) |
| 145 | + (when (eq (process-status proc) 'exit) |
| 146 | + (unwind-protect |
| 147 | + (when (with-current-buffer source (eq proc flymake-phpstan--proc)) |
| 148 | + (funcall report-fn |
| 149 | + (flymake-phpstan--parse |
| 150 | + (with-current-buffer (process-buffer proc) (buffer-string)) |
| 151 | + source))) |
| 152 | + (kill-buffer (process-buffer proc)))))))) |
91 | 153 |
|
92 | 154 | (defun flymake-phpstan-analyze-original (original) |
93 | 155 | "Return non-NIL if ORIGINAL is non-NIL and buffer is not modified." |
|
109 | 171 | (let* ((source (current-buffer)) |
110 | 172 | (args (phpstan-get-command-args |
111 | 173 | :include-executable t |
112 | | - :format "raw" |
| 174 | + :format "json" |
113 | 175 | :editor (list |
114 | 176 | :analyze-original #'flymake-phpstan-analyze-original |
115 | 177 | :original-file buffer-file-name |
|
0 commit comments