about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/haskell-mode-20180913.348/haskell-align-imports.el
blob: 694ee259295b0a6f73c7d9b74ee6e0e9ea449734 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
;;; haskell-align-imports.el --- Align the import lines in a Haskell file -*- lexical-binding: t -*-

;; Copyright (C) 2010  Chris Done

;; Author: Chris Done <chrisdone@gmail.com>

;; This file is not part of GNU Emacs.

;; This program is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of
;; the License, or (at your option) any later version.

;; This program is distributed in the hope that it will be
;; useful, but WITHOUT ANY WARRANTY; without even the implied
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE.  See the GNU General Public License for more details.

;; You should have received a copy of the GNU General Public
;; License along with this program.  If not, see
;; <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Consider the following imports list:
;;
;; import One
;; import Two as A
;; import qualified Three
;; import qualified Four as PRELUDE
;; import Five (A)
;; import Six (A,B)
;; import qualified Seven (A,B)
;; import "abc" Eight
;; import "abc" Nine as TWO
;; import qualified "abc" Ten
;; import qualified "defg" Eleven as PRELUDE
;; import "barmu" Twelve (A)
;; import "zotconpop" Thirteen (A,B)
;; import qualified "z" Fourteen (A,B)
;; import Fifteen hiding (A)
;; import Sixteen as TWO hiding (A)
;; import qualified Seventeen hiding (A)
;; import qualified Eighteen as PRELUDE hiding (A)
;; import "abc" Nineteen hiding (A)
;; import "abc" Twenty as TWO hiding (A)
;;
;; When haskell-align-imports is run within the same buffer, the
;; import list is transformed to:
;;
;; import "abc"            Eight
;; import qualified        Eighteen as PRELUDE hiding (A)
;; import qualified "defg" Eleven as PRELUDE
;; import                  Fifteen hiding (A)
;; import                  Five (A)
;; import qualified        Four as PRELUDE
;; import qualified "z"    Fourteen  (A,B)
;; import "abc"            Nine as TWO
;; import "abc"            Nineteen hiding (A)
;; import                  One
;; import qualified        Seven (A,B)
;; import qualified        Seventeen hiding (A)
;; import                  Six (A,B)
;; import                  Sixteen as TWO hiding (A)
;; import qualified "abc"  Ten
;; import "zotconpop"      Thirteen (A,B)
;; import qualified        Three
;; import "barmu"          Twelve (A)
;; import "abc"            Twenty as TWO hiding (A)
;; import                  Two as A
;;
;; If you want everything after module names to be padded out, too,
;; customize `haskell-align-imports-pad-after-name', and you'll get:
;;
;; import                  One
;; import                  Two       as A
;; import qualified        Three
;; import qualified        Four      as PRELUDE
;; import                  Five      (A)
;; import                  Six       (A,B)
;; import qualified        Seven     (A,B)
;; import "abc"            Eight
;; import "abc"            Nine      as TWO
;; import qualified "abc"  Ten
;; import qualified "defg" Eleven    as PRELUDE
;; import "barmu"          Twelve    (A)
;; import "zotconpop"      Thirteen  (A,B)
;; import qualified "z"    Fourteen  (A,B)
;; import                  Fifteen   hiding (A)
;; import                  Sixteen   as TWO hiding (A)
;; import qualified        Seventeen hiding (A)
;; import qualified        Eighteen  as PRELUDE hiding (A)
;; import "abc"            Nineteen  hiding (A)
;; import "abc"            Twenty    as TWO hiding (A)

;;; Code:

(require 'cl-lib)

(defvar haskell-align-imports-regexp
  (concat "^\\(import[ ]+\\)"
          "\\(qualified \\)?"
          "[ ]*\\(\"[^\"]*\" \\)?"
          "[ ]*\\([A-Za-z0-9_.']+\\)"
          "[ ]*\\([ ]*as [A-Z][^ ]*\\)?"
          "[ ]*\\((.*)\\)?"
          "\\([ ]*hiding (.*)\\)?"
          "\\( -- .*\\)?[ ]*$")
  "Regex used for matching components of an import.")

(defcustom haskell-align-imports-pad-after-name
  nil
  "Pad layout after the module name also."
  :type 'boolean
  :group 'haskell-interactive)

;;;###autoload
(defun haskell-align-imports ()
  "Align all the imports in the buffer."
  (interactive)
  (when (haskell-align-imports-line-match)
    (save-excursion
      (goto-char (point-min))
      (let* ((imports (haskell-align-imports-collect))
             (padding (haskell-align-imports-padding imports)))
        (mapc (lambda (x)
                (goto-char (cdr x))
                (delete-region (point) (line-end-position))
                (insert (haskell-align-imports-chomp
                         (haskell-align-imports-fill padding (car x)))))
              imports))))
  nil)

(defun haskell-align-imports-line-match ()
  "Try to match the current line as a regexp."
  (let ((line (buffer-substring-no-properties (line-beginning-position)
                                              (line-end-position))))
    (if (string-match "^import " line)
        line
      nil)))

(defun haskell-align-imports-collect ()
  "Collect a list of mark / import statement pairs."
  (let ((imports '()))
    (while (not (or (equal (point) (point-max)) (haskell-align-imports-after-imports-p)))
      (let ((line (haskell-align-imports-line-match-it)))
        (when line
          (let ((match
                 (haskell-align-imports-merge-parts
                  (cl-loop for i from 1 to 8
                           collect (haskell-align-imports-chomp (match-string i line))))))
            (setq imports (cons (cons match (line-beginning-position))
                                imports)))))
      (forward-line))
    imports))

(defun haskell-align-imports-merge-parts (l)
  "Merge together parts of an import statement that shouldn't be separated."
  (let ((parts (apply #'vector l))
        (join (lambda (ls)
                (cl-reduce (lambda (a b)
                             (concat a
                                     (if (and (> (length a) 0)
                                              (> (length b) 0))
                                         " "
                                       "")
                                     b))
                           ls))))
    (if haskell-align-imports-pad-after-name
        (list (funcall join (list (aref parts 0)
                                  (aref parts 1)
                                  (aref parts 2)))
              (aref parts 3)
              (funcall join (list (aref parts 4)
                                  (aref parts 5)
                                  (aref parts 6)))
              (aref parts 7))
      (list (funcall join (list (aref parts 0)
                                (aref parts 1)
                                (aref parts 2)))
            (funcall join (list (aref parts 3)
                                (aref parts 4)
                                (aref parts 5)
                                (aref parts 6)
                                (aref parts 7)))))))

(defun haskell-align-imports-chomp (str)
  "Chomp leading and tailing whitespace from STR."
  (if str
      (replace-regexp-in-string "\\(^[[:space:]\n]*\\|[[:space:]\n]*$\\)" ""
                                str)
    ""))

(defun haskell-align-imports-padding (imports)
  "Find the padding for each part of the import statements."
  (if (null imports)
      imports
    (cl-reduce (lambda (a b) (cl-mapcar #'max a b))
               (mapcar (lambda (x) (mapcar #'length (car x)))
                       imports))))

(defun haskell-align-imports-fill (padding line)
  "Fill an import line using the padding worked out from all statements."
  (mapconcat #'identity
             (cl-mapcar (lambda (pad part)
                          (if (> (length part) 0)
                              (concat part (make-string (- pad (length part)) ? ))
                            (make-string pad ? )))
                        padding
                        line)
             " "))

(defun haskell-align-imports-line-match-it ()
  "Try to match the current line as a regexp."
  (let ((line (buffer-substring-no-properties (line-beginning-position)
                                              (line-end-position))))
    (if (string-match haskell-align-imports-regexp line)
        line
      nil)))

(defun haskell-align-imports-after-imports-p ()
  "Are we after the imports list?"
  (save-excursion
    (goto-char (line-beginning-position))
    (let ((case-fold-search nil))
      (not (not (search-forward-regexp "\\( = \\|\\<instance\\>\\| :: \\| ∷ \\)"
                                       (line-end-position) t 1))))))

(provide 'haskell-align-imports)

;;; haskell-align-imports.el ends here