diff options
| -rw-r--r-- | lisp/vc/diff-mode.el | 8 | ||||
| -rw-r--r-- | test/data/vc/diff-mode/hello_emacs.c | 6 | ||||
| -rw-r--r-- | test/data/vc/diff-mode/hello_emacs_1.c | 1 | ||||
| -rw-r--r-- | test/data/vc/diff-mode/hello_world.c | 6 | ||||
| -rw-r--r-- | test/data/vc/diff-mode/hello_world_1.c | 1 | ||||
| -rw-r--r-- | test/lisp/vc/diff-mode-tests.el | 112 |
6 files changed, 131 insertions, 3 deletions
diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index 158489c97fe..607c7b583ed 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el | |||
| @@ -2391,6 +2391,7 @@ and the position in MAX." | |||
| 2391 | 2391 | ||
| 2392 | (defun diff-syntax-fontify (beg end) | 2392 | (defun diff-syntax-fontify (beg end) |
| 2393 | "Highlight source language syntax in diff hunk between BEG and END." | 2393 | "Highlight source language syntax in diff hunk between BEG and END." |
| 2394 | (remove-overlays beg end 'diff-mode 'syntax) | ||
| 2394 | (save-excursion | 2395 | (save-excursion |
| 2395 | (diff-syntax-fontify-hunk beg end t) | 2396 | (diff-syntax-fontify-hunk beg end t) |
| 2396 | (diff-syntax-fontify-hunk beg end nil))) | 2397 | (diff-syntax-fontify-hunk beg end nil))) |
| @@ -2402,9 +2403,10 @@ and the position in MAX." | |||
| 2402 | (defun diff-syntax-fontify-hunk (beg end old) | 2403 | (defun diff-syntax-fontify-hunk (beg end old) |
| 2403 | "Highlight source language syntax in diff hunk between BEG and END. | 2404 | "Highlight source language syntax in diff hunk between BEG and END. |
| 2404 | When OLD is non-nil, highlight the hunk from the old source." | 2405 | When OLD is non-nil, highlight the hunk from the old source." |
| 2405 | (remove-overlays beg end 'diff-mode 'syntax) | ||
| 2406 | (goto-char beg) | 2406 | (goto-char beg) |
| 2407 | (let* ((hunk (buffer-substring-no-properties beg end)) | 2407 | (let* ((hunk (buffer-substring-no-properties beg end)) |
| 2408 | ;; Trim a trailing newline to find hunk in diff-syntax-fontify-props | ||
| 2409 | ;; in diffs that have no newline at end of diff file. | ||
| 2408 | (text (string-trim-right (or (ignore-errors (diff-hunk-text hunk (not old) nil)) ""))) | 2410 | (text (string-trim-right (or (ignore-errors (diff-hunk-text hunk (not old) nil)) ""))) |
| 2409 | (line (if (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?") | 2411 | (line (if (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?") |
| 2410 | (if old (match-string 1) | 2412 | (if old (match-string 1) |
| @@ -2431,7 +2433,7 @@ When OLD is non-nil, highlight the hunk from the old source." | |||
| 2431 | (setq props (diff-syntax-fontify-props nil text line-nb t))) | 2433 | (setq props (diff-syntax-fontify-props nil text line-nb t))) |
| 2432 | ;; Get properties from the file | 2434 | ;; Get properties from the file |
| 2433 | (with-temp-buffer | 2435 | (with-temp-buffer |
| 2434 | (insert-file-contents file t) | 2436 | (insert-file-contents file) |
| 2435 | (setq props (diff-syntax-fontify-props file text line-nb))))) | 2437 | (setq props (diff-syntax-fontify-props file text line-nb))))) |
| 2436 | ;; Get properties from a cached revision | 2438 | ;; Get properties from a cached revision |
| 2437 | (let* ((buffer-name (format " *diff-syntax:%s.~%s~*" | 2439 | (let* ((buffer-name (format " *diff-syntax:%s.~%s~*" |
| @@ -2459,7 +2461,7 @@ When OLD is non-nil, highlight the hunk from the old source." | |||
| 2459 | (if (and file (file-exists-p file) (file-regular-p file)) | 2461 | (if (and file (file-exists-p file) (file-regular-p file)) |
| 2460 | ;; Try to get full text from the file | 2462 | ;; Try to get full text from the file |
| 2461 | (with-temp-buffer | 2463 | (with-temp-buffer |
| 2462 | (insert-file-contents file t) | 2464 | (insert-file-contents file) |
| 2463 | (setq props (diff-syntax-fontify-props file text line-nb))) | 2465 | (setq props (diff-syntax-fontify-props file text line-nb))) |
| 2464 | ;; Otherwise, get properties from the hunk alone | 2466 | ;; Otherwise, get properties from the hunk alone |
| 2465 | (with-temp-buffer | 2467 | (with-temp-buffer |
diff --git a/test/data/vc/diff-mode/hello_emacs.c b/test/data/vc/diff-mode/hello_emacs.c new file mode 100644 index 00000000000..c7ed7538c3a --- /dev/null +++ b/test/data/vc/diff-mode/hello_emacs.c | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | int main() | ||
| 3 | { | ||
| 4 | printf("Hello, Emacs!\n"); | ||
| 5 | return 0; | ||
| 6 | } | ||
diff --git a/test/data/vc/diff-mode/hello_emacs_1.c b/test/data/vc/diff-mode/hello_emacs_1.c new file mode 100644 index 00000000000..62145a6b44a --- /dev/null +++ b/test/data/vc/diff-mode/hello_emacs_1.c | |||
| @@ -0,0 +1 @@ | |||
| int main() { printf("Hello, Emacs!\n"); return 0; } \ No newline at end of file | |||
diff --git a/test/data/vc/diff-mode/hello_world.c b/test/data/vc/diff-mode/hello_world.c new file mode 100644 index 00000000000..dcbe06c6012 --- /dev/null +++ b/test/data/vc/diff-mode/hello_world.c | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | int main() | ||
| 3 | { | ||
| 4 | printf("Hello, World!\n"); | ||
| 5 | return 0; | ||
| 6 | } | ||
diff --git a/test/data/vc/diff-mode/hello_world_1.c b/test/data/vc/diff-mode/hello_world_1.c new file mode 100644 index 00000000000..606afb371cb --- /dev/null +++ b/test/data/vc/diff-mode/hello_world_1.c | |||
| @@ -0,0 +1 @@ | |||
| int main() { printf("Hello, World!\n"); return 0; } \ No newline at end of file | |||
diff --git a/test/lisp/vc/diff-mode-tests.el b/test/lisp/vc/diff-mode-tests.el index 4276974c2ac..8e690548f05 100644 --- a/test/lisp/vc/diff-mode-tests.el +++ b/test/lisp/vc/diff-mode-tests.el | |||
| @@ -21,7 +21,10 @@ | |||
| 21 | ;;; Code: | 21 | ;;; Code: |
| 22 | 22 | ||
| 23 | (require 'diff-mode) | 23 | (require 'diff-mode) |
| 24 | (require 'diff) | ||
| 24 | 25 | ||
| 26 | (defconst diff-mode-tests--datadir | ||
| 27 | (expand-file-name "test/data/vc/diff-mode" source-directory)) | ||
| 25 | 28 | ||
| 26 | (ert-deftest diff-mode-test-ignore-trailing-dashes () | 29 | (ert-deftest diff-mode-test-ignore-trailing-dashes () |
| 27 | "Check to make sure we successfully ignore trailing -- made by | 30 | "Check to make sure we successfully ignore trailing -- made by |
| @@ -199,5 +202,114 @@ youthfulness | |||
| 199 | (kill-buffer buf2) | 202 | (kill-buffer buf2) |
| 200 | (delete-directory temp-dir 'recursive)))))) | 203 | (delete-directory temp-dir 'recursive)))))) |
| 201 | 204 | ||
| 205 | (ert-deftest diff-mode-test-font-lock () | ||
| 206 | "Check font-locking of diff hunks." | ||
| 207 | (let ((default-directory diff-mode-tests--datadir) | ||
| 208 | (old "hello_world.c") | ||
| 209 | (new "hello_emacs.c") | ||
| 210 | (diff-buffer (get-buffer-create "*Diff*")) | ||
| 211 | (diff-font-lock-refine t) | ||
| 212 | (diff-font-lock-syntax t) | ||
| 213 | diff-beg) | ||
| 214 | (diff-no-select old new '("-u") 'no-async diff-buffer) | ||
| 215 | (with-current-buffer diff-buffer | ||
| 216 | (font-lock-ensure) | ||
| 217 | (narrow-to-region (progn (diff-hunk-next) | ||
| 218 | (setq diff-beg (diff-beginning-of-hunk))) | ||
| 219 | (diff-end-of-hunk)) | ||
| 220 | |||
| 221 | (should (equal-including-properties | ||
| 222 | (buffer-string) | ||
| 223 | #("@@ -1,6 +1,6 @@ | ||
| 224 | #include <stdio.h> | ||
| 225 | int main() | ||
| 226 | { | ||
| 227 | - printf(\"Hello, World!\\n\"); | ||
| 228 | + printf(\"Hello, Emacs!\\n\"); | ||
| 229 | return 0; | ||
| 230 | } | ||
| 231 | " | ||
| 232 | 0 15 (face diff-hunk-header) | ||
| 233 | 16 36 (face diff-context) | ||
| 234 | 36 48 (face diff-context) | ||
| 235 | 48 51 (face diff-context) | ||
| 236 | 51 52 (face diff-indicator-removed) | ||
| 237 | 52 81 (face diff-removed) | ||
| 238 | 81 82 (face diff-indicator-added) | ||
| 239 | 82 111 (face diff-added) | ||
| 240 | 111 124 (face diff-context) | ||
| 241 | 124 127 (face diff-context)))) | ||
| 242 | |||
| 243 | (should (equal (mapcar (lambda (o) | ||
| 244 | (list (- (overlay-start o) diff-beg) | ||
| 245 | (- (overlay-end o) diff-beg) | ||
| 246 | (append (and (overlay-get o 'diff-mode) | ||
| 247 | `(diff-mode ,(overlay-get o 'diff-mode))) | ||
| 248 | (and (overlay-get o 'face) | ||
| 249 | `(face ,(overlay-get o 'face)))))) | ||
| 250 | (sort (overlays-in (point-min) (point-max)) | ||
| 251 | (lambda (a b) (< (overlay-start a) (overlay-start b))))) | ||
| 252 | '((0 127 (diff-mode fine)) | ||
| 253 | (0 127 (diff-mode syntax)) | ||
| 254 | (17 25 (diff-mode syntax face font-lock-preprocessor-face)) | ||
| 255 | (26 35 (diff-mode syntax face font-lock-string-face)) | ||
| 256 | (37 40 (diff-mode syntax face font-lock-type-face)) | ||
| 257 | (41 45 (diff-mode syntax face font-lock-function-name-face)) | ||
| 258 | (61 78 (diff-mode syntax face font-lock-string-face)) | ||
| 259 | (69 74 (diff-mode fine face diff-refine-removed)) | ||
| 260 | (91 108 (diff-mode syntax face font-lock-string-face)) | ||
| 261 | (99 104 (diff-mode fine face diff-refine-added)) | ||
| 262 | (114 120 (diff-mode syntax face font-lock-keyword-face)))))))) | ||
| 263 | |||
| 264 | (ert-deftest diff-mode-test-font-lock-syntax-one-line () | ||
| 265 | "Check diff syntax highlighting for one line with no newline at end." | ||
| 266 | (let ((default-directory diff-mode-tests--datadir) | ||
| 267 | (old "hello_world_1.c") | ||
| 268 | (new "hello_emacs_1.c") | ||
| 269 | (diff-buffer (get-buffer-create "*Diff*")) | ||
| 270 | (diff-font-lock-refine nil) | ||
| 271 | (diff-font-lock-syntax t) | ||
| 272 | diff-beg) | ||
| 273 | (diff-no-select old new '("-u") 'no-async diff-buffer) | ||
| 274 | (with-current-buffer diff-buffer | ||
| 275 | (font-lock-ensure) | ||
| 276 | (narrow-to-region (progn (diff-hunk-next) | ||
| 277 | (setq diff-beg (diff-beginning-of-hunk))) | ||
| 278 | (diff-end-of-hunk)) | ||
| 279 | |||
| 280 | (should (equal-including-properties | ||
| 281 | (buffer-string) | ||
| 282 | #("@@ -1 +1 @@ | ||
| 283 | -int main() { printf(\"Hello, World!\\n\"); return 0; } | ||
| 284 | \\ No newline at end of file | ||
| 285 | +int main() { printf(\"Hello, Emacs!\\n\"); return 0; } | ||
| 286 | \\ No newline at end of file | ||
| 287 | " | ||
| 288 | 0 11 (face diff-hunk-header) | ||
| 289 | 12 13 (face diff-indicator-removed) | ||
| 290 | 13 65 (face diff-removed) | ||
| 291 | 65 93 (face diff-context) | ||
| 292 | 93 94 (face diff-indicator-added) | ||
| 293 | 94 146 (face diff-added) | ||
| 294 | 146 174 (face diff-context)))) | ||
| 295 | |||
| 296 | (should (equal (mapcar (lambda (o) | ||
| 297 | (list (- (overlay-start o) diff-beg) | ||
| 298 | (- (overlay-end o) diff-beg) | ||
| 299 | (append (and (overlay-get o 'diff-mode) | ||
| 300 | `(diff-mode ,(overlay-get o 'diff-mode))) | ||
| 301 | (and (overlay-get o 'face) | ||
| 302 | `(face ,(overlay-get o 'face)))))) | ||
| 303 | (sort (overlays-in (point-min) (point-max)) | ||
| 304 | (lambda (a b) (< (overlay-start a) (overlay-start b))))) | ||
| 305 | '((0 174 (diff-mode syntax)) | ||
| 306 | (13 16 (diff-mode syntax face font-lock-type-face)) | ||
| 307 | (17 21 (diff-mode syntax face font-lock-function-name-face)) | ||
| 308 | (33 50 (diff-mode syntax face font-lock-string-face)) | ||
| 309 | (53 59 (diff-mode syntax face font-lock-keyword-face)) | ||
| 310 | (94 97 (diff-mode syntax face font-lock-type-face)) | ||
| 311 | (98 102 (diff-mode syntax face font-lock-function-name-face)) | ||
| 312 | (114 131 (diff-mode syntax face font-lock-string-face)) | ||
| 313 | (134 140 (diff-mode syntax face font-lock-keyword-face)))))))) | ||
| 202 | 314 | ||
| 203 | (provide 'diff-mode-tests) | 315 | (provide 'diff-mode-tests) |