Skip to content

Commit b9f1a46

Browse files
authored
Merge pull request #1869 from Commelina/master
haskell-cabal: fix indentation with leading commas and newlines
2 parents e9c3567 + 648d7c2 commit b9f1a46

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

haskell-cabal.el

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,20 @@ Possible results are \\='section-header \\='subsection-header \\='section-data
474474
:end (save-match-data (haskell-cabal-subsection-end))
475475
:data-start-column (save-excursion (goto-char (match-end 0))
476476
(current-column))
477+
;; Note: Redundant leading commas are allowed since Cabal 2.2.
478+
;; Example:
479+
;; build-depends:
480+
;; , base
481+
;; , text
482+
;;
483+
;; Note: More than one newlines are allowed after the subsection name.
484+
;; Example:
485+
;; build-depends:
486+
;;
487+
;;
488+
;; base
477489
:data-indent-column (save-excursion (goto-char (match-end 0))
478-
(when (looking-at "\n +\\(\\w*\\)") (goto-char (match-beginning 1)))
490+
(when (looking-at "\n[\n\t ]* +\\([\\w,]*\\)") (goto-char (match-beginning 1)))
479491
(current-column)
480492
)))))
481493

@@ -1058,11 +1070,24 @@ Source names from main-is and c-sources sections are left untouched
10581070
(cl-case (haskell-cabal-classify-line)
10591071
(section-data
10601072
(save-excursion
1061-
(let ((indent (haskell-cabal-section-data-indent-column
1062-
(haskell-cabal-subsection))))
1063-
(indent-line-to indent)
1073+
(let* ((subsection (haskell-cabal-subsection))
1074+
(beginning (haskell-cabal-section-start subsection))
1075+
(indent-column (haskell-cabal-section-data-indent-column subsection))
1076+
(subsection-leading-comma-p (save-excursion
1077+
(goto-char beginning)
1078+
(looking-at ",\\|\n[ \t\n]*,"))))
1079+
(indent-line-to indent-column)
10641080
(beginning-of-line)
1065-
(when (looking-at "[ ]*\\([ ]\\{2\\},[ ]*\\)")
1081+
;; Only do extra adjustment if the first item is not comma leading.
1082+
;; Example of the two cases:
1083+
;;
1084+
;; | aaa | aaa
1085+
;; | , bbb | , bbb
1086+
;;
1087+
;; | , aaa | , aaa
1088+
;; | , bbb | , bbb
1089+
(when (and (not subsection-leading-comma-p)
1090+
(looking-at "[ ]*\\([ ]\\{2\\},[ ]*\\)"))
10661091
(replace-match ", " t t nil 1)))))
10671092
(empty
10681093
(indent-relative)))

0 commit comments

Comments
 (0)