Skip to content

Commit 7a5b87c

Browse files
gpetiotJulow
andauthored
Add a page about how the configuration is computed (#2446)
Co-authored-by: Jules Aguillon <[email protected]>
1 parent 3d8b8e7 commit 7a5b87c

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

doc/configuration.mld

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{0 How OCamlFormat computes its configuration}
2+
3+
{1 Configuration files}
4+
5+
Ocamlformat fetches the following files on the file system:
6+
7+
1. [.git], [.hg] or [dune-project]
8+
2. [.ocamlformat] and [.ocp-indent]
9+
3. [.ocamlformat-ignore] and [.ocamlformat-enable]
10+
11+
(1.) files are used to determine the {b project root}, which can be overriden by the [--root] option, they are looked up from the path of the file to format, and upwards following parent directories, until the first one is found.
12+
13+
(2.) and (3.) are looked up from the path of the file to format, and following up the parents up to the project root.
14+
15+
(2.) are the {b configuration files}, they contain the options used to configure ocamlformat. A global [.ocamlformat] file can also be used: [$XDG_CONFIG_HOME/ocamlformat] (if defined).
16+
17+
18+
{1 How the configuration is built}
19+
20+
The configuration files are considered in sequence, starting from the root of the project, and down to the directory of the file to format, overriding one or many options at each application.
21+
22+
+ The initial configuration is equal to the [default] (or [conventional]) profile.
23+
+ The options passed through the configuration files are applied.
24+
+ The options passed through the [OCAMLFORMAT] environment variable are applied, overriding one or many options at a time.
25+
+ The options passed through the command line are applied, overriding one or many options at a time.
26+
27+
When the option [--enable-outside-detected-project] is set, [.ocamlformat] files outside of the project are read, if no [.ocamlformat] file has been found then then apply the global configuration [$XDG_CONFIG_HOME/ocamlformat] (if defined). The global configuration file is ignore in any other case.
28+
29+
When this option is not set, [.ocamlformat] files outside of the project are ignored.
30+
31+
If no configuration file is found, the formatting is disabled.
32+
33+
{1 Overriding the configuration in the source}
34+
35+
Note that some options can be overriden directly in the source, with attributes like:
36+
37+
{@ocaml[
38+
(* attributes attached to algebraic constructs *)
39+
x [@ocamlformat "option=value,option=value"];;
40+
41+
(* item attributes, attached to "blocks" *)
42+
y [@@ocamlformat "option=value,option=value"];;
43+
44+
(* floating attributes, standalone *)
45+
[@@@ocamlformat "option=value,option=value"]
46+
]}
47+
48+
All "formatting options" (listed in the {{!page-manpage_ocamlformat}manpage}) can be set in attributes.
49+
50+
Among the non-formatting options only [enable]/[disable] can be set in floatting attributes.

doc/editor_setup.mld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{1 Enable formatting outside project}
44

55
OCamlFormat detects your current project if there is a [.git], a [.hg] or a [dune-project] file in one of the ancestry directories.
6+
For more details, read about {{!page-configuration}how OCamlFormat finds its root project and computes its configuration}.
67

78
By default, when the option [--enable-outside-detected-project] is not set, [.ocamlformat] files outside of the current project (including the one in [XDG_CONFIG_HOME]) are not read. If no configuration file is found, then the formatting is disabled.
89

doc/getting_started.mld

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,7 @@ Options can be modified by the means of:
7272
- a global [[@@@ocamlformat "option=VAL"]] attribute in the processed file
7373
- an [[@@ocamlformat "option=VAL"]] attribute on an expression in the processed file
7474

75-
[.ocamlformat] files in the containing and all ancestor directories for each input file are used, as well as the global [.ocamlformat] file defined in [$XDG_CONFIG_HOME/ocamlformat]. The global [.ocamlformat] file has the lowest priority, then the closer the directory is to the processed file, the higher the priority.
76-
77-
When the option [--enable-outside-detected-project] is set, [.ocamlformat] files outside of the project (including the one in [XDG_CONFIG_HOME]) are read. The project root of an input file is taken to be the nearest ancestor directory that contains a [.git] or [.hg] or [dune-project] file.
78-
When this option is not set, [.ocamlformat] files outside of the project are ignored. If no configuration file is found, formatting is disabled.
79-
80-
An [.ocamlformat-ignore] file specifies files that OCamlFormat should ignore. Each line in an [.ocamlformat-ignore] file specifies a filename relative to the directory containing the [.ocamlformat-ignore] file. Lines starting with [#] are ignored and can be used as comments.
75+
For more details, read about {{!page-configuration}how OCamlFormat finds its root project and computes its configuration}.
8176

8277
{1 Version}
8378

doc/index.mld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ OCamlFormat is a tool to format OCaml code.
99

1010
{1 Table of Content}
1111
- {{!page-getting_started}Getting started}
12+
- {{!page-configuration}How OCamlFormat computes its configuration}
1213
- {{!page-editor_setup}Editor setup}
1314
- {{!page-manpage_ocamlformat}Manpage}
1415
- {{!page-howtos}How-To's}

0 commit comments

Comments
 (0)