JuliaFormatter.jl icon indicating copy to clipboard operation
JuliaFormatter.jl copied to clipboard

Inconsistent white-space around binary operators

Open odow opened this issue 4 years ago • 8 comments

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]

odow avatar Sep 11 '19 18:09 odow

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.

domluna avatar Sep 12 '19 03:09 domluna

Our goal has been the fewer special the better, and special cases only with well justified reasoning.

odow avatar Sep 12 '19 04:09 odow

I'm not seeing many cases where spaces are used between operators in an index, albeit in some files it's there but inconsistent.

domluna avatar Sep 21 '19 17:09 domluna

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.

mlubin avatar Nov 17 '19 16:11 mlubin

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).

baggepinnen avatar Sep 21 '21 05:09 baggepinnen

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

domluna avatar Sep 21 '21 17:09 domluna

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!

baggepinnen avatar Feb 18 '23 08:02 baggepinnen

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.

domluna avatar Feb 18 '23 21:02 domluna