diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/actions/CutLineActionHandler.java b/platform/platform-impl/src/com/intellij/openapi/editor/actions/CutLineActionHandler.java index b64040de3ef6..4088ea0271ac 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/actions/CutLineActionHandler.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/actions/CutLineActionHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2015 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,14 +27,10 @@ import org.jetbrains.annotations.Nullable; class CutLineActionHandler extends EditorWriteActionHandler { private final boolean myToLineStart; - private final boolean myIgnoreSelection; - private final boolean myCopyToClipboard; - CutLineActionHandler(boolean toLineStart, boolean ignoreSelection, boolean copyToClipboard) { - super(!copyToClipboard); // as CutLineEndAction interacts with clipboard, multi-caret support for it needs to be implemented explicitly (todo) + CutLineActionHandler(boolean toLineStart) { + super(true); myToLineStart = toLineStart; - myIgnoreSelection = ignoreSelection; - myCopyToClipboard = copyToClipboard; } @Override @@ -42,7 +38,7 @@ class CutLineActionHandler extends EditorWriteActionHandler { if (caret == null) { caret = editor.getCaretModel().getCurrentCaret(); } - if (!myIgnoreSelection && caret.hasSelection()) { + if (caret.hasSelection()) { delete(editor, caret, caret.getSelectionStart(), caret.getSelectionEnd()); return; } @@ -79,13 +75,8 @@ class CutLineActionHandler extends EditorWriteActionHandler { delete(editor, caret, start, end); } - private void delete(@NotNull Editor editor, @NotNull Caret caret, int start, int end) { - if (myCopyToClipboard) { - KillRingUtil.copyToKillRing(editor, start, end, true); - } - else { - CopyPasteManager.getInstance().stopKillRings(); - } + private static void delete(@NotNull Editor editor, @NotNull Caret caret, int start, int end) { + CopyPasteManager.getInstance().stopKillRings(); editor.getDocument().deleteString(start, end); // in case the caret was in the virtual space, we force it to go back to the real offset diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/actions/CutLineEndAction.java b/platform/platform-impl/src/com/intellij/openapi/editor/actions/CutLineEndAction.java index 838567887f25..53936a874135 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/actions/CutLineEndAction.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/actions/CutLineEndAction.java @@ -24,8 +24,58 @@ */ package com.intellij.openapi.editor.actions; +import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.editor.*; +import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler; +import com.intellij.util.DocumentUtil; +import com.intellij.util.text.CharArrayUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + public class CutLineEndAction extends TextComponentEditorAction { public CutLineEndAction() { - super(new CutLineActionHandler(false, true, true)); + super(new Handler()); + } + + private static class Handler extends EditorWriteActionHandler { + private Handler() { + super(false); + } + + @Override + public void executeWriteAction(final Editor editor, @Nullable Caret caret, DataContext dataContext) { + if (caret == null && editor.getCaretModel().getCaretCount() > 1) { + editor.getCaretModel().runForEachCaret(new CaretAction() { + @Override + public void perform(Caret caret) { + caret.setSelection(caret.getOffset(), getEndOffset(caret)); + } + }); + // We don't support kill-ring operations for multiple carets currently + EditorCopyPasteHelper.getInstance().copySelectionToClipboard(editor); + EditorModificationUtil.deleteSelectedTextForAllCarets(editor); + } + else { + if (caret == null) { + caret = editor.getCaretModel().getCurrentCaret(); + } + int startOffset = caret.getOffset(); + int endOffset = getEndOffset(caret); + KillRingUtil.cut(editor, startOffset, endOffset); + // in case the caret was in the virtual space, we force it to go back to the real offset + caret.moveToOffset(startOffset); + } + } + + private static int getEndOffset(@NotNull Caret caret) { + Document document = caret.getEditor().getDocument(); + int startOffset = caret.getOffset(); + int endOffset = DocumentUtil.getLineEndOffset(startOffset, document); + if (endOffset < document.getTextLength() && + CharArrayUtil.isEmptyOrSpaces(document.getImmutableCharSequence(), startOffset, endOffset)) { + endOffset++; + } + return endOffset; + } } } diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/actions/DeleteToLineEndAction.java b/platform/platform-impl/src/com/intellij/openapi/editor/actions/DeleteToLineEndAction.java index c7f942ee38ad..9fb21fb67b7d 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/actions/DeleteToLineEndAction.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/actions/DeleteToLineEndAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2010 JetBrains s.r.o. + * Copyright 2000-2015 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,6 @@ import com.intellij.openapi.editor.actionSystem.EditorAction; public class DeleteToLineEndAction extends EditorAction { public DeleteToLineEndAction() { - super(new CutLineActionHandler(false, false, false)); + super(new CutLineActionHandler(false)); } } diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/actions/DeleteToLineStartAction.java b/platform/platform-impl/src/com/intellij/openapi/editor/actions/DeleteToLineStartAction.java index bdff4a18e60c..87dd580e105b 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/actions/DeleteToLineStartAction.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/actions/DeleteToLineStartAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2015 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,6 @@ import com.intellij.openapi.editor.actionSystem.EditorAction; public class DeleteToLineStartAction extends EditorAction { public DeleteToLineStartAction() { - super(new CutLineActionHandler(true, false, false)); + super(new CutLineActionHandler(true)); } } diff --git a/platform/platform-tests/testData/editor/multiCaret/EditorCutLineEnd-after.txt b/platform/platform-tests/testData/editor/multiCaret/EditorCutLineEnd-after.txt new file mode 100644 index 000000000000..92e772cec7cb --- /dev/null +++ b/platform/platform-tests/testData/editor/multiCaret/EditorCutLineEnd-after.txt @@ -0,0 +1,2 @@ +li +line \ No newline at end of file diff --git a/platform/platform-tests/testData/editor/multiCaret/EditorCutLineEnd-before.txt b/platform/platform-tests/testData/editor/multiCaret/EditorCutLineEnd-before.txt new file mode 100644 index 000000000000..5b5697ebe207 --- /dev/null +++ b/platform/platform-tests/testData/editor/multiCaret/EditorCutLineEnd-before.txt @@ -0,0 +1,2 @@ +line1 +line2 \ No newline at end of file