* bug#70260: [PATCH] Flymake support for rust-ts-mode (clippy-driver)
@ 2024-04-07 16:42 Dmitry Gutov
2024-04-08 0:26 ` Dmitry Gutov
0 siblings, 1 reply; 4+ messages in thread
From: Dmitry Gutov @ 2024-04-07 16:42 UTC (permalink / raw)
To: 70260; +Cc: randy taylor
[-- 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"
^ permalink raw reply related [flat|nested] 4+ messages in thread
* bug#70260: [PATCH] Flymake support for rust-ts-mode (clippy-driver)
2024-04-07 16:42 bug#70260: [PATCH] Flymake support for rust-ts-mode (clippy-driver) Dmitry Gutov
@ 2024-04-08 0:26 ` Dmitry Gutov
2024-04-09 2:42 ` Randy Taylor
0 siblings, 1 reply; 4+ messages in thread
From: Dmitry Gutov @ 2024-04-08 0:26 UTC (permalink / raw)
To: 70260; +Cc: randy taylor
[-- Attachment #1: Type: text/plain, Size: 278 bytes --]
Minor fix and a TODO comment for the "cargo clippy" case.
On 07/04/2024 19:42, Dmitry Gutov wrote:
> 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-v2.diff --]
[-- Type: text/x-patch, Size: 4159 bytes --]
diff --git a/lisp/progmodes/rust-ts-mode.el b/lisp/progmodes/rust-ts-mode.el
index c67ac43e4d0..c16e8a6c4c4 100644
--- a/lisp/progmodes/rust-ts-mode.el
+++ b/lisp/progmodes/rust-ts-mode.el
@@ -48,6 +48,20 @@ 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" "-"))
+ ;; TODO: Maybe add diagnostics filtering by file name,
+ ;; to limit non-project list to the current buffer.
+ ;; Or annotate them with file names, at least.
+ (const :tag "Clippy cargo" ("cargo" "clippy"))
+ (repeat :tag "Custom command" string))
+ :group 'rust)
+
(defvar rust-ts-mode-prettify-symbols-alist
'(("&&" . ?∧) ("||" . ?∨)
("<=" . ?≤) (">=" . ?≥) ("!=" . ?≠)
@@ -417,6 +431,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\\|help\\).*\\)\n +--> [^:]+:"
+ "\\([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 +539,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"
^ permalink raw reply related [flat|nested] 4+ messages in thread
* bug#70260: [PATCH] Flymake support for rust-ts-mode (clippy-driver)
2024-04-08 0:26 ` Dmitry Gutov
@ 2024-04-09 2:42 ` Randy Taylor
2024-04-09 19:41 ` Dmitry Gutov
0 siblings, 1 reply; 4+ messages in thread
From: Randy Taylor @ 2024-04-09 2:42 UTC (permalink / raw)
To: Dmitry Gutov; +Cc: 70260
On Sunday, April 7th, 2024 at 20:26, Dmitry Gutov <dmitry@gutov.dev> wrote:
>
>
> Minor fix and a TODO comment for the "cargo clippy" case.
>
> On 07/04/2024 19:42, Dmitry Gutov wrote:
>
> > 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.
Thanks for working on this.
I'm not really familiar with flymake integration, but this
looks good to me. I have a few nits below.
+(defcustom rust-ts-flymake-command '("clippy-driver" "-")
+ "The external tool that will be used to perform the syntax check.
^^^^^^^^^^^^^^^^
maybe just "the check"?
Or something similar, since more is being done than just syntax checking.
+This is a non empty list of strings, the checker tool possibly followed
^^^^^^^^^ ^
non-empty? A colon would work well here.
+(defvar rust-ts--flymake-proc nil)
Should this be defvar-local?
+ ;; Flymake
^
A period at the end would make me happy :).
^ permalink raw reply [flat|nested] 4+ messages in thread
* bug#70260: [PATCH] Flymake support for rust-ts-mode (clippy-driver)
2024-04-09 2:42 ` Randy Taylor
@ 2024-04-09 19:41 ` Dmitry Gutov
0 siblings, 0 replies; 4+ messages in thread
From: Dmitry Gutov @ 2024-04-09 19:41 UTC (permalink / raw)
To: Randy Taylor; +Cc: 70260-done
Version: 30.1
On 09/04/2024 05:42, Randy Taylor wrote:
> On Sunday, April 7th, 2024 at 20:26, Dmitry Gutov <dmitry@gutov.dev> wrote:
>>
>>
>> Minor fix and a TODO comment for the "cargo clippy" case.
>>
>> On 07/04/2024 19:42, Dmitry Gutov wrote:
>>
>>> 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.
>
> Thanks for working on this.
Thanks for taking a look.
I imagine it won't be used for larger projects, edited assisted by LSP -
LSP clients have their own Flymake integration. But I found it useful a
few months ago when doing some leetcode exercises.
> I'm not really familiar with flymake integration, but this
> looks good to me. I have a few nits below.
>
> +(defcustom rust-ts-flymake-command '("clippy-driver" "-")
> + "The external tool that will be used to perform the syntax check.
> ^^^^^^^^^^^^^^^^
> maybe just "the check"?
> Or something similar, since more is being done than just syntax checking.
>
> +This is a non empty list of strings, the checker tool possibly followed
> ^^^^^^^^^ ^
> non-empty? A colon would work well here.
Thank you. This was actually copied from some exiting flymake backends,
but they could use such edits as well.
> +(defvar rust-ts--flymake-proc nil)
> Should this be defvar-local?
Not necessarily - there would usually be only one simultaneous check
running, and the pointer to the associated buffer is stored in the closure.
> + ;; Flymake
> ^
> A period at the end would make me happy :).
Sure.
Pushed to master as ccced8c3e43, closing.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-04-09 19:41 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-07 16:42 bug#70260: [PATCH] Flymake support for rust-ts-mode (clippy-driver) Dmitry Gutov
2024-04-08 0:26 ` Dmitry Gutov
2024-04-09 2:42 ` Randy Taylor
2024-04-09 19:41 ` Dmitry Gutov
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.