Skip to content

Commit 9d16cb4

Browse files
TikhonJelvisjaspervdj
authored andcommitted
Add option to automatically group imports
1 parent 4647a2a commit 9d16cb4

File tree

5 files changed

+691
-43
lines changed

5 files changed

+691
-43
lines changed

data/stylish-haskell.yaml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,107 @@ steps:
294294
# Default: false
295295
post_qualify: false
296296

297+
# Automatically group imports based on their module names, with
298+
# a blank line separating each group. Groups are ordered in
299+
# alphabetical order.
300+
#
301+
# By default, this groups by the first part of each module's
302+
# name (Control.* will be grouped together, Data.*... etc), but
303+
# this can be configured with the group_patterns setting.
304+
#
305+
# When enabled, this rewrites existing blank lines and groups.
306+
#
307+
# - true: Group imports by the first part of the module name.
308+
#
309+
# > import Control.Applicative
310+
# > import Control.Monad
311+
# > import Control.Monad.MonadError
312+
# >
313+
# > import Data.Functor
314+
#
315+
# - false: Keep import groups as-is (still sorting and
316+
# formatting the imports within each group)
317+
#
318+
# > import Control.Monad
319+
# > import Data.Functor
320+
# >
321+
# > import Control.Applicative
322+
# > import Control.Monad.MonadError
323+
#
324+
# Default: false
325+
group_imports: false
326+
327+
# A list of rules specifying how to group modules and how to
328+
# order the groups.
329+
#
330+
# Each rule has a match field; the rule only applies to module
331+
# names matched by this pattern. Patterns are POSIX extended
332+
# regular expressions; see the documentation of Text.Regex.TDFA
333+
# for details:
334+
# https://hackage.haskell.org/package/regex-tdfa-1.3.1.2/docs/Text-Regex-TDFA.html
335+
#
336+
# Rules are processed in order, so only the *first* rule that
337+
# matches a specific module will apply. Any module names that do
338+
# not match a single rule will be put into a single group at the
339+
# end of the import block.
340+
#
341+
# Example: group MyApp modules first, with everything else in
342+
# one group at the end.
343+
#
344+
# group_rules:
345+
# - match: "^MyApp\\>"
346+
#
347+
# > import MyApp
348+
# > import MyApp.Foo
349+
# >
350+
# > import Control.Monad
351+
# > import MyApps
352+
# > import Test.MyApp
353+
#
354+
# A rule can also optionally have a sub_group pattern. Imports
355+
# that match the rule will be broken up into further groups by
356+
# the part of the module name matched by the sub_group pattern.
357+
#
358+
# Example: group MyApp modules first, then everything else
359+
# sub-grouped by the first part of the module name.
360+
#
361+
# group_rules:
362+
# - match: "^MyApp\\>"
363+
# - match: "."
364+
# sub_group: "^[^.]+"
365+
#
366+
# > import MyApp
367+
# > import MyApp.Foo
368+
# >
369+
# > import Control.Applicative
370+
# > import Control.Monad
371+
# >
372+
# > import Data.Map
373+
#
374+
# A pattern only needs to match part of the module name, which
375+
# could be in the middle. You can use ^pattern to anchor to the
376+
# beginning of the module name, pattern$ to anchor to the end
377+
# and ^pattern$ to force a full match. Example:
378+
#
379+
# - "Test\\." would match "Test.Foo" and "Foo.Test.Lib"
380+
# - "^Test\\." would match "Test.Foo" but not "Foo.Test.Lib"
381+
# - "\\.Test$" would match "Foo.Test" but not "Foo.Test.Lib"
382+
# - "^Test$" would *only* match "Test"
383+
#
384+
# You can use \\< and \\> to anchor against the beginning and
385+
# end of words, respectively. For example:
386+
#
387+
# - "^Test\\." would match "Test.Foo" but not "Test" or "Tests"
388+
# - "^Test\\>" would match "Test.Foo" and "Test", but not
389+
# "Tests"
390+
#
391+
# The default is a single rule that matches everything and
392+
# sub-groups based on the first component of the module name.
393+
#
394+
# Default: [{ "match" : ".*", "sub_group": "^[^.]+" }]
395+
group_rules:
396+
- match: ".*"
397+
sub_group: "^[^.]+"
297398

298399
# Language pragmas
299400
- language_pragmas:

lib/Language/Haskell/Stylish/Config.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ parseImports config o = fmap (Imports.step columns) $ Imports.Options
287287
<*> o A..:? "separate_lists" A..!= def Imports.separateLists
288288
<*> o A..:? "space_surround" A..!= def Imports.spaceSurround
289289
<*> o A..:? "post_qualify" A..!= def Imports.postQualified
290+
<*> o A..:? "group_imports" A..!= def Imports.groupImports
291+
<*> o A..:? "group_rules" A..!= def Imports.groupRules
290292
where
291293
def f = f Imports.defaultOptions
292294

0 commit comments

Comments
 (0)