From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philip Kaludercic Newsgroups: gmane.emacs.bugs Subject: bug#57884: [PATCH] Flymake backend using the shellcheck program Date: Sun, 18 Sep 2022 21:18:36 +0000 Message-ID: <87sfko755f.fsf@posteo.net> References: <87a66yaqwc.fsf@gmail.com> <83bkre0w4m.fsf@gnu.org> <871qs9c3er.fsf@gmail.com> <87zgewdhhd.fsf@posteo.net> <87fsgoyi0l.fsf@gmail.com> <87sfko4zjq.fsf@posteo.net> <87edw8n71p.fsf@gmail.com> <8735co4wxx.fsf@posteo.net> <878rmgcw2e.fsf@gmail.com> <87h7148mba.fsf@posteo.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="5658"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Eli Zaretskii , 57884@debbugs.gnu.org, Stefan Kangas To: Augusto Stoffel Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Sep 18 23:19:19 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oa1h9-0001LQ-Fz for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 18 Sep 2022 23:19:19 +0200 Original-Received: from localhost ([::1]:52126 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oa1h7-0000JU-Vk for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 18 Sep 2022 17:19:18 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:49688) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oa1gt-0000Iz-3Y for bug-gnu-emacs@gnu.org; Sun, 18 Sep 2022 17:19:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:52338) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oa1gr-00046a-Rw for bug-gnu-emacs@gnu.org; Sun, 18 Sep 2022 17:19:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oa1gr-0004NR-Mw for bug-gnu-emacs@gnu.org; Sun, 18 Sep 2022 17:19:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Philip Kaludercic Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 18 Sep 2022 21:19:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 57884 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 57884-submit@debbugs.gnu.org id=B57884.166353593716815 (code B ref 57884); Sun, 18 Sep 2022 21:19:01 +0000 Original-Received: (at 57884) by debbugs.gnu.org; 18 Sep 2022 21:18:57 +0000 Original-Received: from localhost ([127.0.0.1]:51416 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oa1gk-0004N6-0c for submit@debbugs.gnu.org; Sun, 18 Sep 2022 17:18:57 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]:57577) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oa1ga-0004Mk-Kp for 57884@debbugs.gnu.org; Sun, 18 Sep 2022 17:18:51 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id 26734240108 for <57884@debbugs.gnu.org>; Sun, 18 Sep 2022 23:18:38 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1663535919; bh=haQIXtEeNnXZrGdVyARqq4ZXq4rnYj605lMDzQmYvDE=; h=From:To:Cc:Subject:Autocrypt:Date:From; b=m9I+KLU/+O+8BZdU0/34UM4NY57UePUBaeXAkxOiUb5JbfsDHiPg/VXExQSrVkxiQ hxrLiDVoFlPB/X7U8c9eZZVo24t4mCawNmXMV7DPXOfQPZmuXHt1zVgDL1gE0OnBmG blyt2T1xO1+XqZMIhuB71iX7Jccq3LbmUpwsRl4rUljPw7ygoM7o46f5phg97z+FS8 gEjTA1cF/t/JYZF9OTQoKOwUdGdNDeJ4DNPSlqbHTW9j/fN1mivTVsylYBM85k9IaK yBuuHE0/6JpFXCRU1m4BEUteFqh/LnAJAusgsVI2oDUSnuOYSGK13Twy/GvwbQaSd8 onB7jow2O96Og== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4MW0zJ62L4z9rxP; Sun, 18 Sep 2022 23:18:36 +0200 (CEST) In-Reply-To: <87h7148mba.fsf@posteo.net> (Philip Kaludercic's message of "Sun, 18 Sep 2022 22:22:33 +0200") Autocrypt: addr=philipk@posteo.net; prefer-encrypt=nopreference; keydata= mDMEYHHqUhYJKwYBBAHaRw8BAQdAp3GdmYJ6tm5McweY6dEvIYIiry+Oz9rU4MH6NHWK0Ee0QlBo aWxpcCBLYWx1ZGVyY2ljIChnZW5lcmF0ZWQgYnkgYXV0b2NyeXB0LmVsKSA8cGhpbGlwa0Bwb3N0 ZW8ubmV0PoiQBBMWCAA4FiEEDM2H44ZoPt9Ms0eHtVrAHPRh1FwFAmBx6lICGwMFCwkIBwIGFQoJ CAsCBBYCAwECHgECF4AACgkQtVrAHPRh1FyTkgEAjlbGPxFchvMbxzAES3r8QLuZgCxeAXunM9gh io0ePtUBALVhh9G6wIoZhl0gUCbQpoN/UJHI08Gm1qDob5zDxnIHuDgEYHHqUhIKKwYBBAGXVQEF AQEHQNcRB+MUimTMqoxxMMUERpOR+Q4b1KgncDZkhrO2ql1tAwEIB4h4BBgWCAAgFiEEDM2H44Zo Pt9Ms0eHtVrAHPRh1FwFAmBx6lICGwwACgkQtVrAHPRh1Fw1JwD/Qo7kvtib8jy7puyWrSv0MeTS g8qIxgoRWJE/KKdkCLEA/jb9b9/g8nnX+UcwHf/4VfKsjExlnND3FrBviXUW6NcB X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:243019 Archived-At: --=-=-= Content-Type: text/plain Philip Kaludercic writes: >> Anyway, I rewrote the backend to use the JSON output of shellcheck, >> which has the advantage that it provides the end position of each >> diagnostic, so Flymake doesn't have to guess it (which is by nature >> sometimes inaccurate). Let me know what you think. > > LGTM, but I haven't tested it yet. I just tried it out and it behaves the way you advertised it. BTW, this diff describes the changes required if you were to pull out the sentinel definition into a named function: --=-=-= Content-Type: text/plain Content-Disposition: inline diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el index 558b62b20a..d52e385b36 100644 --- a/lisp/progmodes/sh-script.el +++ b/lisp/progmodes/sh-script.el @@ -3129,50 +3129,49 @@ 'sh--json-read (require 'json) 'json-read)) +(defun sh-shellcheck-sentinel (proc _event) + (when (memq (process-status proc) '(exit signal)) + (unwind-protect + (if (with-current-buffer (process-get proc 'source) + (not (eq proc sh--shellcheck-process))) + (flymake-log :warning "Canceling obsolete check %s" proc) + (with-current-buffer (process-buffer proc) + (goto-char (point-min)) + (thread-last + (sh--json-read) + (alist-get 'comments) + (seq-filter + (lambda (item) + (let-alist item (string= .file "-")))) + (mapcar + (lambda (item) + (let-alist item + (flymake-make-diagnostic + (process-get proc 'source) + (cons .line .column) + (unless (and (eq .line .endLine) + (eq .column .endColumn)) + (cons .endLine .endColumn)) + (pcase .level + ("error" :error) + ("warning" :warning) + (_ :note)) + (format "SC%s: %s" .code .message))))) + (funcall (process-get proc 'report-fn))))) + (kill-buffer (process-buffer proc))))) + (defun sh-shellcheck-flymake (report-fn &rest _args) "Flymake backend using the shellcheck program. Takes a Flymake callback REPORT-FN as argument, as expected of a member of `flymake-diagnostic-functions'." (when (process-live-p sh--shellcheck-process) (kill-process sh--shellcheck-process)) - (let* ((source (current-buffer)) - (dialect (named-let recur ((s sh-shell)) + (let* ((dialect (named-let recur ((s sh-shell)) (pcase s ((or 'bash 'dash 'sh) (symbol-name s)) ('ksh88 "ksh") ((guard s) - (recur (alist-get s sh-ancestor-alist)))))) - (sentinel - (lambda (proc _event) - (when (memq (process-status proc) '(exit signal)) - (unwind-protect - (if (with-current-buffer source - (not (eq proc sh--shellcheck-process))) - (flymake-log :warning "Canceling obsolete check %s" proc) - (with-current-buffer (process-buffer proc) - (goto-char (point-min)) - (thread-last - (sh--json-read) - (alist-get 'comments) - (seq-filter - (lambda (item) - (let-alist item (string= .file "-")))) - (mapcar - (lambda (item) - (let-alist item - (flymake-make-diagnostic - source - (cons .line .column) - (unless (and (eq .line .endLine) - (eq .column .endColumn)) - (cons .endLine .endColumn)) - (pcase .level - ("error" :error) - ("warning" :warning) - (_ :note)) - (format "SC%s: %s" .code .message))))) - (funcall report-fn)))) - (kill-buffer (process-buffer proc))))))) + (recur (alist-get s sh-ancestor-alist))))))) (unless dialect (error "`sh-shellcheck-flymake' is not suitable for shell type `%s'" sh-shell)) @@ -3185,7 +3184,9 @@ sh-shellcheck-flymake "-s" ,dialect ,@sh-shellcheck-arguments "-") - :sentinel sentinel)) + :sentinel #'sh-shellcheck-sentinel)) + (process-put sh--shellcheck-process 'source (current-buffer)) + (process-put sh--shellcheck-process 'report-fn report-fn) (save-restriction (widen) (process-send-region sh--shellcheck-process (point-min) (point-max)) --=-=-= Content-Type: text/plain I still don't think it looks that bad, but I don't insist on it. --=-=-=--