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

Fix issue in keyword indentation with compound keywords #159

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- Fix issue in keyword indentation with compound keywords ([#159](https://github.com/JuliaEditorSupport/julia-emacs/pull/159))

# 0.4

- increase lookback ([#98](https://github.com/JuliaEditorSupport/julia-emacs/pull/98)), fixes [#5](https://github.com/JuliaEditorSupport/julia-emacs/issues/5)
Expand Down
28 changes: 28 additions & 0 deletions julia-mode-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,34 @@ var = func(begin
end
)"))

(ert-deftest julia--test-indent-block-compound-keywords ()
"We should indent on compound keywords inside a block."
(julia--should-indent "
begin
abstract type MyType end
test
end" "
begin
abstract type MyType end
test
end"))

(ert-deftest julia--test-indent-block-compound-keywords-with-nesting ()
"We should indent on compound keywords and again inside them."
(julia--should-indent "
begin
abstract type MyType
test1
end
test2
end" "
begin
abstract type MyType
test1
end
test2
end"))

;;; font-lock tests

(ert-deftest julia--test-symbol-font-locking-at-bol ()
Expand Down
17 changes: 16 additions & 1 deletion julia-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,11 @@
(list "if" "while" "for" "begin" "try" "function" "let" "macro"
"quote" "do" "module" "baremodule"
;; "immutable" "type" ;; remove after 0.6
"abstract type" "primitive type" "struct" "mutable struct"))
"struct"))

;; For matching on keywords made up of two tokens
(defconst julia-block-start-compound-keywords
(list "abstract type" "primitive type" "mutable struct"))

;; For keywords that begin a block without additional indentation
(defconst julia-block-start-keywords-no-indent
Expand Down Expand Up @@ -400,6 +404,15 @@ a keyword if used as a field name, X.word, or quoted, :word."
(not (julia-in-brackets)))
(not (julia-in-comment))))


(defun julia-at-compound-keyword (ckw-list)
"Return non-nil if point is at second word of a compound keyword."
(let ((compound-tail (current-word)))
(save-excursion
(backward-word 1)
(let ((compound-keyword (concat (current-word) " " compound-tail)))
(member compound-keyword ckw-list)))))

;; if backward-sexp gives an error, move back 1 char to move over the '('
(defun julia-safe-backward-sexp ()
(if (condition-case nil (backward-sexp) (error t))
Expand Down Expand Up @@ -443,6 +456,8 @@ Do not move back beyond position MIN."
(setq nesting-count
(cond ((julia-at-keyword julia-block-start-keywords)
(+ nesting-count 1))
((julia-at-compound-keyword julia-block-start-compound-keywords)
(+ nesting-count 1))
((julia-at-keyword '("end"))
(- nesting-count 1))
(t nesting-count))))
Expand Down