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

command 'superbol-free reformat to-free FILES.cob' #201

Open
wants to merge 2 commits 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
4 changes: 2 additions & 2 deletions src/lsp/cobol_indent/config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ let verbose = false

let fixed_format = {
name = "fixed" ;
free = false ; (* whether there is an indicator or inline
format = SFFixed ; (* whether there is an indicator or inline
comments *)
skip_before = 6 ; (* skip 6 columns, 1..6 *)
max_text_length = 65; (* skip columns 8..72 *)
}

let free_format = {
name = "free" ;
free = true ;
format = SFFree ;
skip_before = 0 ;
max_text_length = 65536;
}
Expand Down
7 changes: 7 additions & 0 deletions src/lsp/cobol_indent/config.mli
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ val load :
val to_string : Types.config -> string

val generate : ?config:Types.config -> ?only_comment:bool -> string -> unit


val free_format : Types.source_format
val fixed_format : Types.source_format
val variable_format : Types.source_format
val xcard_format : Types.source_format
val cobolx_format : Types.source_format
28 changes: 13 additions & 15 deletions src/lsp/cobol_indent/editor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
(* *)
(**************************************************************************)

open Ez_file.V1

open Types

let verbose = Engine.verbose
Expand All @@ -22,8 +20,13 @@ let verbose = Engine.verbose
(* ~char: the position within the line. For now, it does not change with
insertions/deletions, but it should, in the future... *)

let apply_edits ~contents ~range ~config ~filename ~edits ~symbolic =
let apply_edits (type t) ~contents ~range ~config ~edits
(output : t output) =

let symbolic = match output with
| Output_contents -> false
| Output_edits -> true
in
let len = String.length contents in
let b = Buffer.create (if symbolic then 4 else 2 * len ) in
let ops = ref [] in
Expand Down Expand Up @@ -57,7 +60,7 @@ let apply_edits ~contents ~range ~config ~filename ~edits ~symbolic =
if line = edit.lnum then
skip_before ~pos ~char
~nbefore:(
(if config.source_format.free then 0 else 1) +
(if config.source_format.format = SFFree then 0 else 1) +
config.source_format.skip_before )
edit
~line edits
Expand Down Expand Up @@ -137,7 +140,7 @@ let apply_edits ~contents ~range ~config ~filename ~edits ~symbolic =
if verbose then
Printf.eprintf "iter_eol ~pos:%d ~line:%d ~addspaces:%d\n%!"
pos line addspaces;
if config.source_format.free then
if config.source_format.format = SFFree then
iter_edits ~pos ~char ~line edits
else
let textlen =
Expand Down Expand Up @@ -225,13 +228,8 @@ let apply_edits ~contents ~range ~config ~filename ~edits ~symbolic =

in
iter_edits ~pos:0 ~char:0 ~line:1 edits; (* lnum starts at 1 ? *)
if not symbolic then begin
let contents = Buffer.contents b in
if filename = "-" then
Printf.printf "%s%!" contents
else begin
EzFile.write_file filename contents ;
Printf.eprintf "File %S indented\n%!" filename
end;
end;
List.rev !ops
match output with
| Output_contents ->
( Buffer.contents b : t )
| Output_edits ->
( { edits ; operations = List.rev !ops } : t )
6 changes: 2 additions & 4 deletions src/lsp/cobol_indent/editor.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
(* *)
(**************************************************************************)


val apply_edits :
contents:string ->
range:Types.range ->
config:Types.config ->
filename:string ->
edits:Types.indent_record list ->
symbolic:bool ->
Types.edit_space_operation list
edits:Types.indent_record list -> 't Types.output -> 't
18 changes: 3 additions & 15 deletions src/lsp/cobol_indent/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,12 @@ open Types
closest line with a saved state. *)
let indent
~source_format
~config
~dialect (* why ? we usually don't care about the dialect to indent *)
~filename
?(verbose=false)
?output
?contents
?range
()
output
=
ignore ( config ) ;
ignore ( dialect ) ;
let contents = match contents with
| None -> EzFile.read_file filename
| Some contents -> contents
Expand Down Expand Up @@ -138,12 +133,5 @@ let indent
in
let edits = iter [] edits in

let symbolic, filename =
match output with
| Some filename -> false, filename
| None -> true, "-"
in
let ops = Editor.apply_edits
~contents ~edits ~range ~filename ~config ~symbolic
in
edits, ops
Editor.apply_edits
~contents ~edits ~range ~config output
10 changes: 2 additions & 8 deletions src/lsp/cobol_indent/main.mli
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@
(**************************************************************************)

val indent :
source_format:Cobol_config.source_format_spec ->
config: Types.unparsed_config ->
dialect:'c ->
source_format:Cobol_config.Types.source_format_spec ->
filename:string ->
?verbose:bool ->
?output:string ->
?contents:string ->
?range:Types.range ->
unit ->
Types.indent_record list * Types.edit_space_operation list
?contents:string -> ?range:Types.range -> 'c Types.output -> 'c
Loading
Loading