Skip to content

Commit d4d62a3

Browse files
authored
Implement --import-wrap options (#46)
* Implement --import-wrap options * Update .tidyrc.json * Pass option through from CLI.
1 parent dcfc6e3 commit d4d62a3

File tree

7 files changed

+125
-11
lines changed

7 files changed

+125
-11
lines changed

.tidyrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"importWrap": "source",
23
"indent": 2,
34
"operatorsFile": null,
45
"ribbon": 1,

bin/Bin/FormatOptions.purs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import Data.Argonaut.Encode (assoc, encodeJson, extend)
1313
import Data.Either (Either)
1414
import Data.Maybe (Maybe(..), fromMaybe, maybe)
1515
import Data.Traversable (traverse)
16-
import PureScript.CST.Tidy (TypeArrowOption(..), UnicodeOption(..))
16+
import PureScript.CST.Tidy (ImportWrapOption(..), TypeArrowOption(..), UnicodeOption(..))
1717

1818
type FormatOptions =
19-
{ indent :: Int
19+
{ importWrap :: ImportWrapOption
20+
, indent :: Int
2021
, operatorsFile :: Maybe String
2122
, ribbon :: Number
2223
, typeArrowPlacement :: TypeArrowOption
@@ -26,7 +27,8 @@ type FormatOptions =
2627

2728
defaults :: FormatOptions
2829
defaults =
29-
{ indent: 2
30+
{ importWrap: ImportWrapSource
31+
, indent: 2
3032
, operatorsFile: Nothing
3133
, ribbon: 1.0
3234
, typeArrowPlacement: TypeArrowFirst
@@ -37,7 +39,17 @@ defaults =
3739
formatOptions :: ArgParser FormatOptions
3840
formatOptions =
3941
Arg.fromRecord
40-
{ indent:
42+
{ importWrap:
43+
Arg.choose "import wrap"
44+
[ Arg.flag [ "--import-wrap-source", "-iws" ]
45+
"Imports are wrapped only when breaks are in the source.\nDefault. Works well with IDE imports."
46+
$> ImportWrapSource
47+
, Arg.flag [ "--import-wrap-auto", "-iwa" ]
48+
"Imports are wrapped based on the `--width` option."
49+
$> ImportWrapAuto
50+
]
51+
# Arg.default defaults.importWrap
52+
, indent:
4153
Arg.argument [ "--indent", "-i" ]
4254
"Number of spaces to use as indentation.\nDefaults to 2."
4355
# Arg.int
@@ -88,14 +100,16 @@ unicodeOption =
88100
fromJson :: Json -> Either JsonDecodeError FormatOptions
89101
fromJson json = do
90102
obj <- decodeJson json
103+
importWrap <- traverse importWrapFromString =<< obj .:? "importWrap"
91104
indent <- obj .:? "indent"
92105
operatorsFile <- obj .:? "operatorsFile"
93106
ribbon <- obj .:? "ribbon"
94107
typeArrowPlacement <- traverse typeArrowPlacementFromString =<< obj .:? "typeArrowPlacement"
95108
unicode <- traverse unicodeFromString =<< obj .:? "unicode"
96109
width <- obj .:? "width"
97110
pure
98-
{ indent: fromMaybe defaults.indent indent
111+
{ importWrap: fromMaybe defaults.importWrap importWrap
112+
, indent: fromMaybe defaults.indent indent
99113
, operatorsFile: operatorsFile <|> defaults.operatorsFile
100114
, ribbon: fromMaybe defaults.ribbon ribbon
101115
, typeArrowPlacement: fromMaybe defaults.typeArrowPlacement typeArrowPlacement
@@ -106,6 +120,7 @@ fromJson json = do
106120
toJson :: FormatOptions -> Json
107121
toJson options =
108122
jsonEmptyObject
123+
# extend (assoc "importWrap" (importWrapToString options.importWrap))
109124
# extend (assoc "indent" options.indent)
110125
# extend (assoc "operatorsFile" (maybe jsonNull encodeJson options.operatorsFile))
111126
# extend (assoc "ribbon" options.ribbon)
@@ -136,3 +151,14 @@ unicodeToString = case _ of
136151
UnicodeSource -> "source"
137152
UnicodeAlways -> "always"
138153
UnicodeNever -> "never"
154+
155+
importWrapFromString :: String -> Either JsonDecodeError ImportWrapOption
156+
importWrapFromString = case _ of
157+
"source" -> pure ImportWrapSource
158+
"auto" -> pure ImportWrapAuto
159+
other -> throwError $ UnexpectedValue (Json.fromString other)
160+
161+
importWrapToString :: ImportWrapOption -> String
162+
importWrapToString = case _ of
163+
ImportWrapSource -> "source"
164+
ImportWrapAuto -> "auto"

bin/Main.purs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ formatCommand args operators contents = do
290290
ParseSucceeded ok -> do
291291
let
292292
opts = defaultFormatOptions
293-
{ operators = remapOperators operators ok
293+
{ importWrap = args.importWrap
294+
, operators = remapOperators operators ok
294295
, typeArrowPlacement = args.typeArrowPlacement
295296
, unicode = args.unicode
296297
}
@@ -299,7 +300,8 @@ formatCommand args operators contents = do
299300
let
300301
opts =
301302
defaultFormatOptions
302-
{ operators = remapOperators operators ok
303+
{ importWrap = args.importWrap
304+
, operators = remapOperators operators ok
303305
, typeArrowPlacement = args.typeArrowPlacement
304306
, unicode = args.unicode
305307
}
@@ -308,7 +310,8 @@ formatCommand args operators contents = do
308310
Left err.error
309311

310312
type WorkerConfig =
311-
{ indent :: Int
313+
{ importWrap :: String
314+
, indent :: Int
312315
, operatorsFile :: String
313316
, ribbon :: Number
314317
, typeArrowPlacement :: String
@@ -318,7 +321,8 @@ type WorkerConfig =
318321

319322
toWorkerConfig :: FormatOptions -> WorkerConfig
320323
toWorkerConfig options =
321-
{ indent: options.indent
324+
{ importWrap: FormatOptions.importWrapToString options.importWrap
325+
, indent: options.indent
322326
, operatorsFile: fromMaybe ".tidyoperators.default" options.operatorsFile
323327
, ribbon: options.ribbon
324328
, typeArrowPlacement: FormatOptions.typeArrowPlacementToString options.typeArrowPlacement
@@ -357,7 +361,10 @@ formatWorker = Worker.make \{ receive, reply, workerData: { shouldCheck, operato
357361

358362
formatOptions :: FormatOptions
359363
formatOptions =
360-
{ indent: config.indent
364+
{ importWrap:
365+
fromRight' (\_ -> unsafeCrashWith "Unknown importWrap value") do
366+
FormatOptions.importWrapFromString config.importWrap
367+
, indent: config.indent
361368
, operatorsFile: Nothing
362369
, ribbon: config.ribbon
363370
, typeArrowPlacement:

src/PureScript/CST/Tidy.purs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module PureScript.CST.Tidy
22
( FormatOptions
33
, defaultFormatOptions
44
, TypeArrowOption(..)
5+
, ImportWrapOption(..)
56
, Format
67
, formatModule
78
, formatDecl
@@ -45,11 +46,18 @@ data TypeArrowOption
4546

4647
derive instance eqTypeArrowOption :: Eq TypeArrowOption
4748

49+
data ImportWrapOption
50+
= ImportWrapSource
51+
| ImportWrapAuto
52+
53+
derive instance eqImportWrapOption :: Eq ImportWrapOption
54+
4855
type FormatOptions e a =
4956
{ formatError :: e -> FormatDoc a
5057
, unicode :: UnicodeOption
5158
, typeArrowPlacement :: TypeArrowOption
5259
, operators :: PrecedenceMap
60+
, importWrap :: ImportWrapOption
5361
}
5462

5563
defaultFormatOptions :: forall e a. FormatError e => FormatOptions e a
@@ -58,6 +66,7 @@ defaultFormatOptions =
5866
, unicode: UnicodeSource
5967
, typeArrowPlacement: TypeArrowFirst
6068
, operators: Map.empty
69+
, importWrap: ImportWrapSource
6170
}
6271

6372
class FormatError e where
@@ -172,10 +181,17 @@ formatModule conf (Module { header: ModuleHeader header, body: ModuleBody body }
172181
anchor (foldMap (formatParenListNonEmpty NotGrouped formatExport conf) header.exports)
173182
`space`
174183
anchor (formatToken conf header."where")
175-
, joinWithMap break (formatImportDecl conf) header.imports
184+
, case conf.importWrap of
185+
ImportWrapAuto ->
186+
imports
187+
ImportWrapSource ->
188+
locally (_ { pageWidth = top, ribbonRatio = 1.0 }) imports
176189
, joinWithMap break (formatDecl conf) body.decls
177190
, foldr (formatComment leadingLineComment) mempty body.trailingComments
178191
]
192+
where
193+
imports =
194+
joinWithMap break (formatImportDecl conf) header.imports
179195

180196
formatExport :: forall e a. Format (Export e) e a
181197
formatExport conf = case _ of

test/FormatDirective.purs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ parseDirectivesFromModule (Module { header: ModuleHeader header, body }) =
112112
, operators: default.operators
113113
, unicode: opts.unicode
114114
, typeArrowPlacement: opts.typeArrowPlacement
115+
, importWrap: opts.importWrap
115116
}
116117
}
117118
where

test/snapshots/ImportWrap.output

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
module ImportWrap where
2+
3+
import Test (Test, Test, Test, Test, Test, Test, Test, Test, Test)
4+
import Test
5+
( Test
6+
, Test
7+
, Test
8+
, Test
9+
, Test
10+
, Test
11+
, Test
12+
, Test
13+
, Test
14+
)
15+
16+
-- @format --import-wrap-auto --width 20
17+
module ImportWrap where
18+
19+
import Test
20+
( Test
21+
, Test
22+
, Test
23+
, Test
24+
, Test
25+
, Test
26+
, Test
27+
, Test
28+
, Test
29+
)
30+
import Test
31+
( Test
32+
, Test
33+
, Test
34+
, Test
35+
, Test
36+
, Test
37+
, Test
38+
, Test
39+
, Test
40+
)
41+
42+
-- @format --import-wrap-source --width 20
43+
module ImportWrap where
44+
45+
import Test (Test, Test, Test, Test, Test, Test, Test, Test, Test)
46+
import Test
47+
( Test
48+
, Test
49+
, Test
50+
, Test
51+
, Test
52+
, Test
53+
, Test
54+
, Test
55+
, Test
56+
)

test/snapshots/ImportWrap.purs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- @format --import-wrap-source --width 20
2+
-- @format --import-wrap-auto --width 20
3+
module ImportWrap where
4+
5+
import Test (Test, Test, Test, Test, Test, Test, Test, Test, Test)
6+
import Test (Test, Test,
7+
Test, Test, Test, Test, Test, Test, Test)

0 commit comments

Comments
 (0)