Skip to content

Commit dc775d5

Browse files
committed
Merge pull request #1335 from czipperz/end-of-defun-patch
Going forward defun should go to end of defun
2 parents dec48ff + 1f654f8 commit dc775d5

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

haskell-decl-scan.el

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
;; Copyright (C) 2004, 2005, 2007, 2009 Free Software Foundation, Inc.
44
;; Copyright (C) 1997-1998 Graeme E Moss
5+
;; Copyright (C) 2016 Chris Gregory
56

67
;; Author: 1997-1998 Graeme E Moss <[email protected]>
78
;; Maintainer: Stefan Monnier <[email protected]>
@@ -354,7 +355,38 @@ there."
354355
"Move forward to the first character that starts a top-level
355356
declaration. As `haskell-ds-backward-decl' but forward."
356357
(interactive)
357-
(haskell-ds-move-to-decl t (haskell-ds-bird-p) nil))
358+
(let ((p (point)) b e empty was-at-bob)
359+
;; Go back to beginning of defun, then go to beginning of next
360+
(haskell-ds-move-to-decl nil (haskell-ds-bird-p) nil)
361+
(setq b (point))
362+
(haskell-ds-move-to-decl t (haskell-ds-bird-p) nil)
363+
(setq e (point))
364+
;; tests if line is empty
365+
(setq empty (and (<= (point) p)
366+
(not (eolp))))
367+
(setq was-at-bob (and (= (point-min) b)
368+
(= b p)
369+
(< p e)))
370+
;; this conditional allows for when empty lines at end, first
371+
;; `C-M-e' will go to end of defun, next will go to end of file.
372+
(when (or was-at-bob
373+
empty)
374+
(if (or (and was-at-bob
375+
(= ?\n
376+
(save-excursion
377+
(goto-char (point-min))
378+
(following-char))))
379+
empty)
380+
(haskell-ds-move-to-decl t (haskell-ds-bird-p) nil))
381+
;; Then go back to end of current
382+
(forward-line -1)
383+
(while (and (eolp)
384+
;; prevent infinite loop
385+
(not (= (point)
386+
(point-min))))
387+
(forward-line -1))
388+
(forward-line 1)))
389+
(point))
358390

359391
(defun haskell-ds-generic-find-next-decl (bird-literate)
360392
"Find the name, position and type of the declaration at or after point.

tests/haskell-decl-scan-tests.el

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
;;; haskell-decl-scan-tests.el -*- lexical-binding: t -*-
2+
3+
;; Copyright © 2016 Chris Gregory. All rights reserved.
4+
5+
;; This file is part of haskell-mode package.
6+
;; You can contact with authors using GitHub issue tracker:
7+
;; https://github.com/haskell/haskell-mode/issues
8+
9+
;; This file is free software; you can redistribute it and/or modify
10+
;; it under the terms of the GNU General Public License as published by
11+
;; the Free Software Foundation; either version 3, or (at your option)
12+
;; any later version.
13+
14+
;; This file is distributed in the hope that it will be useful,
15+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
;; GNU General Public License for more details.
18+
19+
;; You should have received a copy of the GNU General Public License
20+
;; along with GNU Emacs; see the file COPYING. If not, write to
21+
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22+
;; Boston, MA 02110-1301, USA.
23+
24+
;;; Commentary:
25+
26+
;; This package provides regression tests for haskell-decl-scan package.
27+
28+
;;; Code:
29+
30+
(require 'ert)
31+
(require 'haskell-decl-scan)
32+
(require 'haskell-test-utils)
33+
34+
(ert-deftest haskell-ds-backward-decl-1 ()
35+
"Test running haskell-ds-backward-decl"
36+
(with-temp-buffer
37+
(insert-lines "" "fun :: Int -> Int" "fun = id"
38+
"" "f2 :: Int" "f2 = 3" "")
39+
(goto-char (point-max))
40+
(should (haskell-ds-backward-decl))
41+
(should (looking-at-p "f2 :: Int"))
42+
(should (haskell-ds-backward-decl))
43+
(should (looking-at-p "fun :: Int -> Int"))
44+
(should-not (haskell-ds-backward-decl))
45+
(should (= (point-min) (point)))))
46+
47+
(ert-deftest haskell-ds-backward-decl-2 ()
48+
"Test running haskell-ds-backward-decl"
49+
(with-temp-buffer
50+
(insert-lines "" "" "fun :: Int -> Int"
51+
"" "" "fun = id"
52+
"" "" "f2 :: Int"
53+
"" "" "f2 = 3"
54+
"" "" "")
55+
(goto-char (point-max))
56+
(should (haskell-ds-backward-decl))
57+
(should (looking-at-p "f2 :: Int"))
58+
(should (haskell-ds-backward-decl))
59+
(should (looking-at-p "fun :: Int -> Int"))
60+
(should-not (haskell-ds-backward-decl))
61+
(should (= (point-min) (point)))))
62+
63+
(ert-deftest haskell-ds-forward-decl-1 ()
64+
"Test running haskell-ds-backward-decl"
65+
(with-temp-buffer
66+
(insert-lines "" "fun :: Int -> Int" "fun = id"
67+
"" "f2 :: Int" "f2 = 3"
68+
"")
69+
(goto-char (point-min))
70+
(should (haskell-ds-forward-decl))
71+
(should (looking-at-p "$"))
72+
(should (= (point) (save-excursion (goto-line 4) (point))))
73+
(should (haskell-ds-forward-decl))
74+
(should (looking-at-p "f2 :: Int"))
75+
(should (= (point-max) (haskell-ds-forward-decl)))))
76+
77+
(ert-deftest haskell-ds-forward-decl-2 ()
78+
"Test running haskell-ds-backward-decl"
79+
(with-temp-buffer
80+
(insert-lines "" "" "fun :: Int -> Int"
81+
"" "" "fun = id"
82+
"" "" "f2 :: Int"
83+
"" "" "f2 = 3"
84+
"" "" "")
85+
(goto-char (point-min))
86+
(should (haskell-ds-forward-decl))
87+
(should (looking-at-p "$"))
88+
(should (= (point) (save-excursion (goto-line 7) (point))))
89+
(should (haskell-ds-forward-decl))
90+
(should (looking-at-p "f2 :: Int"))
91+
(should (haskell-ds-forward-decl))
92+
(should (= (point) (save-excursion (goto-line 13) (point))))
93+
(should (= (point-max) (progn (haskell-ds-forward-decl) (point))))))
94+
95+
(provide 'haskell-decl-scan-tests)
96+
97+
;;; haskell-decl-scan-tests.el ends here

0 commit comments

Comments
 (0)