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

Indent text from buffer, not disk #71

Merged
merged 1 commit into from
Oct 26, 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
9 changes: 7 additions & 2 deletions src/lsp/cobol_indent/cobol_indent.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ let indent_range' = Indenter.indent_range'

(*indent the whole file and print*)
let indent_file ~dialect ~source_format ~file ~indent_config =
indent_range' ~dialect ~source_format ~range:None ~indent_config ~file
let contents = Ez_file.V1.EzFile.read_file file in
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 =
indent_range' ~dialect ~source_format ~range ~indent_config ~file
let contents = Ez_file.V1.EzFile.read_file file in
indent_range' ~dialect ~source_format ~range ~indent_config
~filename:file ~contents
|> Fmt.pr "%s"
20 changes: 6 additions & 14 deletions src/lsp/cobol_indent/indenter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,22 @@ 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 ~file =
let file_content = Ez_file.V1.EzFile.read_file file in
(*
Not satisfied with the `Cobol_preproc.fold_text_lines`,
this function has an argument which is the name of file,
so when using lsp, every time using the formatting,
we must save the file before, it is not convenient.
(* NB: not anymore. *)
*)
let indent_range' ~dialect ~source_format ~range ~filename ~contents =
nberth marked this conversation as resolved.
Show resolved Hide resolved
let state =
Cobol_preproc.fold_source_lines ~dialect ~source_format
~skip_compiler_directives_text:false
~skip_compiler_directives_text:true
~f:(fun _lnum line acc -> Indent_check.check_indentation line acc)
(String { contents = file_content; filename = file; })
(String { filename; contents })
{ scope = BEGIN; context = []; acc = []; range }
in
(* NB: note here we ignore diagnostics *)
let ind_recds = state.result.acc in
indenter ~source_format file_content ind_recds state.result.range
indenter ~source_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 ~file =
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 ~file
indent_range' ~dialect ~source_format ~range ~filename ~contents
3 changes: 2 additions & 1 deletion src/lsp/cobol_indent/indenter.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ val indent_range'
-> source_format:Cobol_config.source_format_spec
-> indent_config:string option
-> range:Indent_type.range option
-> file:string
-> filename:string
-> contents:string
-> string
11 changes: 7 additions & 4 deletions src/lsp/cobol_lsp/lsp_request.ml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ let handle_references state (params: ReferenceParams.t) =
let handle_range_formatting registry params =
let open DocumentRangeFormattingParams in
let { textDocument = doc; range = {start; end_}; _ } = params in
let Lsp_document.{ project; _ } = Lsp_server.find_document doc registry in
let Lsp_document.{ project; textdoc; _ } =
Lsp_server.find_document doc registry
in
let range_to_indent =
Cobol_indent.Type.{
start_line = start.line + 1;
Expand All @@ -180,7 +182,8 @@ let handle_range_formatting registry params =
~dialect:(Cobol_config.dialect project.cobol_config)
~source_format:project.source_format
~indent_config:None
~file:(Lsp.Uri.to_path doc.uri)
~filename:(Lsp.Uri.to_path doc.uri)
~contents:(Lsp.Text_document.text textdoc)
~range:(Some range_to_indent)
in
Some [TextEdit.create ~newText ~range]
Expand All @@ -198,14 +201,14 @@ let handle_formatting registry params =
~start:(Position.create ~character:0 ~line:0)
~end_:(Position.create ~character:width ~line:length)
in
let path = Lsp.Uri.to_path doc.uri in
try
let newText =
Cobol_indent.indent_range'
~dialect:(Cobol_config.dialect project.cobol_config)
~source_format:project.source_format
~indent_config:None
~file:path
~filename:(Lsp.Uri.to_path doc.uri)
~contents:(Lsp.Text_document.text textdoc)
~range:None
in
Some [TextEdit.create ~newText ~range:edit_range]
Expand Down