fix(javadoc-handler): insert leading tokens before start of comment

GitOrigin-RevId: 2fc351433f7841f9753d67e2a8d925a9991a1716
This commit is contained in:
Mathias Boulay
2024-09-17 16:28:17 +00:00
committed by intellij-monorepo-bot
parent 68242cd5b9
commit 027d4fb022
5 changed files with 52 additions and 5 deletions
@@ -8,13 +8,19 @@ import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.javadoc.PsiMarkdownCodeBlock;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.DocumentUtil;
import com.intellij.util.text.CharArrayUtil;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public class JavaDocMarkdownEnterHandler extends EnterHandlerDelegateAdapter {
@Override
public Result preprocessEnter(@NotNull PsiFile file,
@@ -30,14 +36,28 @@ public class JavaDocMarkdownEnterHandler extends EnterHandlerDelegateAdapter {
@ApiStatus.Internal
static Result preProcessEnterImpl(@NotNull PsiFile file,
@NotNull Editor editor,
@NotNull Ref<Integer> caretOffset,
@NotNull Ref<Integer> caretAdvance,
@NotNull DataContext dataContext,
EditorActionHandler originalHandler) {
@NotNull Editor editor,
@NotNull Ref<Integer> caretOffset,
@NotNull Ref<Integer> caretAdvance,
@NotNull DataContext dataContext,
EditorActionHandler originalHandler) {
PsiElement caretElement = file.findElementAt(caretOffset.get());
if (caretElement == null) return Result.Continue;
if (caretElement.getNode().getElementType() == JavaDocTokenType.DOC_COMMENT_LEADING_ASTERISKS) {
TextRange textRange = caretElement.getTextRange();
if (textRange.getStartOffset() == caretOffset.get() &&
Objects.requireNonNull(PsiTreeUtil.getParentOfType(caretElement, PsiDocComment.class)).getTextOffset() == textRange.getStartOffset()) {
// Bail out if the caret is placed are before the start of the comment
return Result.Continue;
}
// Avoid breaking a comment by placing the caret beyond the leading token, taking into account a potential space
int newOffset = textRange.getEndOffset();
caretOffset.set(editor.getDocument().getCharsSequence().charAt(newOffset) == ' ' ? newOffset + 1 : newOffset);
}
// EOL whitespace is not useful, we only need the tokens behind it
if (caretElement instanceof PsiWhiteSpace) {
// In multiline whitespaces, check cursor position to check whether the handler should trigger
@@ -0,0 +1,9 @@
// "_ignore" "true"
/// - [the `java.util` package][java.util]
/// - [a class][String]
/// - [a field][String#CASE_INSENSITIVE_ORDER]
/// - [a method][String#chars()]
class C{}
@@ -0,0 +1,6 @@
// "_ignore" "true"
/// Hello there
///
/// ````````
class C{}
@@ -0,0 +1,7 @@
// "_ignore" "true"
<caret>/// - [the `java.util` package][java.util]
/// - [a class][String]
/// - [a field][String#CASE_INSENSITIVE_ORDER]
/// - [a method][String#chars()]
<caret>class C{}
@@ -0,0 +1,5 @@
// "_ignore" "true"
/// Hello there
/<caret>// ````````
class C{}