Skip to content

Conversation

avh4
Copy link
Contributor

@avh4 avh4 commented Apr 25, 2022

This is just the first part of #42, adding just the CLI subcommand and not actually implementing any formatting yet.

@robinheghan Are you up for merging pieces (like this PR) before the entire formatting feature is fully working? If not, then I'll create a feature branch that these smaller PRs will merge into. (Also note, if this is merged, it's trivial to disable it just by removing it from the list of available subcommands.)

This PR includes:

  • adds gren format to the CLI
  • with no additional options, reads gren.json to find the source directories to format
  • files and directories can be explicitly given instead
  • directories are recursively searched for .gren files
  • .gren/, node_modules/, and .git/ are ignored when recursively searching directories
  • the user is asked to confirm before continuing
  • --yes can be used to skip the confirmation prompt
  • --stdin can be used to format stdin to stdout
  • gives an error if --stdin is used with explicit file names
  • "Formatting " is printed as each file is processed
  • the following logic is ready for the real formatting implementation, though as of this PR, "formatting" just results in the original contents unchanged:
    • "(no change)" or "CHANGED" is printed as each file is finished being processed
    • if the file is changed, the file is overwritten with the new contents
    • if the file is not changed, the file is not touched

I tried my best to guess what the preferred coding style is based on what's already there. Lmk if there's any trivial stuff that you want done differently.

Console "screenshots"

$ gren format

-- FORMAT WHAT? ----------------------------------------------------------------

I cannot find a gren.json so I am not sure what you want me to format. Normally
you run `gren format` from within a project!

If you need to format gren files outside of a project, tell me which files or
directories to format:

    gren format Example.gren
$ gren format --help
The `format` command rewrites .gren files to use Gren's preferred style:

    gren format <zero-or-more-gren-files-and/or-directories>

If no files or directories are given, all .gren files in all source and test
directories will be formatted.

You can customize this command with the following flags:

    --yes
        Assume yes for all interactive prompts.
    
    --stdin
        Format stdin and write it to stdout.
$ cd gren/html
$ gren format
This will overwrite the following files to use Gren's preferred style:

    /home/avh4/workspace/gren/html/src/Html/Lazy.gren
    /home/avh4/workspace/gren/html/src/Html/Attributes.gren
    /home/avh4/workspace/gren/html/src/Html/Events.gren
    /home/avh4/workspace/gren/html/src/Html/Keyed.gren
    /home/avh4/workspace/gren/html/src/Html.gren

This cannot be undone! Make sure to back up these files before proceeding.

Are you sure you want to overwrite these files with formatted versions? [Y/n]:y
Formatting /home/avh4/workspace/gren/html/src/Html/Lazy.gren (no changes)
Formatting /home/avh4/workspace/gren/html/src/Html/Attributes.gren (no changes)
Formatting /home/avh4/workspace/gren/html/src/Html/Events.gren (no changes)
Formatting /home/avh4/workspace/gren/html/src/Html/Keyed.gren (no changes)
Formatting /home/avh4/workspace/gren/html/src/Html.gren (no changes)

@avh4 avh4 requested a review from robinheghan April 25, 2022 01:18
@avh4 avh4 mentioned this pull request Apr 25, 2022
31 tasks
Copy link
Member

@robinheghan robinheghan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems good to me!

I much prefer reviewing small PRs, so adding bit-by-bit like this is a good strategy, especially when it's trivial to feature toggle it like you suggest.

For code formatting I use: https://github.com/tweag/ormolu

Other than that, I too try to adapt to the surrounding code.

I made a small suggestion, but once you've considered it you're free to merge :)

Great work! 💯

Comment on lines 18 to 21
mkAbsoluteSrcDir :: FilePath -> IO AbsoluteSrcDir
mkAbsoluteSrcDir srcDir =
AbsoluteSrcDir
<$> Dir.canonicalizePath srcDir
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have this allergy against repeating the module name in the module's functions.

I realize that the mkX pattern is probably used elsewhere in this project, but for me I think a function name like init (AbsoluteSrcDir.init) works better.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not even sure if mk* is used elsewhere here... I was just using my recollection of this article https://kowainik.github.io/posts/naming-conventions#mk

I don't really have a personal preference. init sounds good to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, init is kinda bad because it conflicts with Prelude.init.

How about AbsoluteSrcDir.fromFilePath ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fromFilePath is much better :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🥳

@avh4 avh4 merged commit 1652386 into main Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants