Skip to content

Commit aa3c7ab

Browse files
committed
Define prefix grabbing predicate and its tests
1 parent 34724c7 commit aa3c7ab

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

haskell-completions.el

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,19 @@
2828

2929
;;; Code:
3030

31+
(defun haskell-completions-can-grab-prefix ()
32+
"Check if the case is appropriate for grabbing completion prefix.
33+
Returns t if point is either at whitespace character, or at
34+
punctuation, or at line end and preceeding character is not a
35+
whitespace or new line, otherwise returns nil.
36+
37+
Returns nil in presense of active region."
38+
(when (not (region-active-p))
39+
(when (looking-at (rx (| space line-end punct)))
40+
(when (not (bobp))
41+
(save-excursion
42+
(backward-char)
43+
(not (looking-at (rx (| space line-end)))))))))
44+
3145
(provide 'haskell-completions)
3246
;;; haskell-completions.el ends here

tests/haskell-completions-tests.el

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,48 @@
2727

2828
;;; Code:
2929

30+
(require 'ert)
31+
(require 'haskell-mode)
32+
(require 'haskell-completions)
33+
34+
35+
(ert-deftest haskell-completions-can-grab-prefix-test ()
36+
"Tests the function `haskell-completions-can-grab-prefix'."
37+
(with-temp-buffer
38+
(haskell-mode)
39+
(should (eql nil (haskell-completions-can-grab-prefix)))
40+
(insert " ")
41+
(should (eql nil (haskell-completions-can-grab-prefix)))
42+
(insert "a")
43+
(should (eql t (haskell-completions-can-grab-prefix)))
44+
(save-excursion
45+
(insert " ")
46+
(should (eql nil (haskell-completions-can-grab-prefix)))
47+
(insert "bc-")
48+
(should (eql t (haskell-completions-can-grab-prefix)))
49+
(insert "\n")
50+
(should (eql nil (haskell-completions-can-grab-prefix)))
51+
(insert "def:#!")
52+
(should (eql t (haskell-completions-can-grab-prefix))))
53+
(should (eql t (haskell-completions-can-grab-prefix)))
54+
;; punctuation tests
55+
(save-excursion (insert ")"))
56+
(should (eql t (haskell-completions-can-grab-prefix)))
57+
(save-excursion (insert ","))
58+
(should (eql t (haskell-completions-can-grab-prefix)))
59+
(save-excursion (insert "'"))
60+
(should (eql t (haskell-completions-can-grab-prefix)))
61+
;; should return nil in the middle of word
62+
(save-excursion (insert "bcd"))
63+
(should (eql nil (haskell-completions-can-grab-prefix)))
64+
;; region case
65+
(let ((p (point)))
66+
(goto-char (point-min))
67+
(push-mark)
68+
(goto-char p)
69+
(activate-mark)
70+
(should (eql nil (haskell-completions-can-grab-prefix))))))
71+
72+
3073
(provide 'haskell-completions-tests)
3174
;;; haskell-completions-tests.el ends here

0 commit comments

Comments
 (0)