Skip to content

Commit

Permalink
Merge pull request #824 from JetBrains/fleet
Browse files Browse the repository at this point in the history
Asynchronous key processing for Fleet
  • Loading branch information
lippfi authored Feb 23, 2024
2 parents db722fc + 3c94091 commit 00808af
Show file tree
Hide file tree
Showing 88 changed files with 1,944 additions and 1,079 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ publishChannels=eap

# Kotlinx serialization also uses some version of kotlin stdlib under the hood. However,
# we exclude this version from the dependency and use our own version of kotlin that is specified above
kotlinxSerializationVersion=1.5.1
kotlinxSerializationVersion=1.6.2

slackUrl=
youtrackToken=
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/maddyhome/idea/vim/VimPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,22 +211,22 @@ public static boolean isNotEnabled() {
public static void setEnabled(final boolean enabled) {
if (isEnabled() == enabled) return;

if (!enabled) {
getInstance().turnOffPlugin(true);
}

getInstance().enabled = enabled;

if (enabled) {
getInstance().turnOnPlugin();
}

if (enabled) {
VimInjectorKt.getInjector().getListenersNotifier().notifyPluginTurnedOn();
} else {
VimInjectorKt.getInjector().getListenersNotifier().notifyPluginTurnedOff();
}

if (!enabled) {
getInstance().turnOffPlugin(true);
}

if (enabled) {
getInstance().turnOnPlugin();
}

StatusBarIconFactory.Util.INSTANCE.updateIcon();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class VimTypedActionHandler(origHandler: TypedActionHandler) : TypedActio
val modifiers = if (charTyped == ' ' && VimKeyListener.isSpaceShift) KeyEvent.SHIFT_DOWN_MASK else 0
val keyStroke = KeyStroke.getKeyStroke(charTyped, modifiers)
val startTime = if (traceTime) System.currentTimeMillis() else null
handler.handleKey(editor.vim, keyStroke, injector.executionContextManager.onEditor(editor.vim, context.vim))
handler.handleKey(editor.vim, keyStroke, injector.executionContextManager.onEditor(editor.vim, context.vim), handler.keyHandlerState)
if (startTime != null) {
val duration = System.currentTimeMillis() - startTime
LOG.info("VimTypedAction '$charTyped': $duration ms")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ public class VimShortcutKeyAction : AnAction(), DumbAware, ActionRemoteBehaviorS
// Should we use HelperKt.getTopLevelEditor(editor) here, as we did in former EditorKeyHandler?
try {
val start = if (traceTime) System.currentTimeMillis() else null
KeyHandler.getInstance().handleKey(
val keyHandler = KeyHandler.getInstance()
keyHandler.handleKey(
editor.vim,
keyStroke,
injector.executionContextManager.onEditor(editor.vim, e.dataContext.vim),
keyHandler.keyHandlerState,
)
if (start != null) {
val duration = System.currentTimeMillis() - start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import com.maddyhome.idea.vim.state.mode.SelectionType
public class CommandState(private val machine: VimStateMachine) {

public val isOperatorPending: Boolean
get() = machine.isOperatorPending
get() = machine.isOperatorPending(machine.mode)

public val mode: Mode
get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public object VimExtensionFacade {
@JvmStatic
public fun executeNormalWithoutMapping(keys: List<KeyStroke>, editor: Editor) {
val context = injector.executionContextManager.onEditor(editor.vim)
keys.forEach { KeyHandler.getInstance().handleKey(editor.vim, it, context, false, false) }
val keyHandler = KeyHandler.getInstance()
keys.forEach { keyHandler.handleKey(editor.vim, it, context, false, false, keyHandler.keyHandlerState) }
}

/** Returns a single key stroke from the user input similar to 'getchar()'. */
Expand All @@ -159,7 +160,7 @@ public object VimExtensionFacade {
LOG.trace("Unit test mode is active")
val mappingStack = KeyHandler.getInstance().keyStack
mappingStack.feedSomeStroke() ?: TestInputModel.getInstance(editor).nextKeyStroke()?.also {
if (editor.vim.vimStateMachine.isRecording) {
if (injector.registerGroup.isRecording) {
KeyHandler.getInstance().modalEntryKeys += it
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public void execute(@NotNull VimEditor editor, @NotNull ExecutionContext context

final ArgumentTextObjectHandler textObjectHandler = new ArgumentTextObjectHandler(isInner);
//noinspection DuplicatedCode
if (!vimStateMachine.isOperatorPending()) {
if (!vimStateMachine.isOperatorPending(editor.getMode())) {
editor.nativeCarets().forEach((VimCaret caret) -> {
final TextRange range = textObjectHandler.getRange(editor, caret, context, count, 0);
if (range != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ internal class Matchit : VimExtension {

// Normally we want to jump to the start of the matching pair. But when moving forward in operator
// pending mode, we want to include the entire match. isInOpPending makes that distinction.
val isInOpPending = commandState.isOperatorPending
val isInOpPending = commandState.isOperatorPending(editor.mode)

if (isInOpPending) {
val matchitAction = MatchitAction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import com.maddyhome.idea.vim.extension.VimExtensionFacade.putKeyMappingIfMissin
import com.maddyhome.idea.vim.extension.exportOperatorFunction
import com.maddyhome.idea.vim.group.visual.VimSelection
import com.maddyhome.idea.vim.helper.exitVisualMode
import com.maddyhome.idea.vim.state.mode.mode
import com.maddyhome.idea.vim.helper.vimStateMachine
import com.maddyhome.idea.vim.key.OperatorFunction
import com.maddyhome.idea.vim.newapi.IjVimEditor
Expand Down Expand Up @@ -163,13 +162,14 @@ internal class ReplaceWithRegister : VimExtension {
caretAfterInsertedText = false,
putToLine = -1,
)
val vimEditor = editor.vim
ClipboardOptionHelper.IdeaputDisabler().use {
VimPlugin.getPut().putText(
IjVimEditor(editor),
vimEditor,
injector.executionContextManager.onEditor(editor.vim),
putData,
operatorArguments = OperatorArguments(
editor.vimStateMachine?.isOperatorPending ?: false,
editor.vimStateMachine?.isOperatorPending(vimEditor.mode) ?: false,
0,
editor.vim.mode,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void execute(@NotNull VimEditor editor, @NotNull ExecutionContext context

final EntireTextObjectHandler textObjectHandler = new EntireTextObjectHandler(ignoreLeadingAndTrailing);
//noinspection DuplicatedCode
if (!vimStateMachine.isOperatorPending()) {
if (!vimStateMachine.isOperatorPending(editor.getMode())) {
((IjVimEditor) editor).getEditor().getCaretModel().runForEachCaret((Caret caret) -> {
final TextRange range = textObjectHandler.getRange(editor, new IjVimCaret(caret), context, count, 0);
if (range != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public void execute(@NotNull VimEditor editor, @NotNull ExecutionContext context

final IndentObjectHandler textObjectHandler = new IndentObjectHandler(includeAbove, includeBelow);

if (!vimStateMachine.isOperatorPending()) {
if (!vimStateMachine.isOperatorPending(editor.getMode())) {
((IjVimEditor)editor).getEditor().getCaretModel().runForEachCaret((Caret caret) -> {
final TextRange range = textObjectHandler.getRange(vimEditor, new IjVimCaret(caret), context, count, 0);
if (range != null) {
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/maddyhome/idea/vim/group/EditorGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public void editorCreated(@NotNull Editor editor) {
switchToInsertMode.run();
}
});
updateCaretsVisualAttributes(editor);
updateCaretsVisualAttributes(new IjVimEditor(editor));
}

public void editorDeinit(@NotNull Editor editor, boolean isReleased) {
Expand Down Expand Up @@ -288,6 +288,18 @@ public void notifyIdeaJoin(@NotNull VimEditor editor) {
notifyIdeaJoin(((IjVimEditor) editor).getEditor().getProject(), editor);
}

@Override
public void updateCaretsVisualAttributes(@NotNull VimEditor editor) {
Editor ijEditor = ((IjVimEditor) editor).getEditor();
CaretVisualAttributesHelperKt.updateCaretsVisualAttributes(ijEditor);
}

@Override
public void updateCaretsVisualPosition(@NotNull VimEditor editor) {
Editor ijEditor = ((IjVimEditor) editor).getEditor();
CaretVisualAttributesHelperKt.updateCaretsVisualAttributes(ijEditor);
}

public static class NumberChangeListener implements EffectiveOptionValueChangeListener {
public static NumberChangeListener INSTANCE = new NumberChangeListener();

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/maddyhome/idea/vim/group/MacroGroup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.progress.ProcessCanceledException
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.util.PotemkinProgress
import com.maddyhome.idea.vim.KeyHandler
import com.maddyhome.idea.vim.KeyHandler.Companion.getInstance
import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.VimEditor
Expand Down Expand Up @@ -77,11 +78,12 @@ internal class MacroGroup : VimMacroBase() {
} catch (e: ProcessCanceledException) {
return@runnable
}
val keyHandler = KeyHandler.getInstance()
ProgressManager.getInstance().executeNonCancelableSection {
// Prevent autocompletion during macros.
// See https://github.com/JetBrains/ideavim/pull/772 for details
CompletionServiceImpl.setCompletionPhase(CompletionPhase.NoCompletion)
getInstance().handleKey(editor, key, context)
keyHandler.handleKey(editor, key, context, keyHandler.keyHandlerState)
}
if (injector.messages.isError()) return@runnable
}
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/com/maddyhome/idea/vim/group/ProcessGroup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.intellij.openapi.progress.ProgressManager
import com.intellij.util.execution.ParametersListUtil
import com.intellij.util.text.CharSequenceReader
import com.maddyhome.idea.vim.KeyHandler.Companion.getInstance
import com.maddyhome.idea.vim.KeyProcessResult
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.VimEditor
Expand All @@ -37,7 +38,6 @@ import com.maddyhome.idea.vim.state.mode.Mode
import com.maddyhome.idea.vim.state.mode.Mode.NORMAL
import com.maddyhome.idea.vim.state.mode.Mode.VISUAL
import com.maddyhome.idea.vim.state.mode.ReturnableFromCmd
import com.maddyhome.idea.vim.state.mode.mode
import com.maddyhome.idea.vim.ui.ex.ExEntryPanel
import com.maddyhome.idea.vim.vimscript.model.CommandLineVimLContext
import java.io.BufferedWriter
Expand Down Expand Up @@ -85,24 +85,27 @@ public class ProcessGroup : VimProcessGroupBase() {
modeBeforeCommandProcessing = currentMode
val initText = getRange(editor, cmd)
injector.markService.setVisualSelectionMarks(editor)
editor.vimStateMachine.mode = Mode.CMD_LINE(currentMode)
editor.mode = Mode.CMD_LINE(currentMode)
val panel = ExEntryPanel.getInstance()
panel.activate(editor.ij, context.ij, ":", initText, 1)
}

public override fun processExKey(editor: VimEditor, stroke: KeyStroke): Boolean {
public override fun processExKey(editor: VimEditor, stroke: KeyStroke, processResultBuilder: KeyProcessResult.KeyProcessResultBuilder): Boolean {
// This will only get called if somehow the key focus ended up in the editor while the ex entry window
// is open. So I'll put focus back in the editor and process the key.

val panel = ExEntryPanel.getInstance()
if (panel.isActive) {
requestFocus(panel.entry)
panel.handleKey(stroke)

processResultBuilder.addExecutionStep { _, _, _ ->
requestFocus(panel.entry)
panel.handleKey(stroke)
}
return true
} else {
getInstance(editor).mode = NORMAL()
getInstance().reset(editor)
processResultBuilder.addExecutionStep { _, lambdaEditor, _ ->
lambdaEditor.mode = NORMAL()
getInstance().reset(lambdaEditor)
}
return false
}
}
Expand All @@ -112,7 +115,7 @@ public class ProcessGroup : VimProcessGroupBase() {
panel.deactivate(true)
var res = true
try {
getInstance(editor).mode = NORMAL()
editor.mode = NORMAL()

logger.debug("processing command")

Expand Down Expand Up @@ -152,7 +155,7 @@ public class ProcessGroup : VimProcessGroupBase() {
}

public override fun cancelExEntry(editor: VimEditor, resetCaret: Boolean) {
editor.vimStateMachine.mode = NORMAL()
editor.mode = NORMAL()
getInstance().reset(editor)
val panel = ExEntryPanel.getInstance()
panel.deactivate(true, resetCaret)
Expand All @@ -162,7 +165,7 @@ public class ProcessGroup : VimProcessGroupBase() {
val initText = getRange(editor, cmd) + "!"
val currentMode = editor.mode
check(currentMode is ReturnableFromCmd) { "Cannot enable cmd mode from $currentMode" }
editor.vimStateMachine.mode = Mode.CMD_LINE(currentMode)
editor.mode = Mode.CMD_LINE(currentMode)
val panel = ExEntryPanel.getInstance()
panel.activate(editor.ij, context.ij, ":", initText, 1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal object IdeaSelectionControl {

logger.debug("Some carets have selection. State before adjustment: ${editor.vim.mode}")

editor.vim.vimStateMachine.mode = Mode.NORMAL()
editor.vim.mode = Mode.NORMAL()

activateMode(editor, chooseSelectionMode(editor, selectionSource, true))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ internal abstract class VimKeyHandler(nextHandler: EditorActionHandler?) : Octop
override fun executeHandler(editor: Editor, caret: Caret?, dataContext: DataContext?) {
val enterKey = key(key)
val context = injector.executionContextManager.onEditor(editor.vim, dataContext?.vim)
KeyHandler.getInstance().handleKey(editor.vim, enterKey, context)
val keyHandler = KeyHandler.getInstance()
keyHandler.handleKey(editor.vim, enterKey, context, keyHandler.keyHandlerState)
}

override fun isHandlerEnabled(editor: Editor, dataContext: DataContext?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.globalOptions
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.common.IsReplaceCharListener
import com.maddyhome.idea.vim.common.ModeChangeListener
import com.maddyhome.idea.vim.newapi.IjVimEditor
import com.maddyhome.idea.vim.newapi.ij
import com.maddyhome.idea.vim.newapi.vim
import com.maddyhome.idea.vim.options.EffectiveOptionValueChangeListener
import com.maddyhome.idea.vim.options.helpers.GuiCursorMode
import com.maddyhome.idea.vim.options.helpers.GuiCursorOptionHelper
import com.maddyhome.idea.vim.options.helpers.GuiCursorType
import com.maddyhome.idea.vim.state.mode.Mode
import com.maddyhome.idea.vim.state.mode.inBlockSelection
import com.maddyhome.idea.vim.state.mode.mode
import org.jetbrains.annotations.TestOnly
import java.awt.Color

Expand Down Expand Up @@ -138,3 +141,31 @@ private object AttributesCache {

@TestOnly
internal fun getGuiCursorMode(editor: Editor) = editor.guicursorMode()

public class CaretVisualAttributesListener : IsReplaceCharListener, ModeChangeListener {
override fun isReplaceCharChanged(editor: VimEditor) {
updateCaretsVisual(editor)
}

override fun modeChanged(editor: VimEditor, oldMode: Mode) {
updateCaretsVisual(editor)
}

private fun updateCaretsVisual(editor: VimEditor) {
if (injector.globalOptions().ideaglobalmode) {
updateAllEditorsCaretsVisual()
} else {
val ijEditor = (editor as IjVimEditor).editor
ijEditor.updateCaretsVisualAttributes()
ijEditor.updateCaretsVisualPosition()
}
}

public fun updateAllEditorsCaretsVisual() {
injector.editorGroup.getEditors().forEach { editor ->
val ijEditor = (editor as IjVimEditor).editor
ijEditor.updateCaretsVisualAttributes()
ijEditor.updateCaretsVisualPosition()
}
}
}
12 changes: 6 additions & 6 deletions src/main/java/com/maddyhome/idea/vim/helper/ModeExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ internal fun Editor.exitSelectMode(adjustCaretPosition: Boolean) {
val returnTo = this.vim.vimStateMachine.mode.returnTo
when (returnTo) {
ReturnTo.INSERT -> {
this.vim.vimStateMachine.mode = Mode.INSERT
this.vim.mode = Mode.INSERT
}

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

null -> {
this.vim.vimStateMachine.mode = Mode.NORMAL()
this.vim.mode = Mode.NORMAL()
}
}
SelectionVimListenerSuppressor.lock().use {
Expand All @@ -67,15 +67,15 @@ internal fun VimEditor.exitSelectMode(adjustCaretPosition: Boolean) {
val returnTo = this.vimStateMachine.mode.returnTo
when (returnTo) {
ReturnTo.INSERT -> {
this.vimStateMachine.mode = Mode.INSERT
this.mode = Mode.INSERT
}

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

null -> {
this.vimStateMachine.mode = Mode.NORMAL()
this.mode = Mode.NORMAL()
}
}
SelectionVimListenerSuppressor.lock().use {
Expand Down
Loading

0 comments on commit 00808af

Please sign in to comment.