Skip to content

Commit

Permalink
VIM-3376: Remove usages of the EditorDataContext
Browse files Browse the repository at this point in the history
EditorDataContext cannot be used because the platform cannot convert it to the async data context. Taking the fact that it's not clear why this custom context exists, I decided to get rid of it at all.

```
Cannot convert to AsyncDataContext at 'keyboard shortcut' DataContextWrapper(CaretSpecificDataContext(com.maddyhome.idea.vim.helper.EditorDataContext)). Please use CustomizedDataContext or its inheritors like SimpleDataContext
```

Class EditorDataContext cannot be removed because it's used in github.zgqq.intellij-enhance plugin
  • Loading branch information
AlexPl292 committed Apr 26, 2024
1 parent 54d6119 commit b66da76
Show file tree
Hide file tree
Showing 18 changed files with 23 additions and 55 deletions.
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.keyHandlerState)
handler.handleKey(editor.vim, keyStroke, 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 @@ -79,12 +79,7 @@ public class VimShortcutKeyAction : AnAction(), DumbAware/*, LightEditCompatible
try {
val start = if (traceTime) System.currentTimeMillis() else null
val keyHandler = KeyHandler.getInstance()
keyHandler.handleKey(
editor.vim,
keyStroke,
injector.executionContextManager.onEditor(editor.vim, e.dataContext.vim),
keyHandler.keyHandlerState,
)
keyHandler.handleKey(editor.vim, keyStroke, e.dataContext.vim, keyHandler.keyHandlerState)
if (start != null) {
val duration = System.currentTimeMillis() - start
LOG.info("VimShortcut update '$keyStroke': $duration ms")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public object VimExtensionFacade {
*/
@JvmStatic
public fun executeNormalWithoutMapping(keys: List<KeyStroke>, editor: Editor) {
val context = injector.executionContextManager.onEditor(editor.vim)
val context = injector.executionContextManager.getEditorExecutionContext(editor.vim)
val keyHandler = KeyHandler.getInstance()
keys.forEach { keyHandler.handleKey(editor.vim, it, context, false, false, keyHandler.keyHandlerState) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public void editorCreated(@NotNull Editor editor) {
// Note that we need a similar check in `VimEditor.isWritable` to allow Escape to work to exit insert mode. We need
// to know that a read-only editor that is hosting a console view with a running process can be treated as writable.
Runnable switchToInsertMode = () -> {
ExecutionContext.Editor context = injector.getExecutionContextManager().onEditor(new IjVimEditor(editor), null);
ExecutionContext context = injector.getExecutionContextManager().getEditorExecutionContext(new IjVimEditor(editor));
VimPlugin.getChange().insertBeforeCursor(new IjVimEditor(editor), context);
KeyHandler.getInstance().reset(new IjVimEditor(editor));
};
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/maddyhome/idea/vim/group/SearchGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ else if (match == null) {
RangeHighlighter hl =
SearchHighlightsHelper.addSubstitutionConfirmationHighlight(((IjVimEditor)editor).getEditor(), startoff,
endoff);
final ReplaceConfirmationChoice choice = confirmChoice(((IjVimEditor)editor).getEditor(), match, ((IjVimCaret)caret).getCaret(), startoff);
final ReplaceConfirmationChoice choice = confirmChoice(((IjVimEditor)editor).getEditor(), context, match, ((IjVimCaret)caret).getCaret(), startoff);
((IjVimEditor)editor).getEditor().getMarkupModel().removeHighlighter(hl);
switch (choice) {
case SUBSTITUTE_THIS:
Expand Down Expand Up @@ -841,8 +841,7 @@ else if (match == null) {
caret.moveToOffset(startoff);
if (expression != null) {
try {
match =
expression.evaluate(editor, injector.getExecutionContextManager().onEditor(editor, null), parent).toInsertableString();
match = expression.evaluate(editor, context, parent).toInsertableString();
}
catch (Exception e) {
exceptions.add((ExException)e);
Expand Down Expand Up @@ -993,7 +992,9 @@ public Pair<Boolean, Triple<Object, String, Object>> search_regcomp(CharPointer
return new Pair<>(true, new Triple<>(regmatch, pattern, sp));
}

private static @NotNull ReplaceConfirmationChoice confirmChoice(@NotNull Editor editor, @NotNull String match, @NotNull Caret caret, int startoff) {
private static @NotNull ReplaceConfirmationChoice confirmChoice(@NotNull Editor editor,
@NotNull ExecutionContext context,
@NotNull String match, @NotNull Caret caret, int startoff) {
final Ref<ReplaceConfirmationChoice> result = Ref.create(ReplaceConfirmationChoice.QUIT);
final Function1<KeyStroke, Boolean> keyStrokeProcessor = key -> {
final ReplaceConfirmationChoice choice;
Expand Down Expand Up @@ -1027,7 +1028,6 @@ public Pair<Boolean, Triple<Object, String, Object>> search_regcomp(CharPointer
else {
// XXX: The Ex entry panel is used only for UI here, its logic might be inappropriate for this method
final ExEntryPanel exEntryPanel = ExEntryPanel.getInstanceWithoutShortcuts();
ExecutionContext.Editor context = injector.getExecutionContextManager().onEditor(new IjVimEditor(editor), null);
exEntryPanel.activate(editor, ((IjEditorExecutionContext)context).getContext(), MessageHelper.message("replace.with.0", match), "", 1);
new IjVimCaret(caret).moveToOffset(startoff);
ModalEntry.INSTANCE.activate(new IjVimEditor(editor), keyStrokeProcessor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ 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)
val context = dataContext?.vim ?: injector.executionContextManager.getEditorExecutionContext(editor.vim)
val keyHandler = KeyHandler.getInstance()
keyHandler.handleKey(editor.vim, enterKey, context, keyHandler.keyHandlerState)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.intellij.openapi.editor.ex.util.EditorUtil
import com.intellij.openapi.util.Key
import com.intellij.openapi.util.UserDataHolder

@Deprecated("Do not use context wrappers, use existing provided contexts. If no context available, use `injector.getExecutionContextManager().getEditorExecutionContext(editor)`")
internal class EditorDataContext @Deprecated("Please use `init` method") constructor(
private val editor: Editor,
private val editorContext: DataContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@ import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.util.Key
import com.intellij.openapi.util.UserDataHolder
import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.injector

internal open class IjEditorExecutionContext(override val context: DataContext) : ExecutionContext.Editor {
override fun updateEditor(editor: VimEditor): ExecutionContext {
return IjEditorExecutionContext(injector.executionContextManager.onEditor(editor, context.vim).ij)
}
}
internal open class IjEditorExecutionContext(override val context: DataContext) : ExecutionContext

// This key is stored in data context when the action is started from vim
internal val runFromVimKey = Key.create<Boolean>("RunFromVim")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ import com.maddyhome.idea.vim.helper.EditorDataContext

@Service
internal class IjExecutionContextManager : ExecutionContextManagerBase() {
override fun onEditor(editor: VimEditor, prevContext: ExecutionContext?): ExecutionContext.Editor {
if (prevContext is ExecutionContext.Editor) {
return prevContext
}
return IjEditorExecutionContext(EditorDataContext.init((editor as IjVimEditor).editor, prevContext?.ij))
}

override fun getEditorExecutionContext(editor: VimEditor): ExecutionContext {
return EditorUtil.getEditorDataContext(editor.ij).vim
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/maddyhome/idea/vim/ui/ExOutputPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ private void close(final @Nullable KeyEvent e) {
KeyHandler.getInstance().getKeyStack().dump());
}
KeyHandler.getInstance().getKeyStack().addKeys(keys);
ExecutionContext.Editor context = injector.getExecutionContextManager().onEditor(new IjVimEditor(myEditor), null);
ExecutionContext context = injector.getExecutionContextManager().getEditorExecutionContext(new IjVimEditor(myEditor));
VimPlugin.getMacro().playbackKeys(new IjVimEditor(myEditor), context, 1);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ class CaretVisualAttributesHelperTest : VimTestCase() {
)
injector.actionExecutor.executeAction(
"EditorCloneCaretBelow",
injector.executionContextManager.onEditor(fixture.editor.vim),
injector.executionContextManager.getEditorExecutionContext(fixture.editor.vim),
)
kotlin.test.assertEquals(2, fixture.editor.caretModel.caretCount)
assertCaretVisualAttributes(CaretVisualAttributes.Shape.BLOCK, 0f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ abstract class VimTestCase {

fun typeText(keys: List<KeyStroke?>, editor: Editor, project: Project?) {
val keyHandler = KeyHandler.getInstance()
val dataContext = injector.executionContextManager.onEditor(editor.vim)
val dataContext = injector.executionContextManager.getEditorExecutionContext(editor.vim)
TestInputModel.getInstance(editor).setKeyStrokes(keys.filterNotNull())
runWriteCommand(
project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,10 @@ package com.maddyhome.idea.vim.api
* This would be ideal if we could provide a typed solution, but sofar this is just a holder
*/

public sealed interface ExecutionContext {
public interface ExecutionContext {
public val context: Any

// TODO: 10.02.2022 Not sure about this method
public fun updateEditor(editor: VimEditor): ExecutionContext

public interface Editor : ExecutionContext
}

public interface ExecutionContextManager {
public fun onEditor(editor: VimEditor, prevContext: ExecutionContext? = null): ExecutionContext.Editor
public fun getEditorExecutionContext(editor: VimEditor): ExecutionContext
}
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,7 @@ public abstract class VimSearchGroupBase : VimSearchGroup {
caret.moveToOffset(matchRange.startOffset)
if (expression != null) {
match = try {
expression.evaluate(editor, injector.executionContextManager.onEditor(editor, null), parent)
.toInsertableString()
expression.evaluate(editor, context, parent).toInsertableString()
} catch (e: Exception) {
exceptions.add(e as ExException)
""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ public class ExecutionContextManagerStub : ExecutionContextManager {
vimLogger<ExecutionContextManagerStub>().warn("ExecutionContextManagerStub is used. Please replace it with your own implementation of ExecutionContextManager.")
}

override fun onEditor(editor: VimEditor, prevContext: ExecutionContext?): ExecutionContext.Editor {
TODO("Not yet implemented")
}

override fun getEditorExecutionContext(editor: VimEditor): ExecutionContext {
TODO("Not yet implemented")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public object MappingProcessor: KeyConsumer {
keyHandler.handleKey(
editor,
keyStroke,
injector.executionContextManager.onEditor(editor),
injector.executionContextManager.getEditorExecutionContext(editor),
allowKeyMappings = true,
mappingCompleted = lastKeyInSequence,
keyState,
Expand Down Expand Up @@ -202,7 +202,6 @@ public object MappingProcessor: KeyConsumer {
) {
val mappingState = keyState.mappingState
mappingState.resetMappingSequence()
val currentContext = context.updateEditor(editor)
log.trace("Executing mapping info")
try {
mappingState.startMapExecution()
Expand Down Expand Up @@ -234,7 +233,7 @@ public object MappingProcessor: KeyConsumer {
// If we've just evaluated the previous key sequence, make sure to also handle the current key
if (mappingInfo !== currentMappingInfo) {
log.trace("Evaluating the current key")
KeyHandler.getInstance().handleKey(editor, key, currentContext, allowKeyMappings = true, false, keyState)
KeyHandler.getInstance().handleKey(editor, key, context, allowKeyMappings = true, false, keyState)
}
log.trace("Success processing of mapping")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public class ToKeysMappingInfo(

override fun execute(editor: VimEditor, context: ExecutionContext, keyState: KeyHandlerState) {
LOG.debug("Executing 'ToKeys' mapping info...")
val editorDataContext = injector.executionContextManager.onEditor(editor, context)
val fromIsPrefix = KeyHandler.isPrefix(fromKeys, toKeys)
val keyHandler = KeyHandler.getInstance()
LOG.trace { "Adding new keys to keyStack as toKeys of mapping. State before adding keys: ${keyHandler.keyStack.dump()}" }
Expand All @@ -95,7 +94,7 @@ public class ToKeysMappingInfo(
while (keyHandler.keyStack.hasStroke()) {
val keyStroke = keyHandler.keyStack.feedStroke()
val recursive = isRecursive && !(first && fromIsPrefix)
keyHandler.handleKey(editor, keyStroke, editorDataContext, recursive, false, keyState)
keyHandler.handleKey(editor, keyStroke, context, recursive, false, keyState)
first = false
}
} finally {
Expand Down Expand Up @@ -123,14 +122,13 @@ public class ToExpressionMappingInfo(

override fun execute(editor: VimEditor, context: ExecutionContext, keyState: KeyHandlerState) {
LOG.debug("Executing 'ToExpression' mapping info...")
val editorDataContext = injector.executionContextManager.onEditor(editor, context)
val toKeys = injector.parser.parseKeys(toExpression.evaluate(editor, context, CommandLineVimLContext).toString())
val fromIsPrefix = KeyHandler.isPrefix(fromKeys, toKeys)
var first = true
for (keyStroke in toKeys) {
val recursive = isRecursive && !(first && fromIsPrefix)
val keyHandler = KeyHandler.getInstance()
keyHandler.handleKey(editor, keyStroke, editorDataContext, recursive, false, keyState)
keyHandler.handleKey(editor, keyStroke, context, recursive, false, keyState)
first = false
}
}
Expand Down Expand Up @@ -258,8 +256,7 @@ public class ToActionMappingInfo(

override fun execute(editor: VimEditor, context: ExecutionContext, keyState: KeyHandlerState) {
LOG.debug("Executing 'ToAction' mapping...")
val editorDataContext = injector.executionContextManager.onEditor(editor, context)
injector.actionExecutor.executeAction(action, editorDataContext)
injector.actionExecutor.executeAction(action, context)
}

public companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public data class EditFileCommand(val ranges: Ranges, val argument: String) : Co

// Don't open a choose file dialog under a write action
injector.application.invokeLater {
injector.actionExecutor.executeAction("OpenFile", injector.executionContextManager.onEditor(editor, context))
injector.actionExecutor.executeAction("OpenFile", context)
}

return ExecutionResult.Success
Expand Down

0 comments on commit b66da76

Please sign in to comment.