summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuri Linkov <juri@linkov.net>2019-02-02 23:23:16 +0200
committerJuri Linkov <juri@linkov.net>2019-02-02 23:23:16 +0200
commit42c8399059bb311c8cfaf9428f0a29032d71011d (patch)
treec6f6a6596fffe3339004294e63e309e4a57ab9c9
parentadc31c6bdcdb52c2bc0240982d3e8ce870af1fab (diff)
downloademacs-42c8399059bb311c8cfaf9428f0a29032d71011d.tar.gz
* test/lisp/vc/diff-mode-tests.el (diff-mode-test-font-lock): New test.
(diff-mode-test-font-lock-syntax-one-line): New test for one line. * test/data/vc/diff-mode/hello_world.c: * test/data/vc/diff-mode/hello_world_1.c: * test/data/vc/diff-mode/hello_emacs.c: * test/data/vc/diff-mode/hello_emacs_1.c: New fixtures. * lisp/vc/diff-mode.el (diff-syntax-fontify): Move remove-overlays from diff-syntax-fontify-hunk. (Bug#33567) (diff-syntax-fontify-hunk): Remove VISIT arg from insert-file-contents.
-rw-r--r--lisp/vc/diff-mode.el8
-rw-r--r--test/data/vc/diff-mode/hello_emacs.c6
-rw-r--r--test/data/vc/diff-mode/hello_emacs_1.c1
-rw-r--r--test/data/vc/diff-mode/hello_world.c6
-rw-r--r--test/data/vc/diff-mode/hello_world_1.c1
-rw-r--r--test/lisp/vc/diff-mode-tests.el112
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."
(defun diff-syntax-fontify (beg end)
"Highlight source language syntax in diff hunk between BEG and END."
+ (remove-overlays beg end 'diff-mode 'syntax)
(save-excursion
(diff-syntax-fontify-hunk beg end t)
(diff-syntax-fontify-hunk beg end nil)))
@@ -2402,9 +2403,10 @@ and the position in MAX."
(defun diff-syntax-fontify-hunk (beg end old)
"Highlight source language syntax in diff hunk between BEG and END.
When OLD is non-nil, highlight the hunk from the old source."
- (remove-overlays beg end 'diff-mode 'syntax)
(goto-char beg)
(let* ((hunk (buffer-substring-no-properties beg end))
+ ;; Trim a trailing newline to find hunk in diff-syntax-fontify-props
+ ;; in diffs that have no newline at end of diff file.
(text (string-trim-right (or (ignore-errors (diff-hunk-text hunk (not old) nil)) "")))
(line (if (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?")
(if old (match-string 1)
@@ -2431,7 +2433,7 @@ When OLD is non-nil, highlight the hunk from the old source."
(setq props (diff-syntax-fontify-props nil text line-nb t)))
;; Get properties from the file
(with-temp-buffer
- (insert-file-contents file t)
+ (insert-file-contents file)
(setq props (diff-syntax-fontify-props file text line-nb)))))
;; Get properties from a cached revision
(let* ((buffer-name (format " *diff-syntax:%s.~%s~*"
@@ -2459,7 +2461,7 @@ When OLD is non-nil, highlight the hunk from the old source."
(if (and file (file-exists-p file) (file-regular-p file))
;; Try to get full text from the file
(with-temp-buffer
- (insert-file-contents file t)
+ (insert-file-contents file)
(setq props (diff-syntax-fontify-props file text line-nb)))
;; Otherwise, get properties from the hunk alone
(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 @@
+#include <stdio.h>
+int main()
+{
+ printf("Hello, Emacs!\n");
+ return 0;
+}
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 @@
+#include <stdio.h>
+int main()
+{
+ printf("Hello, World!\n");
+ return 0;
+}
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 @@
;;; Code:
(require 'diff-mode)
+(require 'diff)
+(defconst diff-mode-tests--datadir
+ (expand-file-name "test/data/vc/diff-mode" source-directory))
(ert-deftest diff-mode-test-ignore-trailing-dashes ()
"Check to make sure we successfully ignore trailing -- made by
@@ -199,5 +202,114 @@ youthfulness
(kill-buffer buf2)
(delete-directory temp-dir 'recursive))))))
+(ert-deftest diff-mode-test-font-lock ()
+ "Check font-locking of diff hunks."
+ (let ((default-directory diff-mode-tests--datadir)
+ (old "hello_world.c")
+ (new "hello_emacs.c")
+ (diff-buffer (get-buffer-create "*Diff*"))
+ (diff-font-lock-refine t)
+ (diff-font-lock-syntax t)
+ diff-beg)
+ (diff-no-select old new '("-u") 'no-async diff-buffer)
+ (with-current-buffer diff-buffer
+ (font-lock-ensure)
+ (narrow-to-region (progn (diff-hunk-next)
+ (setq diff-beg (diff-beginning-of-hunk)))
+ (diff-end-of-hunk))
+
+ (should (equal-including-properties
+ (buffer-string)
+ #("@@ -1,6 +1,6 @@
+ #include <stdio.h>
+ int main()
+ {
+- printf(\"Hello, World!\\n\");
++ printf(\"Hello, Emacs!\\n\");
+ return 0;
+ }
+"
+ 0 15 (face diff-hunk-header)
+ 16 36 (face diff-context)
+ 36 48 (face diff-context)
+ 48 51 (face diff-context)
+ 51 52 (face diff-indicator-removed)
+ 52 81 (face diff-removed)
+ 81 82 (face diff-indicator-added)
+ 82 111 (face diff-added)
+ 111 124 (face diff-context)
+ 124 127 (face diff-context))))
+
+ (should (equal (mapcar (lambda (o)
+ (list (- (overlay-start o) diff-beg)
+ (- (overlay-end o) diff-beg)
+ (append (and (overlay-get o 'diff-mode)
+ `(diff-mode ,(overlay-get o 'diff-mode)))
+ (and (overlay-get o 'face)
+ `(face ,(overlay-get o 'face))))))
+ (sort (overlays-in (point-min) (point-max))
+ (lambda (a b) (< (overlay-start a) (overlay-start b)))))
+ '((0 127 (diff-mode fine))
+ (0 127 (diff-mode syntax))
+ (17 25 (diff-mode syntax face font-lock-preprocessor-face))
+ (26 35 (diff-mode syntax face font-lock-string-face))
+ (37 40 (diff-mode syntax face font-lock-type-face))
+ (41 45 (diff-mode syntax face font-lock-function-name-face))
+ (61 78 (diff-mode syntax face font-lock-string-face))
+ (69 74 (diff-mode fine face diff-refine-removed))
+ (91 108 (diff-mode syntax face font-lock-string-face))
+ (99 104 (diff-mode fine face diff-refine-added))
+ (114 120 (diff-mode syntax face font-lock-keyword-face))))))))
+
+(ert-deftest diff-mode-test-font-lock-syntax-one-line ()
+ "Check diff syntax highlighting for one line with no newline at end."
+ (let ((default-directory diff-mode-tests--datadir)
+ (old "hello_world_1.c")
+ (new "hello_emacs_1.c")
+ (diff-buffer (get-buffer-create "*Diff*"))
+ (diff-font-lock-refine nil)
+ (diff-font-lock-syntax t)
+ diff-beg)
+ (diff-no-select old new '("-u") 'no-async diff-buffer)
+ (with-current-buffer diff-buffer
+ (font-lock-ensure)
+ (narrow-to-region (progn (diff-hunk-next)
+ (setq diff-beg (diff-beginning-of-hunk)))
+ (diff-end-of-hunk))
+
+ (should (equal-including-properties
+ (buffer-string)
+ #("@@ -1 +1 @@
+-int main() { printf(\"Hello, World!\\n\"); return 0; }
+\\ No newline at end of file
++int main() { printf(\"Hello, Emacs!\\n\"); return 0; }
+\\ No newline at end of file
+"
+ 0 11 (face diff-hunk-header)
+ 12 13 (face diff-indicator-removed)
+ 13 65 (face diff-removed)
+ 65 93 (face diff-context)
+ 93 94 (face diff-indicator-added)
+ 94 146 (face diff-added)
+ 146 174 (face diff-context))))
+
+ (should (equal (mapcar (lambda (o)
+ (list (- (overlay-start o) diff-beg)
+ (- (overlay-end o) diff-beg)
+ (append (and (overlay-get o 'diff-mode)
+ `(diff-mode ,(overlay-get o 'diff-mode)))
+ (and (overlay-get o 'face)
+ `(face ,(overlay-get o 'face))))))
+ (sort (overlays-in (point-min) (point-max))
+ (lambda (a b) (< (overlay-start a) (overlay-start b)))))
+ '((0 174 (diff-mode syntax))
+ (13 16 (diff-mode syntax face font-lock-type-face))
+ (17 21 (diff-mode syntax face font-lock-function-name-face))
+ (33 50 (diff-mode syntax face font-lock-string-face))
+ (53 59 (diff-mode syntax face font-lock-keyword-face))
+ (94 97 (diff-mode syntax face font-lock-type-face))
+ (98 102 (diff-mode syntax face font-lock-function-name-face))
+ (114 131 (diff-mode syntax face font-lock-string-face))
+ (134 140 (diff-mode syntax face font-lock-keyword-face))))))))
(provide 'diff-mode-tests)