Skip to content

Commit 1d04044

Browse files
authored
Merge pull request #368 from syohex/implement-defun-commands
Implement defun commands
2 parents 8e77a84 + fefadd6 commit 1d04044

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

elixir-mode.el

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,32 @@ just return nil."
467467
(backward-char)
468468
(fill-region (point) (mark))))
469469

470+
(defun elixir-beginning-of-defun (&optional arg)
471+
(interactive "p")
472+
(let ((command last-command)
473+
(regexp (concat "^\\s-*" (elixir-rx builtin-declaration)))
474+
case-fold-search)
475+
(while (and (re-search-backward regexp nil t (or arg 1))
476+
(elixir-syntax-in-string-or-comment-p)))
477+
(goto-char (line-beginning-position))))
478+
479+
(defun elixir-end-of-defun ()
480+
(interactive)
481+
(goto-char (line-beginning-position))
482+
(if (re-search-forward "\\_<do:" (line-end-position) t)
483+
(goto-char (line-end-position))
484+
(goto-char (line-end-position))
485+
(let ((level (save-excursion
486+
(elixir-beginning-of-defun)
487+
(current-indentation)))
488+
finish)
489+
(while (and (not finish) (re-search-forward "^\\s-*\\_<end\\_>" nil t))
490+
(when (and (not (elixir-syntax-in-string-or-comment-p))
491+
(= (current-indentation) level))
492+
(setq finish t)))
493+
(when (looking-back "^\\s-*\\_<end" (line-beginning-position))
494+
(forward-line 1)))))
495+
470496
(easy-menu-define elixir-mode-menu elixir-mode-map
471497
"Elixir mode menu."
472498
'("Elixir"
@@ -491,6 +517,10 @@ just return nil."
491517
#'elixir-syntax-propertize-function)
492518
(set (make-local-variable 'imenu-generic-expression)
493519
elixir-imenu-generic-expression)
520+
521+
(set (make-local-variable 'beginning-of-defun-function) #'elixir-beginning-of-defun)
522+
(set (make-local-variable 'end-of-defun-function) #'elixir-end-of-defun)
523+
494524
(smie-setup elixir-smie-grammar 'verbose-elixir-smie-rules
495525
:forward-token 'elixir-smie-forward-token
496526
:backward-token 'elixir-smie-backward-token))

test/elixir-mode-moving-test.el

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
;;; elixir-mode-moving-test.el --- Tests for moving cursor functions
2+
3+
;;; Code:
4+
5+
(require 'test-helper)
6+
7+
(ert-deftest beginning-of-defun ()
8+
:tags '(moving)
9+
(elixir-test-with-temp-buffer
10+
"def foo do
11+
:bar
12+
end
13+
"
14+
(search-forward ":bar")
15+
(call-interactively 'beginning-of-defun)
16+
(should (= (point) (point-min)))))
17+
18+
(ert-deftest beginning-of-defun-nested ()
19+
:tags '(moving)
20+
(elixir-test-with-temp-buffer
21+
"
22+
defmodule Foo do
23+
def bar do
24+
:baz
25+
end
26+
end
27+
"
28+
(search-forward ":baz")
29+
(call-interactively 'beginning-of-defun)
30+
(should (and (= (line-number-at-pos) 3) (bolp)))))
31+
32+
(ert-deftest end-of-defun ()
33+
:tags '(moving)
34+
(elixir-test-with-temp-buffer
35+
"def foo do
36+
:bar
37+
end
38+
"
39+
(search-forward ":bar")
40+
(call-interactively 'end-of-defun)
41+
(should (= (point) (point-max)))))
42+
43+
(ert-deftest end-of-defun-oneline ()
44+
:tags '(moving)
45+
(elixir-test-with-temp-buffer
46+
"def foo do: :bar"
47+
(search-forward ":bar")
48+
(call-interactively 'end-of-defun)
49+
(should (= (point) (line-end-position)))))
50+
51+
(ert-deftest end-of-defun-nested ()
52+
:tags '(moving)
53+
(elixir-test-with-temp-buffer
54+
"
55+
defmodule Foo do
56+
def bar do
57+
:baz
58+
end
59+
end
60+
"
61+
(forward-line 1)
62+
(call-interactively 'end-of-defun)
63+
(should (= (point) (point-max)))
64+
65+
(goto-char (point-min))
66+
(search-forward ":baz")
67+
(call-interactively 'end-of-defun)
68+
(should (and (= (line-number-at-pos) 6) (bolp)))))
69+
70+
(ert-deftest end-of-mark-defun ()
71+
:tags '(moving)
72+
(elixir-test-with-temp-buffer
73+
"
74+
defmodule Foo do
75+
def bar do
76+
:baz
77+
end
78+
end
79+
"
80+
(goto-char (point-min))
81+
(search-forward ":baz")
82+
(call-interactively 'mark-defun)
83+
(should (= (count-lines (region-beginning) (region-end)) 3))))
84+
85+
(provide 'elixir-mode-moving-test)
86+
87+
;;; elixir-mode-helper-test.el ends here

0 commit comments

Comments
 (0)