Skip to content

Commit

Permalink
Update MotionQuoteAction.kt to support iq and aq motions
Browse files Browse the repository at this point in the history
  • Loading branch information
oca159 authored Jan 8, 2025
1 parent 1b030a6 commit f0256ac
Showing 1 changed file with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,61 @@ class MotionOuterBlockDoubleQuoteAction : TextObjectActionHandler() {
}
}

@CommandOrMotion(keys = ["iq"], modes = [Mode.VISUAL, Mode.OP_PENDING])
class MotionInnerAnyQuoteProximityAction : TextObjectActionHandler() {

override val flags: EnumSet<CommandFlags> = enumSetOf(CommandFlags.FLAG_TEXT_BLOCK)

override val visualType: TextObjectVisualType = TextObjectVisualType.CHARACTER_WISE

override fun getRange(
editor: VimEditor,
caret: ImmutableVimCaret,
context: ExecutionContext,
count: Int,
rawCount: Int,
): TextRange? {
return listOf('`', '"', '\'')
.mapNotNull { quote ->
injector.searchHelper.findBlockQuoteInLineRange(editor, caret, quote, false)?.let { range -> range to quote }
}
.minByOrNull { it.first.distanceTo(caret.offset) }?.first
}
}

@CommandOrMotion(keys = ["aq"], modes = [Mode.VISUAL, Mode.OP_PENDING])
class MotionOuterAnyQuoteProximityAction : TextObjectActionHandler() {

override val flags: EnumSet<CommandFlags> = enumSetOf(CommandFlags.FLAG_TEXT_BLOCK)

override val visualType: TextObjectVisualType = TextObjectVisualType.CHARACTER_WISE

override fun getRange(
editor: VimEditor,
caret: ImmutableVimCaret,
context: ExecutionContext,
count: Int,
rawCount: Int,
): TextRange? {
return listOf('`', '"', '\'')
.mapNotNull { quote ->
injector.searchHelper.findBlockQuoteInLineRange(editor, caret, quote, true)?.let { range -> range to quote }
}
.minByOrNull { it.first.distanceTo(caret.offset) }?.first
}
}

// Extension function to calculate distance
private fun TextRange.distanceTo(caretOffset: Int): Int {
return if (caretOffset < startOffset) {
startOffset - caretOffset
} else if (caretOffset > endOffset) {
caretOffset - endOffset
} else {
0 // Caret is inside the range
}
}

@CommandOrMotion(keys = ["a'"], modes = [Mode.VISUAL, Mode.OP_PENDING])
class MotionOuterBlockSingleQuoteAction : TextObjectActionHandler() {

Expand Down

0 comments on commit f0256ac

Please sign in to comment.