Skip to content

Commit c1afdd0

Browse files
committed
Merge pull request #1059 from gracjan/pr-literate-font-lock
Add literate font lock tests, remove unused parts
2 parents cbbb779 + e48ae1f commit c1afdd0

File tree

2 files changed

+92
-51
lines changed

2 files changed

+92
-51
lines changed

haskell-font-lock.el

-49
Original file line numberDiff line numberDiff line change
@@ -319,55 +319,6 @@ Returns keywords suitable for `font-lock-keywords'."
319319
'haskell-operator-face))))
320320
keywords))
321321

322-
(defvar haskell-font-lock-latex-cache-pos nil
323-
"Position of cache point used by `haskell-font-lock-latex-cache-in-comment'.
324-
Should be at the start of a line.")
325-
(make-variable-buffer-local 'haskell-font-lock-latex-cache-pos)
326-
327-
(defvar haskell-font-lock-latex-cache-in-comment nil
328-
"If `haskell-font-lock-latex-cache-pos' is outside a
329-
\\begin{code}..\\end{code} block (and therefore inside a comment),
330-
this variable is set to t, otherwise nil.")
331-
(make-variable-buffer-local 'haskell-font-lock-latex-cache-in-comment)
332-
333-
(defun haskell-font-lock-latex-comments (end)
334-
"Sets `match-data' according to the region of the buffer before end
335-
that should be commented under LaTeX-style literate scripts."
336-
(let ((start (point)))
337-
(if (= start end)
338-
;; We're at the end. No more to fontify.
339-
nil
340-
(if (not (eq start haskell-font-lock-latex-cache-pos))
341-
;; If the start position is not cached, calculate the state
342-
;; of the start.
343-
(progn
344-
(setq haskell-font-lock-latex-cache-pos start)
345-
;; If the previous \begin{code} or \end{code} is a
346-
;; \begin{code}, then start is not in a comment, otherwise
347-
;; it is in a comment.
348-
(setq haskell-font-lock-latex-cache-in-comment
349-
(if (and
350-
(re-search-backward
351-
"^\\(\\(\\\\begin{code}\\)\\|\\(\\\\end{code}\\)\\)$"
352-
(point-min) t)
353-
(match-end 2))
354-
nil t))
355-
;; Restore position.
356-
(goto-char start)))
357-
(if haskell-font-lock-latex-cache-in-comment
358-
(progn
359-
;; If start is inside a comment, search for next \begin{code}.
360-
(re-search-forward "^\\\\begin{code}$" end 'move)
361-
;; Mark start to end of \begin{code} (if present, till end
362-
;; otherwise), as a comment.
363-
(set-match-data (list start (point)))
364-
;; Return point, as a normal regexp would.
365-
(point))
366-
;; If start is inside a code block, search for next \end{code}.
367-
(if (re-search-forward "^\\\\end{code}$" end t)
368-
;; If one found, mark it as a comment, otherwise finish.
369-
(point))))))
370-
371322
(defconst haskell-basic-syntactic-keywords
372323
'(;; Character constants (since apostrophe can't have string syntax).
373324
;; Beware: do not match something like 's-}' or '\n"+' since the first '

tests/haskell-font-lock-tests.el

+92-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ after a test as this aids interactive debugging."
5151
(haskell-mode)
5252
,@body)))
5353

54-
(defun check-properties (lines props)
54+
(defun check-properties (lines props &optional literate)
5555
"Check if syntax properties and font-lock properties as set properly.
5656
5757
LINES is a list of strings that will be inserted to a new
@@ -63,10 +63,12 @@ if all of its characters have syntax and face. See
6363
(kill-buffer "*haskell-mode-buffer*"))
6464
(save-current-buffer
6565
(set-buffer (get-buffer-create "*haskell-mode-buffer*"))
66-
(haskell-mode)
6766
(dolist (line lines)
6867
(insert line)
6968
(insert "\n"))
69+
(if literate
70+
(literate-haskell-mode)
71+
(haskell-mode))
7072
(font-lock-fontify-buffer)
7173
(goto-char (point-min))
7274
(dolist (prop props)
@@ -464,3 +466,91 @@ if all of its characters have syntax and face. See
464466
(check-properties
465467
'("Q +++ 12.12")
466468
'(("+++" t haskell-definition-face))))
469+
470+
(ert-deftest haskell-literate-bird-1 ()
471+
(check-properties
472+
'("Comment1"
473+
""
474+
"> code1 = 1"
475+
"> code2 = 1"
476+
""
477+
"Comment2"
478+
""
479+
"> code3 = 1"
480+
""
481+
"Comment3")
482+
'(("Comment1" t haskell-literate-comment-face)
483+
("code1" t haskell-definition-face)
484+
("code2" t haskell-definition-face)
485+
("Comment2" t haskell-literate-comment-face)
486+
("code3" t haskell-definition-face)
487+
("Comment3" t haskell-literate-comment-face))
488+
'literate))
489+
490+
(ert-deftest haskell-literate-bird-2 ()
491+
;; Haskell Report requires empty line before bird code block. So it
492+
;; is a code block, just in error.
493+
:expected-result :failed
494+
(check-properties
495+
'("Comment1"
496+
"> code1 = 1"
497+
"> code2 = 1"
498+
"Comment2"
499+
""
500+
"> code3 = 1"
501+
""
502+
"Comment3")
503+
'(("Comment1" t haskell-literate-comment-face)
504+
(">" t font-lock-warning-face)
505+
("code1" t haskell-definition-face)
506+
("code2" t haskell-definition-face)
507+
("Comment2" t haskell-literate-comment-face)
508+
("code3" t haskell-definition-face)
509+
("Comment3" t haskell-literate-comment-face))
510+
'literate))
511+
512+
(ert-deftest haskell-literate-latex-1 ()
513+
(check-properties
514+
'("Comment1"
515+
""
516+
"\\begin{code}"
517+
"code1 = 1"
518+
"code2 = 1"
519+
"\\end{code}"
520+
""
521+
"Comment2"
522+
"\\begin{code}"
523+
"code3 = 1"
524+
"\\end{code}"
525+
"Comment3")
526+
'(("Comment1" t haskell-literate-comment-face)
527+
("code1" t haskell-definition-face)
528+
("code2" t haskell-definition-face)
529+
("Comment2" t haskell-literate-comment-face)
530+
("code3" t haskell-definition-face)
531+
("Comment3" t haskell-literate-comment-face))
532+
'literate))
533+
534+
(ert-deftest haskell-literate-mixed-1 ()
535+
:expected-result :failed
536+
;; Although Haskell Report does not advice mixing modes, it is a
537+
;; perfectly valid construct that we should support in syntax
538+
;; highlighting.
539+
(check-properties
540+
'("Comment1"
541+
""
542+
"> code1 = 1"
543+
"> code2 = 1"
544+
""
545+
"Comment2"
546+
"\\begin{code}"
547+
"code3 = 1"
548+
"\\end{code}"
549+
"Comment3")
550+
'(("Comment1" t haskell-literate-comment-face)
551+
("code1" t haskell-definition-face)
552+
("code2" t haskell-definition-face)
553+
("Comment2" t haskell-literate-comment-face)
554+
("code3" t haskell-definition-face)
555+
("Comment3" t haskell-literate-comment-face))
556+
'literate))

0 commit comments

Comments
 (0)