Skip to content

Simplify haskell-indentation-phrase-rest #1269

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 3 commits into from
Apr 10, 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
82 changes: 17 additions & 65 deletions haskell-indentation.el
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,6 @@
:group 'haskell
:prefix "haskell-indentation-")

(defcustom haskell-indentation-indent-leftmost t
"Indent to the left margin after certain keywords.
For example after \"let .. in\", \"case .. of\"). If set to t it
will only indent to the left. If nil only relative to the
containing expression. If set to the symbol 'both then both
positions are allowed."
:type 'symbol
:group 'haskell-indentation)

(defcustom haskell-indentation-layout-offset 2
"Extra indentation to add before expressions in a Haskell layout list."
:type 'integer
Expand All @@ -73,11 +64,6 @@ positions are allowed."
:type 'integer
:group 'haskell-indentation)

(defcustom haskell-indentation-ifte-offset 2
"Extra indentation after the keywords \"if\", \"then\", or \"else\"."
:type 'integer
:group 'haskell-indentation)

(defcustom haskell-indentation-where-pre-offset 2
"Extra indentation before the keyword \"where\"."
:type 'integer
Expand Down Expand Up @@ -520,7 +506,6 @@ fixes up only indentation."
("type" . haskell-indentation-data)
("newtype" . haskell-indentation-data)
("if" . haskell-indentation-if)
("case" . haskell-indentation-case)
("let" .
,(apply-partially 'haskell-indentation-phrase
'(haskell-indentation-declaration-layout
Expand All @@ -534,6 +519,10 @@ fixes up only indentation."
("rec" .
,(apply-partially 'haskell-indentation-with-starter
'haskell-indentation-expression-layout))
("case" .
,(apply-partially 'haskell-indentation-phrase
'(haskell-indentation-expression
"of" haskell-indentation-case-layout)))
("\\" .
,(apply-partially 'haskell-indentation-with-starter
'haskell-indentation-lambda-maybe-lambdacase))
Expand Down Expand Up @@ -574,7 +563,7 @@ fixes up only indentation."

(defun haskell-indentation-case-layout ()
"Parse layout list with case expressions."
(haskell-indentation-layout #'haskell-indentation-case-item))
(haskell-indentation-layout #'haskell-indentation-case))

(defun haskell-indentation-lambda-maybe-lambdacase ()
"Parse lambda or lambda-case expression.
Expand Down Expand Up @@ -761,7 +750,7 @@ Skip the keyword or parenthesis." ; FIXME: better description needed
((equal current-token end)
(haskell-indentation-read-next-token))))))

(defun haskell-indentation-case-item-alternative ()
(defun haskell-indentation-case-alternative ()
"" ; FIXME
(setq left-indent (current-column))
(haskell-indentation-separated #'haskell-indentation-expression "," nil)
Expand All @@ -772,15 +761,15 @@ Skip the keyword or parenthesis." ; FIXME: better description needed
;; otherwise fallthrough
))

(defun haskell-indentation-case-item ()
(defun haskell-indentation-case ()
"" ; FIXME
(haskell-indentation-expression)
(cond ((eq current-token 'end-tokens)
(haskell-indentation-add-indentation current-indent))
((string= current-token "|")
(haskell-indentation-with-starter
(apply-partially #'haskell-indentation-separated
#'haskell-indentation-case-item-alternative "|" nil)
#'haskell-indentation-case-alternative "|" nil)
nil))
((string= current-token "->")
(haskell-indentation-statement-right #'haskell-indentation-expression))
Expand Down Expand Up @@ -957,24 +946,14 @@ layout starts."
(haskell-indentation-with-starter
(lambda ()
(haskell-indentation-separated
#'haskell-indentation-case-item-alternative "|" nil))
#'haskell-indentation-case-alternative "|" nil))
nil)
(haskell-indentation-phrase-rest
'(haskell-indentation-expression
"then" haskell-indentation-expression
"else" haskell-indentation-expression))))
nil))

(defun haskell-indentation-case ()
"" ; FIXME
(haskell-indentation-with-starter
(lambda ()
(haskell-indentation-expression)
(when (string= current-token "of")
(haskell-indentation-with-starter
#'haskell-indentation-case-layout)))
nil))

(defun haskell-indentation-phrase (phrase)
"" ; FIXME
(haskell-indentation-with-starter
Expand Down Expand Up @@ -1002,41 +981,14 @@ layout starts."
(t (throw 'parse-end nil))))
((null (cdr phrase)))
((equal (cadr phrase) current-token)
(let* ((on-new-line (= (current-column)
(haskell-indentation-current-indentation)))
(lines-between (- parse-line-number starter-line))
(left-indent (if (<= lines-between 0)
left-indent
starter-indent)))
(haskell-indentation-read-next-token)
(when (eq current-token 'end-tokens)
(cond ((member (cadr phrase) '("then" "else"))
(haskell-indentation-add-indentation
(+ starter-indent haskell-indentation-ifte-offset)))

((member (cadr phrase) '("in" "->"))
;; expression ending in another expression
(when (or (not haskell-indentation-indent-leftmost)
(eq haskell-indentation-indent-leftmost 'both))
(haskell-indentation-add-indentation
(+ starter-indent haskell-indentation-starter-offset)))
(when haskell-indentation-indent-leftmost
(haskell-indentation-add-indentation
(if on-new-line
(+ left-indent haskell-indentation-starter-offset)
left-indent))))
(t
(when (or (not haskell-indentation-indent-leftmost)
(eq haskell-indentation-indent-leftmost 'both))
(haskell-indentation-add-indentation
(+ starter-indent haskell-indentation-starter-offset)))
(when haskell-indentation-indent-leftmost
(haskell-indentation-add-indentation
(if on-new-line
(+ left-indent haskell-indentation-starter-offset)
left-indent)))))
(throw 'parse-end nil))
(setq phrase1 (cddr phrase))))
(haskell-indentation-read-next-token)
(when (eq current-token 'end-tokens)
(haskell-indentation-add-indentation
(+ starter-indent haskell-indentation-starter-offset))
(haskell-indentation-add-indentation
(+ left-indent haskell-indentation-starter-offset))
(throw 'parse-end nil))
(setq phrase1 (cddr phrase)))
((string= (cadr phrase) "in"))))))

(defun haskell-indentation-add-indentation (indent)
Expand Down
8 changes: 4 additions & 4 deletions tests/haskell-indentation-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ fun = \\x -> do"
(hindent-test "16a A lambda""
fun = \\x ->"
(1 0)
(2 2))
(2 2 8))

(hindent-test "16u Lambda and a do block""
fun = \\x → do"
Expand All @@ -378,7 +378,7 @@ fun = \\x → do"
(hindent-test "16au A lambda""
fun = \\x →"
(1 0)
(2 2))
(2 2 8))

(hindent-test "17a* A type for a function""
fun :: Int
Expand Down Expand Up @@ -514,7 +514,7 @@ x = let y
z"
(1 0)
(2 4)
(3 6))
(3 2 6))

(hindent-test "19c* \"let\" in a \"do\"""
x = do
Expand Down Expand Up @@ -826,7 +826,7 @@ fact n = case n of
_ | n > 0
, True == True -> n * fact (n - 1)"
(1 0)
(2 2)
(2 2 11)
;; returns (0 2 2 6), to investigate
(3 0 2 6)
(4 4)
Expand Down