]> git.eshelyaron.com Git - emacs.git/commitdiff
* test/lisp/vc/diff-mode-tests.el (diff-mode-test-font-lock): New test.
authorJuri Linkov <juri@linkov.net>
Sat, 2 Feb 2019 21:23:16 +0000 (23:23 +0200)
committerJuri Linkov <juri@linkov.net>
Sat, 2 Feb 2019 21:23:16 +0000 (23:23 +0200)
(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.

lisp/vc/diff-mode.el
test/data/vc/diff-mode/hello_emacs.c [new file with mode: 0644]
test/data/vc/diff-mode/hello_emacs_1.c [new file with mode: 0644]
test/data/vc/diff-mode/hello_world.c [new file with mode: 0644]
test/data/vc/diff-mode/hello_world_1.c [new file with mode: 0644]
test/lisp/vc/diff-mode-tests.el

index 158489c97fe98460d22b6ced109672253e551bab..607c7b583edaae619049522a87898936179fce63 100644 (file)
@@ -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 (file)
index 0000000..c7ed753
--- /dev/null
@@ -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 (file)
index 0000000..62145a6
--- /dev/null
@@ -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 (file)
index 0000000..dcbe06c
--- /dev/null
@@ -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 (file)
index 0000000..606afb3
--- /dev/null
@@ -0,0 +1 @@
+int main() { printf("Hello, World!\n"); return 0; }
\ No newline at end of file
index 4276974c2acccc597e7fe542c98560da27fa3d0e..8e690548f05fe967850f4a67b5dfcb622393e8cb 100644 (file)
 ;;; 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)