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. 10
      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

10
src/runestone.jl

@ -3,9 +3,12 @@
# 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))
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') str′ = replace(str, r"\h*(\r\n|\r|\n)" => '\n')
# If the next sibling is also a NewlineWs we can trim trailing # If the next sibling is also a NewlineWs we can trim trailing
# whitespace from this node too # whitespace from this node too
@ -14,6 +17,11 @@ function trim_trailing_whitespace(ctx::Context, node::Node)
# str′ = replace(str′, r"(\r\n|\r|\n)\h*" => '\n') # str′ = replace(str′, r"(\r\n|\r|\n)\h*" => '\n')
str′ = replace(str′, r"\n\h*" => '\n') str′ = replace(str′, r"\n\h*" => '\n')
end end
else
@assert kind(node) === K"Comment"
# Strip trailing spaces and tabs from comments
str′ = rstrip(str, (' ', '\t'))
end
if str == str′ if str == str′
return nothing return nothing
end end

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