1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-25 00:21:41 +03:00
juvix/app/Commands
Paul Cadman 1ab3aa06da
Add juvix format command (#1886)
This PR adds `juvix format` that can be used to format either a single
Juvix file or all files in a Juvix project.

## Usage

```
$ juvix format --help
Usage: juvix format JUVIX_FILE_OR_PROJECT [--check] [--in-place]

  Format a Juvix file or Juvix project

  When the command is run with an unformatted file it prints the reformatted source to standard output.
  When the command is run with a project directory it prints a list of unformatted files in the project.

Available options:
  JUVIX_FILE_OR_PROJECT    Path to a .juvix file or to a directory containing a
                           Juvix project.
  --check                  Do not print reformatted sources or unformatted file
                           paths to standard output.
  --in-place               Do not print reformatted sources to standard output.
                           Overwrite the target's contents with the formatted
                           version if the formatted version differs from the
                           original content.
  -h,--help                Show this help text
```

## Location of main implementation

The implementation is split into two components:
* The src API: `format` and `formatProject`
73952ba15c/src/Juvix/Formatter.hs
* The CLI interface:  

73952ba15c/app/Commands/Format.hs

## in-place uses polysemy Resource effect

The `--in-place` option makes a backup of the target file and restores
it if there's an error during processing to avoid data loss. The
implementation of this uses the polysemy [Resource
effect](https://hackage.haskell.org/package/polysemy-1.9.0.0/docs/Polysemy-Resource.html).
The recommended way to interpret the resource effect is to use
`resourceToIOFinal` which makes it necessary to change the effects
interpretation in main to use `Final IO`:
73952ba15c/app/Main.hs (L15)

## Format input is `FilePath`

The format options uses `FilePath` instead of `AppFile f` for the input
file/directory used by other commands. This is because we cannot
determine if the input string is a file or directory in the CLI parser
(we require IO). I discussed some ideas with @janmasrovira on how to
improve this in a way that would also solve other issues with CLI input
file/parsing but I want to defer this to a separate PR as this one is
already quite large.

One consequence of Format using `FilePath` as the input option is that
the code that changes the working directory to the root of the project
containing the CLI input file is changed to work with `FilePath`:


f715ef6a53/app/TopCommand/Options.hs (L33)

## New dependencies

This PR adds new dependencies on `temporary` and `polysemy-zoo`.

`temporary` is used for `emptySystemTempFile` in the implementation of
the TempFile interpreter for IO:


73952ba15c/src/Juvix/Data/Effect/Files/IO.hs (L49)

`polysemy-zoo` is used for the `Fresh` effect and `absorbMonadThrow` in
the implementation of the pure TempFile interpreter:

73952ba15c/src/Juvix/Data/Effect/Files/Pure.hs (L91)

NB: The pure TempFile interpreter is not used, but it seemed a good idea
to include it while it's fresh in my mind.

* Closes https://github.com/anoma/juvix/issues/1777

---------

Co-authored-by: Jonathan Cubides <jonathan.cubides@uib.no>
2023-03-29 15:51:04 +02:00
..
Compile New compilation pipeline (#1832) 2023-03-14 16:24:07 +01:00
Dev End-to-end Geb compilation tests (#1942) 2023-03-29 14:02:40 +02:00
Doctor Refactor CLI (#1527) 2022-09-14 16:16:15 +02:00
Eval Print JuvixCore correctly (#1875) 2023-03-15 16:41:39 +01:00
Extra Pattern matching compilation (#1874) 2023-03-27 10:42:27 +02:00
Format Add juvix format command (#1886) 2023-03-29 15:51:04 +02:00
Html Refactor html command with extra options (#1725) 2023-01-17 18:11:59 +01:00
Repl Add REPL option to apply Core transformations (#1796) 2023-02-01 13:00:06 +00:00
Typecheck Support basic dependencies (#1622) 2022-12-20 13:05:40 +01:00
Base.hs Refactor CLI (#1527) 2022-09-14 16:16:15 +02:00
Compile.hs Test core to geb translation (#1865) 2023-03-27 15:32:03 +02:00
Dev.hs New compilation pipeline (#1832) 2023-03-14 16:24:07 +01:00
Doctor.hs Update CI to install Smoke, Github actions, and Makefile fixes (#1735) 2023-01-24 11:50:23 +01:00
Eval.hs Test core to geb translation (#1865) 2023-03-27 15:32:03 +02:00
Format.hs Add juvix format command (#1886) 2023-03-29 15:51:04 +02:00
Html.hs Keep regular comments in html output (#1766) 2023-01-27 13:24:28 +01:00
Init.hs Fix juvix init (#1835) 2023-02-10 17:53:23 +01:00
Repl.hs Add the --unroll option (#1935) 2023-03-28 11:41:05 +02:00
Typecheck.hs Update CI to install Smoke, Github actions, and Makefile fixes (#1735) 2023-01-24 11:50:23 +01:00