Browse Source

Trim trailing whitespace in comments (#53)

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/54/head
Fredrik Ekre 1 year ago committed by GitHub
parent
commit
7bceb1c7e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  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.
#### Trailing spaces #### 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 ```diff
-1 + 1 -1 + 1
+1 + 1 +1 + 1
-x = 2 # x is two
+x = 2 # x is two
``` ```
#### Tabs #### Tabs

24
src/runestone.jl

@ -3,16 +3,24 @@
# This is the runestone where all the formatting transformations are implemented. # This is the runestone where all the formatting transformations are implemented.
function trim_trailing_whitespace(ctx::Context, node::Node) 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) @assert is_leaf(node)
str = String(read_bytes(ctx, node)) str = String(read_bytes(ctx, node))
str′ = replace(str, r"\h*(\r\n|\r|\n)" => '\n') local str′::String
# If the next sibling is also a NewlineWs we can trim trailing if kind(node) === K"NewlineWs"
# whitespace from this node too # Strip all whitespace up until the newline while normalizing line endings to \njK:w
next_kind = next_sibling_kind(ctx) str′ = replace(str, r"\h*(\r\n|\r|\n)" => '\n')
if next_kind === K"NewlineWs" # If the next sibling is also a NewlineWs we can trim trailing
# str′ = replace(str′, r"(\r\n|\r|\n)\h*" => '\n') # whitespace from this node too
str′ = replace(str′, r"\n\h*" => '\n') 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 end
if str == str′ if str == str′
return nothing return nothing

4
test/runtests.jl

@ -70,6 +70,10 @@ end
# Trailing whitespace just before closing indent token # Trailing whitespace just before closing indent token
@test format_string("begin\n a = 1 \nend") == "begin\n a = 1\nend" @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" @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 end
@testset "Hex/oct/bin literal integers" begin @testset "Hex/oct/bin literal integers" begin

Loading…
Cancel
Save