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

Allow expanding all short function definitions #861

Merged
merged 7 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "JuliaFormatter"
uuid = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
authors = ["Dominique Luna <dluna132@gmail.com>"]
version = "1.0.59"
Copy link
Owner

Choose a reason for hiding this comment

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

this could be 1.0.60 since it's not a breaking change of any kind

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed it. Although personally, I would interpret this PR as a feature that should increase the minor version instead of a bugfix that should increase the patch version.

version = "1.0.60"

[deps]
CSTParser = "00ebfdb7-1f24-5e51-bd34-a7502290713f"
Expand Down
9 changes: 8 additions & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,21 @@ Transforms a *short* function definition
f(arg1, arg2) = body
```

to a *long* function definition if the short function definition exceeds the maximum margin.
to a *long* function definition if the short function definition exceeds the maximum margin. Or
if `force_long_function_def` is set to `true`.

```julia
function f(arg2, arg2)
body
end
```

### `force_long_function_def`
> default: `false`

If `true` tweaks the behavior of `short_to_long_function_def` to force the transformation no matter
how short the function definition is.

### `long_to_short_function_def`

> default: `false`
Expand Down
17 changes: 17 additions & 0 deletions src/options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Base.@kwdef struct Options
pipe_to_function_call::Bool = false
short_to_long_function_def::Bool = false
long_to_short_function_def::Bool = false
force_long_function_def::Bool = false
always_use_return::Bool = false
whitespace_in_kwargs::Bool = true
annotate_untyped_fields_with_any::Bool = true
Expand All @@ -35,6 +36,22 @@ Base.@kwdef struct Options
yas_style_nesting::Bool = false
short_circuit_to_if::Bool = false
disallow_single_arg_nesting::Bool = false
function Options(args...)
opts = new(args...)
if (opts.force_long_function_def === true) &&
(opts.short_to_long_function_def === false)
msg = """
The combination `force_long_function_def = true` and `short_to_long_function_def = false` is invalid.
"""
throw(ArgumentError(msg))
end
return opts
end
Comment on lines +39 to +49
Copy link
Owner

Choose a reason for hiding this comment

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

I think this function is causing failures on Julia < v1.6

Copy link
Owner

Choose a reason for hiding this comment

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

ERROR: LoadError: LoadError: syntax: ... is not supported inside "new"

Copy link
Owner

Choose a reason for hiding this comment

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

@jw3126 update this when you get the chance and it should be g2g

end

function Base.show(io::IO, opt::Options)
print(io, "Options")
print(io, NamedTuple(key => getproperty(opt, key) for key in fieldnames(Options)))
end

function needs_alignment(opts::Options)
Expand Down
2 changes: 1 addition & 1 deletion src/styles/default/nest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function nest!(ds::DefaultStyle, fst::FST, s::State)
elseif fst.typ === Binary
line_margin = s.line_offset + length(fst) + fst.extra_margin
if s.opts.short_to_long_function_def &&
line_margin > s.opts.margin &&
((line_margin > s.opts.margin) || s.opts.force_long_function_def) &&
fst.ref !== nothing &&
CSTParser.defines_function(fst.ref[]) &&
!parent_is(fst.ref[], n -> n.head == :let)
Expand Down
14 changes: 14 additions & 0 deletions test/options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,20 @@
bodybodybodybodybodybodybody
end"""
@test fmt(str_, 4, length(str_), short_to_long_function_def = true) == str_
@test fmt(
str_,
4,
length(str_),
short_to_long_function_def = true,
force_long_function_def = true,
) == str
@test fmt(
str_,
4,
length(str_),
short_to_long_function_def = false,
force_long_function_def = false,
) == str_
@test fmt(str_, 4, length(str_) - 1, short_to_long_function_def = true) == str
end

Expand Down
Loading