- OCaml 95%
- Raku 5%
- Shell <1%
ocaml-merlin #
Structured Merlin queries for OCaml tools.
Overview #
A linter, a documentation generator or a refactoring tool needs the same
facts about OCaml source that an editor gets from
Merlin: the outline of a file, the
occurrences of an identifier, the types enclosing a position, the parse and
typed trees. This library asks Merlin for them and returns typed values. The
same calls run over merlin-lib linked into the tool (Lib, the default)
or over an ocamlmerlin subprocess, one per query (Single) or a long-lived
server (Server).
Every entry into compiler-libs runs under one mutex, Merlin.Cl_lock, and
reads of .cmt and .cmti files are checked against the source they
describe, so a stale artefact is refused rather than trusted.
Installation #
The opam package is called merlin, the same name as the Merlin editor
service in the public opam-repository, so a plain opam install merlin
installs that one. Install this library from the overlay repository:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install merlin
Usage #
Extracting File Outline #
open Merlin
let () =
let t = v ~backend:Single () in
let file = "lib/main.ml" in
let source =
Source.v ~file
~content:(lazy (In_channel.with_open_text file In_channel.input_all))
in
match outline t ~source with
| Ok items ->
List.iter (fun (item : Merlin.Outline.item) ->
Fmt.pr "%s: %a@." item.name Merlin.Outline.pp_symbol_kind item.kind
) items
| Error e ->
Fmt.epr "Error: %s@." e
Finding Occurrences #
let () =
let t = Merlin.v ~backend:Single () in
match Merlin.occurrences t ~file:"lib/main.ml" ~line:10 ~col:5 ~scope:Buffer with
| Ok result ->
List.iter (fun occ -> Fmt.pr "%a@." Merlin.pp_occurrence occ)
result.occurrences
| Error e ->
Fmt.epr "Error: %s@." e
Enclosing Types #
let print_enclosing t =
let file = "lib/main.ml" in
let source =
Merlin.Source.v ~file
~content:(lazy (In_channel.with_open_text file In_channel.input_all))
in
match Merlin.enclosing t ~source ~line:10 ~col:5 with
| Ok locs ->
(* Returns locations from innermost to outermost *)
List.iter (fun loc -> Fmt.pr "%a@." Merlin.pp_location loc) locs
| Error e -> Fmt.epr "Error: %s@." e
Backends #
| Backend | Constructor | Description |
|---|---|---|
| Library | Lib |
Links merlin-lib and calls it directly (the default) |
| Single | Single |
Spawns one ocamlmerlin process per query |
| Server | Server |
Talks to one ocamlmerlin server across queries |
Outline Utilities #
(* Flatten nested outline to list *)
let all_items outline = Merlin.Outline.flatten outline
(* Filter to just values *)
let all_values outline = Merlin.Outline.values outline
(* Find by name *)
let item outline = Merlin.Outline.by_name "my_function" outline
Limitations #
- The subprocess backends need
ocamlmerlininstalled and on thePATH. - Reading an AST dump covers the common cases, and some Merlin output variants are not handled.
- A query takes as long as Merlin takes to answer, which grows with the project.
Licence #
ISC