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

improve user-defined config file detection #311

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 9 additions & 3 deletions doc/ocp-indent.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,15 @@ requiring them to change their settings in any way (except that, obviously, they
need to use ocp-indent !).

If a `.ocp-indent` file is found in the current directory or its ancestors, it
overrides definitions from `$XDG_CONFIG_HOME/ocp/ocp-indent.conf`,
`~/.ocp/ocp-indent.conf` and the built-in default. The command-line can of
course still be used to override parameters defined in the files.
overrides definitions from the user-defined configuration file (see below)
and the built-in default. The command-line can of course still be used to
override parameters defined in the files.

The user-defined configuration file depends on the platform you're using.
On linux it is `$XDG_CONFIG_HOME/ocp-indent/config` with `$XDG_CONFIG_HOME`
defaulting to `$HOME/.config/`. On macOS it is
`$HOME/Library/Application Support/com.OCamlPro.ocp-indent/config`. On
Windows it is `{FOLDERID_ApplicationData}/OCamlPro/ocp-indent/config`.

Have a look at ocp-indent's own [`.ocp-indent`](.ocp-indent) file for an
example.
Expand Down
1 change: 1 addition & 0 deletions ocp-indent.opam
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ depends: [
"cmdliner" {>= "1.0.0"}
"ocamlfind"
"base-bytes"
"directories" {>= "0.2"}
]
post-messages: [
"This package requires additional configuration for use in editors. Install package 'user-setup', or manually:
Expand Down
2 changes: 1 addition & 1 deletion src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
(name ocp_indent_lib)
(wrapped false)
(public_name ocp-indent.lib)
(libraries ocp-indent.utils)
(libraries ocp-indent.utils directories)
(modules indentConfig indentBlock indentPrinter)
(flags :standard -w -9 -warn-error -57)
)
Expand Down
10 changes: 7 additions & 3 deletions src/indentArgs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,13 @@ let info =
option, or as a configuration definition in one of the following, \
searched in order: a file named `.ocp-indent' in the current directory \
or its parents (which allows for per-project indentation settings), \
the file `\\$XDG_CONFIG_HOME/ocp/ocp-indent.conf', the file \
`\\$HOME/.ocp/ocp-indent.conf', or the environment variable \
\\$OCP_INDENT_CONFIG."
the user-defined configuration file (see below), or the environment \
variable \\$OCP_INDENT_CONFIG.";
`P "The user-defined configuration file depends on the platform you're \
using. On Linux it is `\\$XDG_CONFIG_HOME/ocp-indent/config' with \
`\\$XDG_CONFIG_HOME' defaulting to `\\$HOME/.config/'. On macOS it is \
`\\$HOME/Library/Application Support/com.OCamlPro.ocp-indent/config'. \
On Windows it is `{FOLDERID_ApplicationData}/OCamlPro/ocp-indent/config'"
] @
IndentConfig.man
@ [
Expand Down
28 changes: 15 additions & 13 deletions src/indentConfig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -400,22 +400,24 @@ let rec find_conf_file path =
if parent <> path then find_conf_file parent
else None

module App_id = struct
let qualifier = "com"
let organization = "OCamlPro"
let application = "ocp-indent"
end

module Project_dirs = Directories.Project_dirs (App_id)

let local_default ?(path=Sys.getcwd()) () =
let conf = default in
let conf, syn, dlink =
try
let (/) = Filename.concat in
let xdg_path = ( match Sys.getenv "XDG_CONFIG_HOME" with
| "" -> (Sys.getenv "HOME") / ".config"
| exception Not_found -> (Sys.getenv "HOME") / ".config"
| x -> x ) / "ocp" / "ocp-indent.conf" in
if Sys.file_exists xdg_path
then load ~indent:conf xdg_path
else let legacy_path = (Sys.getenv "HOME") / ".ocp" / "ocp-indent.conf" in
if Sys.file_exists legacy_path
then load ~indent:conf legacy_path
else conf, [], []
with Not_found -> conf, [], []
let default = conf, [], [] in
match Project_dirs.config_dir with
| None -> default
| Some config_dir ->
let config_path = Filename.concat config_dir "config" in
if Sys.file_exists config_path then load ~indent:conf config_path
else default
in
let conf, syn, dlink = match find_conf_file path with
| Some c ->
Expand Down