@@ -40,7 +40,7 @@ main = do
40
40
Opts {.. } <- execParser optsParserInfo
41
41
let formatOne' =
42
42
formatOne
43
- optCabal
43
+ optConfigFileOpts
44
44
optMode
45
45
optSourceType
46
46
optConfig
@@ -67,7 +67,7 @@ main = do
67
67
-- | Format a single input.
68
68
formatOne ::
69
69
-- | How to use .cabal files
70
- CabalOpts ->
70
+ ConfigFileOpts ->
71
71
-- | Mode of operation
72
72
Mode ->
73
73
-- | The 'SourceType' requested by the user
@@ -77,7 +77,7 @@ formatOne ::
77
77
-- | File to format or stdin as 'Nothing'
78
78
Maybe FilePath ->
79
79
IO ExitCode
80
- formatOne CabalOpts {.. } mode reqSourceType rawConfig mpath =
80
+ formatOne ConfigFileOpts {.. } mode reqSourceType rawConfig mpath =
81
81
withPrettyOrmoluExceptions (cfgColorMode rawConfig) $ do
82
82
let getCabalInfoForSourceFile' sourceFile = do
83
83
cabalSearchResult <- getCabalInfoForSourceFile sourceFile
@@ -99,21 +99,24 @@ formatOne CabalOpts {..} mode reqSourceType rawConfig mpath =
99
99
<> sourceFile
100
100
return (Just cabalInfo)
101
101
CabalFound cabalInfo -> return (Just cabalInfo)
102
+ getDotOrmoluForSourceFile' sourceFile = do
103
+ if optDoNotUseDotOrmolu
104
+ then return Nothing
105
+ else Just <$> getDotOrmoluForSourceFile sourceFile
102
106
case FP. normalise <$> mpath of
103
107
-- input source = STDIN
104
108
Nothing -> do
105
- resultConfig <-
106
- ( if optDoNotUseCabal
107
- then pure Nothing
108
- else case optStdinInputFile of
109
- Just stdinInputFile ->
110
- getCabalInfoForSourceFile' stdinInputFile
111
- Nothing -> throwIO OrmoluMissingStdinInputFile
112
- )
113
- >>= patchConfig Nothing
109
+ mcabalInfo <- case (optStdinInputFile, optDoNotUseCabal) of
110
+ (_, True ) -> return Nothing
111
+ (Nothing , False ) -> throwIO OrmoluMissingStdinInputFile
112
+ (Just inputFile, False ) -> getCabalInfoForSourceFile' inputFile
113
+ mdotOrmolu <- case optStdinInputFile of
114
+ Nothing -> return Nothing
115
+ Just inputFile -> getDotOrmoluForSourceFile' inputFile
116
+ config <- patchConfig Nothing mcabalInfo mdotOrmolu
114
117
case mode of
115
118
Stdout -> do
116
- ormoluStdin resultConfig >>= TIO. putStr
119
+ ormoluStdin config >>= TIO. putStr
117
120
return ExitSuccess
118
121
InPlace -> do
119
122
hPutStrLn
@@ -126,41 +129,44 @@ formatOne CabalOpts {..} mode reqSourceType rawConfig mpath =
126
129
originalInput <- getContentsUtf8
127
130
let stdinRepr = " <stdin>"
128
131
formattedInput <-
129
- ormolu resultConfig stdinRepr originalInput
132
+ ormolu config stdinRepr originalInput
130
133
handleDiff originalInput formattedInput stdinRepr
131
134
-- input source = a file
132
135
Just inputFile -> do
133
- resultConfig <-
134
- ( if optDoNotUseCabal
135
- then pure Nothing
136
- else getCabalInfoForSourceFile' inputFile
137
- )
138
- >>= patchConfig (Just (detectSourceType inputFile))
136
+ mcabalInfo <-
137
+ if optDoNotUseCabal
138
+ then return Nothing
139
+ else getCabalInfoForSourceFile' inputFile
140
+ mdotOrmolu <- getDotOrmoluForSourceFile' inputFile
141
+ config <-
142
+ patchConfig
143
+ (Just (detectSourceType inputFile))
144
+ mcabalInfo
145
+ mdotOrmolu
139
146
case mode of
140
147
Stdout -> do
141
- ormoluFile resultConfig inputFile >>= TIO. putStr
148
+ ormoluFile config inputFile >>= TIO. putStr
142
149
return ExitSuccess
143
150
InPlace -> do
144
151
-- ormoluFile is not used because we need originalInput
145
152
originalInput <- readFileUtf8 inputFile
146
153
formattedInput <-
147
- ormolu resultConfig inputFile originalInput
154
+ ormolu config inputFile originalInput
148
155
when (formattedInput /= originalInput) $
149
156
writeFileUtf8 inputFile formattedInput
150
157
return ExitSuccess
151
158
Check -> do
152
159
-- ormoluFile is not used because we need originalInput
153
160
originalInput <- readFileUtf8 inputFile
154
161
formattedInput <-
155
- ormolu resultConfig inputFile originalInput
162
+ ormolu config inputFile originalInput
156
163
handleDiff originalInput formattedInput inputFile
157
164
where
158
- patchConfig mdetectedSourceType mcabalInfo = do
165
+ patchConfig mdetectedSourceType mcabalInfo mdotOrmolu = do
159
166
let sourceType =
160
167
fromMaybe
161
168
ModuleSource
162
169
(reqSourceType <|> mdetectedSourceType)
163
- mdotOrmolu <- traverse parseDotOrmoluForSourceFile mcabalInfo
164
170
let mfixityOverrides = fst <$> mdotOrmolu
165
171
mmoduleReexports = snd <$> mdotOrmolu
166
172
return $
@@ -189,8 +195,8 @@ data Opts = Opts
189
195
optMode :: ! Mode ,
190
196
-- | Ormolu 'Config'
191
197
optConfig :: ! (Config RegionIndices ),
192
- -- | Options related to info extracted from .cabal files
193
- optCabal :: CabalOpts ,
198
+ -- | Options related to info extracted from files
199
+ optConfigFileOpts :: ConfigFileOpts ,
194
200
-- | Source type option, where 'Nothing' means autodetection
195
201
optSourceType :: ! (Maybe SourceType ),
196
202
-- | Haskell source files to format or stdin (when the list is empty)
@@ -208,10 +214,12 @@ data Mode
208
214
Check
209
215
deriving (Eq , Show )
210
216
211
- -- | Configuration related to .cabal files .
212
- data CabalOpts = CabalOpts
217
+ -- | Options related to configuration stored in the file system .
218
+ data ConfigFileOpts = ConfigFileOpts
213
219
{ -- | DO NOT extract default-extensions and dependencies from .cabal files
214
220
optDoNotUseCabal :: Bool ,
221
+ -- | DO NOT look for @.ormolu@ files
222
+ optDoNotUseDotOrmolu :: Bool ,
215
223
-- | Optional path to a file which will be used to find a .cabal file
216
224
-- when using input from stdin
217
225
optStdinInputFile :: Maybe FilePath
@@ -262,21 +270,23 @@ optsParser =
262
270
]
263
271
)
264
272
<*> configParser
265
- <*> cabalOptsParser
273
+ <*> configFileOptsParser
266
274
<*> sourceTypeParser
267
275
<*> (many . strArgument . mconcat )
268
276
[ metavar " FILE" ,
269
277
help " Haskell source files to format or stdin (the default)"
270
278
]
271
279
272
- cabalOptsParser :: Parser CabalOpts
273
- cabalOptsParser =
274
- CabalOpts
280
+ configFileOptsParser :: Parser ConfigFileOpts
281
+ configFileOptsParser =
282
+ ConfigFileOpts
275
283
<$> (switch . mconcat )
276
284
[ long " no-cabal" ,
277
- help $
278
- " Do not extract default-extensions and dependencies from .cabal files"
279
- ++ " , do not look for .ormolu files"
285
+ help " Do not extract default-extensions and dependencies from .cabal files"
286
+ ]
287
+ <*> (switch . mconcat )
288
+ [ long " no-dot-ormolu" ,
289
+ help " Do not look for .ormolu files"
280
290
]
281
291
<*> (optional . strOption . mconcat )
282
292
[ long " stdin-input-file" ,
0 commit comments