Commit
Author: Michael Davis [mcarsondavis@gmail.com]
Hash: 16d01ac5f22eb48f4c7b726a421bdc72ed4d4051
Timestamp: Sun, 30 Jul 2023 19:36:04 +0000 (1 year ago)

+179 -120 +/-3 browse
Organize changes into hunks
Organize changes into hunks

This introduces some hierarchy for the nodes so that each
'diff --git a b' command is a block which can have 'hunks'. Each 'hunk'
has a 'location' and a set of 'changes' under it which contain the
'addition's, 'deletion's and 'context' nodes.
1diff --git a/grammar.js b/grammar.js
2index f6337a0..278ccee 100644
3--- a/grammar.js
4+++ b/grammar.js
5 @@ -9,11 +9,13 @@ module.exports = grammar({
6
7 rules: {
8 source: ($) =>
9- seq(repeat(seq(optional($._line), NEWLINE)), optional($._line)),
10+ seq(
11+ repeat(choice($.block, seq(optional($._line), NEWLINE))),
12+ optional($._line)
13+ ),
14
15 _line: ($) =>
16 choice(
17- $.command,
18 $.file_change,
19 $.binary_change,
20 $.index,
21 @@ -26,6 +28,42 @@ module.exports = grammar({
22 $.context
23 ),
24
25+ block: ($) =>
26+ prec.right(
27+ seq(
28+ $.command,
29+ NEWLINE,
30+ repeat(
31+ seq(
32+ choice($.file_change, $.binary_change, $.index, $.similarity),
33+ NEWLINE
34+ )
35+ ),
36+ optional(seq($.old_file, NEWLINE, $.new_file, NEWLINE, $.hunks))
37+ )
38+ ),
39+
40+ hunks: ($) => prec.right(repeat1($.hunk)),
41+
42+ hunk: ($) =>
43+ prec.right(
44+ seq(
45+ field("location", $.location),
46+ NEWLINE,
47+ optional(field("changes", $.changes))
48+ )
49+ ),
50+
51+ changes: ($) =>
52+ prec.right(
53+ repeat1(
54+ seq(
55+ choice($.addition, $.deletion, $.context),
56+ prec.right(repeat1(NEWLINE))
57+ )
58+ )
59+ ),
60+
61 // FIXME: remove git assumption
62 command: ($) => iseq("diff", "--git", $.filename),
63
64 diff --git a/test/corpus/binary.txt b/test/corpus/binary.txt
65index 7698b66..08114ae 100644
66--- a/test/corpus/binary.txt
67+++ b/test/corpus/binary.txt
68 @@ -8,15 +8,16 @@ Binary files a/tree-sitter-gitdiff.wasm and b/tree-sitter-gitdiff.wasm differ
69 --------------------------------------------------------------------------------
70
71 (source
72- (command
73- (filename))
74- (index
75- (commit)
76- (commit)
77- (mode))
78- (binary_change
79- (filename)
80- (filename)))
81+ (block
82+ (command
83+ (filename))
84+ (index
85+ (commit)
86+ (commit)
87+ (mode))
88+ (binary_change
89+ (filename)
90+ (filename))))
91
92 ================================================================================
93 Deleted binary file
94 @@ -29,13 +30,14 @@ Binary files a/docs/playground.png and /dev/null differ
95 --------------------------------------------------------------------------------
96
97 (source
98- (command
99- (filename))
100- (file_change
101- (mode))
102- (index
103- (commit)
104- (commit))
105- (binary_change
106- (filename)
107- (filename)))
108+ (block
109+ (command
110+ (filename))
111+ (file_change
112+ (mode))
113+ (index
114+ (commit)
115+ (commit))
116+ (binary_change
117+ (filename)
118+ (filename))))
119 diff --git a/test/corpus/text.txt b/test/corpus/text.txt
120index 82e0a40..bc4049c 100644
121--- a/test/corpus/text.txt
122+++ b/test/corpus/text.txt
123 @@ -19,27 +19,31 @@ index dc36969..f37fde0 100644
124 --------------------------------------------------------------------------------
125
126 (source
127- (command
128- (filename))
129- (index
130- (commit)
131- (commit)
132- (mode))
133- (old_file
134- (filename))
135- (new_file
136- (filename))
137- (location
138- (linerange)
139- (linerange))
140- (context)
141- (context)
142- (deletion)
143- (addition)
144- (addition)
145- (addition)
146- (context)
147- (context))
148+ (block
149+ (command
150+ (filename))
151+ (index
152+ (commit)
153+ (commit)
154+ (mode))
155+ (old_file
156+ (filename))
157+ (new_file
158+ (filename))
159+ (hunks
160+ (hunk
161+ (location
162+ (linerange)
163+ (linerange))
164+ (changes
165+ (context)
166+ (context)
167+ (deletion)
168+ (addition)
169+ (addition)
170+ (addition)
171+ (context)
172+ (context))))))
173
174 ================================================================================
175 New empty file
176 @@ -51,13 +55,14 @@ index 0000000..e69de29
177 --------------------------------------------------------------------------------
178
179 (source
180- (command
181- (filename))
182- (file_change
183- (mode))
184- (index
185- (commit)
186- (commit)))
187+ (block
188+ (command
189+ (filename))
190+ (file_change
191+ (mode))
192+ (index
193+ (commit)
194+ (commit))))
195
196 ================================================================================
197 Deleted empty file
198 @@ -69,13 +74,14 @@ index e69de29..0000000
199 --------------------------------------------------------------------------------
200
201 (source
202- (command
203- (filename))
204- (file_change
205- (mode))
206- (index
207- (commit)
208- (commit)))
209+ (block
210+ (command
211+ (filename))
212+ (file_change
213+ (mode))
214+ (index
215+ (commit)
216+ (commit))))
217
218 ================================================================================
219 File rename
220 @@ -88,13 +94,14 @@ rename to tmp.md
221 --------------------------------------------------------------------------------
222
223 (source
224- (command
225- (filename))
226- (similarity)
227- (file_change
228- (filename))
229- (file_change
230- (filename)))
231+ (block
232+ (command
233+ (filename))
234+ (similarity)
235+ (file_change
236+ (filename))
237+ (file_change
238+ (filename))))
239
240 ================================================================================
241 Location with no context
242 @@ -123,22 +130,26 @@ index 00000000..ee9808dc
243 --------------------------------------------------------------------------------
244
245 (source
246- (command
247- (filename))
248- (file_change
249- (mode))
250- (index
251- (commit)
252- (commit))
253- (old_file
254- (filename))
255- (new_file
256- (filename))
257- (location
258- (linerange)
259- (linerange))
260- (addition)
261- (context))
262+ (block
263+ (command
264+ (filename))
265+ (file_change
266+ (mode))
267+ (index
268+ (commit)
269+ (commit))
270+ (old_file
271+ (filename))
272+ (new_file
273+ (filename))
274+ (hunks
275+ (hunk
276+ (location
277+ (linerange)
278+ (linerange))
279+ (changes
280+ (addition)
281+ (context))))))
282
283 ================================================================================
284 Just a file declaration and no newline
285 @@ -184,47 +195,55 @@ index 321c90a..b4a5cba 100644
286 --------------------------------------------------------------------------------
287
288 (source
289- (command
290- (filename))
291- (index
292- (commit)
293- (commit)
294- (mode))
295- (old_file
296- (filename))
297- (new_file
298- (filename))
299- (location
300- (linerange)
301- (linerange))
302- (context)
303- (context)
304- (context)
305- (deletion)
306- (addition)
307- (addition)
308- (context)
309- (command
310- (filename))
311- (index
312- (commit)
313- (commit)
314- (mode))
315- (old_file
316- (filename))
317- (new_file
318- (filename))
319- (location
320- (linerange)
321- (linerange))
322- (context)
323- (context)
324- (addition)
325- (addition)
326- (addition)
327- (addition)
328- (addition)
329- (addition))
330+ (block
331+ (command
332+ (filename))
333+ (index
334+ (commit)
335+ (commit)
336+ (mode))
337+ (old_file
338+ (filename))
339+ (new_file
340+ (filename))
341+ (hunks
342+ (hunk
343+ (location
344+ (linerange)
345+ (linerange))
346+ (changes
347+ (context)
348+ (context)
349+ (context)
350+ (deletion)
351+ (addition)
352+ (addition)
353+ (context)))))
354+ (block
355+ (command
356+ (filename))
357+ (index
358+ (commit)
359+ (commit)
360+ (mode))
361+ (old_file
362+ (filename))
363+ (new_file
364+ (filename))
365+ (hunks
366+ (hunk
367+ (location
368+ (linerange)
369+ (linerange))
370+ (changes
371+ (context)
372+ (context)
373+ (addition)
374+ (addition)
375+ (addition)
376+ (addition)
377+ (addition)
378+ (addition))))))
379
380 ================================================================================
381 Leading dash deletions