Skip to content

Commit

Permalink
use ocp-indent api for Partial_ast.indent
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot committed Mar 30, 2021
1 parent d0feffc commit 56a34fb
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 120 deletions.
4 changes: 2 additions & 2 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
(>= 20200624))
(menhirSdk
(>= 20200624))
(ocp-indent :with-test)
ocp-indent
(bisect_ppx
(and
:with-test
Expand Down Expand Up @@ -100,7 +100,7 @@
(>= 20200624))
(menhirSdk
(>= 20200624))
(ocp-indent :with-test)
ocp-indent
(bisect_ppx
(and
:with-test
Expand Down
34 changes: 12 additions & 22 deletions lib/Indent.ml
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,16 @@ module Valid_ast = struct
end

module Partial_ast = struct
let indent_line ?prev ~i ~line nlines =
if i = nlines + 1 then 0
else
let indent = String.(length line - length (lstrip line)) in
match prev with
| Some (prev_indent, prev_line) -> (
match last_token prev_line with
| Some last_tok -> (
let first_tok = first_token prev_line in
match Source.indent_after_token last_tok with
| Some i -> prev_indent + i
| None -> (
let tok = first_token line in
match
Source.indent_before_token ~start_prev_line:first_tok tok
with
| Some i -> max 0 (prev_indent + i)
| None -> indent ) )
| None -> indent )
| _ -> indent

let indent_range ~lines ~range = indent_aux ~lines ~range ~indent_line
let indent_range ~source ~range:(low, high) =
let config, _, _ = IndentConfig.local_default () in
let output : _ IndentPrinter.output =
{ debug= false
; config
; in_lines= (fun i -> low <= i && i <= high)
; adaptive= true
; indent_empty= false
; kind= Numeric (fun i acc -> i :: acc) }
in
let stream = Nstream.of_string source in
List.rev @@ IndentPrinter.proceed output stream IndentBlock.empty []
end
4 changes: 1 addition & 3 deletions lib/Indent.mli
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ module Valid_ast : sig
end

module Partial_ast : sig
val indent_line : ?prev:int * string -> i:int -> line:string -> int -> int

val indent_range : lines:string list -> range:int * int -> int list
val indent_range : source:string -> range:int * int -> int list
end
2 changes: 1 addition & 1 deletion lib/Translation_unit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ let numeric (type a b) (fg0 : a list Ast_passes.Ast0.t)
check_range nlines range
>>| fun () ->
Ocaml_common.Location.input_name := input_name ;
let fallback () = Indent.Partial_ast.indent_range ~lines ~range in
let fallback () = Indent.Partial_ast.indent_range ~source ~range in
let indent_parsed ({ast= parsed_ast; source= parsed_source; _} as parsed)
source range =
match
Expand Down
5 changes: 3 additions & 2 deletions lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@
(library
(name ocamlformat_lib)
(flags
(:standard -open Base -open Import -open Compat))
(:standard -open Base -open Import -open Ocamlformat_compat.Compat))
(instrumentation
(backend bisect_ppx))
(libraries
format_
import
ocaml-version
ocp-indent.lib
odoc.model
odoc.parser
parse_wyc
re
uuseg
uuseg.string
token_latest
compat
ocamlformat_compat
dune-build-info
ppxlib))
2 changes: 1 addition & 1 deletion ocamlformat-rpc.opam
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ depends: [
"menhir" {>= "20180528"}
"menhirLib" {>= "20200624"}
"menhirSdk" {>= "20200624"}
"ocp-indent" {with-test}
"ocp-indent"
"bisect_ppx" {with-test & >= "2.5.0"}
"odoc" {>= "1.4.2"}
"ppxlib" {>= "0.22.0"}
Expand Down
2 changes: 1 addition & 1 deletion ocamlformat.opam
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ depends: [
"menhir" {>= "20180528"}
"menhirLib" {>= "20200624"}
"menhirSdk" {>= "20200624"}
"ocp-indent" {with-test}
"ocp-indent"
"bisect_ppx" {with-test & >= "2.5.0"}
"odoc" {>= "1.4.2"}
"ppxlib" {>= "0.22.0"}
Expand Down
2 changes: 1 addition & 1 deletion test/passing/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1947,7 +1947,7 @@
(deps .ocamlformat )
(action
(with-outputs-to partial.ml.output
(run %{bin:ocamlformat} --numeric=1-14 %{dep:partial.ml}))))
(run %{bin:ocamlformat} --numeric=2-14 %{dep:partial.ml}))))

(rule
(alias runtest)
Expand Down
8 changes: 4 additions & 4 deletions test/passing/format_invalid_files_with_locations.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
0
0
0
21
21
0
0
0
0
0
21
21
2 changes: 1 addition & 1 deletion test/passing/partial.ml.opts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
--numeric=1-14
--numeric=2-14
1 change: 0 additions & 1 deletion test/passing/partial.ml.ref
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
3
5
5
5
Expand Down
83 changes: 9 additions & 74 deletions test/unit/test_indent.ml
Original file line number Diff line number Diff line change
@@ -1,81 +1,16 @@
open Base
open Ocamlformat_lib

module Partial_ast = struct
let get_indent line = String.(length line - length (lstrip line))

let get_inputs input =
let lines = String.split_lines input in
let nb_lines = List.length lines in
match List.last lines with
| Some last -> (Some (get_indent last, last), nb_lines, "", nb_lines)
| None -> (None, 0, "", nb_lines)

let prepare_output input indent =
let spaces = String.make indent ' ' in
input ^ "\n" ^ spaces ^ "^"

let tests_indent_line =
let test name ~input ~expected =
let test_name = "Partial_ast.indent_line: " ^ name in
( test_name
, `Quick
, fun () ->
let prev, i, line, nb_lines = get_inputs input in
let indent =
Indent.Partial_ast.indent_line ?prev ~i ~line nb_lines
in
let output = prepare_output input indent in
Alcotest.(check string) test_name expected output )
in
[ test "empty" ~input:{||} ~expected:{|
^|}
; test "after if" ~input:{|
if|} ~expected:{|
if
^|}
; test "after then" ~input:{|
let foo () =
if bar then|}
~expected:{|
let foo () =
if bar then
^|}
; test "after module struct" ~input:{|
module M = struct|}
~expected:{|
module M = struct
^|}
; test "after module sig" ~input:{|
module M : sig|}
~expected:{|
module M : sig
^|}
; test "after =" ~input:{|
let x =
let y =|}
~expected:{|
let x =
let y =
^|}
; test "after = but over indented"
~input:{|
let x =
let y =|}
~expected:{|
let x =
let y =
^|} ]

let tests_indent_range =
let test name ~input ~range ~expected =
let test name ~input:source ~range ~expected =
let test_name = "Partial_ast.indent_range: " ^ name in
( test_name
, `Quick
, fun () ->
let lines = String.split_lines input in
let indent = Indent.Partial_ast.indent_range ~lines ~range in
let output = Test_translation_unit.reindent ~lines ~range indent in
let indent = Indent.Partial_ast.indent_range ~source ~range in
let output =
Test_translation_unit.reindent ~source ~range indent
in
Alcotest.(check string) test_name expected output )
in
[ test "empty" ~input:"" ~range:(1, 1) ~expected:""
Expand Down Expand Up @@ -115,9 +50,9 @@ in
fooooooo
in
foooooooooooooooooooooooooo
foooooooooooooooooooo foooooooooooooooo fooooooooo
in|}
; test "partial let" ~range:(1, 14)
foooooooooooooooooooo foooooooooooooooo fooooooooo
in|}
; test "partial let" ~range:(2, 14)
~input:
{| let () =
ffff;
Expand Down Expand Up @@ -151,7 +86,7 @@ quot, rem
quot, rem|}
]

let tests = tests_indent_line @ tests_indent_range
let tests = tests_indent_range
end

let tests = Partial_ast.tests
21 changes: 16 additions & 5 deletions test/unit/test_translation_unit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ let test_parse_and_format_expression =
[ make_test "List.map" ~input:"List.map (fun x->\nx*x) [(1 + 9); 2;3] "
~expected:(Ok "List.map (fun x -> x * x) [ 1 + 9; 2; 3 ]\n") ]

let reindent ~lines ~range:(low, high) indents =
let reindent ~source ~range:(low, high) indents =
let lines = String.split_lines source in
let low = low - 1 and high = high - 1 in
String.concat ~sep:"\n"
@@ List.mapi lines ~f:(fun i line ->
Expand All @@ -139,10 +140,9 @@ let test_numeric =
, fun () ->
let opts = Conf.{debug= false; margin_check= false} in
let got =
let lines = String.split_lines source in
Translation_unit.numeric Use_file ~input_name:"_" ~source ~range
Conf.default_profile opts
|> Result.map ~f:(reindent ~lines ~range)
|> Result.map ~f:(reindent ~source ~range)
in
Alcotest.check Alcotest.(result string errmsg) test_name expected got
)
Expand Down Expand Up @@ -286,7 +286,7 @@ foooooo|}
in
list_fl grps fmt_grp|}
)
; make_test "partial let" ~range:(1, 14)
; make_test "partial let" ~range:(2, 14)
~source:
{| let () =
ffff;
Expand Down Expand Up @@ -336,6 +336,9 @@ let read_file f = Stdio.In_channel.with_file f ~f:Stdio.In_channel.input_all
let test_numeric_file =
let fmt_ast_source = read_file "../../lib/Fmt_ast.ml" in
let partial_source = read_file "../passing/partial.ml" in
let inv_with_loc_source =
read_file "../passing/format_invalid_files_with_locations.ml"
in
let make_test name ~source ~range ~expected =
let test_name = "numeric (file): " ^ name in
( test_name
Expand All @@ -355,7 +358,15 @@ let test_numeric_file =
; make_test "fmt_ast.ml (111)" ~source:fmt_ast_source ~range:(111, 111)
~expected:(Ok [6])
; make_test "partial.ml (1-14)" ~source:partial_source ~range:(1, 14)
~expected:(Ok [3; 5; 5; 5; 5; 7; 9; 9; 9; 7; 7; 7; 0; 7]) ]
~expected:(Ok [0; 2; 2; 2; 2; 4; 6; 6; 6; 4; 4; 4; 0; 4])
; make_test "partial.ml (2-14)" ~source:partial_source ~range:(2, 14)
~expected:(Ok [5; 5; 5; 5; 7; 9; 9; 9; 7; 7; 7; 0; 7])
; make_test "format_invalid_files_with_locations.ml (7-11)" ~range:(7, 11)
~source:inv_with_loc_source
~expected:(Ok [21; 21; 0; 21; 21])
; make_test "format_invalid_files_with_locations.ml (10-11)"
~range:(10, 11) ~source:inv_with_loc_source
~expected:(Ok [0; 0]) ]

let tests =
List.concat
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_translation_unit.mli
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
val tests : unit Alcotest.test_case list

val reindent : lines:string list -> range:int * int -> int list -> string
val reindent : source:string -> range:int * int -> int list -> string
2 changes: 1 addition & 1 deletion vendor/compat/dune
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
(run ./gen/gen.exe)))

(library
(name compat)
(name ocamlformat_compat)
(package ocamlformat)
(libraries base compiler-libs.common))

0 comments on commit 56a34fb

Please sign in to comment.