@@ -13,10 +13,11 @@ import Data.Argonaut.Encode (assoc, encodeJson, extend)
1313import Data.Either (Either )
1414import Data.Maybe (Maybe (..), fromMaybe , maybe )
1515import Data.Traversable (traverse )
16- import PureScript.CST.Tidy (TypeArrowOption (..), UnicodeOption (..))
16+ import PureScript.CST.Tidy (ImportWrapOption (..), TypeArrowOption (..), UnicodeOption (..))
1717
1818type 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
2728defaults :: FormatOptions
2829defaults =
29- { indent: 2
30+ { importWrap: ImportWrapSource
31+ , indent: 2
3032 , operatorsFile: Nothing
3133 , ribbon: 1.0
3234 , typeArrowPlacement: TypeArrowFirst
@@ -37,7 +39,17 @@ defaults =
3739formatOptions :: ArgParser FormatOptions
3840formatOptions =
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.\n Default. 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.\n Defaults to 2."
4355 # Arg .int
@@ -88,14 +100,16 @@ unicodeOption =
88100fromJson :: Json -> Either JsonDecodeError FormatOptions
89101fromJson 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
106120toJson :: FormatOptions -> Json
107121toJson 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"
0 commit comments