|
|
@ -19,10 +19,7 @@ import com.maddyhome.idea.vim.api.getLeadingCharacterOffset
|
|
|
|
import com.maddyhome.idea.vim.api.injector
|
|
|
|
import com.maddyhome.idea.vim.api.injector
|
|
|
|
import com.maddyhome.idea.vim.api.setChangeMarks
|
|
|
|
import com.maddyhome.idea.vim.api.setChangeMarks
|
|
|
|
import com.maddyhome.idea.vim.command.MappingMode
|
|
|
|
import com.maddyhome.idea.vim.command.MappingMode
|
|
|
|
import com.maddyhome.idea.vim.state.mode.Mode
|
|
|
|
|
|
|
|
import com.maddyhome.idea.vim.command.OperatorArguments
|
|
|
|
import com.maddyhome.idea.vim.command.OperatorArguments
|
|
|
|
import com.maddyhome.idea.vim.state.mode.SelectionType
|
|
|
|
|
|
|
|
import com.maddyhome.idea.vim.state.mode.selectionType
|
|
|
|
|
|
|
|
import com.maddyhome.idea.vim.common.TextRange
|
|
|
|
import com.maddyhome.idea.vim.common.TextRange
|
|
|
|
import com.maddyhome.idea.vim.extension.ExtensionHandler
|
|
|
|
import com.maddyhome.idea.vim.extension.ExtensionHandler
|
|
|
|
import com.maddyhome.idea.vim.extension.VimExtension
|
|
|
|
import com.maddyhome.idea.vim.extension.VimExtension
|
|
|
@ -34,7 +31,6 @@ import com.maddyhome.idea.vim.extension.VimExtensionFacade.putExtensionHandlerMa
|
|
|
|
import com.maddyhome.idea.vim.extension.VimExtensionFacade.putKeyMappingIfMissing
|
|
|
|
import com.maddyhome.idea.vim.extension.VimExtensionFacade.putKeyMappingIfMissing
|
|
|
|
import com.maddyhome.idea.vim.extension.VimExtensionFacade.setOperatorFunction
|
|
|
|
import com.maddyhome.idea.vim.extension.VimExtensionFacade.setOperatorFunction
|
|
|
|
import com.maddyhome.idea.vim.extension.VimExtensionFacade.setRegisterForCaret
|
|
|
|
import com.maddyhome.idea.vim.extension.VimExtensionFacade.setRegisterForCaret
|
|
|
|
import com.maddyhome.idea.vim.state.mode.mode
|
|
|
|
|
|
|
|
import com.maddyhome.idea.vim.helper.runWithEveryCaretAndRestore
|
|
|
|
import com.maddyhome.idea.vim.helper.runWithEveryCaretAndRestore
|
|
|
|
import com.maddyhome.idea.vim.key.OperatorFunction
|
|
|
|
import com.maddyhome.idea.vim.key.OperatorFunction
|
|
|
|
import com.maddyhome.idea.vim.newapi.IjVimCaret
|
|
|
|
import com.maddyhome.idea.vim.newapi.IjVimCaret
|
|
|
@ -43,6 +39,10 @@ import com.maddyhome.idea.vim.newapi.ij
|
|
|
|
import com.maddyhome.idea.vim.newapi.vim
|
|
|
|
import com.maddyhome.idea.vim.newapi.vim
|
|
|
|
import com.maddyhome.idea.vim.options.helpers.ClipboardOptionHelper
|
|
|
|
import com.maddyhome.idea.vim.options.helpers.ClipboardOptionHelper
|
|
|
|
import com.maddyhome.idea.vim.put.PutData
|
|
|
|
import com.maddyhome.idea.vim.put.PutData
|
|
|
|
|
|
|
|
import com.maddyhome.idea.vim.state.mode.Mode
|
|
|
|
|
|
|
|
import com.maddyhome.idea.vim.state.mode.SelectionType
|
|
|
|
|
|
|
|
import com.maddyhome.idea.vim.state.mode.mode
|
|
|
|
|
|
|
|
import com.maddyhome.idea.vim.state.mode.selectionType
|
|
|
|
import org.jetbrains.annotations.NonNls
|
|
|
|
import org.jetbrains.annotations.NonNls
|
|
|
|
import java.awt.event.KeyEvent
|
|
|
|
import java.awt.event.KeyEvent
|
|
|
|
import javax.swing.KeyStroke
|
|
|
|
import javax.swing.KeyStroke
|
|
|
@ -83,7 +83,7 @@ internal class VimSurroundExtension : VimExtension {
|
|
|
|
override val isRepeatable = true
|
|
|
|
override val isRepeatable = true
|
|
|
|
|
|
|
|
|
|
|
|
override fun execute(editor: VimEditor, context: ExecutionContext, operatorArguments: OperatorArguments) {
|
|
|
|
override fun execute(editor: VimEditor, context: ExecutionContext, operatorArguments: OperatorArguments) {
|
|
|
|
setOperatorFunction(Operator(supportsMultipleCursors = false)) // TODO
|
|
|
|
setOperatorFunction(Operator(supportsMultipleCursors = false, count = 1)) // TODO
|
|
|
|
executeNormalWithoutMapping(injector.parser.parseKeys("g@"), editor.ij)
|
|
|
|
executeNormalWithoutMapping(injector.parser.parseKeys("g@"), editor.ij)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -104,7 +104,7 @@ internal class VimSurroundExtension : VimExtension {
|
|
|
|
val lastNonWhiteSpaceOffset = getLastNonWhitespaceCharacterOffset(editor.text(), lineStartOffset, lineEndOffset)
|
|
|
|
val lastNonWhiteSpaceOffset = getLastNonWhitespaceCharacterOffset(editor.text(), lineStartOffset, lineEndOffset)
|
|
|
|
if (lastNonWhiteSpaceOffset != null) {
|
|
|
|
if (lastNonWhiteSpaceOffset != null) {
|
|
|
|
val range = TextRange(lineStartOffset, lastNonWhiteSpaceOffset + 1)
|
|
|
|
val range = TextRange(lineStartOffset, lastNonWhiteSpaceOffset + 1)
|
|
|
|
performSurround(pair, range, it)
|
|
|
|
performSurround(pair, range, it, count = operatorArguments.count1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// it.moveToOffset(lineStartOffset)
|
|
|
|
// it.moveToOffset(lineStartOffset)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -125,7 +125,7 @@ internal class VimSurroundExtension : VimExtension {
|
|
|
|
private class VSurroundHandler : ExtensionHandler {
|
|
|
|
private class VSurroundHandler : ExtensionHandler {
|
|
|
|
override fun execute(editor: VimEditor, context: ExecutionContext, operatorArguments: OperatorArguments) {
|
|
|
|
override fun execute(editor: VimEditor, context: ExecutionContext, operatorArguments: OperatorArguments) {
|
|
|
|
// NB: Operator ignores SelectionType anyway
|
|
|
|
// NB: Operator ignores SelectionType anyway
|
|
|
|
if (!Operator(supportsMultipleCursors = true).apply(editor, context, editor.mode.selectionType)) {
|
|
|
|
if (!Operator(supportsMultipleCursors = true, count = operatorArguments.count1).apply(editor, context, editor.mode.selectionType)) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
runWriteAction {
|
|
|
|
runWriteAction {
|
|
|
@ -261,7 +261,7 @@ internal class VimSurroundExtension : VimExtension {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private class Operator(private val supportsMultipleCursors: Boolean) : OperatorFunction {
|
|
|
|
private class Operator(private val supportsMultipleCursors: Boolean, private val count: Int) : OperatorFunction {
|
|
|
|
override fun apply(vimEditor: VimEditor, context: ExecutionContext, selectionType: SelectionType?): Boolean {
|
|
|
|
override fun apply(vimEditor: VimEditor, context: ExecutionContext, selectionType: SelectionType?): Boolean {
|
|
|
|
val editor = vimEditor.ij
|
|
|
|
val editor = vimEditor.ij
|
|
|
|
val c = getChar(editor)
|
|
|
|
val c = getChar(editor)
|
|
|
@ -273,11 +273,11 @@ internal class VimSurroundExtension : VimExtension {
|
|
|
|
val change = VimPlugin.getChange()
|
|
|
|
val change = VimPlugin.getChange()
|
|
|
|
if (supportsMultipleCursors) {
|
|
|
|
if (supportsMultipleCursors) {
|
|
|
|
editor.runWithEveryCaretAndRestore {
|
|
|
|
editor.runWithEveryCaretAndRestore {
|
|
|
|
applyOnce(editor, change, pair)
|
|
|
|
applyOnce(editor, change, pair, count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
applyOnce(editor, change, pair)
|
|
|
|
applyOnce(editor, change, pair, count)
|
|
|
|
// Jump back to start
|
|
|
|
// Jump back to start
|
|
|
|
executeNormalWithoutMapping(injector.parser.parseKeys("`["), editor)
|
|
|
|
executeNormalWithoutMapping(injector.parser.parseKeys("`["), editor)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -285,18 +285,15 @@ internal class VimSurroundExtension : VimExtension {
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun applyOnce(editor: Editor, change: VimChangeGroup, pair: Pair<String, String>) {
|
|
|
|
private fun applyOnce(editor: Editor, change: VimChangeGroup, pair: Pair<String, String>, count: Int) {
|
|
|
|
// XXX: Will it work with line-wise or block-wise selections?
|
|
|
|
// XXX: Will it work with line-wise or block-wise selections?
|
|
|
|
val primaryCaret = editor.caretModel.primaryCaret
|
|
|
|
val primaryCaret = editor.caretModel.primaryCaret
|
|
|
|
val range = getSurroundRange(primaryCaret.vim)
|
|
|
|
val range = getSurroundRange(primaryCaret.vim)
|
|
|
|
if (range != null) {
|
|
|
|
if (range != null) {
|
|
|
|
change.insertText(IjVimEditor(editor), IjVimCaret(primaryCaret), range.startOffset, pair.first)
|
|
|
|
val start = RepeatedCharSequence.of(pair.first, count)
|
|
|
|
change.insertText(
|
|
|
|
val end = RepeatedCharSequence.of(pair.second, count)
|
|
|
|
IjVimEditor(editor),
|
|
|
|
change.insertText(IjVimEditor(editor), IjVimCaret(primaryCaret), range.startOffset, start)
|
|
|
|
IjVimCaret(primaryCaret),
|
|
|
|
change.insertText(IjVimEditor(editor), IjVimCaret(primaryCaret), range.endOffset + start.length, end)
|
|
|
|
range.endOffset + pair.first.length,
|
|
|
|
|
|
|
|
pair.second
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -378,15 +375,15 @@ internal class VimSurroundExtension : VimExtension {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun performSurround(pair: Pair<String, String>, range: TextRange, caret: VimCaret, tagsOnNewLines: Boolean = false) {
|
|
|
|
private fun performSurround(pair: Pair<String, String>, range: TextRange, caret: VimCaret, count: Int, tagsOnNewLines: Boolean = false) {
|
|
|
|
runWriteAction {
|
|
|
|
runWriteAction {
|
|
|
|
val editor = caret.editor
|
|
|
|
val editor = caret.editor
|
|
|
|
val change = VimPlugin.getChange()
|
|
|
|
val change = VimPlugin.getChange()
|
|
|
|
val leftSurround = pair.first + if (tagsOnNewLines) "\n" else ""
|
|
|
|
val leftSurround = RepeatedCharSequence.of(pair.first + if (tagsOnNewLines) "\n" else "", count)
|
|
|
|
|
|
|
|
|
|
|
|
val isEOF = range.endOffset == editor.text().length
|
|
|
|
val isEOF = range.endOffset == editor.text().length
|
|
|
|
val hasNewLine = editor.endsWithNewLine()
|
|
|
|
val hasNewLine = editor.endsWithNewLine()
|
|
|
|
val rightSurround = if (tagsOnNewLines) {
|
|
|
|
val rightSurround = (if (tagsOnNewLines) {
|
|
|
|
if (isEOF && !hasNewLine) {
|
|
|
|
if (isEOF && !hasNewLine) {
|
|
|
|
"\n" + pair.second
|
|
|
|
"\n" + pair.second
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -394,7 +391,7 @@ internal class VimSurroundExtension : VimExtension {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
pair.second
|
|
|
|
pair.second
|
|
|
|
}
|
|
|
|
}).let { RepeatedCharSequence.of(it, count) }
|
|
|
|
|
|
|
|
|
|
|
|
change.insertText(editor, caret, range.startOffset, leftSurround)
|
|
|
|
change.insertText(editor, caret, range.startOffset, leftSurround)
|
|
|
|
change.insertText(editor, caret, range.endOffset + leftSurround.length, rightSurround)
|
|
|
|
change.insertText(editor, caret, range.endOffset + leftSurround.length, rightSurround)
|
|
|
|