Skip to content

Commit

Permalink
format Package.juvix when formatting project
Browse files Browse the repository at this point in the history
  • Loading branch information
janmasrovira committed Dec 5, 2024
1 parent 1a76df9 commit c15ca0e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
3 changes: 3 additions & 0 deletions app/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ getEntryPointStdin' RunAppIOArgs {..} = do
| otherwise -> return Nothing
set entryPointStdin estdin <$> entryPointFromGlobalOptionsNoFile root opts

askPackageDotJuvixPath :: (Members '[App] r) => Sem r (Path Abs File)
askPackageDotJuvixPath = mkPackagePath . (^. rootRootDir) <$> askRoot

fromRightGenericError :: (Members '[App] r, ToGenericError err, Typeable err) => Either err a -> Sem r a
fromRightGenericError = fromRightJuvixError . mapLeft JuvixError

Expand Down
17 changes: 12 additions & 5 deletions app/Commands/Format.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,22 @@ targetFromOptions opts = do
-- | Formats the project on the root
formatProject ::
forall r.
(Members '[App, EmbedIO, TaggedLock, Logger, Files, Output FormattedFileInfo] r) =>
(Members '[App, EmbedIO, TaggedLock, Logger, ScopeEff, Files, Output FormattedFileInfo] r) =>
Sem r FormatResult
formatProject = silenceProgressLog . runPipelineOptions . runPipelineSetup $ do
res :: [(ImportNode, PipelineResult ModuleInfo)] <- processProject
res' :: [(ImportNode, SourceCode)] <- forM res $ \(node, nfo) -> do
pkgId :: PackageId <- (^. entryPointPackageId) <$> ask
src <- runReader pkgId (formatModuleInfo node nfo)
pkgId :: PackageId <- (^. entryPointPackageId) <$> ask
res' :: [(ImportNode, SourceCode)] <- runReader pkgId $ forM res $ \(node, nfo) -> do
src <- formatModuleInfo node nfo
return (node, src)
formatProjectSourceCode res'
formatRes <- formatProjectSourceCode res'
formatPkgRes <- formatPackageDotJuvix
return (formatRes <> formatPkgRes)

formatPackageDotJuvix :: forall r. (Members '[App, Files, Logger, Output FormattedFileInfo, ScopeEff] r) => Sem r FormatResult
formatPackageDotJuvix = do
pkgDotJuvix <- askPackageDotJuvixPath
ifM (fileExists' pkgDotJuvix) (format pkgDotJuvix) (return mempty)

runCommand :: forall r. (Members AppEffects r) => FormatOptions -> Sem r ()
runCommand opts = do
Expand Down
13 changes: 6 additions & 7 deletions src/Juvix/Formatter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ format p = do
-- contents of a file) for every processed file.
--
-- NB: This function does not traverse into Juvix sub-projects, i.e into
-- subdirectories that contain a juvix.yaml file.
-- subdirectories that contain a Package.juvix file.
formatProjectSourceCode ::
forall r.
(Members '[Output FormattedFileInfo] r) =>
Expand Down Expand Up @@ -178,12 +178,11 @@ formatResultSourceCode filepath src = do
mkResult :: FormatResult -> Sem r FormatResult
mkResult res = do
output
( FormattedFileInfo
{ _formattedFileInfoPath = filepath,
_formattedFileInfoContents = src ^. sourceCodeFormatted,
_formattedFileInfoContentsModified = res == FormatResultNotFormatted
}
)
FormattedFileInfo
{ _formattedFileInfoPath = filepath,
_formattedFileInfoContents = src ^. sourceCodeFormatted,
_formattedFileInfoContentsModified = res == FormatResultNotFormatted
}
return res

formatScoperResult' ::
Expand Down

0 comments on commit c15ca0e

Please sign in to comment.