Skip to content

Commit

Permalink
Trim trailing whitespace in comments
Browse files Browse the repository at this point in the history
This patch adds trimming of trailing whitespace inside of comments in
addition to the trimming of trailing whitespace in code. Note that
trailing whitespace inside of multiline is not trimmed since doing so
would change the content of the string.

Closes #50.
  • Loading branch information
fredrikekre committed Aug 28, 2024
1 parent 21808b0 commit 9d479b0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -657,10 +657,14 @@ Braces are consistently used around the right hand side of `where` expressions.
#### Trailing spaces
Trailing spaces are removed. Example:
Trailing spaces are removed in code and comments (but not inside of multiline strings where
doing so would change the meaning of the code). Examples:
```diff
-1 + 1
+1 + 1
-x = 2 # x is two
+x = 2 # x is two
```
#### Tabs
Expand Down
24 changes: 16 additions & 8 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
# This is the runestone where all the formatting transformations are implemented.

function trim_trailing_whitespace(ctx::Context, node::Node)
kind(node) === K"NewlineWs" || return nothing
kind(node) in KSet"NewlineWs Comment" || return nothing
@assert is_leaf(node)
str = String(read_bytes(ctx, node))
str′ = replace(str, r"\h*(\r\n|\r|\n)" => '\n')
# If the next sibling is also a NewlineWs we can trim trailing
# whitespace from this node too
next_kind = next_sibling_kind(ctx)
if next_kind === K"NewlineWs"
# str′ = replace(str′, r"(\r\n|\r|\n)\h*" => '\n')
str′ = replace(str′, r"\n\h*" => '\n')
local str′::String
if kind(node) === K"NewlineWs"
# Strip all whitespace up until the newline while normalizing line endings to \njK:w
str′ = replace(str, r"\h*(\r\n|\r|\n)" => '\n')
# If the next sibling is also a NewlineWs we can trim trailing
# whitespace from this node too
next_kind = next_sibling_kind(ctx)
if next_kind === K"NewlineWs"
# str′ = replace(str′, r"(\r\n|\r|\n)\h*" => '\n')
str′ = replace(str′, r"\n\h*" => '\n')
end
else
@assert kind(node) === K"Comment"
# Strip trailing spaces and tabs from comments
str′ = rstrip(str, (' ', '\t'))
end
if str == str′
return nothing
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ end
# Trailing whitespace just before closing indent token
@test format_string("begin\n a = 1 \nend") == "begin\n a = 1\nend"
@test format_string("let\n a = 1 \nend") == "let\n a = 1\nend"
# Trailing whitespace in comments
@test format_string("# comment ") == format_string("# comment ") ==
format_string("# comment\t") == format_string("# comment\t\t") ==
format_string("# comment \t ") == format_string("# comment\t \t") == "# comment"
end

@testset "Hex/oct/bin literal integers" begin
Expand Down

0 comments on commit 9d479b0

Please sign in to comment.