Skip to content

Commit fb2224d

Browse files
committed
Bugfix for documentation block completion
1 parent 905e211 commit fb2224d

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/main/java/com/intellij/plugins/haxe/ide/editor/HaxeDocumentationEnterHandler.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
import com.intellij.psi.util.PsiUtilCore;
1818
import org.jetbrains.annotations.NotNull;
1919

20+
import java.util.regex.Pattern;
21+
2022
import static com.intellij.plugins.haxe.ide.HaxeCommenter.DOC_COMMENT_PREFIX;
23+
import static com.intellij.plugins.haxe.ide.HaxeCommenter.DOC_COMMENT_SUFFIX;
2124

2225
/// Custom logic to correctly handle completion of haxe documentation block (the default javadoc stuff does not work correctly for haxedocs)
2326
/// if block is incomplete then this handler will add the end "tag" and make sure we get correct indentation
@@ -61,7 +64,7 @@ public Result preprocessEnter(
6164
String docIndent = indentOptions.USE_TAB_CHARACTER ? "\t" : " ";
6265
String docNewLine = indent + docIndent;
6366

64-
String toInsert = "\n" + docNewLine + "\n" + indent + HaxeCommenter.DOC_COMMENT_SUFFIX;
67+
String toInsert = "\n" + docNewLine + "\n" + indent + DOC_COMMENT_SUFFIX;
6568
document.insertString(caretOffset, toInsert);
6669

6770
editor.getCaretModel().moveToOffset(caretOffset + 1 + docNewLine.length());
@@ -77,18 +80,32 @@ public Result preprocessEnter(
7780
private static boolean isInsideDocsWithoutCloseTag(@NotNull PsiFile file, int caretOffset) {
7881
PsiElement elementAtOffset = PsiUtilCore.getElementAtOffset(file, caretOffset);
7982
if (elementAtOffset instanceof HaxePsiDocCommentImpl docComment) {
80-
String text = docComment.getText();
83+
String text = getDocumentWithoutDocumentationBlocks(docComment);
8184
if (caretOffset < elementAtOffset.getTextOffset() + DOC_COMMENT_PREFIX.length()) {
8285
return false;
8386
}
84-
if(text.endsWith(HaxeCommenter.BLOCK_COMMENT_SUFFIX) || text.endsWith(HaxeCommenter.DOC_COMMENT_SUFFIX)) {
87+
if(text.endsWith(HaxeCommenter.BLOCK_COMMENT_SUFFIX) || text.endsWith(DOC_COMMENT_SUFFIX)) {
8588
return false;
8689
}
8790
return true;
8891
}
8992
return false;
9093
}
9194

95+
/**
96+
* Removes all other documentation blocks from text to avoid accidentally finding endTag belonging to a different block.
97+
*/
98+
private static @NotNull String getDocumentWithoutDocumentationBlocks(HaxePsiDocCommentImpl docComment) {
99+
String text = docComment.getText();
100+
int startTag = text.indexOf(DOC_COMMENT_PREFIX);
101+
String startOfDocument = text.substring(0, startTag + DOC_COMMENT_PREFIX.length());
102+
String restOfDocument = text.substring(startTag + DOC_COMMENT_PREFIX.length());
103+
104+
String escapedPrefix = Pattern.quote(DOC_COMMENT_PREFIX);
105+
String escapedSuffix = Pattern.quote(DOC_COMMENT_SUFFIX);
106+
// return text without any other documentation blocks
107+
return startOfDocument + restOfDocument.replaceAll("(?s)" + escapedPrefix + ".*" + escapedSuffix, "");
108+
}
92109

93110
/**
94111
* Temp solution to find line indentation (should probably be handled by formatter)

0 commit comments

Comments
 (0)