IDEA-133854: Make Copy/Duplicate last line include the missing line end

Not supported for the "Cut" action yet.

GitOrigin-RevId: e6cf61f210c1bb0f671d41d9208052f061ba3c8f
This commit is contained in:
Eldar Abusalimov
2022-09-05 13:34:33 +02:00
committed by intellij-monorepo-bot
parent dcbbf9a250
commit 7516b6623d
7 changed files with 20 additions and 28 deletions

View File

@@ -31,7 +31,8 @@ xxx<caret>
}
void testEmpty() {
doTest '<caret>', "txt", '<caret>'
doTest '<caret>', "txt", '''
<caret>'''
}
private void doTest(String before, @NonNls String ext, String after) {

View File

@@ -102,15 +102,15 @@ public class JsonCopyPasteTest extends CodeInsightFixtureTestCase {
doTestFromTextToJson(" \"react-dom\": \"^16.5.2\"\n", "{\n" +
" \"name\": \"untitled\",\n" +
" \"version\": \"1.0.0\",\n" +
" \"dependencies\": {<caret>\n" +
" \"react\": \"^16.5.2\",\n" +
" \"dependencies\": {\n" +
" \"react\"<caret>: \"^16.5.2\",\n" +
" \"react-dom\": \"^16.5.2\"\n" +
" }\n" +
"}", "{\n" +
" \"name\": \"untitled\",\n" +
" \"version\": \"1.0.0\",\n" +
" \"dependencies\": { \"react-dom\": \"^16.5.2\",\n" +
"\n" +
" \"dependencies\": {\n" +
" \"react-dom\": \"^16.5.2\",\n" +
" \"react\": \"^16.5.2\",\n" +
" \"react-dom\": \"^16.5.2\"\n" +
" }\n" +

View File

@@ -54,9 +54,6 @@ public class CopyAction extends TextComponentEditorAction implements HintManager
caret.moveToVisualPosition(caret.getSelectionStartPosition());
}
});
if (!selectionModel.hasSelection(true)) {
return;
}
}
CopyPasteOptions copyPasteOptions = new CopyPasteOptions(isEntireLineFromEmptySelection);

View File

@@ -12,7 +12,6 @@ import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler;
import com.intellij.openapi.editor.ex.util.EditorUtil;
import com.intellij.openapi.util.TextRange;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class DuplicateAction extends EditorAction {
public DuplicateAction() {
@@ -50,14 +49,10 @@ public class DuplicateAction extends EditorAction {
}
}
@Nullable
static TextRange duplicateLinesRange(@NotNull Editor editor,
@NotNull VisualPosition rangeStart,
@NotNull VisualPosition rangeEnd) {
static @NotNull TextRange duplicateLinesRange(@NotNull Editor editor,
@NotNull VisualPosition rangeStart,
@NotNull VisualPosition rangeEnd) {
TextRange range = EditorUtil.calcSurroundingTextRange(editor, rangeStart, rangeEnd);
if (range.isEmpty()) {
return null;
}
String s = editor.getDocument().getText(range);
int offset = editor.getCaretModel().getOffset();
int newOffset = offset + range.getLength();

View File

@@ -28,11 +28,8 @@ public final class DuplicateLinesAction extends EditorAction {
}
VisualPosition rangeStart = editor.offsetToVisualPosition(Math.min(selStart, selEnd));
VisualPosition rangeEnd = editor.offsetToVisualPosition(Math.max(selStart, selEnd));
TextRange copiedRange =
DuplicateAction.duplicateLinesRange(editor, rangeStart, rangeEnd);
if (copiedRange != null) {
editor.getSelectionModel().setSelection(copiedRange.getStartOffset(), copiedRange.getEndOffset());
}
TextRange copiedRange = DuplicateAction.duplicateLinesRange(editor, rangeStart, rangeEnd);
editor.getSelectionModel().setSelection(copiedRange.getStartOffset(), copiedRange.getEndOffset());
}
else {
VisualPosition caretPos = editor.getCaretModel().getVisualPosition();

View File

@@ -58,10 +58,11 @@ public class EditorCopyPasteHelperImpl extends EditorCopyPasteHelper {
int[] endOffsets = new int[carets.size()];
for (int i = 0; i < carets.size(); i++) {
buf.append(separator);
String caretSelectedText = carets.get(i).getSelectedText();
String caretSelectedText = StringUtil.notNullize(carets.get(i).getSelectedText());
startOffsets[i] = buf.length();
if (caretSelectedText != null) {
buf.append(caretSelectedText);
buf.append(caretSelectedText);
if (options.isEntireLineFromEmptySelection() && !caretSelectedText.endsWith("\n")) {
buf.append("\n"); // make sure the last line with no '\n' is still copied as a real line
}
endOffsets[i] = buf.length();
separator = "\n";
@@ -130,6 +131,7 @@ public class EditorCopyPasteHelperImpl extends EditorCopyPasteHelper {
public static @NotNull TextRange insertEntireLineAboveCaret(@NotNull Editor editor, @NotNull String text) {
int caretOffset = editor.getCaretModel().getOffset();
int lineStartOffset = EditorUtil.getNotFoldedLineStartOffset(editor, caretOffset);
if (!text.endsWith("\n")) text += "\n";
editor.getDocument().insertString(lineStartOffset, text);
EditorModificationUtilEx.scrollToCaret(editor);
return TextRange.from(lineStartOffset, text.length());

View File

@@ -219,26 +219,26 @@ public class EditorMultiCaretTest extends AbstractEditorTest {
public void testCopyPasteFromEmptySelection() {
initText("<caret>one two \n" +
"three<caret> four\n");
"three<caret> four");
executeAction("EditorCopy");
executeAction("EditorLineEnd");
executeAction("EditorPaste");
checkResultByText("one two \n" +
"one two<caret> \n" +
"three four\n" +
"three four<caret>\n");
"three four<caret>");
}
public void testCopyPasteFromEmptySelectionMultiCaretOnEachLine() {
initText("<caret>one <caret>two \n" +
"three<caret> four<caret>\n");
"three<caret> four<caret>");
executeAction("EditorCopy");
executeAction("EditorLineEnd");
executeAction("EditorPaste");
checkResultByText("one two \n" +
"one two<caret> \n" +
"three four\n" +
"three four<caret>\n");
"three four<caret>");
}
public void testCutAndPaste() {