Skip to content

Commit

Permalink
Format Package.juvix when formatting project (#3219)
Browse files Browse the repository at this point in the history
- Closes #3217, #2898
  • Loading branch information
janmasrovira authored Dec 6, 2024
1 parent 4eb7509 commit 3e367aa
Show file tree
Hide file tree
Showing 19 changed files with 52 additions and 27 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
2 changes: 1 addition & 1 deletion examples/demo/Package.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import PackageDescription.V2 open;
package : Package :=
defaultPackage@{
name := "Demo";
version := mkVersion 0 1 0
version := mkVersion 0 1 0;
};
2 changes: 1 addition & 1 deletion examples/midsquare/Package.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import PackageDescription.V2 open;
package : Package :=
defaultPackage@{
name := "midsquare";
version := mkVersion 0 1 0
version := mkVersion 0 1 0;
};
2 changes: 1 addition & 1 deletion examples/milestone/Bank/Package.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import PackageDescription.V2 open;

package : Package :=
defaultPackage@{
name := "bank"
name := "bank";
};
2 changes: 1 addition & 1 deletion examples/milestone/Collatz/Package.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ package : Package :=
defaultPackage@{
name := "Collatz";
version := mkVersion 0 1 0;
main := just "Collatz.juvix"
main := just "Collatz.juvix";
};
2 changes: 1 addition & 1 deletion examples/milestone/Fibonacci/Package.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ package : Package :=
defaultPackage@{
name := "Fibonacci";
version := mkVersion 0 1 0;
main := just "Fibonacci.juvix"
main := just "Fibonacci.juvix";
};
2 changes: 1 addition & 1 deletion examples/milestone/Hanoi/Package.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ package : Package :=
defaultPackage@{
name := "Hanoi";
version := mkVersion 0 1 0;
main := just "Hanoi.juvix"
main := just "Hanoi.juvix";
};
2 changes: 1 addition & 1 deletion examples/milestone/HelloWorld/Package.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ package : Package :=
defaultPackage@{
name := "HelloWorld";
version := mkVersion 0 1 0;
main := just "HelloWorld.juvix"
main := just "HelloWorld.juvix";
};
2 changes: 1 addition & 1 deletion examples/milestone/PascalsTriangle/Package.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ package : Package :=
defaultPackage@{
name := "PascalsTriangle";
version := mkVersion 0 1 0;
main := just "PascalsTriangle.juvix"
main := just "PascalsTriangle.juvix";
};
2 changes: 1 addition & 1 deletion examples/milestone/TicTacToe/Package.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ package : Package :=
defaultPackage@{
name := "TicTacToe";
version := mkVersion 0 1 0;
main := just "CLI/TicTacToe.juvix"
main := just "CLI/TicTacToe.juvix";
};
2 changes: 1 addition & 1 deletion examples/milestone/Tutorial/Package.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import PackageDescription.V2 open;
package : Package :=
defaultPackage@{
name := "Tutorial";
version := mkVersion 0 1 0
version := mkVersion 0 1 0;
};
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
2 changes: 1 addition & 1 deletion tests/positive/Internal/Positivity2/Package.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import PackageDescription.V2 open;
package : Package :=
defaultPackage@{
name := "positivity2";
dependencies := []
dependencies := [];
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import PackageDescription.V1 open;
package : Package :=
defaultPackage@{
name := "package-juvix";
dependencies := []
dependencies := [];
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import PackageDescription.V1 open;
package : Package :=
defaultPackage@{
name := "abc";
dependencies := []
dependencies := [];
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import PackageDescription.V2 open;
package : Package :=
defaultPackage@{
name := "package-juvix";
dependencies := []
dependencies := [];
};
2 changes: 1 addition & 1 deletion tests/positive/issue3068/Package.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import PackageDescription.V2 open;
package : Package :=
defaultPackage@{
name := "issue3068";
dependencies := []
dependencies := [];
};
16 changes: 16 additions & 0 deletions tests/smoke/Commands/format.smoke.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,22 @@ tests:
stderr: ''
exit-status: 1

- name: format-project-package-dot-juvix
command:
shell:
- bash
script: |
temp=$(mktemp -d)
trap 'rm -rf -- "$temp"' EXIT
cd $temp
juvix init
echo "" >> Package.juvix
juvix format
stderr: ''
stdout:
contains: 'Package.juvix'
exit-status: 1

- name: format-project-containing-unformatted-from-subdir
command:
shell:
Expand Down

0 comments on commit 3e367aa

Please sign in to comment.