Browse Source

Trim trailing whitespace in comments

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.
pull/53/head
Fredrik Ekre 1 year ago
parent
commit
9d479b08f8
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 6
      README.md
  2. 24
      src/runestone.jl
  3. 4
      test/runtests.jl

6
README.md

@ -657,10 +657,14 @@ Braces are consistently used around the right hand side of `where` expressions. @@ -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

24
src/runestone.jl

@ -3,16 +3,24 @@ @@ -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

4
test/runtests.jl

@ -70,6 +70,10 @@ end @@ -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

Loading…
Cancel
Save