Skip to content

Commit

Permalink
Allow expanding all short function definitions (#861)
Browse files Browse the repository at this point in the history
* add max_short_function_length

* bump

* add show method for Options

* add force_long_function_def

* document force_long_function_def

* set version

---------

Co-authored-by: Dominique Luna <dluna132@gmail.com>
  • Loading branch information
jw3126 and domluna authored Aug 22, 2024
1 parent ecf58a3 commit dda8c31
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
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"
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
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

2 comments on commit dda8c31

@domluna
Copy link
Owner

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/113683

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.60 -m "<description of version>" dda8c31d40e6f80b3f5d91cfbb2247fbc551a75b
git push origin v1.0.60

Please sign in to comment.