Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rebased fix of Issue316 #653

Merged
merged 16 commits into from
Nov 11, 2022
Merged

rebased fix of Issue316 #653

merged 16 commits into from
Nov 11, 2022

Conversation

exaexa
Copy link
Contributor

@exaexa exaexa commented Nov 3, 2022

this should close #316 and is a newer version of #380.

I hope the rebase went well, still catching some minor other issues. Let me know in case I should clean up the history.

domluna and others added 6 commits November 3, 2022 12:36
Adds an option `for_in_replacement` which allows "in" to be replaced with
another operator such as "∈" `\in` or "=" when `always_for_in` is
enabled.
@exaexa
Copy link
Contributor Author

exaexa commented Nov 3, 2022

@domluna I still have 4 tests failing for some reason I can't comprehend, the problem is with code like [i for i = 1:10 if i == 2] (array comprehension with if).

One example is this:

always convert `=` to `in` (for loops): Test Failed at /home/exa/work/JuliaFormatter.jl/test/options.jl:253
  Expression: fmt(str_, always_for_in = true) == str
   Evaluated: "[i for i = 1:10 if i == 2]" == "[i for i in 1:10 if i == 2]"
Stacktrace:
 [1] macro expansion
   @ ~/work/julia-1.8.1/share/julia/stdlib/v1.8/Test/src/Test.jl:464 [inlined]
 [2] macro expansion
   @ ~/work/JuliaFormatter.jl/test/options.jl:253 [inlined]
 [3] macro expansion
   @ ~/work/julia-1.8.1/share/julia/stdlib/v1.8/Test/src/Test.jl:1357 [inlined]
 [4] macro expansion
   @ ~/work/JuliaFormatter.jl/test/options.jl:201 [inlined]
 [5] macro expansion
   @ ~/work/julia-1.8.1/share/julia/stdlib/v1.8/Test/src/Test.jl:1357 [inlined]
 [6] top-level scope
   @ ~/work/JuliaFormatter.jl/test/options.jl:2

I can't really spot anything that would have broken these tests; hints very welcome.

EDIT: disregard this, my rebase broke that. Tests should be fixed now.

Copy link
Owner

@domluna domluna left a comment

Choose a reason for hiding this comment

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

Overall very nice, just a couple comments.

src/JuliaFormatter.jl Outdated Show resolved Hide resolved
src/fst.jl Outdated Show resolved Hide resolved
src/fst.jl Outdated
if fst.typ === Binary
idx = findfirst(n -> n.typ === OPERATOR, fst.nodes)
idx === nothing && return
op = fst[idx]

if always_for_in
op.val = "in"
if always_for_in && valid_for_in_op(op.val)
Copy link
Owner

Choose a reason for hiding this comment

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

the validation should occur when the options are initially set. And if the op is invalid it should display a message and not format.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, this is taken from:
https://github.com/domluna/JuliaFormatter.jl/pull/380/files#diff-fdbcedac9cc0bafcd2fa355f83f9558522f56e8e432e9a3775f59612dce9fe8aR1016

I assumed that it validates the "incoming" operator (that we're not rewriting something that's not a cycle), but actually that doesn't make sense because that wouldn't ever get parsed. This condition doesn't make sense here then, will change shortly.

Copy link
Contributor Author

@exaexa exaexa Nov 4, 2022

Choose a reason for hiding this comment

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

OK this is gonna be harder, the validation was there because of this:

always convert `=` to `in` (for loops): Test Failed at /home/exa/work/JuliaFormatter.jl/test/options.jl:253
  Expression: fmt(str_, always_for_in = true) == str
   Evaluated: "[i for i in 1:10 if i in 2]" == "[i for i in 1:10 if i == 2]"

(i.e., it force-replaces the == operator even in the if part)

Any best way to dodge this? (I pushed the option-validationg-but-failing version now)

Copy link
Owner

Choose a reason for hiding this comment

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

you shouldn't need this check the valid_for_in_op(op.val) part of if always_for_in && valid_for_in_op(op.val) anymore since it's being checked when the options are created.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the valid_for_in_op check in the condition is gone (see the new commits); the problem is that it now mistakenly fires on the if part as described above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@domluna is there any good way to dodge this? Should I track that the binary expression is explicitly following a for keyword? (IIRC I saw something like that elsewhere in the code)

src/options.jl Outdated
function validate_options(opts::Options)
@assert valid_for_in_op(opts.for_in_replacement)
opts
end
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@domluna btw I didn't find a good "central" place to validate the options (except perhaps for the place where styles are expanded, but that's very raw), so hooking this into the State constructor seemed like the best way. Please lmk in case there's a better place.

elseif fst.typ === Block || fst.typ === Brackets
for n in fst.nodes
eq_to_in_normalization!(n, always_for_in)
elseif fst.typ === Block || fst.typ === Brackets || fst.typ === Filter
Copy link
Owner

Choose a reason for hiding this comment

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

made this just Filter of !is_leaf since it's a more minimal change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah great, yes, that should work pretty well

@domluna
Copy link
Owner

domluna commented Nov 10, 2022

this should get the tests to pass now, I'll see if the initial validation check can be placed elsewhere but that might be ok how it is.

@domluna
Copy link
Owner

domluna commented Nov 10, 2022

ok I moved it to be a warning I'm happy with this now.

@domluna domluna merged commit f274d45 into domluna:master Nov 11, 2022
@exaexa
Copy link
Contributor Author

exaexa commented Nov 11, 2022

@domluna thanks a lot!

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.

Replace in by
3 participants