Skip to content

Commit 3c70f52

Browse files
committed
Use simple diff to output xform result.
1 parent 560591e commit 3c70f52

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

analysis/src/Xform.ml

+30-1
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,40 @@ let parse ~filename =
111111
in
112112
(structure, print)
113113

114+
let diff ~filename ~newContents =
115+
match Files.readFile ~filename with
116+
| None -> assert false
117+
| Some oldContents ->
118+
let rec findFirstLineDifferent n old new_ =
119+
match (old, new_) with
120+
| old1 :: oldRest, new1 :: newRest ->
121+
if old1 = new1 then findFirstLineDifferent (n + 1) oldRest newRest
122+
else (n, old, new_)
123+
| _ -> (n, old, new_)
124+
in
125+
let oldLines = String.split_on_char '\n' oldContents in
126+
let newLines = String.split_on_char '\n' newContents in
127+
let firstLineDifferent, old, new_ =
128+
findFirstLineDifferent 0 oldLines newLines
129+
in
130+
let firstLineR, _oldR, newR =
131+
findFirstLineDifferent 0 (List.rev old) (List.rev new_)
132+
in
133+
let lastLineEqual = firstLineDifferent + List.length old - firstLineR in
134+
let newLines = List.rev newR in
135+
(firstLineDifferent, lastLineEqual, newLines)
136+
114137
let command ~path ~pos =
115138
if Filename.check_suffix path ".res" then
116139
let structure, print = parse ~filename:path in
117140
match IfThenElse.xform ~pos structure with
118141
| None -> ()
119142
| Some newStructure ->
120143
let formatted = print newStructure in
121-
Printf.printf "Hit IfThenElse. Formatted:\n%s" formatted
144+
let firstLineDifferent, lastLineEqual, newLines =
145+
diff ~filename:path ~newContents:formatted
146+
in
147+
Printf.printf
148+
"Hit IfThenElse firstLineDifferent:%d lastLineEqual:%d newLines:\n%s\n"
149+
firstLineDifferent lastLineEqual
150+
(newLines |> String.concat "\n")

analysis/tests/src/expected/Xform.res.txt

+2-25
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,13 @@
11
Xform tests/src/Xform.res 6:5
2-
Hit IfThenElse. Formatted:
3-
type kind = First | Second | Third
4-
type r = {name: string, age: int}
5-
6-
let ret = _ => assert false
7-
let kind = assert false
8-
2+
Hit IfThenElse firstLineDifferent:6 lastLineEqual:11 newLines:
93
switch kind {
104
| First =>
115
// ^xfm
126
ret("First")
137
| _ => ret("Not First")
14-
}
15-
16-
#kind("First", {name: "abc", age: 3}) != kind ? ret("Not First") : ret("First")
17-
// ^xfm
188

199
Xform tests/src/Xform.res 13:15
20-
Hit IfThenElse. Formatted:
21-
type kind = First | Second | Third
22-
type r = {name: string, age: int}
23-
24-
let ret = _ => assert false
25-
let kind = assert false
26-
27-
if kind == First {
28-
// ^xfm
29-
ret("First")
30-
} else {
31-
ret("Not First")
32-
}
33-
10+
Hit IfThenElse firstLineDifferent:13 lastLineEqual:15 newLines:
3411
switch kind {
3512
| #kind("First", {name: "abc", age: 3}) => ret("First")
3613
// ^xfm

0 commit comments

Comments
 (0)