Skip to content

Commit b578e88

Browse files
committed
Don't transform docstring single-quotes.
1 parent 47b7618 commit b578e88

2 files changed

Lines changed: 79 additions & 16 deletions

File tree

src/format_docstring.jl

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ end
5151
function format_docstring(style::AbstractStyle, state::State, text::AbstractString)
5252
state_indent = state.indent
5353
start_boundary = findfirst(!=('"'), text)
54+
is_triple_quoted = let
55+
# Assuming well-formedness, the string is triple-quoted iif
56+
# the chars after/before (o)pening/(c)losing quotes are also quotes.
57+
o, c = map(f -> f(==('"'), text), (findfirst, findlast))
58+
text[o+1] == text[c-1] == '"'
59+
end
5460
# if the docstring is non-empty
5561
if !isnothing(start_boundary)
5662
_end_boundary = findlast(!=('"'), text)
@@ -119,21 +125,24 @@ function format_docstring(style::AbstractStyle, state::State, text::AbstractStri
119125
# the docstring is empty
120126
formatted = ""
121127
end
122-
# Indent all non-first lines to match the current parser indent
123-
buf = IOBuffer()
124-
indent = " "^state_indent
125-
# This is the first line, so the rest have to be indented. A newline for it will be added below
126-
write(buf, "\"\"\"")
127-
for line in split(formatted, '\n')
128-
# The last line will be empty and will turn into an indent, so no need to indent the last line below
129-
write(buf, '\n')
130-
# don't write empty lines #667
131-
if !all(isspace, line)
132-
write(buf, indent)
133-
write(buf, line)
134-
end
128+
# Render into text lines, taking care of original indentation,
129+
quot = is_triple_quoted ? "\"\"\"" : '"'
130+
indentation = " "^state_indent
131+
indent(line) = indentation * line
132+
clean(line) = all(isspace, line) ? "" : line # don't write empty lines #667
133+
lines = split(formatted, '\n') # Always contains at least an empty last line.
134+
if is_triple_quoted
135+
prep = line -> clean(indent(line)) # All lines are prepared the same way.
136+
lines = Iterators.map(prep, lines)
137+
quot * '\n' * join(lines, '\n') * indent(quot)
138+
else
139+
# The first line needs no indentation.
140+
lines = Iterators.Stateful(lines[1:end-1]) # (drop last empty line)
141+
first = popfirst!(lines) |> clean
142+
isempty(lines) && return quot * first * quot
143+
# Upcoming ones do.
144+
prep = line -> '\n' * indent(line)
145+
lines = Iterators.map(prep, lines)
146+
quot * first * join(lines) * quot
135147
end
136-
write(buf, indent)
137-
write(buf, "\"\"\"")
138-
String(take!(buf))
139148
end

test/options.jl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module OptionsTests
22

3+
using Revise
34
using JuliaFormatter
45
using JuliaFormatter: DefaultStyle, YASStyle, BlueStyle, Options, format_text
56
using JuliaFormatter.Internal: test_format
@@ -711,6 +712,59 @@ end
711712
end
712713
end
713714

715+
@testset "issue 1203" begin
716+
717+
# Use let blocks to check indentation.
718+
triple = """
719+
begin
720+
\"""
721+
doc
722+
\"""
723+
f() = 0
724+
end
725+
"""
726+
727+
single = """
728+
begin
729+
"doc"
730+
f() = 0
731+
end
732+
"""
733+
734+
test_format(triple, triple; format_docstrings=true)
735+
test_format(single, triple; format_docstrings=true)
736+
test_format(single, single; format_docstrings=true)
737+
738+
# Not exactly good taste, but the option leaves it alone as promised.
739+
single_multiline = """
740+
begin
741+
"line1\\n\\
742+
line2"
743+
f() = 0
744+
end
745+
"""
746+
test_format(single_multiline, single_multiline; format_docstrings=true)
747+
748+
# Still drop trailing whitespace on the first line (#667).
749+
test_format(
750+
"""
751+
begin
752+
"$(" ")
753+
doc
754+
"
755+
f() = 0
756+
end
757+
""",
758+
"""
759+
begin
760+
"doc" # <- This docstring is correctly formatted.
761+
f() = 0 # Can somebody help explain why newlines are being inserted here?
762+
end
763+
""",
764+
; format_docstrings=true)
765+
766+
end
767+
714768
@testset "align struct fields" begin
715769
str_ = """
716770
struct Foo

0 commit comments

Comments
 (0)