This commit is contained in:
ry
2022-01-11 18:05:13 -08:00
parent 2046befee2
commit 8e7b654716
846 changed files with 71287 additions and 4 deletions

View File

@@ -0,0 +1,93 @@
#+TITLE: lang/ocaml
#+DATE: June 27, 2017
#+SINCE: v2.0.3
* Table of Contents :TOC:
- [[#description][Description]]
- [[#module-flags][Module Flags]]
- [[#plugins][Plugins]]
- [[#prerequisites][Prerequisites]]
- [[#features][Features]]
- [[#configuration][Configuration]]
- [[#appendix][Appendix]]
- [[#commands][Commands]]
- [[#hacks][Hacks]]
* Description
This module adds [[https://ocaml.org/][OCaml]] support to Doom Emacs, powered by [[https://github.com/ocaml/tuareg][tuareg-mode]].
+ Code completion, documentation look-up, code navigation and refactoring
([[https://github.com/ocaml/merlin/wiki/emacs-from-scratch][merlin]])
+ Type, documentation and function argument display on idle ([[https://github.com/Khady/merlin-eldoc][merlin-eldoc]])
+ REPL ([[https://github.com/ocaml-community/utop][utop]])
+ Syntax-checking (~merlin~ with [[https://github.com/flycheck/flycheck-ocaml][flycheck-ocaml]])
+ Auto-indentation ([[https://github.com/OCamlPro/ocp-indent][ocp-indent]])
+ Code formatting ([[https://github.com/ocaml-ppx/ocamlformat][ocamlformat]])
+ Dune file format ([[http://dune.build/][dune]])
** Module Flags
This module provides no flags.
** Plugins
+ [[https://github.com/ocaml/tuareg][tuareg]]
+ [[https://github.com/ocaml/merlin][merlin]]
+ [[https://github.com/Khady/merlin-eldoc][merlin-eldoc]]
+ [[https://github.com/OCamlPro/ocp-indent][ocp-indent]]
+ [[https://github.com/flycheck/flycheck-ocaml][flycheck-ocaml]]*
+ [[https://github.com/ocaml-community/utop][utop]]*
+ [[https://github.com/ocaml-ppx/ocamlformat][ocamlformat]]*
+ [[https://github.com/ocaml/dune][dune]]
* Prerequisites
This module has no hard dependencies, but it is recommanded that you install
[[http://opam.ocaml.org/][opam]] and the following opam (optional) packages:
#+BEGIN_SRC shell
opam install merlin utop ocp-indent dune ocamlformat
#+END_SRC
* Features
+ The following files should have syntax highlighting support: ~.ml{i,p,y,}~,
~.eliom{i,}~, ~jbuild~, ~dune~, ~opam~
+ =merlin-mode= is activated whenever a =.merlin= file is found (including in a
parent directory) and =ocamlmerlin= executable is present
+ Line-based auto-indentation is provided by =ocp-indent=, if it is available.
* Configuration
+ If =:completion company= is enabled then autocomplete is provided by =merlin=
+ When =:checkers syntax= is enabled then =flycheck-ocaml= is activated to do
on-the-fly syntax/type checking via =merlin=, otherwise this is only done when
the file is saved.
+ Spell checking is activated in comments if =:checkers spell= is active
+ A REPL is provided if =utop= is installed and =:tools eval= is active
+ If =:editor format= is enabled, the =ocamlformat= executable is available and
there is an =.ocamlformat= file present then =format-all-buffer= is bound to
=ocamlformat=, otherwise to =ocp-indent=
+ If =:editor multiple-cursors= is enabled then identifiers can be refactored
with =v R= and multiple cursors (this correctly matches identifier occurrences
according to scope, it is not purely a textual match)
+ If =:emacs imenu= is enabled then top level symbols (modules, type, functions,
etc.) can be looked up using =SPC / i=
Run =doom sync= to install all packages and =doom doctor= to diagnose missing
tools.
* Appendix
** Commands
| Command | Key | Description |
|------------------------------+-------------------+-----------------------------------------------------------|
| =merlin-type-enclosing= | =<localleader> t= | display type under point |
| =tuareg-find-alternate-file= | =<localleader> a= | switch between =.ml= and =.mli= |
| =merlin-locate= | =g d= | lookup definition |
| =merlin-occurences= | =SPC c D= | lookup references |
| =merlin-document= | =K= | lookup documentation |
| =merlin-imenu= | =SPC s i= | symbol lookup in file |
| =merlin-iedit-occurrences= | =v R= | visual refactor identifier under point (multiple cursors) |
| =utop= | =SPC o r= | open =utop= as REPL |
| =utop-eval-region= | =SPC c e= | evaluate selected region in =utop= |
** Hacks
+ =set-ligatures!= is called with the full tuareg prettify symbol list, this
can cause columns to change as certain keywords are shortened (e.g. =fun=
becomes \lambda.
+ =tuareg-opam-update-env= is called the first time =tuareg= is loaded

View File

@@ -0,0 +1,11 @@
;;; lang/ocaml/autoload.el -*- lexical-binding: t; -*-
;;;###autoload
(defun +ocaml/comment-indent-new-line (&optional _)
"Break line at point and indent, continuing comment if within one."
(interactive)
(comment-indent-new-line)
(when (eq (char-before) ?*)
(just-one-space))
(unless (eq (char-after) 32)
(save-excursion (insert " "))))

View File

@@ -0,0 +1,120 @@
;;; lang/ocaml/config.el -*- lexical-binding: t; -*-
;;
;;; Packages
(when (featurep! +lsp)
(add-hook! '(tuareg-mode-local-vars-hook reason-mode-local-vars-hook)
#'lsp!))
(after! tuareg
;; tuareg-mode has the prettify symbols itself
(set-ligatures! 'tuareg-mode :alist
(append tuareg-prettify-symbols-basic-alist
tuareg-prettify-symbols-extra-alist))
;; harmless if `prettify-symbols-mode' isn't active
(setq tuareg-prettify-symbols-full t)
;; Use opam to set environment
(setq tuareg-opam-insinuate t)
(tuareg-opam-update-env (tuareg-opam-current-compiler))
(setq-hook! 'tuareg-mode-hook
comment-line-break-function #'+ocaml/comment-indent-new-line)
(map! :localleader
:map tuareg-mode-map
"a" #'tuareg-find-alternate-file)
(use-package! utop
:when (featurep! :tools eval)
:hook (tuareg-mode-local-vars . +ocaml-init-utop-h)
:init
(set-repl-handler! 'tuareg-mode #'utop)
(set-eval-handler! 'tuareg-mode #'utop-eval-region)
(defun +ocaml-init-utop-h ()
(when (executable-find "utop")
(utop-minor-mode)))
:config
(set-popup-rule! "^\\*utop\\*" :quit nil)))
(use-package! merlin
:unless (featurep! +lsp)
:hook (tuareg-mode-local-vars . +ocaml-init-merlin-h)
:init
(defun +ocaml-init-merlin-h ()
"Activate `merlin-mode' if the ocamlmerlin executable exists."
(when (executable-find "ocamlmerlin")
(merlin-mode)))
(after! tuareg
(set-company-backend! 'tuareg-mode 'merlin-company-backend)
(set-lookup-handlers! 'tuareg-mode :async t
:definition #'merlin-locate
:references #'merlin-occurrences
:documentation #'merlin-document))
:config
(setq merlin-completion-with-doc t)
(map! :localleader
:map tuareg-mode-map
"t" #'merlin-type-enclosing)
(use-package! flycheck-ocaml
:when (featurep! :checkers syntax)
:hook (merlin-mode . +ocaml-init-flycheck-h)
:config
(defun +ocaml-init-flycheck-h ()
"Activate `flycheck-ocaml`"
;; Disable Merlin's own error checking
(setq merlin-error-after-save nil)
;; Enable Flycheck checker
(flycheck-ocaml-setup)))
(use-package! merlin-eldoc
:hook (merlin-mode . merlin-eldoc-setup))
(use-package! merlin-iedit
:when (featurep! :editor multiple-cursors)
:defer t
:init
(map! :map tuareg-mode-map
:v "R" #'merlin-iedit-occurrences))
(use-package! merlin-imenu
:when (featurep! :emacs imenu)
:hook (merlin-mode . merlin-use-merlin-imenu)))
(use-package! ocp-indent
;; must be careful to always defer this, it has autoloads that adds hooks
;; which we do not want if the executable can't be found
:hook (tuareg-mode-local-vars . +ocaml-init-ocp-indent-h)
:config
(defun +ocaml-init-ocp-indent-h ()
"Run `ocp-setup-indent', so long as the ocp-indent binary exists."
(when (executable-find "ocp-indent")
(ocp-setup-indent))))
(use-package! ocamlformat
:when (featurep! :editor format)
:commands ocamlformat
:hook (tuareg-mode-local-vars . +ocaml-init-ocamlformat-h)
:config
(set-formatter! 'ocamlformat #'ocamlformat
:modes '(caml-mode tuareg-mode))
;; TODO Fix region-based formatting support
(defun +ocaml-init-ocamlformat-h ()
(setq +format-with 'ocp-indent)
(when (and (executable-find "ocamlformat")
(locate-dominating-file default-directory ".ocamlformat"))
(when buffer-file-name
(let ((ext (file-name-extension buffer-file-name t)))
(cond ((equal ext ".eliom")
(setq-local ocamlformat-file-kind 'implementation))
((equal ext ".eliomi")
(setq-local ocamlformat-file-kind 'interface)))))
(setq +format-with 'ocamlformat))))

View File

@@ -0,0 +1,24 @@
;; -*- lexical-binding: t; no-byte-compile: t; -*-
;;; lang/ocaml/doctor.el
(assert! (or (not (featurep! +lsp))
(featurep! :tools lsp))
"This module requires (:tools lsp)")
(unless (executable-find "ocamlmerlin")
(warn! "Couldn't find ocamlmerlin. Lookup, completion and syntax checking won't work"))
;; Tuareg can still indent
(unless (executable-find "ocp-indent")
(warn! "Couldn't find ocp-indent. Auto-indentation will be less precise"))
(when (featurep! :tools eval)
(unless (executable-find "utop")
(warn! "Couldn't find utop. REPL won't be available")))
(unless (executable-find "dune")
(warn! "Couldn't find dune. Won't be able to highlight dune files"))
(when (featurep! :editor format)
(unless (executable-find "ocamlformat")
(warn! "Couldn't find ocamlformat. Code-formatting will be unavailable")))

View File

@@ -0,0 +1,25 @@
;; -*- no-byte-compile: t; -*-
;;; lang/ocaml/packages.el
(package! tuareg :pin "00faf47a7c65e4cdcf040f38add1c6a08cd2ee2f")
(unless (featurep! +lsp)
(package! merlin :pin "e4791e22986993c36c3f5c91e8dff93494cc232e")
(package! merlin-eldoc :pin "db7fab1eddfe34781b7e79694f8923b285698032")
(package! merlin-company :pin "e4791e22986993c36c3f5c91e8dff93494cc232e")
(when (featurep! :checkers syntax)
(package! flycheck-ocaml :pin "8707a7bf545a8639a6a5c600a98d9a2ea1487dc9")))
(package! ocp-indent :pin "9e26c0a2699b7076cebc04ece59fb354eb84c11c")
(when (featurep! :tools eval)
(package! utop :pin "c87b8b2817eefd0cd53564618911386b89b587c5"))
(when (featurep! :editor format)
(package! ocamlformat
:recipe (:host github :repo "ocaml-ppx/ocamlformat" :files ("emacs/*.el"))
:pin "22a3707da387f9ab82b42a87c358133b829f2222"))
(package! dune
:recipe (:host github :repo "ocaml/dune" :files ("editor-integration/emacs/*.el"))
:pin "4d0a47edd53dc2141059cbf99bfec858a08e9fe0")