Skip to content

Commit bf2e5af

Browse files
committed
Merge pull request #1269 from gracjan/pr-phrase-rest
Simplify haskell-indentation-phrase-rest
2 parents d577d2b + 7b83c71 commit bf2e5af

File tree

2 files changed

+21
-69
lines changed

2 files changed

+21
-69
lines changed

haskell-indentation.el

+17-65
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,6 @@
4949
:group 'haskell
5050
:prefix "haskell-indentation-")
5151

52-
(defcustom haskell-indentation-indent-leftmost t
53-
"Indent to the left margin after certain keywords.
54-
For example after \"let .. in\", \"case .. of\"). If set to t it
55-
will only indent to the left. If nil only relative to the
56-
containing expression. If set to the symbol 'both then both
57-
positions are allowed."
58-
:type 'symbol
59-
:group 'haskell-indentation)
60-
6152
(defcustom haskell-indentation-layout-offset 2
6253
"Extra indentation to add before expressions in a Haskell layout list."
6354
:type 'integer
@@ -73,11 +64,6 @@ positions are allowed."
7364
:type 'integer
7465
:group 'haskell-indentation)
7566

76-
(defcustom haskell-indentation-ifte-offset 2
77-
"Extra indentation after the keywords \"if\", \"then\", or \"else\"."
78-
:type 'integer
79-
:group 'haskell-indentation)
80-
8167
(defcustom haskell-indentation-where-pre-offset 2
8268
"Extra indentation before the keyword \"where\"."
8369
:type 'integer
@@ -520,7 +506,6 @@ fixes up only indentation."
520506
("type" . haskell-indentation-data)
521507
("newtype" . haskell-indentation-data)
522508
("if" . haskell-indentation-if)
523-
("case" . haskell-indentation-case)
524509
("let" .
525510
,(apply-partially 'haskell-indentation-phrase
526511
'(haskell-indentation-declaration-layout
@@ -534,6 +519,10 @@ fixes up only indentation."
534519
("rec" .
535520
,(apply-partially 'haskell-indentation-with-starter
536521
'haskell-indentation-expression-layout))
522+
("case" .
523+
,(apply-partially 'haskell-indentation-phrase
524+
'(haskell-indentation-expression
525+
"of" haskell-indentation-case-layout)))
537526
("\\" .
538527
,(apply-partially 'haskell-indentation-with-starter
539528
'haskell-indentation-lambda-maybe-lambdacase))
@@ -574,7 +563,7 @@ fixes up only indentation."
574563

575564
(defun haskell-indentation-case-layout ()
576565
"Parse layout list with case expressions."
577-
(haskell-indentation-layout #'haskell-indentation-case-item))
566+
(haskell-indentation-layout #'haskell-indentation-case))
578567

579568
(defun haskell-indentation-lambda-maybe-lambdacase ()
580569
"Parse lambda or lambda-case expression.
@@ -821,7 +810,7 @@ Skip the keyword or parenthesis." ; FIXME: better description needed
821810
((equal current-token end)
822811
(haskell-indentation-read-next-token))))))
823812

824-
(defun haskell-indentation-case-item-alternative ()
813+
(defun haskell-indentation-case-alternative ()
825814
"" ; FIXME
826815
(setq left-indent (current-column))
827816
(haskell-indentation-separated #'haskell-indentation-expression "," nil)
@@ -832,15 +821,15 @@ Skip the keyword or parenthesis." ; FIXME: better description needed
832821
;; otherwise fallthrough
833822
))
834823

835-
(defun haskell-indentation-case-item ()
824+
(defun haskell-indentation-case ()
836825
"" ; FIXME
837826
(haskell-indentation-expression)
838827
(cond ((eq current-token 'end-tokens)
839828
(haskell-indentation-add-indentation current-indent))
840829
((string= current-token "|")
841830
(haskell-indentation-with-starter
842831
(apply-partially #'haskell-indentation-separated
843-
#'haskell-indentation-case-item-alternative "|" nil)
832+
#'haskell-indentation-case-alternative "|" nil)
844833
nil))
845834
((string= current-token "->")
846835
(haskell-indentation-statement-right #'haskell-indentation-expression))
@@ -1018,24 +1007,14 @@ layout starts."
10181007
(haskell-indentation-with-starter
10191008
(lambda ()
10201009
(haskell-indentation-separated
1021-
#'haskell-indentation-case-item-alternative "|" nil))
1010+
#'haskell-indentation-case-alternative "|" nil))
10221011
nil)
10231012
(haskell-indentation-phrase-rest
10241013
'(haskell-indentation-expression
10251014
"then" haskell-indentation-expression
10261015
"else" haskell-indentation-expression))))
10271016
nil))
10281017

1029-
(defun haskell-indentation-case ()
1030-
"" ; FIXME
1031-
(haskell-indentation-with-starter
1032-
(lambda ()
1033-
(haskell-indentation-expression)
1034-
(when (string= current-token "of")
1035-
(haskell-indentation-with-starter
1036-
#'haskell-indentation-case-layout)))
1037-
nil))
1038-
10391018
(defun haskell-indentation-phrase (phrase)
10401019
"" ; FIXME
10411020
(haskell-indentation-with-starter
@@ -1063,41 +1042,14 @@ layout starts."
10631042
(t (throw 'parse-end nil))))
10641043
((null (cdr phrase)))
10651044
((equal (cadr phrase) current-token)
1066-
(let* ((on-new-line (= (current-column)
1067-
(haskell-indentation-current-indentation)))
1068-
(lines-between (- parse-line-number starter-line))
1069-
(left-indent (if (<= lines-between 0)
1070-
left-indent
1071-
starter-indent)))
1072-
(haskell-indentation-read-next-token)
1073-
(when (eq current-token 'end-tokens)
1074-
(cond ((member (cadr phrase) '("then" "else"))
1075-
(haskell-indentation-add-indentation
1076-
(+ starter-indent haskell-indentation-ifte-offset)))
1077-
1078-
((member (cadr phrase) '("in" "->"))
1079-
;; expression ending in another expression
1080-
(when (or (not haskell-indentation-indent-leftmost)
1081-
(eq haskell-indentation-indent-leftmost 'both))
1082-
(haskell-indentation-add-indentation
1083-
(+ starter-indent haskell-indentation-starter-offset)))
1084-
(when haskell-indentation-indent-leftmost
1085-
(haskell-indentation-add-indentation
1086-
(if on-new-line
1087-
(+ left-indent haskell-indentation-starter-offset)
1088-
left-indent))))
1089-
(t
1090-
(when (or (not haskell-indentation-indent-leftmost)
1091-
(eq haskell-indentation-indent-leftmost 'both))
1092-
(haskell-indentation-add-indentation
1093-
(+ starter-indent haskell-indentation-starter-offset)))
1094-
(when haskell-indentation-indent-leftmost
1095-
(haskell-indentation-add-indentation
1096-
(if on-new-line
1097-
(+ left-indent haskell-indentation-starter-offset)
1098-
left-indent)))))
1099-
(throw 'parse-end nil))
1100-
(setq phrase1 (cddr phrase))))
1045+
(haskell-indentation-read-next-token)
1046+
(when (eq current-token 'end-tokens)
1047+
(haskell-indentation-add-indentation
1048+
(+ starter-indent haskell-indentation-starter-offset))
1049+
(haskell-indentation-add-indentation
1050+
(+ left-indent haskell-indentation-starter-offset))
1051+
(throw 'parse-end nil))
1052+
(setq phrase1 (cddr phrase)))
11011053
((string= (cadr phrase) "in"))))))
11021054

11031055
(defun haskell-indentation-add-indentation (indent)

tests/haskell-indentation-tests.el

+4-4
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ fun = \\x -> do"
375375
(hindent-test "16a A lambda""
376376
fun = \\x ->"
377377
(1 0)
378-
(2 2))
378+
(2 2 8))
379379

380380
(hindent-test "16u Lambda and a do block""
381381
fun = \\x → do"
@@ -385,7 +385,7 @@ fun = \\x → do"
385385
(hindent-test "16au A lambda""
386386
fun = \\x →"
387387
(1 0)
388-
(2 2))
388+
(2 2 8))
389389

390390
(hindent-test "17a* A type for a function""
391391
fun :: Int
@@ -521,7 +521,7 @@ x = let y
521521
z"
522522
(1 0)
523523
(2 4)
524-
(3 6))
524+
(3 2 6))
525525

526526
(hindent-test "19c* \"let\" in a \"do\"""
527527
x = do
@@ -833,7 +833,7 @@ fact n = case n of
833833
_ | n > 0
834834
, True == True -> n * fact (n - 1)"
835835
(1 0)
836-
(2 2)
836+
(2 2 11)
837837
;; returns (0 2 2 6), to investigate
838838
(3 0 2 6)
839839
(4 4)

0 commit comments

Comments
 (0)