From 7028483b92cf987d53438eb53c991c1151ccdf04 Mon Sep 17 00:00:00 2001 From: deadgander Date: Tue, 1 Aug 2023 21:28:45 +0100 Subject: [PATCH 1/3] backstep end position to avoid duplicating last line when cursor at start of line --- .../ir/mmd/intellijDev/Actionable/duplicate/DuplicateAction.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ir/mmd/intellijDev/Actionable/duplicate/DuplicateAction.kt b/src/main/java/ir/mmd/intellijDev/Actionable/duplicate/DuplicateAction.kt index 12b21634..3b743194 100644 --- a/src/main/java/ir/mmd/intellijDev/Actionable/duplicate/DuplicateAction.kt +++ b/src/main/java/ir/mmd/intellijDev/Actionable/duplicate/DuplicateAction.kt @@ -75,7 +75,7 @@ abstract class DuplicateAction : MultiCaretAction(), DumbAware { if (start != end) /* has selection */ { startingLine = document.getLineNumber(start) - endingLine = document.getLineNumber(end) + endingLine = document.getLineNumber(end - 1) startOffset = document.getLineStartOffset(startingLine) endOffset = document.getLineEndOffset(endingLine) } else /* no selection */ { From 12ca9d7d4f997c6e02be2a9d1f0bf05443b356c5 Mon Sep 17 00:00:00 2001 From: deadgander Date: Thu, 3 Aug 2023 21:47:48 +0100 Subject: [PATCH 2/3] backstep end position to avoid duplicating last line when cursor at start of line --- .../Actionable/duplicate/DuplicateAction.kt | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/main/java/ir/mmd/intellijDev/Actionable/duplicate/DuplicateAction.kt b/src/main/java/ir/mmd/intellijDev/Actionable/duplicate/DuplicateAction.kt index 3b743194..ed5a92a6 100644 --- a/src/main/java/ir/mmd/intellijDev/Actionable/duplicate/DuplicateAction.kt +++ b/src/main/java/ir/mmd/intellijDev/Actionable/duplicate/DuplicateAction.kt @@ -25,7 +25,11 @@ abstract class DuplicateAction : MultiCaretAction(), DumbAware { end: Int ) = with(getDuplicateString(start, end)) { runWriteCommandAction { + // Reset caret position and selection after inserting text. + val caretOffset = editor.caretModel.offset document.insertString(endOffset, "\n${text}") + editor.caretModel.currentCaret.setSelection(start, end) + editor.caretModel.moveToOffset(caretOffset) } } @@ -38,20 +42,12 @@ abstract class DuplicateAction : MultiCaretAction(), DumbAware { end: Int ) = with(getDuplicateString(start, end)) { runWriteCommandAction { + // Reset caret position and selection after inserting text. + val caretOffset = editor.caretModel.offset document.insertString(startOffset, "${text}\n") - - // Check if the caret is at line start, - // then move the caret manually. - // Due to an issue that caret won't move automatically. - // To be more clear, you can comment out the statements below and see the effect. - // Put the caret at the line start and fire the `duplicate down` action. - if (startOffset == start) (editor.caretModel.getCaretAt(VisualPosition(startingLine, 0)) ?: return@runWriteCommandAction) - .moveToLogicalPosition( - LogicalPosition( - startingLine + (endingLine - startingLine + 1), - 0 - ) - ) + val insertLength = text.length + 1 + editor.caretModel.currentCaret.setSelection(start + insertLength, end + insertLength) + editor.caretModel.moveToOffset(caretOffset + insertLength) } } @@ -72,9 +68,9 @@ abstract class DuplicateAction : MultiCaretAction(), DumbAware { val endingLine: Int val startOffset: Int val endOffset: Int - if (start != end) /* has selection */ { startingLine = document.getLineNumber(start) + // move back one character to avoid selecting last line when at start of line endingLine = document.getLineNumber(end - 1) startOffset = document.getLineStartOffset(startingLine) endOffset = document.getLineEndOffset(endingLine) From 38970ba9a66946f2361f2eb7cac26dc176b9a7ca Mon Sep 17 00:00:00 2001 From: deadgander Date: Thu, 3 Aug 2023 21:52:08 +0100 Subject: [PATCH 3/3] backstep end position to avoid duplicating last line when cursor at start of line --- .../mmd/intellijDev/Actionable/duplicate/DuplicateAction.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/ir/mmd/intellijDev/Actionable/duplicate/DuplicateAction.kt b/src/main/java/ir/mmd/intellijDev/Actionable/duplicate/DuplicateAction.kt index ed5a92a6..4f8d20d9 100644 --- a/src/main/java/ir/mmd/intellijDev/Actionable/duplicate/DuplicateAction.kt +++ b/src/main/java/ir/mmd/intellijDev/Actionable/duplicate/DuplicateAction.kt @@ -25,8 +25,8 @@ abstract class DuplicateAction : MultiCaretAction(), DumbAware { end: Int ) = with(getDuplicateString(start, end)) { runWriteCommandAction { - // Reset caret position and selection after inserting text. - val caretOffset = editor.caretModel.offset + // Reset caret position and selection after inserting text. + val caretOffset = editor.caretModel.offset document.insertString(endOffset, "\n${text}") editor.caretModel.currentCaret.setSelection(start, end) editor.caretModel.moveToOffset(caretOffset) @@ -43,7 +43,7 @@ abstract class DuplicateAction : MultiCaretAction(), DumbAware { ) = with(getDuplicateString(start, end)) { runWriteCommandAction { // Reset caret position and selection after inserting text. - val caretOffset = editor.caretModel.offset + val caretOffset = editor.caretModel.offset document.insertString(startOffset, "${text}\n") val insertLength = text.length + 1 editor.caretModel.currentCaret.setSelection(start + insertLength, end + insertLength)