[javadoc] Snippets: exclude \n from whole-line highlightings; include content before // @end to the region

Fixes IDEA-343473 Quick documentation does not render {@snippet} correctly

GitOrigin-RevId: 3eca79b107b248d6c9ed79b1fdec3e083424c124
This commit is contained in:
Tagir Valeev
2024-01-26 18:19:05 +01:00
committed by intellij-monorepo-bot
parent 9b7cfa29d0
commit b3f30c73f1
6 changed files with 85 additions and 4 deletions

View File

@@ -238,7 +238,7 @@ public class SnippetMarkup {
public record WholeLine() implements Selector {
@Override
public @NotNull List<TextRange> ranges(String string) {
return List.of(TextRange.create(0, string.length()));
return List.of(TextRange.create(0, string.endsWith("\n") ? string.length() - 1 : string.length()));
}
}
@@ -502,7 +502,7 @@ public class SnippetMarkup {
if (prev.content().isBlank() && !prev.content().isEmpty()) {
prev = new PlainText(TextRange.from(text.range().getEndOffset(), 0), "");
}
if (hasColon) {
if (hasColon || (!prev.content().isEmpty() && ContainerUtil.and(markupNodes, mn -> mn instanceof EndRegion))) {
markupNodes.add(0, prev);
}
else {

View File

@@ -2,7 +2,7 @@
<pre>public static void main(String[] args) {
...
System.out.println("<span style="color:#000000;background-color:#ffff00;">Hello</span>");
<span style="color:#000000;background-color:#ffff00;"> System.out.println("Whole line");
</span> ...omitted...}
<span style="color:#000000;background-color:#ffff00;"> System.out.println("Whole line"); </span>
...omitted...}
</pre>
</div><table class='sections'><p></table>

View File

@@ -0,0 +1,12 @@
<html><head><base href="placeholder"></head><body><div class='definition'><pre><span style="color:#000080;font-weight:bold;">public</span> <span style="color:#000080;font-weight:bold;">class</span> <span style="color:#000000;">Hello</span></pre></div><div class='content'>
...
<pre>Objects.requireNonNull(channel, "channel is null");
final var buffer = ByteBuffer.allocate(BYTES);
put(buffer);
buffer.flip();
<span style="color:#000000;background-color:#ffff00;">while (buffer.hasRemaining()) { </span>
<span style="color:#000000;background-color:#ffff00;"> final var written = channel.write(buffer);</span>
<span style="color:#000000;background-color:#ffff00;"> assert written >= 0; // why?</span>
<span style="color:#000000;background-color:#ffff00;">} </span>
return channel;</pre>
</div><table class='sections'><p></table>

View File

@@ -0,0 +1,16 @@
/**
* ...
* {@snippet lang = "java":
* Objects.requireNonNull(channel, "channel is null");
* final var buffer = ByteBuffer.allocate(BYTES);
* put(buffer);
* buffer.flip();
* while (buffer.hasRemaining()) { // @highlight region
* final var written = channel.write(buffer);
* assert written >= 0; // why?
* } // @end
* return channel;
*}
*/
public class Hello {
}

View File

@@ -134,6 +134,7 @@ public class JavaDocInfoGeneratorTest extends JavaCodeInsightTestCase {
public void testInlineTagSnippet() { doTestClass(); }
public void testInlineTagSnippetNoMarkup() { doTestClass(); }
public void testInlineTagSnippetWithoutBody() { doTestClass(); }
public void testInlineTagSnippetHighlightSeveralLines() { doTestClass(); }
public void testExternalSnippetRegion() {
createProjectStructure(getTestDataPath() + TEST_DATA_FOLDER + "externalSnippet");
verifyJavadocFor("Region");

View File

@@ -385,6 +385,58 @@ public class SnippetMarkupTest {
// @end region=main
""", null, true, "..\n");
}
@Test
public void highlightRegion() {
String input = """
Objects.requireNonNull(channel, "channel is null");
final var buffer = ByteBuffer.allocate(BYTES);
put(buffer);
buffer.flip();
while (buffer.hasRemaining()) { // @highlight region
final var written = channel.write(buffer);
assert written >= 0; // why?
} // @end
return channel;
""";
testParsing(input, """
PlainText[range=(0,52), content=Objects.requireNonNull(channel, "channel is null");
]
PlainText[range=(52,99), content=final var buffer = ByteBuffer.allocate(BYTES);
]
PlainText[range=(99,112), content=put(buffer);
]
PlainText[range=(112,127), content=buffer.flip();
]
Highlight[range=(162,179), selector=WholeLine[], region=, type=HIGHLIGHTED]
PlainText[range=(127,159), content=while (buffer.hasRemaining()) {\s
]
PlainText[range=(180,227), content= final var written = channel.write(buffer);
]
PlainText[range=(227,260), content= assert written >= 0; // why?
]
PlainText[range=(260,262), content=}\s
]
EndRegion[range=(265,269), region=null]
PlainText[range=(270,286), content=return channel;
]
PlainText[range=(286,286), content=]""");
testVisitor(input, null, true, """
Objects.requireNonNull(channel, "channel is null");
final var buffer = ByteBuffer.allocate(BYTES);
put(buffer);
buffer.flip();
// [Highlight[range=(162,179), selector=WholeLine[], region=, type=HIGHLIGHTED]]
while (buffer.hasRemaining()) {\s
// [Highlight[range=(162,179), selector=WholeLine[], region=, type=HIGHLIGHTED]]
final var written = channel.write(buffer);
// [Highlight[range=(162,179), selector=WholeLine[], region=, type=HIGHLIGHTED]]
assert written >= 0; // why?
// [Highlight[range=(162,179), selector=WholeLine[], region=, type=HIGHLIGHTED]]
}\s
return channel;
""");
}
private static void testParsing(@NotNull String input, @NotNull String expected) {
assertEquals(expected, SnippetMarkup.parse(input).toString());