From ce165859eea77111f0f0062c2db9a01484e4284b Mon Sep 17 00:00:00 2001 From: t-bltg Date: Sat, 24 Dec 2022 23:55:36 +0100 Subject: [PATCH] add `trailing_zero` option (#672) --- README.md | 6 ++++++ src/JuliaFormatter.jl | 8 ++++++++ src/options.jl | 1 + src/styles/blue/pretty.jl | 1 + src/styles/default/pretty.jl | 35 ++++++++++++++--------------------- src/styles/minimal/pretty.jl | 1 + src/styles/sciml/pretty.jl | 1 + src/styles/yas/pretty.jl | 1 + test/options.jl | 10 ++++++++++ 9 files changed, 43 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index c5cd9d851..590ea5900 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/src/JuliaFormatter.jl b/src/JuliaFormatter.jl index f229db873..5fc3577bd 100644 --- a/src/JuliaFormatter.jl +++ b/src/JuliaFormatter.jl @@ -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, @@ -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, @@ -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` diff --git a/src/options.jl b/src/options.jl index 390321df8..f8b7a323c 100644 --- a/src/options.jl +++ b/src/options.jl @@ -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 diff --git a/src/styles/blue/pretty.jl b/src/styles/blue/pretty.jl index d669995a3..cd45c3c9e 100644 --- a/src/styles/blue/pretty.jl +++ b/src/styles/blue/pretty.jl @@ -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 diff --git a/src/styles/default/pretty.jl b/src/styles/default/pretty.jl index b19ab4d9f..e1003b1b7 100644 --- a/src/styles/default/pretty.jl +++ b/src/styles/default/pretty.jl @@ -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 diff --git a/src/styles/minimal/pretty.jl b/src/styles/minimal/pretty.jl index 267537590..516c3f9cd 100644 --- a/src/styles/minimal/pretty.jl +++ b/src/styles/minimal/pretty.jl @@ -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, diff --git a/src/styles/sciml/pretty.jl b/src/styles/sciml/pretty.jl index bc4b8ac09..c06761272 100644 --- a/src/styles/sciml/pretty.jl +++ b/src/styles/sciml/pretty.jl @@ -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, diff --git a/src/styles/yas/pretty.jl b/src/styles/yas/pretty.jl index 6ff456925..adb9dbd66 100644 --- a/src/styles/yas/pretty.jl +++ b/src/styles/yas/pretty.jl @@ -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, ) diff --git a/test/options.jl b/test/options.jl index 140e2f4fe..6da683700 100644 --- a/test/options.jl +++ b/test/options.jl @@ -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