diff --git a/src/lsp/cobol_indent/cobol_indent.ml b/src/lsp/cobol_indent/cobol_indent.ml index ad15b12d0..21e32576d 100644 --- a/src/lsp/cobol_indent/cobol_indent.ml +++ b/src/lsp/cobol_indent/cobol_indent.ml @@ -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" diff --git a/src/lsp/cobol_indent/indenter.ml b/src/lsp/cobol_indent/indenter.ml index 135129811..766628fe0 100644 --- a/src/lsp/cobol_indent/indenter.ml +++ b/src/lsp/cobol_indent/indenter.ml @@ -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 = 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 diff --git a/src/lsp/cobol_indent/indenter.mli b/src/lsp/cobol_indent/indenter.mli index c6f62793e..a2d1d10c0 100644 --- a/src/lsp/cobol_indent/indenter.mli +++ b/src/lsp/cobol_indent/indenter.mli @@ -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 diff --git a/src/lsp/cobol_lsp/lsp_request.ml b/src/lsp/cobol_lsp/lsp_request.ml index b80534384..52de0eb57 100644 --- a/src/lsp/cobol_lsp/lsp_request.ml +++ b/src/lsp/cobol_lsp/lsp_request.ml @@ -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; @@ -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] @@ -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]