-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDefinitionDiff.elm
More file actions
364 lines (286 loc) · 11.3 KB
/
DefinitionDiff.elm
File metadata and controls
364 lines (286 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
module UnisonShare.DefinitionDiff exposing (..)
import Code.Hash as Hash exposing (Hash)
import Code.Syntax.SyntaxConfig exposing (SyntaxConfig)
import Code.Syntax.SyntaxSegment as SyntaxSegment exposing (SyntaxSegment)
import Html exposing (Html, code, div, header, pre, span, text)
import Html.Attributes exposing (class, style)
import Json.Decode as Decode
import Json.Decode.Extra exposing (when)
import Json.Decode.Pipeline exposing (required, requiredAt)
import Lib.Decode.Helpers exposing (nonEmptyList, whenKindIs)
import List.Extra as ListE
import List.Nonempty as NEL
import UI.Tooltip as Tooltip
type alias DiffSyntaxSegments =
NEL.Nonempty SyntaxSegment
type DiffSegment
= Both DiffSyntaxSegments
| OneSided DiffSyntaxSegments
| AnnotationChange { segment : SyntaxSegment, fromHash : Hash, toHash : Hash }
| SegmentChange { from : SyntaxSegment, to : SyntaxSegment }
type DiffLine
= ChangedLine (List DiffSegment)
| UnchangedLine (List DiffSegment)
-- Spacer includes numLines such that we can avoid a jagged background
-- pattern when it spans over multiple lines by making it 1 tall DOM
-- element instead of small 1 line height elements
| Spacer { numLines : Int }
type alias DiffDetails =
{ type_ : DefinitionType
, left : List DiffLine
, right : List DiffLine
}
type alias MismatchedDetails =
{ type_ : DefinitionType
, left : DiffSyntaxSegments
, right : DiffSyntaxSegments
}
type DefinitionDiff
= Diff DiffDetails
| Mismatched MismatchedDetails
-- Definition Type
type DefinitionType
= Term
| Type
definitionTypeToString : DefinitionType -> String
definitionTypeToString type_ =
case type_ of
Term ->
"Term"
Type ->
"Type"
-- VIEW
viewSegments : SyntaxConfig msg -> String -> NEL.Nonempty SyntaxSegment.SyntaxSegment -> List (Html msg)
viewSegments syntaxConfig className segments =
segments
|> NEL.map (SyntaxSegment.view syntaxConfig)
|> NEL.map (\seg -> span [ class className ] [ seg ])
|> NEL.toList
viewTooltip : Html msg -> Tooltip.Tooltip msg
viewTooltip content =
Tooltip.rich content
|> Tooltip.tooltip
|> Tooltip.withArrow Tooltip.Start
viewDiffSegment : SyntaxConfig msg -> DiffSegment -> List (Html msg)
viewDiffSegment syntaxConfig segment =
let
viewSegment =
SyntaxSegment.view syntaxConfig
viewSegments_ className =
viewSegments syntaxConfig className
in
case segment of
Both segments ->
viewSegments_ "diff-segment both" segments
OneSided segments ->
viewSegments_ "diff-segment one-sided" segments
AnnotationChange change ->
[ viewTooltip
(div [ class "tooltip-changes-summary" ]
[ div [ class "hash-changed" ]
[ text "The hash changed"
, text " from "
, Hash.view change.fromHash
, text " to "
, Hash.view change.toHash
]
]
)
|> Tooltip.view
(span [ class "diff-segment annotation-change" ] [ viewSegment change.segment ])
]
SegmentChange { from, to } ->
[ viewTooltip
(div [ class "tooltip-changes-summary" ]
[ text "Changed from"
, code [] [ viewSegment from ]
]
)
|> Tooltip.view
(span [ class "diff-segment segment-change" ] [ viewSegment to ])
]
viewDiffLine : (DiffSegment -> List (Html msg)) -> String -> Int -> ( Maybe Int, DiffLine ) -> Html msg
viewDiffLine viewSeg changeIndicator gutterWidth ( ln, line ) =
let
gutter indicator =
span [ class "gutter" ]
[ span [ class "line-number" ]
[ text
(String.padLeft
gutterWidth
' '
(ln |> Maybe.map String.fromInt |> Maybe.withDefault "")
)
]
, text " "
, span [ class "change-indicator" ] [ text indicator ]
, text " "
]
in
case line of
ChangedLine segments ->
div [ class "diff-line changed-line" ]
[ gutter changeIndicator
, span [] (List.concatMap viewSeg segments)
]
UnchangedLine segments ->
div [ class "diff-line unchanged-line" ]
[ gutter " "
, span [] (List.concatMap viewSeg segments)
]
Spacer { numLines } ->
div
[ class "diff-line spacer-line"
, style "height" ("calc(var(--diff-line-height) * " ++ String.fromInt numLines ++ ")")
]
[]
viewDiff : (Bool -> SyntaxConfig msg) -> DiffDetails -> Html msg
viewDiff toSyntaxConfig { left, right } =
let
toGutterWidth len =
String.length (String.fromInt len)
toViewDiffSegment isNew =
viewDiffSegment (toSyntaxConfig isNew)
withLineNumbers diffLine ( i, lines ) =
case diffLine of
ChangedLine _ ->
( i + 1, lines ++ [ ( Just (i + 1), diffLine ) ] )
UnchangedLine _ ->
( i + 1, lines ++ [ ( Just (i + 1), diffLine ) ] )
Spacer _ ->
case ListE.unconsLast lines of
Just ( ( _, Spacer { numLines } ), lines_ ) ->
( i, lines_ ++ [ ( Nothing, Spacer { numLines = numLines + 1 } ) ] )
_ ->
( i, lines ++ [ ( Nothing, diffLine ) ] )
viewLeftDiffLine =
viewDiffLine (toViewDiffSegment False) "-" (toGutterWidth (List.length left))
viewRightDiffLine =
viewDiffLine (toViewDiffSegment False) "-" (toGutterWidth (List.length right))
before =
left
|> List.foldl withLineNumbers ( 0, [] )
|> Tuple.second
|> List.map viewLeftDiffLine
after =
right
|> List.foldl withLineNumbers ( 0, [] )
|> Tuple.second
|> List.map viewRightDiffLine
in
div [ class "diff-side-by-side" ]
[ pre [ class "monochrome diff-side left" ]
[ header [ class "diff-left-header" ] [ text "Before" ]
, code [] before
]
, pre [ class "monochrome diff-side right" ]
[ header [ class "diff-right-header" ] [ text "After" ]
, code [] after
]
]
view : (Bool -> SyntaxConfig msg) -> DefinitionDiff -> Html msg
view toSyntaxConfig defDiff =
case defDiff of
Diff details ->
div [] [ viewDiff toSyntaxConfig details ]
Mismatched { left, right } ->
div [ class "diff-side-by-side" ]
[ pre [ class "monochrome diff-side" ] [ code [] (viewSegments (toSyntaxConfig False) "mismatched old" left) ]
, pre [ class "monochrome diff-side" ] [ code [] (viewSegments (toSyntaxConfig True) "mismatched new" right) ]
]
-- DECODE
decodeDiffSyntaxSegments : Decode.Decoder DiffSyntaxSegments
decodeDiffSyntaxSegments =
nonEmptyList SyntaxSegment.decode
decodeSingleDiffSyntaxSegment : Decode.Decoder DiffSyntaxSegments
decodeSingleDiffSyntaxSegment =
Decode.map NEL.fromElement SyntaxSegment.decode
decodeSegment : Decode.Decoder DiffSegment
decodeSegment =
let
decodeDiffTag =
Decode.field "diffTag" Decode.string
decodeBoth =
Decode.succeed Both
|> required "elements" decodeDiffSyntaxSegments
decodeOneSided =
Decode.succeed OneSided
|> required "elements" decodeDiffSyntaxSegments
mkAnnotationChange segment fromHash toHash =
AnnotationChange
{ segment = segment
, fromHash = fromHash
, toHash = toHash
}
decodeAnnotationChange =
Decode.map3
mkAnnotationChange
(SyntaxSegment.decode_ { segmentField = "segment", annotationField = "fromAnnotation" })
(Decode.at [ "fromAnnotation", "contents" ] Hash.decode)
(Decode.at [ "toAnnotation", "contents" ] Hash.decode)
mkSegmentChange from to =
SegmentChange { from = from, to = to }
decodeSegmentChange =
Decode.map2
mkSegmentChange
(SyntaxSegment.decode_ { segmentField = "fromSegment", annotationField = "annotation" })
(SyntaxSegment.decode_ { segmentField = "toSegment", annotationField = "annotation" })
in
Decode.oneOf
[ when decodeDiffTag ((==) "both") decodeBoth
, when decodeDiffTag ((==) "oneSided") decodeOneSided
, when decodeDiffTag ((==) "annotationChange") decodeAnnotationChange
, when decodeDiffTag ((==) "segmentChange") decodeSegmentChange
]
decodeDiffLine : Decode.Decoder DiffLine
decodeDiffLine =
Decode.oneOf
[ whenKindIs "changed" (Decode.map ChangedLine (Decode.field "value" (Decode.list decodeSegment)))
, whenKindIs "unchanged" (Decode.map UnchangedLine (Decode.field "value" (Decode.list decodeSegment)))
-- The spacer numLines will be flatten later on
-- TODO: we should probably do the flattening and add line numbers during parsing...
, whenKindIs "spacer" (Decode.succeed (Spacer { numLines = 1 }))
]
decodeDiff : DefinitionType -> Decode.Decoder DefinitionDiff
decodeDiff definitionType =
let
mkDiff left right =
Diff
{ type_ = definitionType
, left = left
, right = right
}
in
Decode.succeed mkDiff
|> requiredAt [ "diff", "diff", "contents", "left" ] (Decode.list decodeDiffLine)
|> requiredAt [ "diff", "diff", "contents", "right" ] (Decode.list decodeDiffLine)
decodeMismatched : DefinitionType -> Decode.Decoder DefinitionDiff
decodeMismatched definitionType =
let
definitionKey =
case definitionType of
Term ->
"termDefinition"
Type ->
"typeDefinition"
mkMismatched left right =
Mismatched
{ type_ = definitionType
, left = left
, right = right
}
in
Decode.succeed mkMismatched
-- TODO: what about builtins?
|> requiredAt [ "left", definitionKey, "contents" ] decodeDiffSyntaxSegments
|> requiredAt [ "right", definitionKey, "contents" ] decodeDiffSyntaxSegments
decode : DefinitionType -> Decode.Decoder DefinitionDiff
decode definitionType =
let
decodeKind =
Decode.at [ "diff", "diffKind" ] Decode.string
in
Decode.oneOf
[ when decodeKind ((==) "diff") (decodeDiff definitionType)
, when decodeKind ((==) "mismatched") (decodeMismatched definitionType)
]