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

Only enable indenter on free format #74

Merged
merged 4 commits into from
Oct 27, 2023
Merged
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
13 changes: 3 additions & 10 deletions src/lsp/cobol_indent/cobol_indent.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,12 @@
module Type = Indent_type

(*return the result of indentation. use user-defined indent_config*)
let indent_range' = Indenter.indent_range'
let indent_range = Indenter.indent_range

(*indent the whole file and print*)
let indent_file ~dialect ~source_format ~file ~indent_config =
let contents = Ez_file.V1.EzFile.read_file file in
indent_range'
indent_range
~dialect ~source_format ~range:None ~indent_config
~filename:file ~contents
|> Fmt.pr "%s"

(*indent a range of file and print*)
let indent_range ~dialect ~source_format ~file ~range ~indent_config =
let contents = Ez_file.V1.EzFile.read_file file in
indent_range' ~dialect ~source_format ~range ~indent_config
~filename:file ~contents
|> Fmt.pr "%s"
|> Fmt.pr "%s"
1 change: 1 addition & 0 deletions src/lsp/cobol_indent/indent_type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ type context = (context_kind * int) list

type indent_state =
{
src_format: Cobol_preproc.Src_format.any;
scope: context_kind; (*indicate where the current code is*)
context: context; (*the stack of (context_kind, offset)*)
acc: indent_record list;
Expand Down
41 changes: 31 additions & 10 deletions src/lsp/cobol_indent/indenter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@ let indenter ~source_format (str:string) (rdl:indent_record list) range =
let str = List.nth strl (lnum - 1) in
let newstr =
match source_format with
| Cobol_config.SF SFFree ->
| Cobol_preproc.Src_format.SF (NoIndic, FreePaging) ->
if offset > 0 then
let space = String.make offset ' ' in
space^str
else
String.sub str (-offset) (String.length str + offset)
(*TODO: must change if Auto <> SF SFFixed once*)
| SF SFFixed | Auto ->
| SF (FixedIndic, FixedWidth _) ->
(* Indenting temporarily disabled in fixed format
https://github.com/OCamlPro/superbol-studio-oss/issues/52

Support must be improved before enabling again, in particular to
avoid pushing content into the margin.
https://github.com/OCamlPro/superbol-studio-oss/issues/45
*)
if true then str else
let len = String.length str in
let str1 = String.sub str 0 7 in
let str = String.sub str 7 (len-7) in
Expand All @@ -46,8 +53,11 @@ let indenter ~source_format (str:string) (rdl:indent_record list) range =
String.sub str (-offset) (String.length str + offset)
in
str1^str
(*TODO*)
| _ -> str
(* TODO *)
| SF (XOpenIndic, FixedWidth _) -> str
| SF (CRTIndic, FixedWidth _) -> str
| SF (TrmIndic, FixedWidth _) -> str
| SF (CBLXIndic, FixedWidth _) -> str
in
List.mapi (fun i str -> if i = lnum - 1 then newstr else str) (strl)
in
Expand All @@ -62,22 +72,33 @@ let indenter ~source_format (str:string) (rdl:indent_record list) range =
String.concat "\n" strl

(*indent a range of file, with the default indent_config*)
let indent_range' ~dialect ~source_format ~range ~filename ~contents =
let indent_range ~dialect ~source_format ~range ~filename ~contents =
(* Note: this value doesn't actually matter, it will be overriden
immediately by [fold_source_lines] calling [on_initial_source_format]
below. *)
let src_format = Cobol_preproc.Src_format.from_config SFFixed in
let state =
Cobol_preproc.fold_source_lines ~dialect ~source_format
~on_initial_source_format:(fun src_format st -> { st with src_format })
~on_compiler_directive:(fun _ { payload = cd; _} st ->
match cd with
| CDirSource { payload = src_format; _ } -> { st with src_format }
| _ -> st
)
~skip_compiler_directives_text:true
~f:(fun _lnum line acc -> Indent_check.check_indentation line acc)
(String { filename; contents })
{ scope = BEGIN; context = []; acc = []; range }
{ src_format; scope = BEGIN; context = []; acc = []; range }
in
(* NB: note here we ignore diagnostics *)
let ind_recds = state.result.acc in
indenter ~source_format contents ind_recds state.result.range
indenter
~source_format:state.result.src_format contents ind_recds state.result.range
nberth marked this conversation as resolved.
Show resolved Hide resolved

(*indent a range of file, with the user-defined indent_config*)
let indent_range' ~dialect ~source_format ~indent_config ~range ~filename ~contents =
let indent_range ~dialect ~source_format ~indent_config ~range ~filename ~contents =
begin match indent_config with
| Some indent_config -> Indent_config.set_config ~indent_config
| None -> ()
end;
indent_range' ~dialect ~source_format ~range ~filename ~contents
indent_range ~dialect ~source_format ~range ~filename ~contents
2 changes: 1 addition & 1 deletion src/lsp/cobol_indent/indenter.mli
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
(**************************************************************************)

(*indent a range of file, with the user-defined or default indent_config*)
val indent_range'
val indent_range
: dialect: Cobol_config.dialect
-> source_format:Cobol_config.source_format_spec
-> indent_config:string option
Expand Down
4 changes: 2 additions & 2 deletions src/lsp/cobol_lsp/lsp_request.ml
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ let handle_range_formatting registry params =
}
in
let newText =
Cobol_indent.indent_range'
Cobol_indent.indent_range
~dialect:(Cobol_config.dialect project.cobol_config)
~source_format:project.source_format
~indent_config:None
Expand All @@ -203,7 +203,7 @@ let handle_formatting registry params =
in
try
let newText =
Cobol_indent.indent_range'
Cobol_indent.indent_range
~dialect:(Cobol_config.dialect project.cobol_config)
~source_format:project.source_format
~indent_config:None
Expand Down
10 changes: 7 additions & 3 deletions src/lsp/superbol_free_lib/command_indent_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ open Cobol_indent

open Common_args

let action { preproc_options = { source_format; _ } ; _ } ~indent_config
files =
let action { preproc_options = { source_format; config; _ } ; _ }
~indent_config files =
let module Config = (val config) in
List.to_seq files
|> Seq.map (fun file ->
indent_file ~source_format ~file ~indent_config)
let contents = Ez_file.V1.EzFile.read_file file in
indent_range
~source_format ~filename:file ~contents ~indent_config ~range:None
~dialect:Config.dialect |> Fmt.pr "%s")

let cmd =
let files = ref [] in
Expand Down
5 changes: 3 additions & 2 deletions src/lsp/superbol_free_lib/command_indent_range.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ open Common_args
let action { preproc_options = { source_format; config; _ }; _ } ~file ~range
~indent_config =
let module Config = (val config) in
indent_range ~source_format ~file ~range ~indent_config
~dialect:Config.dialect
let contents = Ez_file.V1.EzFile.read_file file in
indent_range ~source_format ~filename:file ~contents ~range ~indent_config
~dialect:Config.dialect |> Fmt.pr "%s"

let cmd =
let file = ref "" in
Expand Down
36 changes: 27 additions & 9 deletions test/lsp/lsp_formatting.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ let doc = {cobol|
|cobol};;

let%expect_test "simple-formatting-request" =
let { projdir; end_with_postproc }, server = make_lsp_project () in
let { projdir; end_with_postproc }, server =
make_lsp_project ~toml:"source-format = \"free\"\n" ()
Copy link
Collaborator

@nberth nberth Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could have deserved a little make_free_format_lsp_project shortcut to avoid duplication.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could, but I believe test code deserves to be as plain as possible, hence I did not do that -- in fact there is much code duplication between the test cases already.

in
let server, prog = add_cobol_doc server ~projdir "prog.cob" doc in
let params =
let options = FormattingOptions.create ~insertSpaces:true ~tabSize:2 () in
Expand Down Expand Up @@ -58,7 +60,9 @@ let doc = {cobol|
move 1 to x. |cobol};;

let%expect_test "formatting-request-nested-if" =
let { projdir; end_with_postproc }, server = make_lsp_project () in
let { projdir; end_with_postproc }, server =
make_lsp_project ~toml:"source-format = \"free\"\n" ()
in
let server, prog = add_cobol_doc server ~projdir "prog.cob" doc in
let params =
let options = FormattingOptions.create ~insertSpaces:true ~tabSize:2 () in
Expand Down Expand Up @@ -100,7 +104,9 @@ let doc = {cobol|
value 999. |cobol};;

let%expect_test "formatting-request-data" =
let { projdir; end_with_postproc }, server = make_lsp_project () in
let { projdir; end_with_postproc }, server =
make_lsp_project ~toml:"source-format = \"free\"\n" ()
in
let server, prog = add_cobol_doc server ~projdir "prog.cob" doc in
let params =
let options = FormattingOptions.create ~insertSpaces:true ~tabSize:2 () in
Expand Down Expand Up @@ -173,7 +179,9 @@ let doc = {cobol|
|cobol};;

let%expect_test "formatting-request-nested-program" =
let { projdir; end_with_postproc }, server = make_lsp_project () in
let { projdir; end_with_postproc }, server =
make_lsp_project ~toml:"source-format = \"free\"\n" ()
in
let server, prog = add_cobol_doc server ~projdir "prog.cob" doc in
let params =
let options = FormattingOptions.create ~insertSpaces:true ~tabSize:2 () in
Expand Down Expand Up @@ -231,7 +239,9 @@ let doc = {cobol|
|cobol};;

let%expect_test "formatting-request-alignment-argument" =
let { projdir; end_with_postproc }, server = make_lsp_project () in
let { projdir; end_with_postproc }, server =
make_lsp_project ~toml:"source-format = \"free\"\n" ()
in
let server, prog = add_cobol_doc server ~projdir "prog.cob" doc in
let params =
let options = FormattingOptions.create ~insertSpaces:true ~tabSize:2 () in
Expand Down Expand Up @@ -263,7 +273,9 @@ let doc = {cobol|
|cobol};;

let%expect_test "formatting-request-else-if" =
let { projdir; end_with_postproc }, server = make_lsp_project () in
let { projdir; end_with_postproc }, server =
make_lsp_project ~toml:"source-format = \"free\"\n" ()
in
let server, prog = add_cobol_doc server ~projdir "prog.cob" doc in
let params =
let options = FormattingOptions.create ~insertSpaces:true ~tabSize:2 () in
Expand Down Expand Up @@ -381,7 +393,9 @@ let doc = {cobol|
|cobol};;

let%expect_test "formatting-request-whole-program" =
let { projdir; end_with_postproc }, server = make_lsp_project () in
let { projdir; end_with_postproc }, server =
make_lsp_project ~toml:"source-format = \"free\"\n" ()
in
let server, prog = add_cobol_doc server ~projdir "prog.cob" doc in
let params =
let options = FormattingOptions.create ~insertSpaces:true ~tabSize:2 () in
Expand Down Expand Up @@ -507,7 +521,9 @@ let doc = {cobol|
|cobol};;

let%expect_test "formatting-request-on-exception" =
let { projdir; end_with_postproc }, server = make_lsp_project () in
let { projdir; end_with_postproc }, server =
make_lsp_project ~toml:"source-format = \"free\"\n" ()
in
let server, prog = add_cobol_doc server ~projdir "prog.cob" doc in
let params =
let options = FormattingOptions.create ~insertSpaces:true ~tabSize:2 () in
Expand Down Expand Up @@ -542,7 +558,9 @@ let doc = {cobol|
|cobol};;

let%expect_test "formatting-request-perform" =
let { projdir; end_with_postproc }, server = make_lsp_project () in
let { projdir; end_with_postproc }, server =
make_lsp_project ~toml:"source-format = \"free\"\n" ()
in
let server, prog = add_cobol_doc server ~projdir "prog.cob" doc in
let params =
let options = FormattingOptions.create ~insertSpaces:true ~tabSize:2 () in
Expand Down
Loading