Skip to content

Commit 5e4400f

Browse files
committed
Fix code folding when folding ends on the last line
Previously, a final FoldBlock that begins one line after the last block and ends at the last line was always created. If the last block ended on the last line of the listing, this resulted in an ArrayIndexOutOfBoundException. This commit updates the converter to only create the final FoldBlock if it's starting line is on or before the last line of the listing. Fixes gh-26
1 parent c27a005 commit 5e4400f

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

src/main/java/io/spring/asciidoctor/backend/codetools/FoldListingContentConverter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,9 @@ static FoldBlocks get(String[] lines, Folders folders) {
271271
}
272272
}
273273
}
274-
foldBlocks.add(createFoldArea(folder, lines, start, lines.length - 1));
274+
if (start < lines.length) {
275+
foldBlocks.add(createFoldArea(folder, lines, start, lines.length - 1));
276+
}
275277
return new FoldBlocks(foldBlocks.stream().filter(FoldBlock::hasLines));
276278
}
277279

src/test/java/io/spring/asciidoctor/backend/codetools/FoldListingContentConverterTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,9 @@ void convertWhenHasCodeFoldingDisabledAsBlockAttributeReturnsUnfolded(ConvertedH
8484
assertThat(html.getElementHtml("code")).satisfies(expected);
8585
}
8686

87+
@Test
88+
void convertWhenFoldingIsTurnedOffOnTheLastLineReturnsFolded(ConvertedHtml html, ExpectedHtml expected) {
89+
assertThat(html.getElementHtml("code")).satisfies(expected);
90+
}
91+
8792
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
= Test Document
2+
3+
[source,java]
4+
----
5+
// @fold:on
6+
public class Test {
7+
8+
}
9+
// @fold:off
10+
----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<span class="fold-block hide-when-folded">public class Test {
2+
3+
}
4+
</span>

0 commit comments

Comments
 (0)