Skip to content

correct indentation for block with multiple matches #231

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 1 commit into from
Aug 4, 2015
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
94 changes: 88 additions & 6 deletions elixir-smie.el
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@
(not (or (memq (char-before) '(?\{ ?\[))
(looking-back elixir-smie--operator-regexp (- (point) 3) t))))

(defun elixir-smie-last-line-end-with-block-operator-p ()
"Return non-nil if the previous line ends with a `->' operator."
(save-excursion
(forward-line -1)
(move-end-of-line 1)
(looking-back elixir-smie--block-operator-regexp (- (point) 3) t)))

(defun elixir-smie-last-line-start-with-block-operator-p ()
(save-excursion
(forward-line -1)
(beginning-of-line)
(looking-at "^\s+->.+$")))

(defun elixir-smie--semi-ends-match ()
"Return non-nil if the current line concludes a match block."
(when (not (eobp))
Expand Down Expand Up @@ -292,7 +305,8 @@
(t (smie-rule-parent))))
(`(:before . "MATCH-STATEMENT-DELIMITER")
(cond
((smie-rule-parent-p "MATCH-STATEMENT-DELIMITER")
((and (smie-rule-parent-p "do")
(smie-rule-hanging-p))
(smie-rule-parent))
((and (not (smie-rule-sibling-p))
(nth 2 smie--parent)
Expand All @@ -302,12 +316,29 @@
(not (nth 2 smie--parent))
(smie-rule-hanging-p))
(smie-rule-parent))))
(`(:after . "MATCH-STATEMENT-DELIMITER")
(cond
((and (smie-rule-parent-p "MATCH-STATEMENT-DELIMITER")
(smie-rule-hanging-p)
(smie-rule-sibling-p))
(if (elixir-smie-last-line-end-with-block-operator-p)
(smie-rule-parent)
0))
(t
(smie-rule-parent))))
(`(:before . "fn")
(smie-rule-parent))
(`(:before . "do:")
(cond
((smie-rule-parent-p "def" "if")
(smie-rule-parent))))
(`(:before . "do")
(cond
((and (smie-rule-parent-p "case")
(smie-rule-hanging-p))
(smie-rule-parent 2))
(t
elixir-smie-indent-basic)))
(`(:before . "end")
(smie-rule-parent))
;; Closing paren on the other line
Expand All @@ -323,12 +354,30 @@
;; ()
;; .....
((smie-rule-parent-p "do")
(smie-rule-parent elixir-smie-indent-basic))
(smie-rule-parent))
(t (smie-rule-parent))))
(`(:before . "[")
(cond
((smie-rule-hanging-p)
(smie-rule-parent))))
(`(:before . "{")
(cond
;; Example
;;
;; case parse do
;; { [ help: true ], _, _ }
;; -> :help
;; { _, [ user, project, count ], _ }
((and (not (smie-rule-hanging-p))
(smie-rule-parent-p "do"))
(smie-rule-parent))
((and (smie-rule-parent-p "MATCH-STATEMENT-DELIMITER")
(not (smie-rule-hanging-p)))
(if (elixir-smie-last-line-end-with-block-operator-p)
(smie-rule-parent elixir-smie-indent-basic)
(if (elixir-smie-last-line-start-with-block-operator-p)
(smie-rule-parent -2)
(smie-rule-parent))))))
(`(:after . "{")
(cond
((smie-rule-hanging-p)
Expand All @@ -346,7 +395,21 @@
(`(:before . "->")
(cond
((smie-rule-hanging-p)
(smie-rule-parent elixir-smie-indent-basic))))
(smie-rule-parent elixir-smie-indent-basic))
;; Example
;;
;; case parse do
;; { [ help: true ], _, _ }
;; -> :help
;; ...
((and (not (smie-rule-hanging-p))
(smie-rule-parent-p "do"))
elixir-smie-indent-basic)
((and (not (smie-rule-hanging-p))
(smie-rule-parent-p "MATCH-STATEMENT-DELIMITER"))
(smie-rule-parent)
)
))
(`(:after . "->")
(cond
;; This first condition is kind of complicated so I'll try to make this
Expand All @@ -372,8 +435,11 @@
;; Otherwise, if just indent by two.
((smie-rule-hanging-p)
(cond
((smie-rule-parent-p "after" "catch" "do" "rescue" "try")
elixir-smie-indent-basic)
((smie-rule-parent-p "catch" "rescue")
(smie-rule-parent (+ elixir-smie-indent-basic
elixir-smie-indent-basic)))
((smie-rule-parent-p "after" "do" "try")
(smie-rule-parent elixir-smie-indent-basic))
(t (smie-rule-parent elixir-smie-indent-basic))))))
(`(:before . ";")
(cond
Expand All @@ -390,16 +456,32 @@
((and (smie-rule-parent-p "if")
(smie-rule-hanging-p))
(smie-rule-parent))
((and (smie-rule-parent-p "else")
(smie-rule-hanging-p))
(smie-rule-parent elixir-smie-indent-basic))
((smie-rule-parent-p "after" "catch" "def" "defmodule" "defp" "do" "else"
"fn" "if" "rescue" "try" "unless")
(smie-rule-parent elixir-smie-indent-basic))
(smie-rule-parent))
;; Example
;;
;; case parse do
;; { [ help: true ], _, _ }
;; -> :help
;; { _, [ user, project, count ], _ }
;; -> { user, project, count }
;; ...
((and (smie-rule-parent-p "->")
(smie-rule-hanging-p))
(smie-rule-parent))
))
(`(:after . ";")
(cond
((smie-rule-parent-p "def")
(smie-rule-parent))
((smie-rule-parent-p "if")
(smie-rule-parent))
((smie-rule-parent-p "after")
(smie-rule-parent elixir-smie-indent-basic))
((and (smie-rule-parent-p "(")
(boundp 'smie--parent)
(save-excursion
Expand Down
46 changes: 46 additions & 0 deletions test/elixir-mode-indentation-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,52 @@ defmodule Foo do
end
")

(elixir-def-indentation-test complex-case-with-matches (:tags '(indentation))
"
case parse do
{ [ help: true ], _, _ }
-> :help
{ _, [ user, project, count ], _ } ->
{ user, project, count }
{ _, [ user, project ], _ } -> { user, project, @default_count }
{ _, [ _, project ], _ } -> { _, project, @default_count }
_ -> :help
end"
"
case parse do
{ [ help: true ], _, _ }
-> :help
{ _, [ user, project, count ], _ } ->
{ user, project, count }
{ _, [ user, project ], _ } -> { user, project, @default_count }
{ _, [ _, project ], _ } -> { _, project, @default_count }
_ -> :help
end")


(elixir-def-indentation-test complex-case-with-matches/2 (:tags '(indentation))
"
case parse do
{ [ help: true ], _, _ }
-> :help
{ _, [ user, project, count ], _ }
-> { user, project, count }
{ _, [ user, project ], _ }
-> { user, project, @default_count }
_ -> :help
end"
"
case parse do
{ [ help: true ], _, _ }
-> :help
{ _, [ user, project, count ], _ }
-> { user, project, count }
{ _, [ user, project ], _ }
-> { user, project, @default_count }
_ -> :help
end")


;; We don't want automatic whitespace cleanup here because of the significant
;; whitespace after `Record' above. By setting `whitespace-action' to nil,
;; `whitespace-mode' won't automatically clean up trailing whitespace (in my
Expand Down