Skip to content

Commit

Permalink
Add superbol.cobol.copyexts configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
nberth committed Jul 29, 2024
1 parent 27ce804 commit d9ff7a1
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 14 deletions.
10 changes: 10 additions & 0 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/lsp/cobol_common/copybook.ml
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,14 @@ let find_lib ~lookup_config:({ lookup_path = libpath;
in
iter_path libpath_files
in
(* Note: GnuCOBOL handles copybook names given as string literals or plain
text-words in the same way (except for distinct case-folding, that we don't
handle yet).
TODO: to handle case folding, a copybook name that is given as a text-word
should be put in uppercase unless [lookup_fold=Lower]. *)
match textname with
| `Alphanum w -> (* assume no more filename extension *)
try_file w []
| `Alphanum w
| `Word w ->
match try_file w libexts with
| Ok lib -> Ok lib
Expand Down
22 changes: 19 additions & 3 deletions src/lsp/cobol_lsp/lsp_project.ml
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ let update_source_format { config; _ } str : bool =
let update_dialect ({ config; _ } as project) str : bool =
try
let { result; diags } =
Superbol_project.Config.cobol_config_from_dialect_name
~verbose:false str in
Superbol_project.Config.cobol_config_from_dialect_name str
~verbose:false
in
if result = config.cobol_config (* note: structural comparison *)
then false
else begin
Expand All @@ -124,13 +125,24 @@ let update_copybooks: t -> Yojson.Safe.t -> bool = fun { config; _ } json ->
in
try
let libpath = convert_each to_libdir json in
if libpath = config.libpath (* note: structural comparison *)
if libpath = config.libpath (* note: structural comparison *)
then false
else (config.libpath <- libpath; true)
with
Yojson.Safe.Util.(Type_error _ | Undefined _) as e ->
Pretty.invalid_arg "%s: %a" (Yojson.Safe.to_string json) Fmt.exn e

let update_copyexts: t -> Yojson.Safe.t -> bool = fun { config; _ } json ->
let open Yojson.Safe.Util in
try
let libexts = convert_each to_string json in
if libexts = config.libexts (* note: structural comparison *)
then false
else (config.libexts <- libexts; true)
with
Yojson.Safe.Util.(Type_error _ | Undefined _) as e ->
Pretty.invalid_arg "%s: %a" (Yojson.Safe.to_string json) Fmt.exn e

(** [update_project_config assoc project] updates the configuration of [project]
according to key/value paires in [assoc]; returns [true] whenever the
configuration upon termination differs from the configuration upon call. *)
Expand All @@ -156,6 +168,7 @@ let update_project_config assoc project : bool =
"dialect", from_string ~f:update_dialect;
"source-format", from_string ~f:update_source_format;
"copybooks", update_copybooks;
"copyexts", update_copyexts;
]


Expand Down Expand Up @@ -199,6 +212,9 @@ let get_project_config ?(flat = true) project : Yojson.Safe.t =

"copybooks",
`List copybooks;

"copyexts",
`List (List.map (fun s -> `String s) config.libexts);
]
in
if flat
Expand Down
7 changes: 7 additions & 0 deletions src/lsp/superbol_free_lib/vscode_extension.ml
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,13 @@ let contributes =
(with_superbol_toml_note "List of copybooks paths.")
~order:3;

Manifest.PROPERTY.strings "superbol.cobol.copyexts"
~markdownDescription:
(with_superbol_toml_note
"File extensions for copybook resolution")
~default:Cobol_common.Copybook.copybook_extensions
~order:1;

(* Paths *)

Manifest.PROPERTY.string "superbol.cobc-path"
Expand Down
30 changes: 23 additions & 7 deletions src/lsp/superbol_project/project_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module TYPES = struct
mutable cobol_config: Cobol_config.t;
mutable source_format: Cobol_config.source_format_spec;
mutable libpath: path list;
mutable copybook_extensions: string list;
mutable libexts: string list;
mutable copybook_if_no_extension: bool;
mutable indent_config: (string * int) list;
toml_handle: Ezr_toml.toml_handle;
Expand Down Expand Up @@ -68,15 +68,15 @@ let cobol_source_format source_format_name =
(* --- *)

let default_libpath = [RelativeToProjectRoot "."]
let default_copybook_extensions = Cobol_common.Copybook.copybook_extensions
let default_libexts = Cobol_common.Copybook.copybook_extensions
let default_copybook_if_no_extension = true
let default_indent_config = []

let default = {
cobol_config = Cobol_config.default;
source_format = Cobol_config.Auto;
libpath = default_libpath;
copybook_extensions = default_copybook_extensions;
libexts = default_libexts;
indent_config = default_indent_config;
copybook_if_no_extension = default_copybook_if_no_extension;
toml_handle = Ezr_toml.make_empty ();
Expand Down Expand Up @@ -108,6 +108,9 @@ let path_repr = function
let libpath_repr libpath =
TOML.value_of_array @@ Array.of_list @@ List.map path_repr libpath

let libexts_repr libexts =
TOML.value_of_strings @@ Array.of_list libexts

let indent_repr indent =
TOML.value_of_table @@
List.fold_left
Expand All @@ -134,6 +137,11 @@ let config_repr config ~name =
~after_comments: ["Where to find copybooks"]
(libpath_repr config.libpath);

option
~name: "copyexts"
~after_comments: ["Copybook filename extensions"]
(libexts_repr config.libexts);

option
~name: "indent"
~after_comments: ["Indenter configuration"]
Expand Down Expand Up @@ -165,6 +173,10 @@ let get_libpath toml =
Array.to_list @@ TOML.get_array toml ["copybooks"]
with Not_found -> default_libpath

let get_libexts toml =
try Array.to_list @@ TOML.get_strings toml ["copyexts"]
with Not_found -> default_libexts

let get_indent_config toml =
try
EzCompat.StringMap.fold (fun name node v ->
Expand All @@ -190,6 +202,7 @@ let load_file ?(verbose=false) config_filename =
toml_handle;
source_format = get_source_format section;
libpath = get_libpath section;
libexts = get_libexts section;
indent_config = get_indent_config section }
in
try
Expand Down Expand Up @@ -220,16 +233,19 @@ let reload ?(verbose=false) ~config_filename config =
cobol_config_from_dialect_name ~verbose @@ get_dialect section
and source_format = get_source_format section
and libpath = get_libpath section
and libexts = get_libexts section
and indent_config = get_indent_config section in
let changed =
config.source_format <> source_format ||
config.libpath <> libpath ||
config.libexts <> libexts ||
config.indent_config <> indent_config ||
config.cobol_config <> cobol_config
in
config.cobol_config <- cobol_config;
config.source_format <- source_format;
config.libpath <- libpath;
config.libexts <- libexts;
config.indent_config <- indent_config;
DIAGS.result changed ~diags
in
Expand All @@ -242,6 +258,7 @@ let reload ?(verbose=false) ~config_filename config =
config.cobol_config <- default.cobol_config;
config.source_format <- default.source_format;
config.libpath <- default.libpath;
config.libexts <- default.libexts;
config.indent_config <- default.indent_config;
DIAGS.result true
with TOML.Types.Error (loc, _code, error) ->
Expand All @@ -259,15 +276,14 @@ let libpath_for ~filename { libpath; _ } =

let copybook_lookup_config_for ~filename config =
Cobol_common.Copybook.lookup_config (libpath_for ~filename config)
~libexts: config.copybook_extensions
~libexts: config.libexts



(* TODO: add config flags to libpath where some directories may only include
copybooks. *)
let detect_copybook ~filename { copybook_extensions;
copybook_if_no_extension; _ } =
List.exists (Filename.check_suffix filename) copybook_extensions ||
let detect_copybook ~filename { libexts; copybook_if_no_extension; _ } =
List.exists (Filename.check_suffix filename) libexts ||
(copybook_if_no_extension && Filename.extension filename = "")


Expand Down
2 changes: 1 addition & 1 deletion src/lsp/superbol_project/project_config.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module TYPES: sig
mutable cobol_config: Cobol_config.t;
mutable source_format: Cobol_config.source_format_spec;
mutable libpath: path list;
mutable copybook_extensions: string list;
mutable libexts: string list;
mutable copybook_if_no_extension: bool;
mutable indent_config: (string * int) list;
toml_handle: Ezr_toml.toml_handle;
Expand Down
10 changes: 10 additions & 0 deletions src/vscode/superbol-vscode-platform/superbol_tasks.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ let config_string key ~config =
try Jsonoo.Decode.string @@ Hashtbl.find config key
with Not_found -> Superbol_workspace.string key

let config_strings key ~config ~append =
append @@
try Jsonoo.Decode.(list string) @@ Hashtbl.find config key
with Jsonoo.Decode_error _ -> [] (* Warning: silenced decode errors for now *)
| Not_found ->Superbol_workspace.strings key

let attr_strings key ~append ~attributes args =
match List.assoc_opt key attributes with
| Some l -> append ([%js.to: string list] l) args
Expand Down Expand Up @@ -112,6 +118,10 @@ let cobc_execution ?config attributes =
end l |>
List.append args
end |>
config_strings "cobol.copyexts" ~config
~append:begin fun exts args ->
List.append args @@ List.flatten @@ List.map (fun e -> ["-ext"; e]) exts
end |>
config_string "cobol.dialect" ~config
~mk:(function "gnucobol" -> "-std=default" | s -> "-std=" ^ s) |>
config_string "cobol.source-format" ~config
Expand Down
2 changes: 1 addition & 1 deletion src/vscode/superbol-vscode-platform/superbol_workspace.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ let string ?scope key =
| Some o when Ojs.is_null o -> ""
| Some s -> Ojs.string_of_js s

let string_list ?scope key =
let strings ?scope key =
let config = WS.getConfiguration ?scope ~section () in
match WS_CONF.get ~section:key config with
| None -> []
Expand Down

0 comments on commit d9ff7a1

Please sign in to comment.