-
Notifications
You must be signed in to change notification settings - Fork 57
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
Add flymake integration #93
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,17 @@ | |
:safe #'stringp | ||
:group 'zig-mode) | ||
|
||
(defcustom zig-flymake t | ||
"Enable zig flymake integration." | ||
:type 'boolean | ||
:safe #'booleanp | ||
:group 'zig-mode) | ||
|
||
(defcustom zig-flymake-command '("zig" "build" "test") | ||
"Command to execute when calling flymake-start." | ||
:type (repeat 'string) | ||
:group 'zig-mode) | ||
|
||
;; zig CLI commands | ||
|
||
(defun zig--run-cmd (cmd &optional source &rest args) | ||
|
@@ -117,6 +128,66 @@ If given a SOURCE, execute the CMD on it." | |
(interactive) | ||
(zig--run-cmd "run" (file-local-name (buffer-file-name)) "-O" zig-run-optimization-mode)) | ||
|
||
;; zig flymake | ||
|
||
(defvar zig--flymake-proc nil | ||
"Current zig flymake process.") | ||
(defun zig-flymake (report-fn &rest _args) | ||
"Zig flymake command, called by flymake-start." | ||
(unless (executable-find "zig") | ||
(error "Cannot find suitable zig executable")) | ||
;; Kill the last zig--flymake-proc, as flymake can spawn them indefinitely. | ||
(when (process-live-p zig--flymake-proc) | ||
(kill-process zig--flymake-proc)) | ||
|
||
(save-restriction | ||
(widen) | ||
(setq | ||
zig--flymake-proc | ||
(make-process | ||
:name "zig-flymake" :noquery t :connection-type 'pipe | ||
:buffer (generate-new-buffer "*zig-flymake*") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth checking here whether this honours buffer-local |
||
:command zig-flymake-command | ||
:sentinel | ||
(lambda (proc _event) | ||
(when (memq (process-status proc) '(exit signal)) | ||
(unwind-protect | ||
(if (eq proc zig--flymake-proc) | ||
(with-current-buffer (process-buffer proc) | ||
(goto-char (point-min)) | ||
(let ((diags '())) | ||
(while (search-forward-regexp | ||
"^\\(.*.zig\\):\\([0-9]+\\):\\([0-9]+\\): \\(.*\\)$" nil t) | ||
(let* ((msg (match-string 4)) | ||
(file (match-string 1)) | ||
(beg-line (string-to-number (match-string 2))) | ||
(beg-col (string-to-number (match-string 3))) | ||
;; Zig does not warn, only errors or notes. | ||
(type (if (string-match "^error" msg) | ||
:error | ||
:note)) | ||
(diag (flymake-make-diagnostic | ||
file | ||
(cons beg-line beg-col) | ||
;; Errors will have properly recalculated ends | ||
;; as when flymake-make-diagnostic gots file as | ||
;; locus, it automatically calls flymake-diag-region | ||
;; when trying to convert line and col into beg end. | ||
nil | ||
type | ||
msg))) | ||
(setq diags (cons diag diags)))) | ||
(funcall report-fn diags))) | ||
(flymake-log :warning "Canceling obsolete check %s" | ||
proc)) | ||
(kill-buffer (process-buffer proc))))))))) | ||
|
||
(defun zig--setup-flymake-backend () | ||
(add-hook 'flymake-diagnostic-functions 'zig-flymake nil t)) | ||
|
||
(if zig-flymake | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doing this once at the top level is not going to work out well because changing zig-flymake after loading the file will make no difference. Better to add the zig--setup-flymake-backend to the default value of zig-mode-hook (which might mean adding an explicit defcustom for zig-mode-hook, or simply omit it and suggest that users call add-hook themselves. If doing that, then you'd rename the function to zig-setup-flymake-backend because it's now effectively a public function. |
||
(add-hook 'zig-mode-hook 'zig--setup-flymake-backend)) | ||
|
||
;; zig fmt | ||
|
||
(reformatter-define zig-format | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:group
is redundant in all thesedefcustom
s because it defaults to the group created by thedefgroup
above.