[javadoc] SnippetMarkup: fix support of empty lines in inline snippets

GitOrigin-RevId: fe29d1d0f86f7b9c497362abff361a94516f45aa
This commit is contained in:
Tagir Valeev
2023-03-17 12:59:11 +01:00
committed by intellij-monorepo-bot
parent c3cc5d8cc3
commit f3af467241
3 changed files with 27 additions and 6 deletions

View File

@@ -6,11 +6,10 @@ import com.intellij.java.JavaBundle;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiReference;
import com.intellij.psi.*;
import com.intellij.psi.impl.FakePsiElement;
import com.intellij.psi.javadoc.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.util.containers.ContainerUtil;
@@ -400,8 +399,25 @@ public class SnippetMarkup {
private static @NotNull List<@NotNull PlainText> preparse(@NotNull PsiSnippetDocTagBody body) {
List<PlainText> output = new ArrayList<>();
for (PsiElement element : body.getContent()) {
output.add(new PlainText(element.getTextRangeInParent().grown(1), element.getText() + "\n"));
for (PsiElement element : body.getChildren()) {
if (element instanceof PsiDocToken token) {
IElementType tokenType = token.getTokenType();
if (tokenType.equals(JavaDocTokenType.DOC_COMMENT_LEADING_ASTERISKS)) continue;
if (tokenType.equals(JavaDocTokenType.DOC_COMMENT_DATA)) {
output.add(new PlainText(element.getTextRangeInParent(), element.getText()));
}
}
else if (element instanceof PsiWhiteSpace) {
String text = element.getText();
if (text.contains("\n")) {
int idx = output.size() - 1;
if (idx >= 0 && !output.get(idx).content().endsWith("\n")) {
output.set(idx, new PlainText(output.get(idx).range(), output.get(idx).content() + "\n"));
} else {
output.add(new PlainText(element.getTextRange(), "\n"));
}
}
}
}
return output;
}

View File

@@ -2,7 +2,9 @@
Attributes:
<pre id="mysnippet"><a href="psi_element://java.lang.System"><code>System</code></a>.out.println("Hello, world!");
int i = <span style="color:#000000;background-color:#ffff00;">0</span>; //empty line
</pre>
System.out.println("After two empty lines");</pre>
<a href="psi_element://Main###mysnippet">See mysnippet</a>.
</div><table class='sections'><p></table>

View File

@@ -3,6 +3,9 @@
* {@snippet id="mysnippet":
* System.out.println("Hello, world!"); // @link substring=System target=java.lang.System
* int i = 0; //empty line // @highlight substring=0
*
*
* System.out.println("After two empty lines");
* }
*
* <a href="#mysnippet">See mysnippet</a>.