diff --git a/elm.cabal b/elm.cabal index 8e2221d76..b3bafb3fc 100644 --- a/elm.cabal +++ b/elm.cabal @@ -237,6 +237,7 @@ Executable lamdera -- CLI Experimental Lamdera.CLI.Annotate Lamdera.CLI.Interpreter + Lamdera.CLI.Format Lamdera.AppConfig Lamdera.Checks diff --git a/ext-common/Ext/ElmFormat.hs b/ext-common/Ext/ElmFormat.hs index 205f372b2..b9e342e87 100644 --- a/ext-common/Ext/ElmFormat.hs +++ b/ext-common/Ext/ElmFormat.hs @@ -9,6 +9,7 @@ module Ext.ElmFormat where import Data.Text (Text) import qualified Data.Text as T import qualified Data.Text as Text +import System.IO (FilePath) import System.IO.Unsafe (unsafePerformIO) import qualified System.Process @@ -20,31 +21,35 @@ import qualified ElmFormat.Cli -- import qualified ElmFormat.Render.Text as Render import ElmVersion import ElmFormat.Messages +import Reporting.Annotation (Located(..), Region(..), Position(..)) +import CommandLine.InfoFormatter (ToConsole(..)) -formatWithEmbedded :: Text -> Either ElmFormat.Messages.InfoMessage Text -formatWithEmbedded inputText = do - ElmFormat.Cli.format ElmVersion.Elm_0_19 ("stdin:nofilepath", inputText) +formatWithEmbedded :: FilePath -> Text -> Either ElmFormat.Messages.InfoMessage Text +formatWithEmbedded filePath inputText = do + ElmFormat.Cli.format ElmVersion.Elm_0_19 (filePath, inputText) -format :: Text -> (Either Text Text) -format text = do - case formatWithEmbedded text of - Left err -> - Left $ Lamdera.show_ err - Right formatted -> - Right formatted +format :: FilePath -> Text -> (Either Text Text) +format filePath text = do + case formatWithEmbedded filePath text of + Left err -> Left $ toConsole err + Right formatted -> Right formatted formatOrPassthrough :: Text -> Text formatOrPassthrough text = do - case format text of + case format "stdin:nofilepath" text of Right formatted -> formatted - Left err -> do - -- let !_ = Lamdera.debug $ "🔥💅 warning: " <> show err - text + Left _ -> text +formatOrPassthroughFile :: FilePath -> Text -> Text +formatOrPassthroughFile filePath text = do + case format filePath text of + Right formatted -> formatted + Left _ -> text + -- Old versions that rely on local elm-format binary diff --git a/extra/Lamdera/CLI.hs b/extra/Lamdera/CLI.hs index dd777f784..d9101d957 100644 --- a/extra/Lamdera/CLI.hs +++ b/extra/Lamdera/CLI.hs @@ -1,6 +1,6 @@ {-# LANGUAGE OverloadedStrings #-} -module Lamdera.CLI (live, login, check, deploy, reset, update, annotate, eval) where +module Lamdera.CLI (live, login, check, deploy, reset, update, annotate, eval, format) where import Text.Read (readMaybe) import qualified Text.PrettyPrint.ANSI.Leijen as P @@ -193,6 +193,25 @@ eval = Terminal.Command "eval" (Common summary) details example args noFlags Lamdera.CLI.Interpreter.run +-- FORMAT +-- @LAMDERA Stub - intercepted in Terminal.hs + + +format :: Terminal.Command +format = + let + summary = + "Format Elm source files." + + details = + "The `format` command is handled directly by elm-format for perfect compatibility." + + example = + reflow "See elm-format documentation at " + in + Terminal.Command "format" (Common summary) details example noArgs noFlags (\_ _ -> return ()) + + -- HELPERS @@ -203,4 +222,4 @@ stack docs = reflow :: String -> P.Doc reflow string = - P.fillSep $ map P.text $ words string + P.fillSep $ map P.text $ words string \ No newline at end of file diff --git a/extra/Lamdera/CLI/Format.hs b/extra/Lamdera/CLI/Format.hs new file mode 100644 index 000000000..564ebb74a --- /dev/null +++ b/extra/Lamdera/CLI/Format.hs @@ -0,0 +1,9 @@ +module Lamdera.CLI.Format + ( run + ) where + +import qualified ElmFormat.Cli + +-- | Delegate to elm-format +run :: [String] -> IO () +run args = ElmFormat.Cli.mainIO args \ No newline at end of file diff --git a/terminal/impl/Terminal.hs b/terminal/impl/Terminal.hs index 27e9e1788..695829abb 100644 --- a/terminal/impl/Terminal.hs +++ b/terminal/impl/Terminal.hs @@ -31,6 +31,7 @@ import qualified Terminal.Error as Error import qualified Lamdera.Version +import qualified Lamdera.CLI.Format import qualified Sanity -- COMMAND @@ -86,21 +87,25 @@ app intro outro commands = Exit.exitSuccess command : chunks -> - do case List.find (\cmd -> toName cmd == command) commands of - Nothing -> - Error.exitWithUnknown command (map toName commands) + -- @LAMDERA format intercept + if command == "format" then + do Lamdera.CLI.Format.run chunks + else + do case List.find (\cmd -> toName cmd == command) commands of + Nothing -> + Error.exitWithUnknown command (map toName commands) - Just (Command _ _ details example args_ flags_ callback) -> - if elem "--help" chunks then - Error.exitWithHelp (Just command) details example args_ flags_ + Just (Command _ _ details example args_ flags_ callback) -> + if elem "--help" chunks then + Error.exitWithHelp (Just command) details example args_ flags_ - else - case snd $ Chomp.chomp Nothing chunks args_ flags_ of - Right (argsValue, flagsValue) -> - callback argsValue flagsValue + else + case snd $ Chomp.chomp Nothing chunks args_ flags_ of + Right (argsValue, flagsValue) -> + callback argsValue flagsValue - Left err -> - Error.exitWithError err + Left err -> + Error.exitWithError err diff --git a/terminal/src/Main.hs b/terminal/src/Main.hs index c5f48c04a..b1c60e269 100644 --- a/terminal/src/Main.hs +++ b/terminal/src/Main.hs @@ -48,6 +48,7 @@ main = , Lamdera.CLI.update , Lamdera.CLI.annotate , Lamdera.CLI.eval + , Lamdera.CLI.format -- , reactor -- , bump -- , diff