Skip to content

Commit

Permalink
add trailing_zero option (#672)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg authored Dec 24, 2022
1 parent 53b4b55 commit ce16585
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 21 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ funccall(
* When set to `false`, the trailing comma is always removed during nesting.
* When set to `nothing`, the trailing comma appears as it does in the original source.

### `trailing_zero`

> default: `true`
Add a trailing zero, if needed.

### `join_lines_based_on_source`

> default: `false`
Expand Down
8 changes: 8 additions & 0 deletions src/JuliaFormatter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function options(s::DefaultStyle)
align_matrix = false,
join_lines_based_on_source = false,
trailing_comma = true,
trailing_zero = true,
indent_submodule = false,
separate_kwargs_with_semicolon = false,
surround_whereop_typeparameters = true,
Expand Down Expand Up @@ -176,6 +177,7 @@ normalize_line_ending(s::AbstractString, replacer = WINDOWS_TO_UNIX) = replace(s
normalize_line_endings = "auto",
align_matrix::Bool = false,
trailing_comma::Bool = false,
trailing_zero::Bool = true,
indent_submodule::Bool = false,
separate_kwargs_with_semicolon::Bool = false,
surround_whereop_typeparameters::Bool = true,
Expand Down Expand Up @@ -485,6 +487,12 @@ value `"nothing"`:
trailing_comma = "nothing"
```
### `trailing_zero`
> default: `true`
Add a trailing zero, if needed.
### `join_lines_based_on_source`
> default: `false`
Expand Down
1 change: 1 addition & 0 deletions src/options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Base.@kwdef struct Options
align_matrix::Bool = false
join_lines_based_on_source::Bool = false
trailing_comma::Union{Bool,Nothing} = true
trailing_zero::Bool = true
indent_submodule::Bool = false
separate_kwargs_with_semicolon::Bool = false
surround_whereop_typeparameters::Bool = true
Expand Down
1 change: 1 addition & 0 deletions src/styles/blue/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function options(style::BlueStyle)
align_matrix = false,
join_lines_based_on_source = false,
trailing_comma = true,
trailing_zero = true,
surround_whereop_typeparameters = true,
)
end
Expand Down
35 changes: 14 additions & 21 deletions src/styles/default/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,32 +369,25 @@ end
)
loc = cursor_loc(s)
if !is_str_or_cmd(cst)
val = cst.val

if val === nothing
return FST(LITERAL, loc[2], loc[1], loc[1], "")
end
(val = cst.val) === nothing && return FST(LITERAL, loc[2], loc[1], loc[1], "")

if cst.head === :FLOAT
fidx = findlast(c -> c == 'f', val)
float_suffix = ""
if fidx !== nothing
if (fidx = findlast(==('f'), val)) === nothing
float_suffix = ""
else
float_suffix = val[fidx:end]
val = val[1:fidx-1]
end

dotidx = findlast(c -> c == '.', val)

if fidx !== nothing && dotidx === nothing
# append a trailing zero prior to the float suffix
val *= ".0"
elseif dotidx == length(val)
# If a floating point ends in `.`, add trailing zero.
val *= '0'
elseif dotidx == 1
val = '0' * val
elseif dotidx == 2 && val[1] == '-'
val = val[1] * '0' * val[2:end]
if findfirst(c -> c == 'e' || c == 'E', val) === nothing
if (dotidx = findlast(==('.'), val)) === nothing
val *= s.opts.trailing_zero ? ".0" : "" # append a trailing zero prior to the suffix
elseif dotidx == length(val)
val *= s.opts.trailing_zero ? "0" : "" # if a float literal ends in `.`, add trailing zero.
elseif dotidx == 1
val = '0' * val # leading zero
elseif dotidx == 2 && (val[1] == '-' || val[1] == '+')
val = val[1] * '0' * val[2:end] # leading zero on signed numbers
end
end
val *= float_suffix
end
Expand Down
1 change: 1 addition & 0 deletions src/styles/minimal/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function options(style::MinimalStyle)
annotate_untyped_fields_with_any = false,
join_lines_based_on_source = true,
trailing_comma = nothing,
trailing_zero = false,
margin = 10_000,
always_for_in = nothing,
whitespace_in_kwargs = false,
Expand Down
1 change: 1 addition & 0 deletions src/styles/sciml/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function options(style::SciMLStyle)
align_pair_arrow = false,
align_matrix = false,
trailing_comma = true,
trailing_zero = true,
indent_submodule = false,
separate_kwargs_with_semicolon = false,
surround_whereop_typeparameters = true,
Expand Down
1 change: 1 addition & 0 deletions src/styles/yas/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function options(style::YASStyle)
normalize_line_endings = "auto",
align_matrix = false,
trailing_comma = true,
trailing_zero = true,
indent_submodule = false,
surround_whereop_typeparameters = true,
)
Expand Down
10 changes: 10 additions & 0 deletions test/options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2310,4 +2310,14 @@
for_in_replacement = "ni!",
)
end

@testset "trailing zero" begin
@test fmt("1e-2", trailing_zero = true) == "1e-2"
@test fmt("1f0", trailing_zero = true) == "1.0f0"
@test fmt("1.", trailing_zero = true) == "1.0"

@test fmt("1e-2", trailing_zero = false) == "1e-2"
@test fmt("1f0", trailing_zero = false) == "1f0"
@test fmt("1.", trailing_zero = false) == "1."
end
end

0 comments on commit ce16585

Please sign in to comment.