JuliaFormatter.jl
JuliaFormatter.jl copied to clipboard
Inconsistent white-space around binary operators
JuMP's style guide suggests that all binary operators (with two exceptions) are surrounded by spaces.
The two exceptions are ^
and :
. So x^2
not x ^ 2
and 1:n
not 1 : n
.
JuliaFormatter is inconsistent about white-spacing operators.
Consider operators used normally (good):
JuliaFormatter.format_text("x+1") # x + 1
JuliaFormatter.format_text("x + 1") # x + 1
And when used to index (bad):
JuliaFormatter.format_text("x[i+1]") # x[i+1]
JuliaFormatter.format_text("x[i + 1]") # x[i+1]
This would be simpler. I don't have a strong preference in the matter but most code I've seen that uses operators in an index has no spaces.
Our goal has been the fewer special the better, and special cases only with well justified reasoning.
I'm not seeing many cases where spaces are used between operators in an index, albeit in some files it's there but inconsistent.
Same thing for space between operators in the context of a range:
julia> JuliaFormatter.format_text("(x + y):b")
"(x+y):b"
julia> JuliaFormatter.format_text("(x + y)")
"(x + y)"
I vote for not having the spacing rules be context specific.
Is there no way to opt out of having spaces around operators, I can only find options for whitespace_ops_in_indices
? When formatting code that uses Unitful I do not want to have
2m/s
replaced with
2m / s
Even nicer would be if one could choose to have whitespace around +,-
but not around *,/
, e.g., by providing a list of operators that are whitespaced (while others are not).
that could work if it's mapped to https://github.com/JuliaLang/Tokenize.jl/blob/master/src/token_kinds.jl
this is already in place
julia> JuliaFormatter.tokenize("+") PLUS::Kind = 569
https://github.com/domluna/JuliaFormatter.jl/blob/master/src/document.jl#L20-L23
I'm busy with a bunch of other thing but I could assist a PR
Hello @domluna! I'm taking a stab at allowing customization of whitespace around binary operators so that
(a + b*c)*d
can stay intact, i.e., no whitespace is inserted around the *
operator. Having stepped through the code for a while with the debugger, I'm not sure where exactly the whitespaces are being added and where the best place to modify the code would be.
Do you think you could point me in the right direction? Thanks!
so these are the calls relevant to this https://github.com/domluna/JuliaFormatter.jl/blob/master/src/styles/default/pretty.jl#L86-L95
and for this it's probably p_binaryopcall and p_chainopcall that matter.
I would then look for places where Whitespace or Placeholder nodes are added such as https://github.com/domluna/JuliaFormatter.jl/blob/master/src/styles/default/pretty.jl#L1593. Placeholder nodes are Whitespace nodes that can be replaced with a Newline node, whereas Whitespace nodes are meant to be static. A place where a Whitespace node would be used is "function foo" - in between "function" and "foo" since there's no reason why that would need to be changed to a newline.