Skip to content

Commit d99af72

Browse files
committed
Merge pull request #1019 from gracjan/pr-shallow-indentation
Support shallow indentation
2 parents ea5cc3b + 1c1d06e commit d99af72

File tree

2 files changed

+56
-54
lines changed

2 files changed

+56
-54
lines changed

haskell-indentation.el

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -794,11 +794,10 @@ parser. If parsing ends here, set indentation to left-indent."
794794
(haskell-indentation-add-left-indent)
795795
(haskell-indentation-add-indentation current-indent)
796796
(throw 'parse-end nil))
797-
(let ((current-indent (current-column)))
798-
(funcall parser)
799-
(when (equal current-token "where")
800-
(haskell-indentation-with-starter
801-
#'haskell-indentation-expression-layout nil))))
797+
(funcall parser)
798+
(when (equal current-token "where")
799+
(haskell-indentation-with-starter
800+
#'haskell-indentation-expression-layout nil)))
802801

803802
(defun haskell-indentation-guard ()
804803
"Parse \"guard\" statement."
@@ -833,35 +832,34 @@ parser. If parsing ends here, set indentation to left-indent."
833832

834833
(defun haskell-indentation-expression ()
835834
"Parse an expression until an unknown token is encountered."
836-
(let ((current-indent (current-column)))
837-
(catch 'return
838-
(while t
839-
(cond
840-
((memq current-token '(value operator))
841-
(haskell-indentation-read-next-token))
842-
((eq current-token 'end-tokens)
843-
(cond ((string= following-token "where")
844-
(haskell-indentation-add-where-pre-indent)) ; before a where
845-
((haskell-indentation-expression-token-p following-token)
846-
(haskell-indentation-add-indentation
847-
current-indent))) ; a normal expression
848-
(throw 'return nil))
849-
(t (let ((parser (assoc current-token
850-
haskell-indentation-expression-list)))
851-
(when (null parser)
852-
(throw 'return nil)) ; not expression token, so exit
853-
(funcall (cdr parser)) ; run parser
854-
(when (and (eq current-token 'end-tokens)
855-
(string= (car parser) "let")
856-
(= haskell-indentation-layout-offset current-indent)
857-
(haskell-indentation-expression-token-p following-token))
858-
;; inside a layout, after a let construct
859-
;; for example: "do let a = 20"
860-
(haskell-indentation-add-layout-indent)
861-
(throw 'parse-end nil))
862-
;; after an 'open' expression such as 'if', exit
863-
(unless (member (car parser) '("(" "[" "{" "case"))
864-
(throw 'return nil)))))))))
835+
(catch 'return
836+
(while t
837+
(cond
838+
((memq current-token '(value operator))
839+
(haskell-indentation-read-next-token))
840+
((eq current-token 'end-tokens)
841+
(cond ((string= following-token "where")
842+
(haskell-indentation-add-where-pre-indent)) ; before a where
843+
((haskell-indentation-expression-token-p following-token)
844+
(haskell-indentation-add-indentation
845+
current-indent))) ; a normal expression
846+
(throw 'return nil))
847+
(t (let ((parser (assoc current-token
848+
haskell-indentation-expression-list)))
849+
(when (null parser)
850+
(throw 'return nil)) ; not expression token, so exit
851+
(funcall (cdr parser)) ; run parser
852+
(when (and (eq current-token 'end-tokens)
853+
(string= (car parser) "let")
854+
(= haskell-indentation-layout-offset current-indent)
855+
(haskell-indentation-expression-token-p following-token))
856+
;; inside a layout, after a let construct
857+
;; for example: "do let a = 20"
858+
(haskell-indentation-add-layout-indent)
859+
(throw 'parse-end nil))
860+
;; after an 'open' expression such as 'if', exit
861+
(unless (member (car parser) '("(" "[" "{" "case"))
862+
(throw 'return nil))))))))
865863

866864
(defun haskell-indentation-separated (parser separator &optional stmt-separator)
867865
"Evaluate PARSER separated by SEPARATOR and STMT-SEPARATOR.

tests/haskell-indentation-tests.el

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -149,39 +149,39 @@ macro quotes them for you."
149149
function = Record
150150
{ field = 123 }"
151151
(1 0)
152-
(2 0 11))
152+
(2 0 2))
153153

154154
(hindent-test "2 Handle underscore in identifiers""
155155
function = do
156156
(_x) <- return ()
157157
z"
158158
(1 0)
159159
(2 2)
160-
(3 0 2 10))
160+
(3 0 2 4))
161161

162162
(hindent-test "2u Handle underscore in identifiers""
163163
function = do
164164
(_x) ← return ()
165165
z"
166166
(1 0)
167167
(2 2)
168-
(3 0 2 9))
168+
(3 0 2 4))
169169

170170
(hindent-test "2a Handle apostrophe in identifiers""
171171
function = do
172172
(_'x') <- return ()
173173
z"
174174
(1 0)
175175
(2 2)
176-
(3 0 2 12))
176+
(3 0 2 4))
177177

178178
(hindent-test "2au Handle apostrophe in identifiers""
179179
function = do
180180
(_'x') ← return ()
181181
z"
182182
(1 0)
183183
(2 2)
184-
(3 0 2 11))
184+
(3 0 2 4))
185185

186186
(hindent-test "3 Import statememnt symbol list 1""
187187
import Control.Concurrent
@@ -206,7 +206,7 @@ fun = [ x | y
206206
, z ]"
207207
(1 0)
208208
(2 10)
209-
(3 0 6))
209+
(3 0 2))
210210

211211
(hindent-test "5a* List comprehension""
212212
fun = [ x | y,
@@ -274,7 +274,7 @@ fun = x ++"
274274
fun = x
275275
++ z"
276276
(1 0)
277-
(2 0 6))
277+
(2 0 2))
278278

279279
(hindent-test "11 Guards with commas""
280280
clunky env var1 var2
@@ -283,7 +283,7 @@ clunky env var1 var2
283283
(1 0)
284284
(2 2)
285285
(3 2)
286-
(4 0 17))
286+
(4 0 4))
287287

288288
(hindent-test "11u Guards with commas""
289289
clunky env var1 var2
@@ -292,7 +292,7 @@ clunky env var1 var2
292292
(1 0)
293293
(2 2)
294294
(3 2)
295-
(4 0 16))
295+
(4 0 4))
296296

297297
(hindent-test "12 \"do\" as expression""
298298
fun = do { putStrLn \"X\";
@@ -576,8 +576,8 @@ foo = do
576576
return ()"
577577
(1 0)
578578
(2 2)
579-
(3 0 2 17)
580-
(4 0 2 17))
579+
(3 0 2 4)
580+
(4 0 2 4))
581581

582582
(hindent-test "25a* support scoped type declarations" "
583583
foo = do
@@ -619,13 +619,13 @@ foo = do
619619
f = a (a 'A)
620620
(a 'A)
621621
"
622-
(2 0 4))
622+
(2 0 2))
623623

624624
(hindent-test "28b character literal (escape sequence)" "
625625
f = '\\\\'
626626
627627
"
628-
(2 0 4))
628+
(2 0 2))
629629

630630

631631
(hindent-test "28c name starting with a quote" "
@@ -638,28 +638,32 @@ function (Operation 'Init) = do
638638
test = [randomQQ| This is a quasiquote with the word in |]
639639
640640
"
641-
(2 0 7))
641+
(2 0 2))
642642

643643
(hindent-test "29b quasiquote multiple lines" "
644644
test = [randomQQ| This is
645645
a quasiquote
646646
with the word in |]
647647
648648
"
649-
(4 0 7))
649+
(4 0 2))
650+
650651
(hindent-test "30* parse '[] identifier correctly" "
651652
instance Callable '[]
652653
"
653654
(1 2))
655+
654656
(hindent-test "31* allow type class declaration without methods" "
655657
class Foo a where
656658
instance Bar Int
657659
"
658660
(2 0))
661+
659662
(hindent-test "32 allow type operators" "
660663
data (:.) a b = a :. b
661664
"
662665
(2 0 16))
666+
663667
(hindent-test "33* parse #else in CPP" "
664668
#ifdef FLAG
665669
foo = ()
@@ -716,7 +720,7 @@ s = do
716720
a <- \"multiline\\
717721
\\ line 2\"
718722
"
719-
(4 0 2 7))
723+
(4 0 2 4))
720724

721725
(hindent-test "39 do not crash after two multiline literals in do block" "
722726
servePost = do
@@ -725,13 +729,13 @@ servePost = do
725729
b <- queryT \"comma is important: , \\
726730
\\ line 2 \"
727731
"
728-
(6 0 2 7))
732+
(6 0 2 4))
729733

730734
(hindent-test "40 parse error in multiline tuple" "
731735
a = ( 1
732736
, "
733737
(2 4)
734-
(3 6))
738+
(3 2))
735739

736740
(hindent-test "41 open do inside a list" "
737741
x = asum [ withX $ do
@@ -857,8 +861,8 @@ fact n =
857861
(1 0)
858862
(2 2)
859863
(3 4)
860-
(4 4)
861-
(5 0 2 4 18))
864+
(4 0 2 4 9)
865+
(5 0 2 4 9))
862866

863867
(ert-deftest haskell-indentation-ret-indents ()
864868
(switch-to-buffer (get-buffer-create "source.hs"))

0 commit comments

Comments
 (0)