Skip to content

Implement defun commands #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions elixir-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,32 @@ just return nil."
(backward-char)
(fill-region (point) (mark))))

(defun elixir-beginning-of-defun (&optional arg)
(interactive "p")
(let ((command last-command)
(regexp (concat "^\\s-*" (elixir-rx builtin-declaration)))
case-fold-search)
(while (and (re-search-backward regexp nil t (or arg 1))
(elixir-syntax-in-string-or-comment-p)))
(goto-char (line-beginning-position))))

(defun elixir-end-of-defun ()
(interactive)
(goto-char (line-beginning-position))
(if (re-search-forward "\\_<do:" (line-end-position) t)
(goto-char (line-end-position))
(goto-char (line-end-position))
(let ((level (save-excursion
(elixir-beginning-of-defun)
(current-indentation)))
finish)
(while (and (not finish) (re-search-forward "^\\s-*\\_<end\\_>" nil t))
(when (and (not (elixir-syntax-in-string-or-comment-p))
(= (current-indentation) level))
(setq finish t)))
(when (looking-back "^\\s-*\\_<end" (line-beginning-position))
(forward-line 1)))))

(easy-menu-define elixir-mode-menu elixir-mode-map
"Elixir mode menu."
'("Elixir"
Expand All @@ -491,6 +517,10 @@ just return nil."
#'elixir-syntax-propertize-function)
(set (make-local-variable 'imenu-generic-expression)
elixir-imenu-generic-expression)

(set (make-local-variable 'beginning-of-defun-function) #'elixir-beginning-of-defun)
(set (make-local-variable 'end-of-defun-function) #'elixir-end-of-defun)

(smie-setup elixir-smie-grammar 'verbose-elixir-smie-rules
:forward-token 'elixir-smie-forward-token
:backward-token 'elixir-smie-backward-token))
Expand Down
87 changes: 87 additions & 0 deletions test/elixir-mode-moving-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
;;; elixir-mode-moving-test.el --- Tests for moving cursor functions

;;; Code:

(require 'test-helper)

(ert-deftest beginning-of-defun ()
:tags '(moving)
(elixir-test-with-temp-buffer
"def foo do
:bar
end
"
(search-forward ":bar")
(call-interactively 'beginning-of-defun)
(should (= (point) (point-min)))))

(ert-deftest beginning-of-defun-nested ()
:tags '(moving)
(elixir-test-with-temp-buffer
"
defmodule Foo do
def bar do
:baz
end
end
"
(search-forward ":baz")
(call-interactively 'beginning-of-defun)
(should (and (= (line-number-at-pos) 3) (bolp)))))

(ert-deftest end-of-defun ()
:tags '(moving)
(elixir-test-with-temp-buffer
"def foo do
:bar
end
"
(search-forward ":bar")
(call-interactively 'end-of-defun)
(should (= (point) (point-max)))))

(ert-deftest end-of-defun-oneline ()
:tags '(moving)
(elixir-test-with-temp-buffer
"def foo do: :bar"
(search-forward ":bar")
(call-interactively 'end-of-defun)
(should (= (point) (line-end-position)))))

(ert-deftest end-of-defun-nested ()
:tags '(moving)
(elixir-test-with-temp-buffer
"
defmodule Foo do
def bar do
:baz
end
end
"
(forward-line 1)
(call-interactively 'end-of-defun)
(should (= (point) (point-max)))

(goto-char (point-min))
(search-forward ":baz")
(call-interactively 'end-of-defun)
(should (and (= (line-number-at-pos) 6) (bolp)))))

(ert-deftest end-of-mark-defun ()
:tags '(moving)
(elixir-test-with-temp-buffer
"
defmodule Foo do
def bar do
:baz
end
end
"
(goto-char (point-min))
(search-forward ":baz")
(call-interactively 'mark-defun)
(should (= (count-lines (region-beginning) (region-end)) 3))))

(provide 'elixir-mode-moving-test)

;;; elixir-mode-helper-test.el ends here