Skip to content

Commit

Permalink
Merge pull request #74 from bclement-ocp/mr-indent-config
Browse files Browse the repository at this point in the history
 Only enable indenter on free format
  • Loading branch information
bclement-ocp authored Oct 27, 2023
2 parents ab1b7d4 + c119853 commit 06efdf2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
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
35 changes: 28 additions & 7 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 @@ -63,16 +73,27 @@ let indenter ~source_format (str:string) (rdl:indent_record list) range =

(*indent a range of file, with the default indent_config*)
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

(*indent a range of file, with the user-defined indent_config*)
let indent_range ~dialect ~source_format ~indent_config ~range ~filename ~contents =
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" ()
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

0 comments on commit 06efdf2

Please sign in to comment.