Skip to content
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

Add beginning of defun and end of defun functions #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions kotlin-mode-lexer.el
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,12 @@ expression as a token with one of the following types:
kotlin-mode--parameter-modifier-keywords
kotlin-mode--function-modifier-keywords))

(defconst kotlin-mode--beginning-of-defun-re
(concat
"\\s-*"
(regexp-opt kotlin-mode--modifier-keywords)
"*\\s-*\\_<fun\\_>"))

(defun kotlin-mode--implicit-semi-p ()
"Return non-nil if the point is after the end of a statement."
(let ((previous-token (save-excursion
Expand Down Expand Up @@ -2489,6 +2495,41 @@ Assuming the point is on a string."
(kotlin-mode--forward-string-chunk)))
(point))

(defun kotlin-mode--beginning-of-defun (&optional arg)
"Move backward to the beginning of the current defun.
With ARG, move backward multiple defuns. Negative ARG means
move forward."
(beginning-of-line)
;; If currently looking at an annotation, go its function's beginning.
(if (looking-at "\\s-*@")
(re-search-forward kotlin-mode--beginning-of-defun-re nil t 1)
(if (< arg 0) (+ arg 1) (- arg 1)))
(when (re-search-backward kotlin-mode--beginning-of-defun-re nil t (or arg 1))
(beginning-of-line)
t))

(defun kotlin-mode--end-of-defun ()
"Move forward to the end of the current defun."
(beginning-of-line)
;; If currently looking at an annotation, go to its function's beginning.
(if (looking-at "\\s-*@")
(kotlin-mode--beginning-of-defun -1))
(while (not (looking-at kotlin-mode--beginning-of-defun-re))
(forward-line -1)
(beginning-of-line))
(if (< (save-excursion (re-search-forward "{"))
(save-excursion (re-search-forward "=")))
(progn
(re-search-forward "{")
(backward-char)
(condition-case nil
(forward-sexp)
(scan-error (goto-char (point-max)))))
;; Single expression function
(re-search-forward "=")
(forward-paragraph)
(backward-char)))

(defun kotlin-mode--goto-non-comment-bol ()
"Back to the beginning of line that is not inside a comment."
(beginning-of-line)
Expand Down
3 changes: 3 additions & 0 deletions kotlin-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,9 @@ and return non-nil. Return nil otherwise."

(delete-overlay kotlin-mode--anchor-overlay)

(setq-local beginning-of-defun-function #'kotlin-mode--beginning-of-defun)
(setq-local end-of-defun-function #'kotlin-mode--end-of-defun)

:group 'kotlin
:syntax-table kotlin-mode-syntax-table)

Expand Down