Skip to content

Commit d577d2b

Browse files
committed
Merge pull request #1275 from gracjan/pr-fix-module-indentation
Fix module declaration indentation
2 parents 84cc626 + b3a9e1b commit d577d2b

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

haskell-indentation.el

+27-14
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ fixes up only indentation."
493493
("type" . haskell-indentation-data)
494494
("newtype" . haskell-indentation-data)
495495
("import" . haskell-indentation-import)
496+
("where" . haskell-indentation-toplevel-where)
496497
("class" . haskell-indentation-class-declaration)
497498
("instance" . haskell-indentation-class-declaration)
498499
("deriving" . haskell-indentation-deriving))
@@ -732,21 +733,32 @@ For example
732733
"Parse module declaration."
733734
(haskell-indentation-with-starter
734735
(lambda ()
735-
(let ((current-indent (current-column)))
736-
(haskell-indentation-read-next-token)
737-
(when (string= current-token "(")
738-
(haskell-indentation-list
739-
#'haskell-indentation-module-export
740-
")" ","))
741-
(when (eq current-token 'end-tokens)
742-
(haskell-indentation-add-indentation current-indent)
743-
(throw 'parse-end nil))
744-
(when (string= current-token "where")
736+
(haskell-indentation-read-next-token)
737+
(when (string= current-token "(")
738+
(haskell-indentation-list
739+
#'haskell-indentation-module-export
740+
")" ","))
741+
(if (string= current-token "where")
745742
(haskell-indentation-read-next-token)
746-
(when (eq current-token 'end-tokens)
747-
(haskell-indentation-add-layout-indent)
743+
744+
(when (eq current-token 'end-tokens)
745+
(when (member following-token '(value no-following-token "("))
746+
(haskell-indentation-add-indentation
747+
(+ starter-indent haskell-indentation-starter-offset))
748+
(haskell-indentation-add-indentation
749+
(+ left-indent haskell-indentation-starter-offset))
748750
(throw 'parse-end nil))
749-
(haskell-indentation-layout #'haskell-indentation-toplevel))))))
751+
(haskell-indentation-add-layout-indent)
752+
(throw 'parse-end nil))))))
753+
754+
(defun haskell-indentation-toplevel-where ()
755+
"Parse 'where' that we may hit as a standalone in module
756+
declaration."
757+
(haskell-indentation-read-next-token)
758+
759+
(when (eq current-token 'end-tokens)
760+
(haskell-indentation-add-layout-indent)
761+
(throw 'parse-end nil)))
750762

751763
(defun haskell-indentation-module-export ()
752764
"Parse export list."
@@ -986,7 +998,8 @@ layout starts."
986998
(haskell-indentation-expression-token-p following-token))
987999
(string= following-token ";")
9881000
(and (equal layout-indent 0)
989-
(member following-token (mapcar #'car haskell-indentation-toplevel-list))))
1001+
(member following-token (mapcar #'car haskell-indentation-toplevel-list))
1002+
(not (string= following-token "where"))))
9901003
(haskell-indentation-add-layout-indent))
9911004
(throw 'return nil))
9921005
(t (throw 'return nil))))))

tests/haskell-indentation-tests.el

+25-2
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ newtype instance T Char = TC Bool"
905905
(3 0)
906906
(4 0 26))
907907

908-
(hindent-test "52a* module simplest case two lines" "
908+
(hindent-test "52a module simplest case two lines" "
909909
module A.B
910910
where"
911911
(1 0)
@@ -917,7 +917,7 @@ module A.B where"
917917
(1 0)
918918
(2 0))
919919

920-
(hindent-test "52c* module with exports" "
920+
(hindent-test "52c module with exports" "
921921
module A.B
922922
( x
923923
, y
@@ -959,6 +959,29 @@ data Foo = Bar
959959
(2 9)
960960
(3 0 2 9))
961961

962+
(hindent-test "56 module name on next line" "
963+
module
964+
X"
965+
(1 0)
966+
(2 2))
967+
968+
(hindent-test "57 module continued" "
969+
module X"
970+
(1 0)
971+
(2 2))
972+
973+
(hindent-test "58 module where same line" "
974+
module X where"
975+
(1 0)
976+
(2 0))
977+
978+
(hindent-test "59 module where same line" "
979+
module X
980+
where"
981+
(1 0)
982+
(2 0)
983+
(3 0))
984+
962985
(ert-deftest haskell-indentation-ret-indents ()
963986
(with-temp-switch-to-buffer
964987
(haskell-mode)

0 commit comments

Comments
 (0)