Skip to content

Commit

Permalink
Merge branch 'master' into superbol-project
Browse files Browse the repository at this point in the history
  • Loading branch information
nberth committed Oct 27, 2023
2 parents 2c941cd + 9faae1b commit 02429e2
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 55 deletions.
97 changes: 95 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"description": "If something is selected, only format the selection"
},
"superbol.path": {
"default": "",
"default": "superbol-free",
"description": "Path to the `superbol` command"
}
}
Expand Down Expand Up @@ -74,6 +74,74 @@
]
}
],
"problemMatchers": [
{
"name": "gnucobol",
"owner": "cobol",
"fileLocation": "absolute",
"pattern": "$gnucobol",
"source": "GnuCOBOL"
},
{
"name": "gnucobol-warning",
"owner": "cobol",
"fileLocation": "absolute",
"pattern": "$gnucobol-warning",
"source": "GnuCOBOL",
"severity": "warning"
},
{
"name": "gnucobol-error",
"owner": "cobol",
"fileLocation": "absolute",
"pattern": "$gnucobol-error",
"source": "GnuCOBOL",
"severity": "error"
},
{
"name": "gnucobol-note",
"owner": "cobol",
"fileLocation": "absolute",
"pattern": "$gnucobol-note",
"source": "GnuCOBOL",
"severity": "info"
}
],
"problemPatterns": [
{
"name": "gnucobol",
"regexp": "^(.*): ?(\\d+): (error|warning): ([^[]*)(\\[(.*)\\])?$",
"file": 1,
"line": 2,
"severity": 3,
"code": 6,
"message": 4
},
{
"name": "gnucobol-warning",
"regexp": "^(.*):(\\d+):\\s?(warning|Warnung|[wW]aarschuwing|[aA]lerta|avertissement|упозорење)\\s?:([^[]*)(\\[(.*)\\])?$",
"file": 1,
"line": 2,
"code": 6,
"message": 4
},
{
"name": "gnucobol-error",
"regexp": "^(.*): ?(\\d+):\\s?(error|Fehler|[fF]out|[eE]rrores|[eE]rrores|erreur|грешка)\\s?:\\s?([^[]*)(\\[(.*)\\])?$",
"file": 1,
"line": 2,
"code": 6,
"message": 4
},
{
"name": "gnucobol-note",
"regexp": "^(.*): ?(\\d+): (note|Anmerkung|[nN]ota): ([^[]*)(\\[(.*)\\])?$",
"file": 1,
"line": 2,
"code": 6,
"message": 4
}
],
"taskDefinitions": [
{
"type": "superbol",
Expand All @@ -83,7 +151,32 @@
"description": "The list of copybooks paths"
},
"sourceFormat": {
"description": "The source format of the code"
"description": "The source format of the code",
"items": "string",
"enum": [
"default",
"gnucobol",
"cobol85",
"cobol2002",
"cobol2014",
"acu",
"acu-strict",
"bs2000",
"bs2000-strict",
"gcos",
"gcos-strict",
"ibm",
"ibm-strict",
"mf",
"mf-strict",
"mvs",
"mvs-strict",
"realia",
"realia-strict",
"rm",
"rm-strict",
"xopen"
]
},
"dialect": {
"description": "The COBOL dialect used"
Expand Down
14 changes: 6 additions & 8 deletions src/lsp/cobol_indent/cobol_indent.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +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 =
indent_range' ~dialect ~source_format ~range:None ~indent_config ~file
|> 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
|> Fmt.pr "%s"
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"
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
53 changes: 33 additions & 20 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,30 +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 ~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 =
(* 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
~skip_compiler_directives_text:false
~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 { contents = file_content; filename = file; })
{ scope = BEGIN; context = []; acc = []; range }
(String { filename; contents })
{ src_format; 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: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 ~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
5 changes: 3 additions & 2 deletions src/lsp/cobol_indent/indenter.mli
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
(**************************************************************************)

(*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
-> range:Indent_type.range option
-> file:string
-> filename:string
-> contents:string
-> string
15 changes: 9 additions & 6 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 @@ -176,11 +178,12 @@ let handle_range_formatting registry params =
}
in
let newText =
Cobol_indent.indent_range'
Cobol_indent.indent_range
~dialect:(Cobol_config.dialect project.config.cobol_config)
~source_format:project.config.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'
Cobol_indent.indent_range
~dialect:(Cobol_config.dialect project.config.cobol_config)
~source_format:project.config.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
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
Loading

0 comments on commit 02429e2

Please sign in to comment.