Skip to content

Commit 6d49dcf

Browse files
committed
Nits/Diff command that doesn't req regex
1 parent 7cdfa4e commit 6d49dcf

File tree

1 file changed

+17
-34
lines changed

1 file changed

+17
-34
lines changed

clang/tools/clang-format/clang-format.el

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,6 @@ is a zero-based file offset, assuming ‘utf-8-unix’ coding."
146146
(lambda (byte &optional _quality _coding-system)
147147
(byte-to-position (1+ byte)))))
148148

149-
(defun clang-format--vc-diff-match-diff-line (line)
150-
;; We are matching something like:
151-
;; "@@ -80 +80 @@" or "@@ -80,2 +80,2 @@"
152-
;; Return as "<LineStart>:<LineEnd>"
153-
(when (string-match "^@@\s-[0-9,]+\s\\+\\([0-9]+\\)\\(,\\([0-9]+\\)\\)?\s@@$" line)
154-
;; If we have multi-line diff
155-
(if (match-string 3 line)
156-
(concat (match-string 1 line)
157-
":"
158-
(number-to-string
159-
(+ (string-to-number (match-string 1 line))
160-
(string-to-number (match-string 3 line)))))
161-
(concat (match-string 1 line) ":" (match-string 1 line)))))
162-
163149
(defun clang-format--vc-diff-get-diff-lines (file-orig file-new)
164150
"Return all line regions that contain diffs between FILE-ORIG and
165151
FILE-NEW. If there is no diff ‘nil’ is returned. Otherwise the
@@ -169,7 +155,7 @@ which can be passed directly to ‘clang-format’."
169155
(with-temp-buffer
170156
;; We could use diff.el:diff-no-select here. The reason we don't
171157
;; is diff-no-select requires extra copies on the buffers which
172-
;; induces noticable slowdowns, especially on larger files.
158+
;; induces noticeable slowdowns, especially on larger files.
173159
(let ((status (call-process
174160
diff-command
175161
nil
@@ -178,35 +164,32 @@ which can be passed directly to ‘clang-format’."
178164
;; Binary diff has different behaviors that we
179165
;; aren't interested in.
180166
"-a"
181-
;; Get minimal diff (copy diff config for git-clang-format).
182-
"-U0"
167+
;; Print new lines in file-new formatted as
168+
;; "--lines=<StartDiff:EndDiff> "
169+
"--changed-group-format=%(N=0?:--lines=%dF:%dM )"
170+
;; Don't print anything for unchanged lines
171+
"--unchanged-group-format="
183172
file-orig
184173
file-new))
185174
(stderr (concat (if (zerop (buffer-size)) "" ": ")
186175
(buffer-substring-no-properties
187-
(point-min) (line-end-position))))
188-
(diff-lines '()))
176+
(point-min) (line-end-position)))))
189177
(cond
190178
((stringp status)
191179
(error "(diff killed by signal %s%s)" status stderr))
192180
;; Return of 0 indicates no diff.
193181
((= status 0) nil)
194182
;; Return of 1 indicates found diffs and no error.
195183
((= status 1)
196-
;; Iterate through all lines in diff buffer and collect all
197-
;; lines in current buffer that have a diff.
198-
(goto-char (point-min))
199-
(while (not (eobp))
200-
(let ((diff-line (clang-format--vc-diff-match-diff-line
201-
(buffer-substring-no-properties
202-
(line-beginning-position)
203-
(line-end-position)))))
204-
(when diff-line
205-
;; Create list line regions with diffs to pass to
206-
;; clang-format.
207-
(push (concat "--lines=" diff-line) diff-lines)))
208-
(forward-line 1))
209-
(reverse diff-lines))
184+
;; We had our diff command printout all diffs as
185+
;; "--lines=S0:E0 --lines=S1:E1 ... --lines=SN:EN " so just
186+
;; split the output into a list to pass to clang-format.
187+
(split-string
188+
(buffer-substring-no-properties (point-min) (point-max))
189+
;; All whitespace (practically only spaces).
190+
"[ \f\t\n\r\v]"
191+
;; Don't create empty entries.
192+
t))
210193
;; Any return != 0 && != 1 indicates some level of error.
211194
(t
212195
(error "(diff returned unsuccessfully %s%s)" status stderr))))))
@@ -235,7 +218,7 @@ supported vc."
235218
(let ((status (call-process
236219
vc-git-program
237220
nil
238-
`(:file, tmpfile-vc-head)
221+
`(:file ,tmpfile-vc-head)
239222
nil
240223
"show" (concat "HEAD:" vc-file-name)))
241224
(stderr (with-temp-buffer

0 commit comments

Comments
 (0)