mirror of
https://github.com/anoma/juvix.git
synced 2024-12-20 21:31:48 +03:00
75bce8f665
* Closes #2392 Changes checklist ----------------- * [X] Abstract out data types for stored module representation (`ModuleInfo` in `Juvix.Compiler.Store.Language`) * [X] Adapt the parser to operate per-module * [X] Adapt the scoper to operate per-module * [X] Adapt the arity checker to operate per-module * [X] Adapt the type checker to operate per-module * [x] Adapt Core transformations to operate per-module * [X] Adapt the pipeline functions in `Juvix.Compiler.Pipeline` * [X] Add `Juvix.Compiler.Pipeline.Driver` which drives the per-module compilation process * [x] Implement module saving / loading in `Pipeline.Driver` * [x] Detect cyclic module dependencies in `Pipeline.Driver` * [x] Cache visited modules in memory in `Pipeline.Driver` to avoid excessive disk operations and repeated hash re-computations * [x] Recompile a module if one of its dependencies needs recompilation and contains functions that are always inlined. * [x] Fix identifier dependencies for mutual block creation in `Internal.fromConcrete` - Fixed by making textually later definitions depend on earlier ones. - Now instances are used for resolution only after the textual point of their definition. - Similarly, type synonyms will be unfolded only after the textual point of their definition. * [x] Fix CLI * [x] Fix REPL * [x] Fix highlighting * [x] Fix HTML generation * [x] Adapt test suite
111 lines
4.0 KiB
Haskell
111 lines
4.0 KiB
Haskell
module Commands.Format where
|
|
|
|
import Commands.Base
|
|
import Commands.Format.Options
|
|
import Data.Text qualified as Text
|
|
import Juvix.Formatter
|
|
|
|
data FormatNoEditRenderMode
|
|
= ReformattedFile Text
|
|
| InputPath (Path Abs File)
|
|
| Silent
|
|
|
|
data FormatRenderMode
|
|
= EditInPlace FormattedFileInfo
|
|
| NoEdit FormatNoEditRenderMode
|
|
|
|
data FormatTarget
|
|
= TargetFile (Path Abs File)
|
|
| TargetProject (Path Abs Dir)
|
|
| TargetStdin
|
|
|
|
isTargetProject :: FormatTarget -> Bool
|
|
isTargetProject = \case
|
|
TargetProject {} -> True
|
|
_ -> False
|
|
|
|
targetFromOptions :: (Members '[Embed IO, App] r) => FormatOptions -> Sem r FormatTarget
|
|
targetFromOptions opts = do
|
|
globalOpts <- askGlobalOptions
|
|
let isStdin = globalOpts ^. globalStdin
|
|
f <- mapM filePathToAbs (opts ^. formatInput)
|
|
pkgDir <- askPkgDir
|
|
case f of
|
|
Just (Left p) -> return (TargetFile p)
|
|
Just Right {} -> return (TargetProject pkgDir)
|
|
Nothing -> do
|
|
isPackageGlobal <- askPackageGlobal
|
|
if
|
|
| isStdin -> return TargetStdin
|
|
| not (isPackageGlobal) -> return (TargetProject pkgDir)
|
|
| otherwise -> do
|
|
printFailureExit $
|
|
Text.unlines
|
|
[ "juvix format error: either 'JUVIX_FILE_OR_PROJECT' or '--stdin' option must be specified",
|
|
"Use the --help option to display more usage information."
|
|
]
|
|
|
|
runCommand :: forall r. (Members '[Embed IO, App, TaggedLock, Resource, Files] r) => FormatOptions -> Sem r ()
|
|
runCommand opts = do
|
|
target <- targetFromOptions opts
|
|
runOutputSem (renderFormattedOutput target opts) $ runScopeFileApp $ do
|
|
res <- case target of
|
|
TargetFile p -> format p
|
|
TargetProject p -> formatProject p
|
|
TargetStdin -> do
|
|
entry <- getEntryPointStdin
|
|
runReader entry formatStdin
|
|
|
|
let exitFail :: IO a
|
|
exitFail = exitWith (ExitFailure 1)
|
|
case res of
|
|
FormatResultFail -> embed exitFail
|
|
FormatResultNotFormatted ->
|
|
{- use exit code 1 for
|
|
* unformatted files when using --check
|
|
* when running the formatter on a Juvix project
|
|
-}
|
|
when (opts ^. formatCheck || isTargetProject target) (embed exitFail)
|
|
FormatResultOK -> pure ()
|
|
|
|
renderModeFromOptions :: FormatTarget -> FormatOptions -> FormattedFileInfo -> FormatRenderMode
|
|
renderModeFromOptions target opts formattedInfo
|
|
| opts ^. formatInPlace = whenContentsModified (EditInPlace formattedInfo)
|
|
| opts ^. formatCheck = NoEdit Silent
|
|
| otherwise = case target of
|
|
TargetFile {} -> NoEdit (ReformattedFile (formattedInfo ^. formattedFileInfoContents))
|
|
TargetProject {} -> whenContentsModified (NoEdit (InputPath (formattedInfo ^. formattedFileInfoPath)))
|
|
TargetStdin -> NoEdit (ReformattedFile (formattedInfo ^. formattedFileInfoContents))
|
|
where
|
|
whenContentsModified :: FormatRenderMode -> FormatRenderMode
|
|
whenContentsModified res
|
|
| formattedInfo ^. formattedFileInfoContentsModified = res
|
|
| otherwise = NoEdit Silent
|
|
|
|
renderFormattedOutput :: forall r. (Members '[Embed IO, App, Resource, Files] r) => FormatTarget -> FormatOptions -> FormattedFileInfo -> Sem r ()
|
|
renderFormattedOutput target opts fInfo = do
|
|
let renderMode = renderModeFromOptions target opts fInfo
|
|
outputResult renderMode
|
|
where
|
|
outputResult :: FormatRenderMode -> Sem r ()
|
|
outputResult = \case
|
|
EditInPlace i@FormattedFileInfo {..} ->
|
|
runTempFileIO
|
|
. restoreFileOnError _formattedFileInfoPath
|
|
$ writeFile' _formattedFileInfoPath (i ^. formattedFileInfoContents)
|
|
NoEdit m -> case m of
|
|
ReformattedFile ts -> renderStdOut ts
|
|
InputPath p -> say (pack (toFilePath p))
|
|
Silent -> return ()
|
|
|
|
runScopeFileApp :: (Members '[App, Embed IO, TaggedLock] r) => Sem (ScopeEff ': r) a -> Sem r a
|
|
runScopeFileApp = interpret $ \case
|
|
ScopeFile p -> do
|
|
let appFile =
|
|
AppPath
|
|
{ _pathPath = mkPrepath (toFilePath p),
|
|
_pathIsInput = False
|
|
}
|
|
runPipeline appFile upToScoping
|
|
ScopeStdin e -> runPipelineEntry e upToScoping
|