Skip to content

Commit

Permalink
Count number of bytes in unicode whitespace (#334)
Browse files Browse the repository at this point in the history
* Count number of bytes in unicode whitespace

fixes #332

* revert change from typo
  • Loading branch information
domluna authored Nov 22, 2020
1 parent fedb67b commit 2b0700c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "JuliaFormatter"
uuid = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
authors = ["Dominique Luna <dluna132@gmail.com>"]
version = "0.10.8"
version = "0.10.9"

[deps]
CSTParser = "00ebfdb7-1f24-5e51-bd34-a7502290713f"
Expand Down
2 changes: 2 additions & 0 deletions src/document.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ function Document(text::AbstractString)

if t.kind === Tokens.COMMENT
goffset += (t.endbyte - t.startbyte + 1)
elseif t.kind === Tokens.WHITESPACE
goffset += (t.endbyte - t.startbyte + 1)
else
goffset += length(Tokenize.untokenize(t))
end
Expand Down
37 changes: 28 additions & 9 deletions test/document.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
@testset "Document" begin
s = """
\"""
𝔽𝔽
@testset "count unicode literals in bytes" begin
s = """
\"""
𝔽𝔽
\"""
struct A end
"""
d = JuliaFormatter.Document(s)
ranges = Dict(1 => 1:4, 2 => 5:7, 3 => 8:8, 4 => 9:12, 5 => 13:25, 6 => 26:25)
@test ranges == d.line_to_range
\"""
struct A end
"""
d = JuliaFormatter.Document(s)
ranges = Dict(1 => 1:4, 2 => 5:7, 3 => 8:8, 4 => 9:12, 5 => 13:25, 6 => 26:25)
@test ranges == d.line_to_range
end

@testset "count unicode whitespace in bytes" begin
s0 = """a = b || c ;
f("A")"""
d = JuliaFormatter.Document(s0)
ranges = Dict(1 => 1:13, 2 => 14:19)
@test ranges == d.line_to_range

# this string has a nbsp after 'c'
# so it should have an additional byte because
# it's unicode
s = """a = b || c ;
f("A")"""
d = JuliaFormatter.Document(s)
ranges = Dict(1 => 1:14, 2 => 15:20)
@test ranges == d.line_to_range
end
end
11 changes: 11 additions & 0 deletions test/issues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -621,4 +621,15 @@
"""
@test format_text(str, align_assignment = true) == str
end

@testset "issue 332" begin
# this string has a nbsp after 'c'
# so it should have an additional byte because
# it's unicode
str_ = """a = b || c ;
f("A")"""
str = """a = b || c;
f("A")"""
@test format_text(str_) == str
end
end

2 comments on commit 2b0700c

@domluna
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/25107

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.10.9 -m "<description of version>" 2b0700cbefb3007fe035474109c4cf18352f86e0
git push origin v0.10.9

Please sign in to comment.