From c125707f37fb35396bb4ed2e841ecd7e11da3472 Mon Sep 17 00:00:00 2001 From: Dmitry Batrak Date: Thu, 4 Oct 2018 09:40:21 +0300 Subject: [PATCH] move logic related to handling Enter in line comments from EnterHandler to EnterInLineCommentHandler --- .../editorActions/CodeDocumentationUtil.java | 21 ++++-------- .../editorActions/EnterHandler.java | 12 +++---- .../enter/EnterHandlerDelegate.java | 3 +- .../enter/EnterInLineCommentHandler.java | 33 ++++++++++++------- 4 files changed, 35 insertions(+), 34 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/CodeDocumentationUtil.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/CodeDocumentationUtil.java index 2609a2b77c40..b029e837e7f2 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/CodeDocumentationUtil.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/CodeDocumentationUtil.java @@ -113,12 +113,11 @@ public class CodeDocumentationUtil { @NotNull public static CommentContext tryParseCommentContext(@NotNull PsiFile file, @NotNull CharSequence chars, int offset, int lineStartOffset) { Commenter langCommenter = LanguageCommenters.INSTANCE.forLanguage(PsiUtilCore.getLanguageAtOffset(file, offset)); - return tryParseCommentContext(langCommenter, chars, offset, lineStartOffset); + return tryParseCommentContext(langCommenter, chars, lineStartOffset); } static CommentContext tryParseCommentContext(@Nullable Commenter langCommenter, @NotNull CharSequence chars, - int offset, int lineStartOffset) { final boolean isInsideCommentLikeCode = langCommenter instanceof CodeDocumentationAwareCommenter; if (!isInsideCommentLikeCode) { @@ -133,11 +132,7 @@ public class CodeDocumentationUtil { && CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getBlockCommentPrefix()); boolean docAsterisk = commenter.getDocumentationCommentLinePrefix() != null && CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getDocumentationCommentLinePrefix()); - final int firstNonSpaceInLine = CharArrayUtil.shiftForward(chars, offset, " \t"); - boolean slashSlash = commenter.getLineCommentPrefix() != null - && CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getLineCommentPrefix()) - && firstNonSpaceInLine < chars.length() && chars.charAt(firstNonSpaceInLine) != '\n'; - return new CommentContext(commenter, docStart, cStyleStart, docAsterisk, slashSlash, commentStartOffset); + return new CommentContext(commenter, docStart, cStyleStart, docAsterisk, commentStartOffset); } /** @@ -157,21 +152,19 @@ public class CodeDocumentationUtil { /** Indicates position at the line that starts from {@code '*'} (non-first and non-last javadoc line in java language). */ public boolean docAsterisk; - /** Indicates position at the line that starts from {@code '//'} (in java language). */ - public boolean slashSlash; - public CommentContext() { commenter = null; lineStart = 0; } - public CommentContext(CodeDocumentationAwareCommenter commenter, boolean docStart, boolean cStyleStart, boolean docAsterisk, - boolean slashSlash, int lineStart) - { + public CommentContext(CodeDocumentationAwareCommenter commenter, + boolean docStart, + boolean cStyleStart, + boolean docAsterisk, + int lineStart) { this.docStart = docStart; this.cStyleStart = cStyleStart; this.docAsterisk = docAsterisk; - this.slashSlash = slashSlash; this.commenter = commenter; this.lineStart = lineStart; } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java index e17ff3260221..5456120b56df 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java @@ -357,7 +357,7 @@ public class EnterHandler extends BaseEnterHandler { Language language = myDataContext instanceof UserDataHolder ? CONTEXT_LANGUAGE.get((UserDataHolder)myDataContext):null; Commenter langCommenter = language != null ? LanguageCommenters.INSTANCE.forLanguage(language) : null; CodeDocumentationUtil.CommentContext commentContext - = CodeDocumentationUtil.tryParseCommentContext(langCommenter, chars, myOffset, lineStart); + = CodeDocumentationUtil.tryParseCommentContext(langCommenter, chars, lineStart); PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(getProject()); if (commentContext.docStart) { @@ -445,9 +445,7 @@ public class EnterHandler extends BaseEnterHandler { boolean docIndentApplied = false; CodeInsightSettings codeInsightSettings = CodeInsightSettings.getInstance(); - if (codeInsightSettings.SMART_INDENT_ON_ENTER || myForceIndent || commentContext.docStart || commentContext.docAsterisk - || commentContext.slashSlash) - { + if (codeInsightSettings.SMART_INDENT_ON_ENTER || myForceIndent || commentContext.docStart || commentContext.docAsterisk) { final int offset = adjustLineIndentNoCommit(getLanguage(myDataContext), myDocument, myEditor, myOffset); if (offset >= 0) { myOffset = offset; @@ -467,7 +465,7 @@ public class EnterHandler extends BaseEnterHandler { } } - if ((commentContext.docAsterisk || commentContext.docStart || commentContext.slashSlash) && !docIndentApplied) { + if ((commentContext.docAsterisk || commentContext.docStart) && !docIndentApplied) { if (myInsertSpace) { if (myOffset == myDocument.getTextLength()) { myDocument.insertString(myOffset, " "); @@ -481,8 +479,8 @@ public class EnterHandler extends BaseEnterHandler { } } - if ((commentContext.docAsterisk || commentContext.slashSlash) && !commentContext.docStart) { - myCaretAdvance += commentContext.slashSlash ? commentContext.commenter.getLineCommentPrefix().trim().length() : 1; + if (commentContext.docAsterisk && !commentContext.docStart) { + myCaretAdvance += 1; } } catch (IncorrectOperationException e) { diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/enter/EnterHandlerDelegate.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/enter/EnterHandlerDelegate.java index 6b85fede6c9e..3fd6287cc7d1 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/enter/EnterHandlerDelegate.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/enter/EnterHandlerDelegate.java @@ -46,7 +46,8 @@ public interface EnterHandlerDelegate { * * @param file The PSI file associated with the document. * @param editor The editor. - * @param caretOffset A reference to the current caret offset in the document. + * @param caretOffset Indicates a place where line break is to be inserted (it's a caret position initially). Method implementation + * can change this value to adjust target line break position. * @param caretAdvance A reference to the number of columns by which the caret must be moved forward. * @param dataContext The data context passed to the enter handler. * @param originalHandler The original handler. diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/enter/EnterInLineCommentHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/enter/EnterInLineCommentHandler.java index ff0a7b7dc034..20ab5ec132bc 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/enter/EnterInLineCommentHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/enter/EnterInLineCommentHandler.java @@ -31,14 +31,19 @@ import com.intellij.openapi.editor.highlighter.HighlighterIterator; import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiFile; -import com.intellij.psi.tree.IElementType; import com.intellij.util.text.CharArrayUtil; import org.jetbrains.annotations.NotNull; public class EnterInLineCommentHandler extends EnterHandlerDelegateAdapter { + private static final String WHITESPACE = " \t"; + @Override - public Result preprocessEnter(@NotNull final PsiFile file, @NotNull final Editor editor, @NotNull final Ref caretOffsetRef, @NotNull final Ref caretAdvance, - @NotNull final DataContext dataContext, final EditorActionHandler originalHandler) { + public Result preprocessEnter(@NotNull final PsiFile file, + @NotNull final Editor editor, + @NotNull final Ref caretOffsetRef, + @NotNull final Ref caretAdvance, + @NotNull final DataContext dataContext, + final EditorActionHandler originalHandler) { final Language language = EnterHandler.getLanguage(dataContext); if (language == null) return Result.Continue; final Commenter languageCommenter = LanguageCommenters.INSTANCE.forLanguage(language); @@ -46,10 +51,11 @@ public class EnterInLineCommentHandler extends EnterHandlerDelegateAdapter { ? (CodeDocumentationAwareCommenter)languageCommenter : null; if (commenter == null) return Result.Continue; int caretOffset = caretOffsetRef.get().intValue(); - if (isInLineComment(editor, caretOffset, commenter)) { + int lineCommentStartOffset = getLineCommentStartOffset(editor, caretOffset, commenter); + if (lineCommentStartOffset >= 0) { Document document = editor.getDocument(); CharSequence text = document.getText(); - final int offset = CharArrayUtil.shiftForward(text, caretOffset, " \t"); + final int offset = CharArrayUtil.shiftForward(text, caretOffset, WHITESPACE); if (offset < document.getTextLength() && text.charAt(offset) != '\n') { String prefix = commenter.getLineCommentPrefix(); assert prefix != null : "Line Comment type is set but Line Comment Prefix is null!"; @@ -58,28 +64,31 @@ public class EnterInLineCommentHandler extends EnterHandlerDelegateAdapter { prefix += " "; } document.insertString(caretOffset, prefix); - return Result.Default; } else { int afterPrefix = offset + prefix.length(); if (afterPrefix < document.getTextLength() && text.charAt(afterPrefix) != ' ') { document.insertString(afterPrefix, " "); - //caretAdvance.set(0); } caretOffsetRef.set(offset); } - return Result.Default; + + int beforeCommentOffset = CharArrayUtil.shiftBackward(text, lineCommentStartOffset - 1, WHITESPACE); + if (beforeCommentOffset < 0 || text.charAt(beforeCommentOffset) == '\n') { + caretAdvance.set(prefix.trim().length() + 1); + } + return Result.DefaultForceIndent; } } return Result.Continue; } - private static boolean isInLineComment(@NotNull Editor editor, int offset, @NotNull CodeDocumentationAwareCommenter commenter) { - if (offset < 1) return false; + private static int getLineCommentStartOffset(@NotNull Editor editor, int offset, @NotNull CodeDocumentationAwareCommenter commenter) { + if (offset < 1) return -1; EditorHighlighter highlighter = ((EditorEx)editor).getHighlighter(); HighlighterIterator iterator = highlighter.createIterator(offset - 1); String prefix = commenter.getLineCommentPrefix(); - return iterator.getTokenType() == commenter.getLineCommentTokenType() - && (iterator.getStart() + (prefix == null ? 0 : prefix.length())) <= offset; + return iterator.getTokenType() == commenter.getLineCommentTokenType() && + (iterator.getStart() + (prefix == null ? 0 : prefix.length())) <= offset ? iterator.getStart() : -1; } }