Skip to content

Commit 8d12d62

Browse files
authored
Fix docstring comment duplication bug (#1229)
Closes #1223 Claude figured it out I also expanded the tests to cover all styles for good measure
1 parent 4b1a073 commit 8d12d62

4 files changed

Lines changed: 277 additions & 189 deletions

File tree

HISTORY_v2.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# v2.11.4
2+
3+
Fixed a bug where comments from outside docstrings would be incorrectly duplicated inside the docstring if the vertical length of the docstring changed during formatting. (#1223, #1229)
4+
15
# v2.11.3
26

37
Fixed a bug where Julia code in docstrings would be formatted incorrectly if the code itself contained characters that had to be escaped. (#1224, #1228)

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "JuliaFormatter"
22
uuid = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
3-
version = "2.11.3"
3+
version = "2.11.4"
44
authors = ["Dominique Luna <dluna132@gmail.com> and contributors"]
55

66
[workspace]

src/styles/default/pretty.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -896,17 +896,23 @@ function p_stringh(
896896
end
897897

898898
for (i, l) in enumerate(lines)
899-
ln = startline + i - 1
899+
# When format_docstrings is enabled, the formatted docstring may have more lines
900+
# than the original (e.g. blank lines added around code fences). Clamping prevents
901+
# add_node! from checking source lines outside the docstring for comments. See
902+
# https://github.com/JuliaEditorSupport/JuliaFormatter.jl/issues/1223
903+
ln = min(startline + i - 1, endline)
900904
l = i == 1 ? l : l[sidx:end]
901905
n = FST(LITERAL, ln, ln, sidx - 1, textwidth(l), l, (), AllowNest, 0, -1, nothing)
902-
add_node!(t, n, s)
906+
# override_join_lines_based_on_source ensures that the lines are always printed on
907+
# separate lines rather than being joined, even if join_lines_based_on_source has
908+
# been enabled by the user
909+
add_node!(t, n, s; override_join_lines_based_on_source = true)
903910
end
904911

905912
# we need to maintain the start and endlines of the original source
906913
t.startline = startline
907914
t.endline = endline
908-
909-
t
915+
return t
910916
end
911917

912918
# GlobalRefDoc (docstring)

0 commit comments

Comments
 (0)