Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions zig-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@
:safe #'stringp
:group 'zig-mode)

(defcustom zig-flymake t
"Enable zig flymake integration."
:type 'boolean
:safe #'booleanp
:group 'zig-mode)
Copy link
Contributor

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 these defcustoms because it defaults to the group created by the defgroup above.


(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)
Expand Down Expand Up @@ -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*")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth checking here whether this honours buffer-local exec-path (ie. $PATH) settings in the initial source buffer, which is necessary when using direnv and envrc.el to put a specific zig on the path.

: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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down