Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

apply JuliaFormatter.jl #536

Merged
merged 3 commits into from
May 11, 2020
Merged

apply JuliaFormatter.jl #536

merged 3 commits into from
May 11, 2020

Conversation

simonbyrne
Copy link
Member

@simonbyrne simonbyrne commented Nov 14, 2019

This applies JuliaFormatter.jl to the whole repository.

Any thoughts/comments?

  • Testing?

@jkozdon
Copy link

jkozdon commented Nov 14, 2019

I like this idea a lot.

Suggestions:

  • Can you post instructions on how to set up githooks to check this?
  • Can we set up the CI to reject non-formatted code? (There is some github actions for this, but I don't know much about these)
  • Add something to README on local usage?
  • Is there some style file? I didn't see anything in the commit to tell me how to do this on my commits or what options to set.

@vchuravy
Copy link
Contributor

I am a huge fan of this, but I would prefer an incremental approach where every new/change code has to go through the formateé. Otherwise we have a hard break in the got history.

On the other hand just doing it might be a lot less work.

@simonbyrne
Copy link
Member Author

I posted this commit just to see if people liked the output. If we're happy with how it looks, I'll add instructions, CI, docs, etc.

* Is there some style file? I didn't see anything in the commit to tell me how to do this on my commits or what options to set.

This was just run using the default settings (indent=4, margin=92). Unfortunately it doesn't appear to have a whole lot of other options.

I am a huge fan of this, but I would prefer an incremental approach where every new/change code has to go through the formateé. Otherwise we have a hard break in the got history.

On the other hand just doing it might be a lot less work.

Yeah, not sure there is an obvious way to make it incremental. Also, since it changes the indentation from 2 to 4, it would be pretty hard to do incrementally.

@blallen
Copy link
Contributor

blallen commented Nov 14, 2019

Do we need to switch the indentation from 2 to 4?

Comment on lines 112 to 256
@launch(
device,
threads = Nfp,
blocks = nrealelem,
faceviscterms!(
bl,
Val(dim),
Val(polyorder),
dg.direction,
dg.gradnumflux,
Q.data,
Qvisc.data,
auxstate.data,
vgeo,
sgeo,
t,
vmapM,
vmapP,
elemtobndy,
topology.realelems,
)
)
Copy link
Contributor

@blallen blallen Nov 14, 2019

Choose a reason for hiding this comment

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

My main reservations about the formatter is that it makes some of the function calls that used to fit on 2-3 lines take 20 lines. While this is cleaner, it also means you see less of the algorithm on your screen at once.

Is there a way to make it only linebreak after the last comma before hitting 92 characters on a line instead of after every comma?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, unfortunately. Personally I feel this is the main downside as well.

Copy link

Choose a reason for hiding this comment

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

For particularly bad places there is always skip formatting

@simonbyrne
Copy link
Member Author

Do we need to switch the indentation from 2 to 4?

No this is one of the few options the formatter provides, but it had been suggested by several people as a way to make the nested function calls more obvious. The cases where we have large numbers of nested loops such as
https://github.com/climate-machine/CLIMA/blob/4e72b21ddcc200c998dba964b9af6354ffeaaddc/src/DGmethods/DGmodel_kernels.jl#L99-L116
might be better handled with a macro similar to Oceaninigans.jl:
https://github.com/climate-machine/Oceananigans.jl/blob/866696432a5214ec5f927bf21835f072a8691c78/src/utils.jl#L13-L24

But again, feedback appreciated.

@jkozdon
Copy link

jkozdon commented Nov 14, 2019

This was just run using the default settings (indent=4, margin=92).

Personal I like 2 spaces and 80 character limit, but much prefer uniformity than these little things.

@simonbyrne
Copy link
Member Author

So the general consensus is that we should enable this. The main concerns:

  1. This will break all incoming PRs, which will require a huge amount of work to get back into shape
  • @vchuravy will look to see if we can do this incrementally?
  1. How should it be implemented:
  • There is a github action which applies it as a check: https://github.com/julia-actions/julia-format, which would require users to manually use the formatter.
  • Ideally it would be applied to PRs directly, but there doesn't seem to be a way to do this at the moment.

Plan:

  • We will enable check for the time being, but don't enforce that it passes
  • If PRs make significant changes to a file, then they should apply the formatter to that file.

The remaining question is what formatting options should be applied? If you have opinions chime in below. The options are here: https://github.com/domluna/JuliaFormatter.jl

@lcw
Copy link
Contributor

lcw commented Feb 25, 2020

It would also be nice to have a git hook that runs the formatter. Below is what we used in a previous project as a pre-commit hook.

#!/bin/bash
#
# Called by git-commit with no arguments.  This checks to make
# sure that all .c and .h files are indented correctly before
# a commit is made.
#
# To enable this hook, make this file executable and place it
# in $GIT_DIR/hooks.

. git-sh-setup

CHFILES=$(git-diff-index -M --name-only --cached HEAD | \
          grep -E '.*\.(c|cpp|h)$' | grep -v 'node_data\.h')

for CHFILE in $CHFILES;
do
  MKTEMPLATE=`basename $CHFILE`.XXXXXXXX
  TEMPFILE=`mktemp -t "$MKTEMPLATE"` || exit 1
  clang-format $GIT_DIR/../$CHFILE > $TEMPFILE
  if diff $GIT_DIR/../$CHFILE $TEMPFILE
  then
    rm -f $TEMPFILE
  else
    rm -f $TEMPFILE
    NEEDS_FORMAT=1
    echo >&2 "$CHFILE needs to be indented with:"
    echo >&2 "   clang-format -i $GIT_DIR/../$CHFILE"
  fi
done
if [ -z "$NEEDS_FORMAT" ]
then
  exit 0
else
  exit 1
fi

@lcw
Copy link
Contributor

lcw commented Feb 25, 2020

I also like having a formal style guide, such as https://github.com/jrevels/YASGuide, which JuliaFormatter.jl almost fully supports.

@blallen
Copy link
Contributor

blallen commented Feb 26, 2020

The remaining question is what formatting options should be applied? If you have opinions chime in below. The options are here: https://github.com/domluna/JuliaFormatter.jl

my opinions:

  • indent = 2
  • margin = 80 (I like to split my screen into four vertical slices)
  • always_for_in = true
  • whitespace_typedefs = true
  • whitespace_ops_in_indices = true
  • remove_extra_newlines = false

@simonbyrne
Copy link
Member Author

I'm not too fussy: I generally use 4 space indent elsewhere, but our kernels have a lot of indentation which makes it cumbersome.

@leios
Copy link
Contributor

leios commented Feb 26, 2020

I agree with almost all the points set in the Coding Conventions section of the documentation. Switching to 80 columns (instead of 79) is a minor change. Outside of that, it looks like we can follow the YASguide with small exceptions for allowed unicode characters and using 2 spaces instead of 4.

The formatter will fix all the spacing, but we need to make sure we are consistent with function names and everything else as well, and that might need to be left to the reviewer(s).

I think I agree with @blallen's list, except that I would set remove_extra_newlines = true. I think this option only removes newlines when there are 2 or more consecutive newines.

Once we have this decided, I will update the Coding Conventions document to reflect the changes here.

@kpamnany
Copy link
Contributor

kpamnany commented Mar 2, 2020

I vote for 4 spaces. The point of indentation is for a clear visual demarcation of blocks, which also means variable scope in many cases. Less than 4 spaces is not clear, IMO.

Apart from that, I'm good with everything else.

@simonbyrne simonbyrne marked this pull request as ready for review March 12, 2020 05:28
@simonbyrne simonbyrne mentioned this pull request Mar 12, 2020
3 tasks
@simonbyrne
Copy link
Member Author

I've updated this PR to the current changes.

Once this is merged, the process to update an existing PR will look something like:

git checkout master .dev/format.jl # to get the most recent formatter script
.dev/format.jl
.dev/format.jl # need to apply twice
git add -u
git commit -m "apply formatter"
git merge master # this should be a relatively clean merge

To squash this all into a single commit, you should then be able to

git reset --soft master
git commit -m "my new commit message"

@simonbyrne
Copy link
Member Author

Please chime in if you have any objections!

@kpamnany kpamnany mentioned this pull request Mar 12, 2020
4 tasks
@simonbyrne
Copy link
Member Author

I might try to do this over the weekend.

Copy link
Contributor

@leios leios left a comment

Choose a reason for hiding this comment

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

I was scrolling through most of the files and it looks fine for the most part. Some of the manual alignment (for example when aligning = signs) is now gone and comments are funky. JuliaFormatter will not allow us to do manual alignment anymore, but maybe it's worth fixing some of the comments while we are at it?

s_b = Ri / Pr_0
if Ri > 0
temp = ((1 - 2 * β_h * Ri) - sqrt(1 + 4 * (β_h - β_m) * s_b))
ζ = zfactor / (2 * β_h * (β_m * Ri - 1)) * temp # Eq. 19 in Ref. Byun1990
Copy link
Contributor

Choose a reason for hiding this comment

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

The comments are super weird in this area (and a few other areas) due to JuliaFormatter not dealing with comments.

Copy link
Member

Choose a reason for hiding this comment

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

This stuff will be gone when the new surface fluxes is merged (#556)

@jkozdon
Copy link

jkozdon commented Mar 19, 2020

Some of the manual alignment (for example when aligning = signs) is now gone and comments are funky. JuliaFormatter will not allow us to do manual alignment anymore, but maybe it's worth fixing some of the comments while we are at it?

You can put stuff in blocks to turn off the formatter:

#! format: off

Stuff in here the formatter skips

#! format: on

@leios
Copy link
Contributor

leios commented Mar 19, 2020

Should we go through and figure out where all the manual formatting is preferred, set syntax off and on before merging? I guess we should at least rebase and apply the formatter again before merging this.

@simonbyrne
Copy link
Member Author

If someone wants to figure out how JuliaFormatter works, we could modify it to suit our own needs.

@simonbyrne
Copy link
Member Author

bors r+

bors bot added a commit that referenced this pull request Mar 23, 2020
536: apply JuliaFormatter.jl r=simonbyrne a=simonbyrne

This applies [JuliaFormatter.jl](https://github.com/domluna/JuliaFormatter.jl) to the whole repository.

Any thoughts/comments?

- [ ] Testing?

Co-authored-by: Simon Byrne <simonbyrne@gmail.com>
@bors
Copy link
Contributor

bors bot commented Mar 23, 2020

Build failed

@simonbyrne
Copy link
Member Author

bors r+

bors bot added a commit that referenced this pull request Mar 24, 2020
536: apply JuliaFormatter.jl r=simonbyrne a=simonbyrne

This applies [JuliaFormatter.jl](https://github.com/domluna/JuliaFormatter.jl) to the whole repository.

Any thoughts/comments?

- [ ] Testing?

Co-authored-by: Simon Byrne <simonbyrne@gmail.com>
@bors
Copy link
Contributor

bors bot commented Mar 24, 2020

Build failed

@codecov
Copy link

codecov bot commented Mar 25, 2020

Codecov Report

Merging #536 into master will decrease coverage by 0.03%.
The diff coverage is 69.33%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #536      +/-   ##
==========================================
- Coverage   70.55%   70.51%   -0.04%     
==========================================
  Files         108      108              
  Lines        6717     6736      +19     
==========================================
+ Hits         4739     4750      +11     
- Misses       1978     1986       +8
Impacted Files Coverage Δ
...mos/Parameterizations/CloudPhysics/Microphysics.jl 95.45% <ø> (ø) ⬆️
.../Common/MoistThermodynamics/MoistThermodynamics.jl 79.71% <ø> (ø) ⬆️
src/Common/PlanetParameters/PlanetaryConstants.jl 100% <ø> (ø) ⬆️
src/Driver/Driver.jl 81.72% <ø> (ø) ⬆️
src/DGmethods_old/DGBalanceLawDiscretizations.jl 0.59% <ø> (ø) ⬆️
src/Mesh/Interpolation.jl 76.28% <ø> (ø) ⬆️
src/Common/ConfigTypes/ConfigTypes.jl 100% <ø> (ø) ⬆️
src/Arrays/CMBuffers.jl 14.81% <0%> (ø) ⬆️
...methods_old/DGBalanceLawDiscretizations_kernels.jl 0% <0%> (ø) ⬆️
src/Common/PlanetParameters/EarthConstants.jl 0% <0%> (ø) ⬆️
... and 45 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f978f6f...b8cee51. Read the comment docs.

@simonbyrne
Copy link
Member Author

bors r+

bors bot added a commit that referenced this pull request Mar 25, 2020
536: apply JuliaFormatter.jl r=simonbyrne a=simonbyrne

This applies [JuliaFormatter.jl](https://github.com/domluna/JuliaFormatter.jl) to the whole repository.

Any thoughts/comments?

- [ ] Testing?

Co-authored-by: Simon Byrne <simonbyrne@gmail.com>
@bors
Copy link
Contributor

bors bot commented Mar 25, 2020

Build failed

@simonbyrne simonbyrne added this to the v 0.1.0 release milestone Apr 9, 2020
@kpamnany kpamnany linked an issue Apr 10, 2020 that may be closed by this pull request
3 tasks
@simonbyrne
Copy link
Member Author

bors r+

@bors
Copy link
Contributor

bors bot commented May 11, 2020

Build succeeded:

@bors bors bot merged commit 3e9471a into master May 11, 2020
@bors bors bot deleted the sb/formatter branch May 11, 2020 22:01
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Formatting as separate commit
8 participants