From b0b77130db9e3e62de31ea12b03391920d1cc5be Mon Sep 17 00:00:00 2001 From: Ivan Sokolov Date: Sun, 12 Sep 2021 05:08:57 +0300 Subject: [PATCH 1/3] Use file:consult/1 for erlang_ls.config --- apps/els_core/src/els_config.erl | 19 ++++++++++++++++--- erlang_ls.config.sample | 18 ++++++++---------- erlang_ls.yaml.sample | 10 ++++++++++ 3 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 erlang_ls.yaml.sample diff --git a/apps/els_core/src/els_config.erl b/apps/els_core/src/els_config.erl index 2fd3652ce..e22d73b8a 100644 --- a/apps/els_core/src/els_config.erl +++ b/apps/els_core/src/els_config.erl @@ -242,10 +242,14 @@ consult_config([], ReportMissingConfig) -> {undefined, #{}}; consult_config([Path | Paths], ReportMissingConfig) -> ?LOG_INFO("Reading config file. path=~p", [Path]), - Options = [{map_node_format, map}], - try yamerl:decode_file(Path, Options) of + try consult_file(Path) of [] -> {Path, #{}}; - [Config] -> {Path, Config} + [Config] -> {Path, Config}; + {ok, Config} -> {Path, maps:from_list(Config)}; + {error, Reason} -> + ?LOG_WARNING( "Could not read config file: path=~p class=~p error=~p" + , [Path, error, Reason]), + consult_config(Paths, ReportMissingConfig) catch Class:Error -> ?LOG_WARNING( "Could not read config file: path=~p class=~p error=~p" @@ -253,6 +257,15 @@ consult_config([Path | Paths], ReportMissingConfig) -> consult_config(Paths, ReportMissingConfig) end. +-spec consult_file(path()) -> [map()] | {ok, [term()]} | {error, term()}. +consult_file(Path) -> + case string:find(Path, ".yaml", trailing) of + nomatch -> + file:consult(Path); + ".yaml" -> + yamerl:decode_file(Path, [{map_node_format, map}]) + end. + -spec report_missing_config() -> ok. report_missing_config() -> Msg = diff --git a/erlang_ls.config.sample b/erlang_ls.config.sample index 45d1ea58b..5db83fa2f 100644 --- a/erlang_ls.config.sample +++ b/erlang_ls.config.sample @@ -1,10 +1,8 @@ -apps_dirs: - - "apps/*" -deps_dirs: - - "_build/default/lib/*" - - "_build/test/lib/*" -include_dirs: - - "apps" - - "apps/*/include" - - "_build/*/lib/" - - "_build/*/lib/*/include" +%% -*- mode: erlang; -*- + +{"apps_dirs", ["apps/*"]}. + +{"deps_dirs", ["_build/default/lib/*", "_build/test/lib/*"]}. + +{"include_dirs", + ["apps", "apps/*/include", "_build/*/lib", "_build/*/lib/*/include"]}. diff --git a/erlang_ls.yaml.sample b/erlang_ls.yaml.sample new file mode 100644 index 000000000..45d1ea58b --- /dev/null +++ b/erlang_ls.yaml.sample @@ -0,0 +1,10 @@ +apps_dirs: + - "apps/*" +deps_dirs: + - "_build/default/lib/*" + - "_build/test/lib/*" +include_dirs: + - "apps" + - "apps/*/include" + - "_build/*/lib/" + - "_build/*/lib/*/include" From a9cbbb447f4aea1fcc1bc73ebcef052871947f5f Mon Sep 17 00:00:00 2001 From: Ivan Sokolov Date: Sat, 25 Dec 2021 19:56:55 +0300 Subject: [PATCH 2/3] Apply comments --- apps/els_core/src/els_config.erl | 46 ++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/apps/els_core/src/els_config.erl b/apps/els_core/src/els_config.erl index e22d73b8a..2c2cc5903 100644 --- a/apps/els_core/src/els_config.erl +++ b/apps/els_core/src/els_config.erl @@ -242,19 +242,43 @@ consult_config([], ReportMissingConfig) -> {undefined, #{}}; consult_config([Path | Paths], ReportMissingConfig) -> ?LOG_INFO("Reading config file. path=~p", [Path]), - try consult_file(Path) of - [] -> {Path, #{}}; - [Config] -> {Path, Config}; - {ok, Config} -> {Path, maps:from_list(Config)}; - {error, Reason} -> - ?LOG_WARNING( "Could not read config file: path=~p class=~p error=~p" - , [Path, error, Reason]), - consult_config(Paths, ReportMissingConfig) + Result = + case filename:extension(Path) of + ".yaml" -> + try_yaml(Path); + ".config" -> + try_eterm(Path, _TryYaml = true) + end, + case Result of + {ok, Config} -> + {Path, Config}; + {error, Class, Error} -> + ?LOG_WARNING("Could not read config file: path=~p class=~p error=~p", + [Path, Class, Error]), + consult_config(Paths, ReportMissingConfig) + end. + +-spec try_yaml(path()) -> {ok, map()} | {error, atom(), term()}. +try_yaml(Path) -> + try yamerl:decode_file(Path, [{map_node_format, map}]) of + [] -> + {ok, #{}}; + [Config] -> + {ok, Config} catch Class:Error -> - ?LOG_WARNING( "Could not read config file: path=~p class=~p error=~p" - , [Path, Class, Error]), - consult_config(Paths, ReportMissingConfig) + {error, Class, Error} + end. + +-spec try_eterm(path(), boolean()) -> {ok, map()} | {error, atom(), term()}. +try_eterm(Path, TryYaml) -> + case consult_file(Path) of + {ok, Config} -> + {ok, maps:from_list(Config)}; + {error, _} when TryYaml -> + try_yaml(Path); + {error, Reason} -> + {error, error, Reason} end. -spec consult_file(path()) -> [map()] | {ok, [term()]} | {error, term()}. From 91e64fc29a26fcfe32407924fd3798b443da3a79 Mon Sep 17 00:00:00 2001 From: Ivan Sokolov Date: Sat, 25 Dec 2021 20:00:29 +0300 Subject: [PATCH 3/3] Remove els_config:consult_file/1 --- apps/els_core/src/els_config.erl | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/apps/els_core/src/els_config.erl b/apps/els_core/src/els_config.erl index 2c2cc5903..9f64e3943 100644 --- a/apps/els_core/src/els_config.erl +++ b/apps/els_core/src/els_config.erl @@ -272,7 +272,7 @@ try_yaml(Path) -> -spec try_eterm(path(), boolean()) -> {ok, map()} | {error, atom(), term()}. try_eterm(Path, TryYaml) -> - case consult_file(Path) of + case file:consult(Path) of {ok, Config} -> {ok, maps:from_list(Config)}; {error, _} when TryYaml -> @@ -281,15 +281,6 @@ try_eterm(Path, TryYaml) -> {error, error, Reason} end. --spec consult_file(path()) -> [map()] | {ok, [term()]} | {error, term()}. -consult_file(Path) -> - case string:find(Path, ".yaml", trailing) of - nomatch -> - file:consult(Path); - ".yaml" -> - yamerl:decode_file(Path, [{map_node_format, map}]) - end. - -spec report_missing_config() -> ok. report_missing_config() -> Msg =