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

Refactor Mode API and implement v_CTRL-O #1078

Merged
merged 16 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
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 @@ -47,7 +47,6 @@ import com.maddyhome.idea.vim.state.mode.selectionType
import org.jetbrains.annotations.NonNls
import java.awt.event.KeyEvent
import javax.swing.KeyStroke
import com.maddyhome.idea.vim.state.mode.returnTo

/**
* Port of vim-surround.
Expand Down Expand Up @@ -289,7 +288,7 @@ internal class VimSurroundExtension : VimExtension {
private fun getSurroundRange(caret: VimCaret): TextRange? {
val editor = caret.editor
if (editor.mode is Mode.CMD_LINE) {
editor.mode = (editor.mode as Mode.CMD_LINE).returnTo()
editor.mode = editor.mode.returnTo
}
return when (editor.mode) {
is Mode.NORMAL -> injector.markService.getChangeMarks(caret)
Expand Down Expand Up @@ -337,7 +336,7 @@ private fun getSurroundPair(c: Char): SurroundPair? = if (c in SURROUND_PAIRS) {
private fun inputTagPair(editor: Editor, context: DataContext): SurroundPair? {
val tagInput = inputString(editor, context, "<", '>')
if (editor.vim.mode is Mode.CMD_LINE) {
editor.vim.mode = editor.vim.mode.returnTo()
editor.vim.mode = editor.vim.mode.returnTo
}
val matcher = tagNameAndAttributesCapturePattern.matcher(tagInput)
return if (matcher.find()) {
Expand All @@ -356,7 +355,7 @@ private fun inputFunctionName(
): SurroundPair? {
val functionNameInput = inputString(editor, context, "function: ", null)
if (editor.vim.mode is Mode.CMD_LINE) {
editor.vim.mode = editor.vim.mode.returnTo()
editor.vim.mode = editor.vim.mode.returnTo
}
if (functionNameInput.isEmpty()) return null
return if (withInternalSpaces) {
Expand Down
9 changes: 1 addition & 8 deletions src/main/java/com/maddyhome/idea/vim/group/MotionGroup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ import com.maddyhome.idea.vim.newapi.IjEditorExecutionContext
import com.maddyhome.idea.vim.newapi.ij
import com.maddyhome.idea.vim.newapi.vim
import com.maddyhome.idea.vim.state.mode.Mode
import com.maddyhome.idea.vim.state.mode.ReturnTo
import com.maddyhome.idea.vim.state.mode.returnTo
import org.jetbrains.annotations.Range
import kotlin.math.max
import kotlin.math.min
Expand Down Expand Up @@ -332,12 +330,7 @@ internal class MotionGroup : VimMotionGroupBase() {
} else {
val state = injector.vimState as VimStateMachineImpl
if (state.mode is Mode.VISUAL) {
val returnTo = state.mode.returnTo
when (returnTo) {
ReturnTo.INSERT -> state.mode = Mode.INSERT
ReturnTo.REPLACE -> state.mode = Mode.REPLACE
null -> state.mode = Mode.NORMAL()
}
state.mode = state.mode.returnTo
}
val keyHandler = KeyHandler.getInstance()
KeyHandler.getInstance().reset(keyHandler.keyHandlerState, state.mode)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/maddyhome/idea/vim/group/copy/PutGroup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal class PutGroup : VimPutBase() {
vimEditor: VimEditor,
vimContext: ExecutionContext,
text: ProcessedTextData,
subMode: SelectionType,
selectionType: SelectionType,
data: PutData,
additionalData: Map<String, Any>,
) {
Expand Down Expand Up @@ -148,7 +148,7 @@ internal class PutGroup : VimPutBase() {
startOffset,
endOffset,
text.typeInRegister,
subMode,
selectionType,
data.caretAfterInsertedText,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import com.maddyhome.idea.vim.state.mode.inCommandLineMode
import com.maddyhome.idea.vim.state.mode.inNormalMode
import com.maddyhome.idea.vim.state.mode.inSelectMode
import com.maddyhome.idea.vim.state.mode.inVisualMode
import com.maddyhome.idea.vim.state.mode.returnTo
import com.maddyhome.idea.vim.vimscript.model.options.helpers.IdeaRefactorModeHelper
import com.maddyhome.idea.vim.vimscript.model.options.helpers.isIdeaRefactorModeKeep
import com.maddyhome.idea.vim.vimscript.model.options.helpers.isIdeaRefactorModeSelect
Expand Down Expand Up @@ -75,7 +74,7 @@ internal object IdeaSelectionControl {
}

if (hasSelection) {
if (editor.vim.inCommandLineMode && editor.vim.mode.returnTo().hasVisualSelection) {
if (editor.vim.inCommandLineMode && editor.vim.mode.returnTo.hasVisualSelection) {
logger.trace { "Modifying selection while in Command-line mode, most likely incsearch" }
return@singleTask
}
Expand Down Expand Up @@ -158,23 +157,23 @@ internal object IdeaSelectionControl {
return when {
editor.isOneLineMode -> {
if (logReason) logger.debug("Enter select mode. Reason: one line mode")
Mode.SELECT(VimPlugin.getVisualMotion().autodetectVisualSubmode(editor.vim))
Mode.SELECT(VimPlugin.getVisualMotion().detectSelectionType(editor.vim))
}
selectionSource == VimListenerManager.SelectionSource.MOUSE && OptionConstants.selectmode_mouse in selectmode -> {
if (logReason) logger.debug("Enter select mode. Selection source is mouse and selectMode option has mouse")
Mode.SELECT(VimPlugin.getVisualMotion().autodetectVisualSubmode(editor.vim))
Mode.SELECT(VimPlugin.getVisualMotion().detectSelectionType(editor.vim))
}
editor.isTemplateActive() && editor.vim.isIdeaRefactorModeSelect -> {
if (logReason) logger.debug("Enter select mode. Template is active and selectMode has template")
Mode.SELECT(VimPlugin.getVisualMotion().autodetectVisualSubmode(editor.vim))
Mode.SELECT(VimPlugin.getVisualMotion().detectSelectionType(editor.vim))
}
selectionSource == VimListenerManager.SelectionSource.OTHER && OptionConstants.selectmode_ideaselection in selectmode -> {
if (logReason) logger.debug("Enter select mode. Selection source is OTHER and selectMode has refactoring")
Mode.SELECT(VimPlugin.getVisualMotion().autodetectVisualSubmode(editor.vim))
Mode.SELECT(VimPlugin.getVisualMotion().detectSelectionType(editor.vim))
}
else -> {
if (logReason) logger.debug("Enter visual mode")
Mode.VISUAL(VimPlugin.getVisualMotion().autodetectVisualSubmode(editor.vim))
Mode.VISUAL(VimPlugin.getVisualMotion().detectSelectionType(editor.vim))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import com.maddyhome.idea.vim.state.mode.SelectionType
* @author Alex Plate
*/
internal class VisualMotionGroup : VimVisualMotionGroupBase() {
override fun autodetectVisualSubmode(editor: VimEditor): SelectionType {
override fun detectSelectionType(editor: VimEditor): SelectionType {
// IJ specific. See https://youtrack.jetbrains.com/issue/VIM-1924.
val project = editor.ij.project
if (project != null && FindManager.getInstance(project).selectNextOccurrenceWasPerformed()) {
return SelectionType.CHARACTER_WISE
}

return super.autodetectVisualSubmode(editor)
return super.detectSelectionType(editor)
}
}
40 changes: 6 additions & 34 deletions src/main/java/com/maddyhome/idea/vim/helper/ModeExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,14 @@ import com.maddyhome.idea.vim.newapi.IjEditorExecutionContext
import com.maddyhome.idea.vim.newapi.IjVimCaret
import com.maddyhome.idea.vim.newapi.IjVimEditor
import com.maddyhome.idea.vim.newapi.vim
import com.maddyhome.idea.vim.state.mode.Mode
import com.maddyhome.idea.vim.state.mode.ReturnTo
import com.maddyhome.idea.vim.state.mode.inSelectMode
import com.maddyhome.idea.vim.state.mode.returnTo

/** [adjustCaretPosition] - if true, caret will be moved one char left if it's on the line end */
internal fun Editor.exitSelectMode(adjustCaretPosition: Boolean) {
if (!this.vim.inSelectMode) return
val vimEditor = this.vim
if (!vimEditor.inSelectMode) return

val returnTo = this.vim.mode.returnTo
when (returnTo) {
ReturnTo.INSERT -> {
this.vim.mode = Mode.INSERT
}

ReturnTo.REPLACE -> {
this.vim.mode = Mode.REPLACE
}

null -> {
this.vim.mode = Mode.NORMAL()
}
}
vimEditor.mode = vimEditor.mode.returnTo
SelectionVimListenerSuppressor.lock().use {
this.caretModel.allCarets.forEach {
it.removeSelection()
Expand All @@ -63,28 +48,15 @@ internal fun Editor.exitSelectMode(adjustCaretPosition: Boolean) {
internal fun VimEditor.exitSelectMode(adjustCaretPosition: Boolean) {
if (!this.inSelectMode) return

val returnTo = this.mode.returnTo
when (returnTo) {
ReturnTo.INSERT -> {
this.mode = Mode.INSERT
}

ReturnTo.REPLACE -> {
this.mode = Mode.REPLACE
}

null -> {
this.mode = Mode.NORMAL()
}
}
mode = mode.returnTo
SelectionVimListenerSuppressor.lock().use {
this.carets().forEach { vimCaret ->
carets().forEach { vimCaret ->
val caret = (vimCaret as IjVimCaret).caret
caret.removeSelection()
caret.vim.vimSelectionStartClear()
if (adjustCaretPosition) {
val lineEnd = IjVimEditor((this as IjVimEditor).editor).getLineEndForOffset(caret.offset)
val lineStart = IjVimEditor(this.editor).getLineStartForOffset(caret.offset)
val lineStart = IjVimEditor(editor).getLineStartForOffset(caret.offset)
if (caret.offset == lineEnd && caret.offset != lineStart) {
caret.moveToInlayAwareOffset(caret.offset - 1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.Key
import com.maddyhome.idea.vim.KeyHandler
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.action.motion.select.SelectToggleVisualMode
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.group.visual.VimVisualTimer
import com.maddyhome.idea.vim.helper.fileSize
import com.maddyhome.idea.vim.helper.inVisualMode
Expand Down Expand Up @@ -56,7 +56,7 @@ internal object AppCodeTemplates {
if (myEditor != null) {
VimVisualTimer.doNow()
if (myEditor.inVisualMode) {
SelectToggleVisualMode.toggleMode(myEditor.vim)
injector.visualMotionGroup.toggleSelectVisual(myEditor.vim)
KeyHandler.getInstance().partialReset(myEditor.vim)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import com.maddyhome.idea.vim.key.interceptors.VimInputInterceptor
import com.maddyhome.idea.vim.newapi.ij
import com.maddyhome.idea.vim.newapi.vim
import com.maddyhome.idea.vim.state.mode.Mode
import com.maddyhome.idea.vim.state.mode.ReturnableFromCmd
import com.maddyhome.idea.vim.ui.ModalEntry
import java.awt.event.KeyEvent
import javax.swing.KeyStroke
Expand Down Expand Up @@ -91,7 +90,7 @@ class ExEntryPanelService : VimCommandLineServiceBase(), VimModalInputService {
}
}
if (text != null) {
Extension.addString(text!!)
Extension.addString(text)
}
return text
}
Expand All @@ -105,9 +104,6 @@ class ExEntryPanelService : VimCommandLineServiceBase(), VimModalInputService {
processing: (String) -> Unit,
) {
val currentMode = editor.mode
check(currentMode is ReturnableFromCmd) {
"Cannot enable cmd mode from current mode $currentMode"
}

// Make sure the Visual selection marks are up to date before we use them.
injector.markService.setVisualSelectionMarks(editor)
Expand Down Expand Up @@ -136,6 +132,7 @@ class ExEntryPanelService : VimCommandLineServiceBase(), VimModalInputService {
return panel
}

@Deprecated("Please use ModalInputService.create()")
override fun createWithoutShortcuts(editor: VimEditor, context: ExecutionContext, label: String, initText: String): VimCommandLine {
val panel = ExEntryPanel.getInstanceWithoutShortcuts()
panel.activate(editor.ij, context.ij, label, initText)
Expand Down Expand Up @@ -172,4 +169,4 @@ internal class WrappedAsModalInputExEntryPanel(internal val exEntryPanel: ExEntr
override fun focus() {
exEntryPanel.focus()
}
}
}
Loading
Loading