-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add any-quotes-text-object extension
- Loading branch information
Showing
7 changed files
with
180 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
src/main/java/com/maddyhome/idea/vim/extension/anyquotestextobj/AnyQuotesTextObj.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Copyright 2003-2023 The IdeaVim authors | ||
* | ||
* Use of this source code is governed by an MIT-style | ||
* license that can be found in the LICENSE.txt file or at | ||
* https://opensource.org/licenses/MIT. | ||
*/ | ||
package com.maddyhome.idea.vim.extension.anyquotestextobj | ||
|
||
import com.maddyhome.idea.vim.KeyHandler | ||
import com.maddyhome.idea.vim.VimPlugin | ||
import com.maddyhome.idea.vim.api.ExecutionContext | ||
import com.maddyhome.idea.vim.api.ImmutableVimCaret | ||
import com.maddyhome.idea.vim.api.VimEditor | ||
import com.maddyhome.idea.vim.api.injector | ||
import com.maddyhome.idea.vim.command.CommandFlags | ||
import com.maddyhome.idea.vim.command.MappingMode | ||
import com.maddyhome.idea.vim.command.OperatorArguments | ||
import com.maddyhome.idea.vim.command.TextObjectVisualType | ||
import com.maddyhome.idea.vim.common.TextRange | ||
import com.maddyhome.idea.vim.extension.ExtensionHandler | ||
import com.maddyhome.idea.vim.extension.VimExtension | ||
import com.maddyhome.idea.vim.extension.VimExtensionFacade.putExtensionHandlerMapping | ||
import com.maddyhome.idea.vim.extension.VimExtensionFacade.putKeyMappingIfMissing | ||
import com.maddyhome.idea.vim.handler.TextObjectActionHandler | ||
import com.maddyhome.idea.vim.helper.enumSetOf | ||
import com.maddyhome.idea.vim.state.KeyHandlerState | ||
import org.jetbrains.annotations.NonNls | ||
import java.util.* | ||
|
||
/** | ||
* Add motions to work with any type of quotes: single, double, and back quotes | ||
* | ||
* <p> | ||
* any quotes provides two motions: | ||
* <ul> | ||
* <li>aq around any quotes.</li> | ||
* <li>iq inside any quotes</li> | ||
* </ul> | ||
* | ||
* @author Osvaldo Cordova Aburto (@oca159) | ||
*/ | ||
internal class AnyQuotesTextObj : VimExtension { | ||
|
||
override fun getName() = "any-quotes-text-object" | ||
|
||
@NonNls | ||
private val NO_MAPPINGS = "anyquotes_no_mappings" | ||
|
||
override fun init() { | ||
putExtensionHandlerMapping( | ||
MappingMode.XO, | ||
injector.parser.parseKeys("<Plug>any-quotes-aq"), | ||
owner, | ||
AroundAnyQuotesHandler(), | ||
false | ||
) | ||
putExtensionHandlerMapping( | ||
MappingMode.XO, | ||
injector.parser.parseKeys("<Plug>any-quotes-iq"), | ||
owner, | ||
InsideAnyQuotesHandler(), | ||
false | ||
) | ||
|
||
val noMappings = VimPlugin.getVariableService().getGlobalVariableValue(NO_MAPPINGS)?.asBoolean() ?: false | ||
if (!noMappings) { | ||
putKeyMappingIfMissing( | ||
MappingMode.XO, | ||
injector.parser.parseKeys("aq"), | ||
owner, | ||
injector.parser.parseKeys("<Plug>any-quotes-aq"), | ||
true | ||
) | ||
putKeyMappingIfMissing( | ||
MappingMode.XO, | ||
injector.parser.parseKeys("iq"), | ||
owner, | ||
injector.parser.parseKeys("<Plug>any-quotes-iq"), | ||
true | ||
) | ||
} | ||
} | ||
|
||
private class InsideAnyQuotesHandler : ExtensionHandler { | ||
override val isRepeatable = true | ||
|
||
override fun execute(editor: VimEditor, context: ExecutionContext, operatorArguments: OperatorArguments) { | ||
val keyHandlerState: KeyHandlerState = KeyHandler.getInstance().keyHandlerState | ||
keyHandlerState.commandBuilder.addAction(MotionInnerAnyQuoteProximityAction()) | ||
} | ||
} | ||
|
||
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 | ||
} | ||
} | ||
|
||
private class AroundAnyQuotesHandler : ExtensionHandler { | ||
override val isRepeatable = true | ||
|
||
override fun execute(editor: VimEditor, context: ExecutionContext, operatorArguments: OperatorArguments) { | ||
val keyHandlerState: KeyHandlerState = KeyHandler.getInstance().keyHandlerState | ||
keyHandlerState.commandBuilder.addAction(MotionOuterAnyQuoteProximityAction()) | ||
} | ||
} | ||
} | ||
|
||
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 | ||
} | ||
} | ||
|
||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters