Skip to content

Commit

Permalink
Enable the checker in .merlin presence
Browse files Browse the repository at this point in the history
Problem: some projects don't use Dune, while using Merlin, configured
   with the .merlin file. `flycheck-ocaml` is only turned on if the
   «dune-project» file is present though. As a result,
   `flycheck-ocaml` is disabled for such projects.

Solution: enable the `flycheck-ocaml` checker if either dune-project
   or .merlin are present.

In a round-about way this fixes flycheck#15. People who don't use Dune, don't
   need to create an empty dune-project to use the checker, which was
   misleading.
  • Loading branch information
temyurchenko committed Jan 6, 2024
1 parent 77f8ddb commit 3f2e52d
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions flycheck-ocaml.el
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,28 @@ Return the corresponding `flycheck-error'."
(or level 'error) (or message .message)
:checker checker)))))

(defun flycheck-ocaml-find-merlin-config-dir ()
"Find the directory containing Merlin configuration.
The configuration is currently considered to be either .merlin or
dune-project."
(and buffer-file-name
(or (locate-dominating-file buffer-file-name "dune-project")
(locate-dominating-file buffer-file-name ".merlin"))))

(defun flycheck-verify-ocaml-merlin (_checker)
"Verify the OCaml Merlin syntax checker."
(let ((command (executable-find (merlin-command)))
(dune-file (and buffer-file-name (locate-dominating-file buffer-file-name "dune-project"))))
(config-dir (flycheck-ocaml-find-merlin-config-dir)))
(list
(flycheck-verification-result-new
:label "Merlin command"
:message (if command (format "Found at %s" command) "Not found")
:face (if command 'success '(bold error)))
(flycheck-verification-result-new
:label "Dune project"
:message (if dune-file (format "Found at %s" dune-file) "Not found")
:face (if dune-file 'success '(bold error)))
:label "Dune project or .merlin"
:message (if config-dir (format "Found at %s" config-dir) "Not found")
:face (if config-dir 'success '(bold error)))
(flycheck-verification-result-new
:label "Merlin mode"
:message (if merlin-mode "enabled" "disabled")
Expand Down Expand Up @@ -149,9 +158,9 @@ See URL `https://github.com/ocaml/merlin'."
:verify #'flycheck-verify-ocaml-merlin
:modes '(caml-mode tuareg-mode reason-mode)
:predicate (lambda () (and merlin-mode
;; Don't check if there is not a 'dune-project'
;; Don't check if Merlin configuration isn't
;; present somewhere in the directory tree
(and buffer-file-name (locate-dominating-file buffer-file-name "dune-project"))
(flycheck-ocaml-find-merlin-config-dir)
;; Don't check if Merlin's own checking is
;; enabled, to avoid duplicate overlays
(not merlin-error-after-save))))
Expand Down

0 comments on commit 3f2e52d

Please sign in to comment.