unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Dmitry Gutov <dmitry@gutov.dev>
To: 70260@debbugs.gnu.org
Cc: randy taylor <dev@rjt.dev>
Subject: bug#70260: [PATCH] Flymake support for rust-ts-mode (clippy-driver)
Date: Sun, 7 Apr 2024 19:42:44 +0300	[thread overview]
Message-ID: <ce5538d7-2379-422b-b530-919668856c91@gutov.dev> (raw)

[-- Attachment #1: Type: text/plain, Size: 161 bytes --]

X-Debbugs-Cc: Randy Taylor <dev@rjt.dev>

Hi Randy and others,

Here's a patch adding rust-ts-mode <-> flymake-mode integration using 
clippy.

Feedback welcome.

[-- Attachment #2: rust-ts-flymake.diff --]
[-- Type: text/x-patch, Size: 3945 bytes --]

diff --git a/lisp/progmodes/rust-ts-mode.el b/lisp/progmodes/rust-ts-mode.el
index c5fc57cc374..6971bcba75a 100644
--- a/lisp/progmodes/rust-ts-mode.el
+++ b/lisp/progmodes/rust-ts-mode.el
@@ -48,6 +48,17 @@ rust-ts-mode-indent-offset
   :safe 'integerp
   :group 'rust)
 
+(defcustom rust-ts-flymake-command '("clippy-driver" "-")
+  "The external tool that will be used to perform the syntax check.
+This is a non empty list of strings, the checker tool possibly followed
+by required arguments.  Once launched it will receive the Rust source
+to be checked as its standard input."
+  :version "30.1"
+  :type '(choice (const :tag "Clippy standalone" ("clippy-driver" "-"))
+                 (const :tag "Clippy cargo" ("cargo" "clippy"))
+                 (repeat :tag "Custom command" string))
+  :group 'rust)
+
 (defvar rust-ts-mode-prettify-symbols-alist
   '(("&&" . ?∧) ("||" . ?∨)
     ("<=" . ?≤)  (">=" . ?≥) ("!=" . ?≠)
@@ -417,6 +428,67 @@ rust-ts-mode--prettify-symbols-compose-p
                    "operator"))
          (_ t))))
 
+(defvar rust-ts--flymake-proc nil)
+
+(defun rust-ts-flymake--helper (process-name command parser-fn)
+  (when (process-live-p rust-ts--flymake-proc)
+    (kill-process rust-ts--flymake-proc))
+
+  (let ((source (current-buffer)))
+    (save-restriction
+      (widen)
+      (setq
+       rust-ts--flymake-proc
+       (make-process
+        :name process-name :noquery t :connection-type 'pipe
+        :buffer (generate-new-buffer (format " *%s*" process-name))
+        :command command
+        :sentinel
+        (lambda (proc _event)
+          (when (and (eq 'exit (process-status proc)) (buffer-live-p source))
+            (unwind-protect
+                (if (with-current-buffer source (eq proc rust-ts--flymake-proc))
+                    (with-current-buffer (process-buffer proc)
+                      (funcall parser-fn proc source))
+                  (flymake-log :debug "Canceling obsolete check %s"
+                               proc))
+              (kill-buffer (process-buffer proc)))))))
+      (process-send-region rust-ts--flymake-proc (point-min) (point-max))
+      (process-send-eof rust-ts--flymake-proc))))
+
+(defun rust-ts-flymake (report-fn &rest _args)
+  "Rust backend for Flymake."
+  (unless (executable-find (car rust-ts-flymake-command))
+    (error "Cannot find the rust flymake program: %s" (car rust-ts-flymake-command)))
+
+  (rust-ts-flymake--helper
+   "rust-ts-flymake"
+   rust-ts-flymake-command
+   (lambda (_proc source)
+     (goto-char (point-min))
+     (cl-loop
+      while (search-forward-regexp
+             (concat
+              "^\\(\\(?:warning\\|error\\).*\\)\n  --> <anon>:"
+              "\\([0-9]+\\):\\([0-9]+\\)\\(\\(?:\n[^\n]+\\)*\\)\n\n")
+             nil t)
+      for msg1 = (match-string 1)
+      for msg2 = (match-string 4)
+      for (beg . end) = (flymake-diag-region
+                         source
+                         (string-to-number (match-string 2))
+                         (string-to-number (match-string 3)))
+      for type = (if (string-match "^warning" msg1)
+                     :warning
+                   :error)
+      collect (flymake-make-diagnostic source
+                                       beg
+                                       end
+                                       type
+                                       (concat msg1 msg2))
+      into diags
+      finally (funcall report-fn diags)))))
+
 ;;;###autoload
 (define-derived-mode rust-ts-mode prog-mode "Rust"
   "Major mode for editing Rust, powered by tree-sitter."
@@ -464,6 +536,9 @@ rust-ts-mode
     (setq-local electric-indent-chars
                 (append "{}():;,#" electric-indent-chars))
 
+    ;; Flymake
+    (add-hook 'flymake-diagnostic-functions #'rust-ts-flymake nil 'local)
+
     ;; Navigation.
     (setq-local treesit-defun-type-regexp
                 (regexp-opt '("enum_item"

             reply	other threads:[~2024-04-07 16:42 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-07 16:42 Dmitry Gutov [this message]
2024-04-08  0:26 ` bug#70260: [PATCH] Flymake support for rust-ts-mode (clippy-driver) Dmitry Gutov
2024-04-09  2:42   ` Randy Taylor
2024-04-09 19:41     ` Dmitry Gutov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ce5538d7-2379-422b-b530-919668856c91@gutov.dev \
    --to=dmitry@gutov.dev \
    --cc=70260@debbugs.gnu.org \
    --cc=dev@rjt.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).