Skip to content

Commit c6b6db9

Browse files
committed
Improve elm format error messages and file handling
1 parent 72e8b88 commit c6b6db9

File tree

2 files changed

+31
-34
lines changed

2 files changed

+31
-34
lines changed

ext-common/Ext/ElmFormat.hs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Ext.ElmFormat where
99
import Data.Text (Text)
1010
import qualified Data.Text as T
1111
import qualified Data.Text as Text
12+
import System.IO (FilePath)
1213

1314
import System.IO.Unsafe (unsafePerformIO)
1415
import qualified System.Process
@@ -20,31 +21,35 @@ import qualified ElmFormat.Cli
2021
-- import qualified ElmFormat.Render.Text as Render
2122
import ElmVersion
2223
import ElmFormat.Messages
24+
import Reporting.Annotation (Located(..), Region(..), Position(..))
25+
import CommandLine.InfoFormatter (ToConsole(..))
2326

2427

25-
formatWithEmbedded :: Text -> Either ElmFormat.Messages.InfoMessage Text
26-
formatWithEmbedded inputText = do
27-
ElmFormat.Cli.format ElmVersion.Elm_0_19 ("stdin:nofilepath", inputText)
28+
formatWithEmbedded :: FilePath -> Text -> Either ElmFormat.Messages.InfoMessage Text
29+
formatWithEmbedded filePath inputText = do
30+
ElmFormat.Cli.format ElmVersion.Elm_0_19 (filePath, inputText)
2831

2932

30-
format :: Text -> (Either Text Text)
31-
format text = do
32-
case formatWithEmbedded text of
33-
Left err ->
34-
Left $ Lamdera.show_ err
35-
Right formatted ->
36-
Right formatted
33+
format :: FilePath -> Text -> (Either Text Text)
34+
format filePath text = do
35+
case formatWithEmbedded filePath text of
36+
Left err -> Left $ toConsole err
37+
Right formatted -> Right formatted
3738

3839

3940
formatOrPassthrough :: Text -> Text
4041
formatOrPassthrough text = do
41-
case format text of
42+
case format "stdin" text of
4243
Right formatted -> formatted
43-
Left err -> do
44-
-- let !_ = Lamdera.debug $ "🔥💅 warning: " <> show err
45-
text
44+
Left _ -> text
4645

4746

47+
formatOrPassthroughFile :: FilePath -> Text -> Text
48+
formatOrPassthroughFile filePath text = do
49+
case format filePath text of
50+
Right formatted -> formatted
51+
Left _ -> text
52+
4853

4954
-- Old versions that rely on local elm-format binary
5055

extra/Lamdera/CLI/Format.hs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ run inputs flags =
142142

143143
formatText :: Format -> T.Text -> IO ()
144144
formatText flags input =
145-
case ElmFormat.format input of
145+
case ElmFormat.format "stdin" input of
146146
Right formatted ->
147147
TIO.putStr formatted
148148
Left err ->
@@ -199,26 +199,18 @@ formatSingleFile :: Format -> FilePath -> IO ()
199199
formatSingleFile flags path = do
200200
TIO.putStrLn $ "Processing file " <> T.pack path
201201
input <- TIO.readFile path
202-
case ElmFormat.format input of
203-
Right formatted ->
204-
if _validate flags
205-
then
206-
if input == formatted
207-
then return ()
208-
else do
209-
TIO.putStrLn $ "File " <> T.pack path <> " would be reformatted"
210-
Exit.exitFailure
202+
case ElmFormat.format path input of
203+
Right formatted ->
204+
if _validate flags && formatted /= input
205+
then do
206+
TIO.putStrLn $ "File " <> T.pack path <> " would be reformatted"
207+
Exit.exitFailure
211208
else case _output flags of
212-
Just outputPath ->
213-
TIO.writeFile outputPath formatted
214-
Nothing ->
215-
TIO.writeFile path formatted
216-
Left err ->
217-
do
218-
TIO.putStrLn $ "Unable to parse file " <> T.pack path <> ": " <> err <> " To see a detailed explanation, run elm make on the file."
219-
if _validate flags
220-
then Exit.exitFailure
221-
else return ()
209+
Just outputPath -> TIO.writeFile outputPath formatted
210+
Nothing -> TIO.writeFile path formatted
211+
Left err -> do
212+
TIO.putStrLn err
213+
Exit.exitFailure
222214

223215
stack :: [P.Doc] -> P.Doc
224216
stack docs =

0 commit comments

Comments
 (0)