Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backstep end position to avoid duplicating last line when cursor at start of line #106

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -72,10 +68,10 @@ abstract class DuplicateAction : MultiCaretAction(), DumbAware {
val endingLine: Int
val startOffset: Int
val endOffset: Int

if (start != end) /* has selection */ {
startingLine = document.getLineNumber(start)
endingLine = document.getLineNumber(end)
// 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)
} else /* no selection */ {
Expand Down