Result (@NotNull final PsiFile file, @NotNull final Editor editor, @NotNull final Ref<Integer> caretOffsetRef, final @NotNull Ref<Integer> caretAdvance, @NotNull final DataContext dataContext, final EditorActionHandler originalHandler) { CodeDocumentationAwareCommenter commenter = EnterInCommentUtil.getDocumentationAwareCommenter(dataContext); if (commenter == null) return Result.Continue; int caretOffset = caretOffsetRef.get(); int lineCommentStartOffset = getLineCommentStartOffset(editor, caretOffset, commenter); if (lineCommentStartOffset < 0) return Result.Continue; Document document = editor.getDocument(); CharSequence text = document.getImmutableCharSequence(); final int offset = CharArrayUtil.shiftForward(text, caretOffset, WHITESPACE); if (offset >= document.getTextLength() || text.charAt(offset) == '\n') return Result.Continue; String prefix = commenter.getLineCommentPrefix(); assert prefix != null : "Line Comment type is set but Line Comment Prefix is null!"; String prefixTrimmed = prefix.trim(); int beforeCommentOffset = CharArrayUtil.shiftBackward(text, lineCommentStartOffset - 1, WHITESPACE); boolean onlyCommentInCaretLine = beforeCommentOffset < 0 || text.charAt(beforeCommentOffset) == '\n'; CharSequence spacing = " "; if (StringUtil.startsWith(text, offset, prefix)) { int afterPrefix = offset + prefixTrimmed.length(); if (afterPrefix < document.getTextLength() && text.charAt(afterPrefix) != ' ') { document.insertString(afterPrefix, spacing); } caretOffsetRef.set(offset); } else { if (onlyCommentInCaretLine) { int indentStart = lineCommentStartOffset + prefix.trim().length(); int indentEnd = CharArrayUtil.shiftForward(text, indentStart, WHITESPACE); CharSequence currentLineSpacing = text.subSequence(indentStart, indentEnd); if (TodoConfiguration.getInstance().isMultiLine() && EnterInCommentUtil.isTodoText(text, lineCommentStartOffset, caretOffset) && EnterInCommentUtil.isTodoText(text, lineCommentStartOffset, DocumentUtil.getLineEndOffset(lineCommentStartOffset, document))) { spacing = currentLineSpacing + " "; } else if (currentLineSpacing.length() > 0) { spacing = currentLineSpacing; } int textStart = CharArrayUtil.shiftForward(text, caretOffset, WHITESPACE); document.deleteString(caretOffset, textStart); } else { if (text.charAt(caretOffset) == ' ') spacing = ""; } document.insertString(caretOffset, prefixTrimmed + spacing); } if (onlyCommentInCaretLine) { caretAdvance.set(prefixTrimmed.length() + spacing.length()); } return Result.DefaultForceIndent; }