-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcodex-ide-diff-model.el
More file actions
285 lines (264 loc) · 10.4 KB
/
Copy pathcodex-ide-diff-model.el
File metadata and controls
285 lines (264 loc) · 10.4 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
;;; codex-ide-diff-model.el --- Parsed diff model helpers for codex-ide -*- lexical-binding: t; -*-
;; Copyright (C) 2026
;; Author: Duncan Gillis
;;; Commentary:
;; This module owns the parsed representation of Codex diff text. It keeps
;; section parsing, body-only file classification, diff stats, and source
;; location mapping separate from buffer rendering.
;;; Code:
(require 'cl-lib)
(require 'subr-x)
(defun codex-ide-diff-model-strip-path-prefix (path)
"Return PATH without a leading diff-side prefix."
(cond
((not (stringp path)) nil)
((or (string-prefix-p "a/" path)
(string-prefix-p "b/" path))
(substring path 2))
(t path)))
(defun codex-ide-diff-model-line-file-start-p
(lines index &optional git-only)
"Return non-nil when LINES at INDEX starts a file diff.
When GIT-ONLY is non-nil, only recognize `diff --git' headers."
(let ((line (nth index lines))
(next (nth (1+ index) lines)))
(or (and line (string-prefix-p "diff --git " line))
(and (not git-only)
line next
(string-prefix-p "--- " line)
(string-prefix-p "+++ " next)))))
(defun codex-ide-diff-model-path-from-diff-git-line (line)
"Return the new path from a diff --git LINE, or nil."
(when (string-match
(rx line-start "diff --git " (? "a/")
(+ (not (any " \n")))
(+ space) (? "b/")
(group (+ (not (any " \n")))))
line)
(codex-ide-diff-model-strip-path-prefix (match-string 1 line))))
(defun codex-ide-diff-model-path-from-header-line (line)
"Return the path from a --- or +++ header LINE, or nil."
(when (string-match
(rx line-start (or "---" "+++") (+ space)
(group (+ (not (any "\t\n")))))
line)
(let ((path (match-string 1 line)))
(unless (equal path "/dev/null")
(codex-ide-diff-model-strip-path-prefix path)))))
(defun codex-ide-diff-model--parse-file-section (lines start)
"Parse a file diff from LINES starting at START.
Return a cons of the parsed file plist and the next line index."
(let ((index start)
(line-count (length lines))
header-lines
hunks
path
old-path
old-null
new-null
(git-block (string-prefix-p "diff --git " (nth start lines))))
(while (and (< index line-count)
(not (and (> index start)
(codex-ide-diff-model-line-file-start-p
lines
index
git-block)))
(not (string-prefix-p "@@" (nth index lines))))
(let ((line (nth index lines)))
(push (cons index line) header-lines)
(cond
((string-prefix-p "diff --git " line)
(setq path (or (codex-ide-diff-model-path-from-diff-git-line line)
path)))
((string-prefix-p "--- " line)
(setq old-null
(or old-null
(string-match-p (rx line-start "---" (+ space) "/dev/null")
line)))
(setq old-path (or (codex-ide-diff-model-path-from-header-line line)
old-path)))
((string-prefix-p "+++ " line)
(setq new-null
(or new-null
(string-match-p (rx line-start "+++" (+ space) "/dev/null")
line)))
(setq path (or (codex-ide-diff-model-path-from-header-line line)
path))))
(setq index (1+ index))))
(while (and (< index line-count)
(not (codex-ide-diff-model-line-file-start-p
lines
index
git-block)))
(let ((line (nth index lines)))
(if (string-prefix-p "@@" line)
(let ((hunk-header (cons index line))
body-lines)
(setq index (1+ index))
(while (and (< index line-count)
(not (codex-ide-diff-model-line-file-start-p
lines
index
git-block))
(not (string-prefix-p "@@" (nth index lines))))
(push (cons index (nth index lines)) body-lines)
(setq index (1+ index)))
(push (list :header hunk-header
:lines (nreverse body-lines))
hunks))
(push (cons index line) header-lines)
(setq index (1+ index)))))
(cons (list :path (or path old-path "changes")
:old-path old-path
:old-null old-null
:new-null new-null
:body-only (null hunks)
:header-lines (nreverse header-lines)
:hunks (nreverse hunks))
index)))
(defun codex-ide-diff-model-parse-files (diff-text)
"Return parsed file sections from DIFF-TEXT."
(let ((lines (split-string diff-text "\n"))
(index 0)
files)
(while (< index (length lines))
(if (codex-ide-diff-model-line-file-start-p lines index)
(let ((parsed (codex-ide-diff-model--parse-file-section lines index)))
(push (car parsed) files)
(setq index (cdr parsed)))
(setq index (1+ index))))
(nreverse files)))
(defun codex-ide-diff-model-group-files-by-path (files)
"Return FILES grouped by display path while preserving hunk order."
(let (grouped)
(dolist (file files)
(let* ((path (plist-get file :path))
(existing
(cl-find path grouped
:key (lambda (candidate)
(plist-get candidate :path))
:test #'equal)))
(if existing
(setf (plist-get existing :hunks)
(append (plist-get existing :hunks)
(plist-get file :hunks)))
(push (copy-tree file) grouped))))
(nreverse grouped)))
(defun codex-ide-diff-model-ordinary-file-header-line-p (line)
"Return non-nil when LINE is redundant in the section diff view."
(or (string-prefix-p "diff --git " line)
(string-prefix-p "--- " line)
(string-prefix-p "+++ " line)
(string-prefix-p "index " line)))
(defun codex-ide-diff-model-body-only-line-count (file)
"Return visible body line count for body-only FILE."
(let ((count 0))
(dolist (indexed-line (plist-get file :header-lines))
(unless (codex-ide-diff-model-ordinary-file-header-line-p
(cdr indexed-line))
(setq count (1+ count))))
count))
(defun codex-ide-diff-model-body-only-side (file &optional directory)
"Return the side represented by body-only FILE, either `added' or `removed'."
(when (or (plist-get file :body-only)
(null (plist-get file :hunks)))
(cond
((plist-get file :old-null) 'added)
((plist-get file :new-null) 'removed)
((let ((path (plist-get file :path)))
(and (stringp path)
(not (string-empty-p path))
(file-exists-p
(expand-file-name path (or directory default-directory)))))
'added)
(t 'removed))))
(defun codex-ide-diff-model-file-stats (file &optional directory)
"Return a plist summarizing additions and deletions in parsed FILE."
(let ((added 0)
(removed 0))
(pcase (codex-ide-diff-model-body-only-side file directory)
('added
(setq added (codex-ide-diff-model-body-only-line-count file)))
('removed
(setq removed (codex-ide-diff-model-body-only-line-count file))))
(dolist (hunk (plist-get file :hunks))
(dolist (indexed-line (plist-get hunk :lines))
(let ((line (cdr indexed-line)))
(cond
((and (string-prefix-p "+" line)
(not (string-prefix-p "+++" line)))
(setq added (1+ added)))
((and (string-prefix-p "-" line)
(not (string-prefix-p "---" line)))
(setq removed (1+ removed)))))))
(list :path (plist-get file :path)
:added added
:removed removed
:changed (+ added removed))))
(defun codex-ide-diff-model--parse-new-start (hunk-line)
"Return the new-file start line from HUNK-LINE, or nil."
(when (string-match
(rx line-start "@@"
(+ (not (any "+")))
"+"
(group (+ digit)))
hunk-line)
(string-to-number (match-string 1 hunk-line))))
(defun codex-ide-diff-model-source-location-for-line (diff-text line-index)
"Return source location for zero-based LINE-INDEX in DIFF-TEXT.
The returned value is a plist containing `:path' and `:line', or nil when the
diff line has no corresponding source location."
(let ((lines (split-string diff-text "\n"))
current-path
current-new-line
location)
(cl-loop
for line in lines
for index from 0
until (> index line-index)
do
(cond
((string-match
(rx line-start "diff --git " (? "a/")
(+ (not (any " \n")))
(+ space) (? "b/")
(group (+ (not (any " \n")))))
line)
(setq current-path (codex-ide-diff-model-strip-path-prefix
(match-string 1 line)))
(setq current-new-line nil)
(when (= index line-index)
(setq location nil)))
((string-match
(rx line-start "+++" (+ space)
(group (+ (not (any "\n")))))
line)
(let ((path (match-string 1 line)))
(unless (equal path "/dev/null")
(setq current-path (codex-ide-diff-model-strip-path-prefix path))
(setq current-new-line 1)))
(when (= index line-index)
(setq location nil)))
((string-prefix-p "@@" line)
(setq current-new-line
(or (codex-ide-diff-model--parse-new-start line)
current-new-line))
(when (= index line-index)
(setq location nil)))
((and current-path current-new-line
(not (string-prefix-p "\\ No newline" line)))
(let ((target-line current-new-line))
(cond
((string-prefix-p "+" line)
(setq current-new-line (1+ current-new-line)))
((string-prefix-p "-" line)
nil)
(t
(setq current-new-line (1+ current-new-line))))
(when (= index line-index)
(setq location
(list :path current-path
:line (max 1 target-line))))))))
location))
(provide 'codex-ide-diff-model)
;;; codex-ide-diff-model.el ends here