Skip to content

Commit ec9aa95

Browse files
committed
Make going forward defun work correctly with comments
1 parent 321ff24 commit ec9aa95

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

haskell-decl-scan.el

+38-1
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,43 @@ there."
351351
(interactive)
352352
(haskell-ds-move-to-decl nil (haskell-ds-bird-p) nil))
353353

354+
(defun haskell-ds-comment-p
355+
(&optional
356+
pt)
357+
"Test if the cursor is on whitespace or a comment.
358+
359+
`PT' defaults to `(point)'"
360+
;; ensure result is `t' or `nil' instead of just truthy
361+
(if (or
362+
;; is cursor on whitespace
363+
(let ((f (following-char)))
364+
(or (= f ?\t)
365+
(= f ?\n)
366+
(= f ? )))
367+
;; http://emacs.stackexchange.com/questions/14269/how-to-detect-if-the-point-is-within-a-comment-area
368+
;; is cursor at begging, inside, or end of comment
369+
(let ((fontfaces (get-text-property (or pt
370+
(point)) 'face)))
371+
(when (not (listp fontfaces))
372+
(setf fontfaces (list fontfaces)))
373+
(delq nil (mapcar
374+
#'(lambda (f)
375+
(or (eq f 'font-lock-comment-face)
376+
(eq f 'font-lock-comment-delimiter-face)))
377+
fontfaces))))
378+
t
379+
nil))
380+
381+
(defun haskell-ds-line-commented-p ()
382+
"Tests if all characters from `point' to `end-of-line' pass
383+
`haskell-ds-comment-p'"
384+
(let ((r t))
385+
(while (and r (not (eolp)))
386+
(if (not (haskell-ds-comment-p))
387+
(setq r nil))
388+
(forward-char))
389+
r))
390+
354391
(defun haskell-ds-forward-decl ()
355392
"Move forward to the first character that starts a top-level
356393
declaration. As `haskell-ds-backward-decl' but forward."
@@ -380,7 +417,7 @@ declaration. As `haskell-ds-backward-decl' but forward."
380417
(haskell-ds-move-to-decl t (haskell-ds-bird-p) nil))
381418
;; Then go back to end of current
382419
(forward-line -1)
383-
(while (and (eolp)
420+
(while (and (haskell-ds-line-commented-p)
384421
;; prevent infinite loop
385422
(not (= (point)
386423
(point-min))))

tests/haskell-decl-scan-tests.el

+70
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@
3131
(require 'haskell-decl-scan)
3232
(require 'haskell-test-utils)
3333

34+
(ert-deftest haskell-ds-line-commented-p-1 ()
35+
"All lines in this buffer should count as comments"
36+
(with-temp-buffer
37+
(haskell-mode)
38+
(insert-lines "" "--hi" " -- hi\t " "" "{-hi-}" " \t{-hi-} ")
39+
(font-lock-fontify-buffer)
40+
(goto-char (point-min))
41+
42+
(while (not (eobp))
43+
(should (haskell-ds-line-commented-p))
44+
(forward-line))))
45+
46+
(ert-deftest haskell-ds-comment-p-1 ()
47+
"All characters in this buffer should count as comments"
48+
(with-temp-buffer
49+
(haskell-mode)
50+
(insert-lines "" "--hi" " -- hi\t " "" "{-hi-}" " \t{-hi-} ")
51+
(font-lock-fontify-buffer)
52+
(goto-char (point-min))
53+
54+
(while (not (bobp))
55+
(should (haskell-ds-comment-p))
56+
(forward-char))))
57+
3458
(ert-deftest haskell-ds-backward-decl-1 ()
3559
"Test running haskell-ds-backward-decl"
3660
(with-temp-buffer
@@ -44,6 +68,27 @@
4468
(should-not (haskell-ds-backward-decl))
4569
(should (= (point-min) (point)))))
4670

71+
(ert-deftest haskell-ds-backward-decl-2-commented ()
72+
"Test running haskell-ds-backward-decl"
73+
(with-temp-buffer
74+
(haskell-mode)
75+
(insert-lines "" "-- documentation" "fun :: Int -> Int"
76+
"" "{- comment -}" "fun = id"
77+
"" " -- space comment" "f2 :: Int"
78+
"" " {- trailing -} \t" "f2 = 3"
79+
"" "" "")
80+
(font-lock-fontify-buffer)
81+
(goto-char (point-max))
82+
83+
(should (haskell-ds-backward-decl))
84+
(should (looking-at-p "f2 :: Int"))
85+
86+
(should (haskell-ds-backward-decl))
87+
(should (looking-at-p "fun :: Int -> Int"))
88+
89+
(should-not (haskell-ds-backward-decl))
90+
(should (bobp))))
91+
4792
(ert-deftest haskell-ds-backward-decl-2 ()
4893
"Test running haskell-ds-backward-decl"
4994
(with-temp-buffer
@@ -92,6 +137,31 @@
92137
(should (= (point) (save-excursion (goto-line 13) (point))))
93138
(should (= (point-max) (progn (haskell-ds-forward-decl) (point))))))
94139

140+
(ert-deftest haskell-ds-forward-decl-2-commented ()
141+
"Test running haskell-ds-backward-decl"
142+
(with-temp-buffer
143+
(haskell-mode)
144+
(insert-lines "" "-- documentation" "fun :: Int -> Int"
145+
"" "{- comment -}" "fun = id"
146+
"" " -- space comment" "f2 :: Int"
147+
"" " {- trailing -} \t" "f2 = 3"
148+
"" "" "")
149+
(font-lock-fontify-buffer)
150+
(goto-char (point-min))
151+
152+
(should (haskell-ds-forward-decl))
153+
(should (looking-at-p "$"))
154+
(should (= (point) (save-excursion (goto-line 7) (point))))
155+
156+
(should (haskell-ds-forward-decl))
157+
(should (looking-at-p "f2 :: Int"))
158+
159+
(should (haskell-ds-forward-decl))
160+
(should (= (point) (save-excursion (goto-line 13) (point))))
161+
162+
(should (= (point-max) (progn (haskell-ds-forward-decl) (point))))
163+
(should (eobp))))
164+
95165
(provide 'haskell-decl-scan-tests)
96166

97167
;;; haskell-decl-scan-tests.el ends here

0 commit comments

Comments
 (0)