Skip to content

Commit

Permalink
Disallow more than three consecutive newlines (2 empty lines)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Jul 13, 2024
1 parent d973e3a commit f24397e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ julia-master --project -m Runic --check --diff "${files[@]}"
This is a list of things that Runic currently is doing:
- [Line width limit](#line-width-limit)
- [Indentation](#indentation)
- [Spaces around operators, assignment, etc](#spaces-around-operators-assignment-etc)
- [Spaces around keywords](#spaces-around-keywords)
Expand All @@ -172,7 +173,7 @@ This is a list of things that Runic currently is doing:
- [Literal floating point numbers](#literal-floating-point-numbers)
- [Literal hex and oct numbers](#literal-hex-and-oct-numbers)
- [Parentheses around operator calls in colon](#parentheses-around-operator-calls-in-colon)
- [`in` instead of `∈` and `=`]()#in-instead-of--and-
- [`in` instead of `∈` and `=`](#in-instead-of--and-)
- [Braces around right hand side of `where`](#braces-around-right-hand-side-of-where)
- [Trailing whitespace](#trailing-whitespace)
Expand Down Expand Up @@ -256,6 +257,26 @@ x = a + b *
d
```
### Vertical spacing
Runic removes empty vertical spacing so that there are at maximum two empty lines between
expressions. Examples:
```diff
-function f()
- x = 1
-
-
-
- return x
-end
+function f()
+ x = 1
+
+
+ return x
+end
```
### Spaces around operators, assignment, etc
Runic formats spaces around infix operators, assignments, comparison chains, and type
Expand Down
1 change: 1 addition & 0 deletions src/Runic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ function format_node!(ctx::Context, node::Node)::Union{Node, Nothing, NullNode}
ctx.call_depth += 1
@return_something insert_delete_mark_newlines(ctx, node)
@return_something trim_trailing_whitespace(ctx, node)
@return_something max_three_consecutive_newlines(ctx, node)
@return_something format_hex_literals(ctx, node)
@return_something format_oct_literals(ctx, node)
@return_something format_float_literals(ctx, node)
Expand Down
25 changes: 25 additions & 0 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,31 @@ function parens_around_op_calls_in_colon(ctx::Context, node::Node)
end
end


# Remove more than two newlines in a row
function max_three_consecutive_newlines(ctx::Context, node::Node)
is_leaf(node) && return nothing
kids = verified_kids(node)
idx = findfirst(x -> kind(x) === K"NewlineWs", kids)
while idx !== nothing
if idx + 3 <= length(kids) &&
(kind(kids[idx + 1]) == kind(kids[idx + 2]) == kind(kids[idx + 3]) == K"NewlineWs")
kids′ = Vector{Node}(undef, length(kids) - 1)
for (i, kid) in pairs(kids)
if i == idx
replace_bytes!(ctx, "", span(kids[idx]))
else
accept_node!(ctx, kid)
kids′[i < idx ? i : (i - 1)] = kid
end
end
return make_node(node, kids′)
end
idx = findnext(x -> kind(x) === K"NewlineWs", kids, idx + 1)
end
return nothing
end

# This function materialized all indentations marked by `insert_delete_mark_newlines`.
function four_space_indent(ctx::Context, node::Node)
kind(node) === K"NewlineWs" || return nothing
Expand Down
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -722,3 +722,16 @@ end
"$(o)\n a, # a\n b,\n$(c)"
end
end

@testset "max three consecutive newlines" begin
f, g = "f() = 1", "g() = 2"
for n in 1:5
nl = "\n"
m = min(n, 3)
@test format_string(f * nl^n * g) == f * nl^m * g
@test format_string("module A" * nl^n * "end") == "module A" * nl^m * "end"
@test format_string("function f()" * nl^n * "end") == "function f()" * nl^m * "end"
@test format_string("function f()" * nl^2 * "x = 1" * nl^n * "end") ==
"function f()" * nl^2 * " x = 1" * nl^m * "end"
end
end

0 comments on commit f24397e

Please sign in to comment.