unify empty line implementation

This commit is contained in:
Dennis Ushakov
2018-07-13 11:11:45 +01:00
parent 1a4a5c09bb
commit cc5f7cd12e
4 changed files with 16 additions and 24 deletions
@@ -133,4 +133,14 @@ public final class DocumentUtil {
public static int getNextCodePointOffset(@NotNull Document document, int offset) {
return offset + (isSurrogatePair(document, offset) ? 2 : 1);
}
public static boolean isLineEmpty(@NotNull Document document, final int line) {
final CharSequence chars = document.getCharsSequence();
int start = document.getLineStartOffset(line);
int end = Math.min(document.getLineEndOffset(line), document.getTextLength() - 1);
for (int i = start; i <= end; i++) {
if (!Character.isWhitespace(chars.charAt(i))) return false;
}
return true;
}
}
@@ -119,7 +119,7 @@ public class CommentByLineCommentHandler extends MultiCaretCodeInsightActionHand
startOffset == document.getLineStartOffset(document.getLineNumber(startOffset)) &&
endOffset == document.getLineEndOffset(document.getLineNumber(endOffset - 1)) + 1;
boolean startingNewLineComment = !hasSelection
&& isLineEmpty(document, document.getLineNumber(startOffset))
&& DocumentUtil.isLineEmpty(document, document.getLineNumber(startOffset))
&& !Comparing.equal(IdeActions.ACTION_COMMENT_LINE,
ActionManagerEx.getInstanceEx().getPrevPreformedActionId());
currentBlock.caretUpdate = startingNewLineComment ? CaretUpdate.PUT_AT_COMMENT_START :
@@ -172,7 +172,7 @@ public class CommentByLineCommentHandler extends MultiCaretCodeInsightActionHand
}
block.commenters[line - startLine] = commenter;
if (!isLineCommented(block, line, commenter) && (singleline || !isLineEmpty(document, line))) {
if (!isLineCommented(block, line, commenter) && (singleline || !DocumentUtil.isLineEmpty(document, line))) {
allLinesCommented = false;
if (commenter instanceof IndentedCommenter) {
final Boolean value = ((IndentedCommenter)commenter).forceIndentedLineComment();
@@ -306,16 +306,6 @@ public class CommentByLineCommentHandler extends MultiCaretCodeInsightActionHand
return blockSuitableCommenter;
}
private static boolean isLineEmpty(Document document, final int line) {
final CharSequence chars = document.getCharsSequence();
int start = document.getLineStartOffset(line);
int end = Math.min(document.getLineEndOffset(line), document.getTextLength() - 1);
for (int i = start; i <= end; i++) {
if (!Character.isWhitespace(chars.charAt(i))) return false;
}
return true;
}
private static boolean isLineCommented(Block block, final int line, final Commenter commenter) {
boolean commented;
int lineEndForBlockCommenting = -1;
@@ -7,10 +7,11 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorModificationUtil;
import com.intellij.openapi.editor.actionSystem.EditorAction;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import static com.intellij.util.DocumentUtil.isLineEmpty;
/**
* Emulates Emacs 'backward-paragraph' action
*/
@@ -62,10 +63,5 @@ public class BackwardParagraphAction extends EditorAction {
caret.moveToOffset(targetOffset);
EditorModificationUtil.scrollToCaret(editor);
}
private static boolean isLineEmpty(Document document, int line) {
return StringUtil.equalsIgnoreWhitespaces(
document.getImmutableCharSequence().subSequence(document.getLineStartOffset(line), document.getLineEndOffset(line)), "");
}
}
}
@@ -7,10 +7,11 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorModificationUtil;
import com.intellij.openapi.editor.actionSystem.EditorAction;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import static com.intellij.util.DocumentUtil.isLineEmpty;
/**
* Emulates Emacs 'forward-paragraph' action
*/
@@ -52,10 +53,5 @@ public class ForwardParagraphAction extends EditorAction {
caret.moveToOffset(targetOffset);
EditorModificationUtil.scrollToCaret(editor);
}
private static boolean isLineEmpty(Document document, int line) {
return StringUtil.equalsIgnoreWhitespaces(
document.getImmutableCharSequence().subSequence(document.getLineStartOffset(line), document.getLineEndOffset(line)), "");
}
}
}