Skip to content

Commit

Permalink
Add logs around pressing of esc and enter keys of the user
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPl292 committed Nov 10, 2023
1 parent 9d20061 commit efd9ed0
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/main/java/com/maddyhome/idea/vim/action/VimShortcutKeyAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,25 @@ internal class VimShortcutKeyAction : AnAction(), DumbAware/*, LightEditCompatib

override fun update(e: AnActionEvent) {
val start = if (traceTime) System.currentTimeMillis() else null
val actionEnableStatus = isEnabled(e)
val keyStroke = getKeyStroke(e)
val actionEnableStatus = isEnabled(e, keyStroke)
e.presentation.isEnabled = actionEnableStatus.isEnabled
actionEnableStatus.printLog()
actionEnableStatus.printLog(keyStroke)
if (start != null) {
val keyStroke = getKeyStroke(e)
val duration = System.currentTimeMillis() - start
LOG.info("VimShortcut update '$keyStroke': $duration ms")
}
}

private fun isEnabled(e: AnActionEvent): ActionEnableStatus {
private fun isEnabled(e: AnActionEvent, keyStroke: KeyStroke?): ActionEnableStatus {
if (!VimPlugin.isEnabled()) return ActionEnableStatus.no("IdeaVim is disabled", LogLevel.DEBUG)
val editor = getEditor(e)
val keyStroke = getKeyStroke(e)
if (editor != null && keyStroke != null) {
if (isOctopusEnabled(keyStroke, editor)) {
return ActionEnableStatus.no("Octopus handler is enabled", LogLevel.DEBUG)
return ActionEnableStatus.no(
"Processing VimShortcutKeyAction for the key that is used in the octopus handler",
LogLevel.ERROR
)
}
if (editor.isIdeaVimDisabledHere) {
return ActionEnableStatus.no("IdeaVim is disabled in this place", LogLevel.INFO)
Expand Down Expand Up @@ -358,10 +360,12 @@ private class ActionEnableStatus(
val message: String,
val logLevel: LogLevel,
) {
fun printLog() {
fun printLog(keyStroke: KeyStroke?) {
val message = "IdeaVim keys are enabled = $isEnabled for key '$keyStroke': $message"
when (logLevel) {
LogLevel.INFO -> LOG.info("IdeaVim keys are enabled = $isEnabled: $message")
LogLevel.DEBUG -> LOG.debug("IdeaVim keys are enabled = $isEnabled: $message")
LogLevel.INFO -> LOG.info(message)
LogLevel.DEBUG -> LOG.debug(message)
LogLevel.ERROR -> LOG.error(message)
}
}

Expand All @@ -374,5 +378,5 @@ private class ActionEnableStatus(
}

private enum class LogLevel {
DEBUG, INFO,
DEBUG, INFO, ERROR,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.handler

import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.actionSystem.EditorActionHandlerBean
import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.StartupActivity

/**
* Logs the chain of handlers for esc and enter
*
* As we made a migration to the new way of handling esc keys (VIM-2974), we may face several issues around that
* One of the possible issues is that some plugin may also register a shortcut for this key and do not pass
* the control to the next handler. In this way, the esc won't work, but there will be no exceptions.
*
* This is a logger that logs the chain of handlers.
*
* Strictly speaking, such access to the extension point is not allowed by the platform. But we can't do this thing
* otherwise, so let's use it as long as we can.
*/
internal class EditorHandlersChainLogger : StartupActivity {
@Suppress("UnresolvedPluginConfigReference")
private val editorHandlers = ExtensionPointName<EditorActionHandlerBean>("com.intellij.editorActionHandler")

override fun runActivity(project: Project) {
val escHandlers = editorHandlers.extensionList
.filter { it.action == "EditorEscape" }
.joinToString("\n") { it.implementationClass }
val enterHandlers = editorHandlers.extensionList
.filter { it.action == "EditorEnter" }
.joinToString("\n") { it.implementationClass }

LOG.info("Esc handlers chain:\n$escHandlers")
LOG.info("Enter handlers chain:\n$enterHandlers")
}

companion object {
val LOG = logger<EditorHandlersChainLogger>()
}
}
49 changes: 49 additions & 0 deletions src/main/java/com/maddyhome/idea/vim/handler/VimEnterHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.intellij.codeInsight.lookup.LookupManager
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.invokeLater
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Caret
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.actionSystem.EditorActionHandler
Expand Down Expand Up @@ -184,6 +185,54 @@ internal class VimEscForRiderHandler(nextHandler: EditorActionHandler) : VimKeyH
}
}

/**
* Empty logger for esc presses
*
* As we made a migration to the new way of handling esc keys (VIM-2974), we may face several issues around that
* One of the possible issues is that some plugin may also register a shortcut for this key and do not pass
* the control to the next handler. In this way, the esc won't work, but there will be no exceptions.
* This handler, that should stand in front of handlers change, just logs the event of pressing the key
* and passes the execution.
*/
internal class VimEscLoggerHandler(private val nextHandler: EditorActionHandler) : EditorActionHandler() {
override fun doExecute(editor: Editor, caret: Caret?, dataContext: DataContext?) {
LOG.info("Esc pressed")
nextHandler.execute(editor, caret, dataContext)
}

override fun isEnabledForCaret(editor: Editor, caret: Caret, dataContext: DataContext?): Boolean {
return nextHandler.isEnabled(editor, caret, dataContext)
}

companion object {
val LOG = logger<VimEscLoggerHandler>()
}
}

/**
* Empty logger for enter presses
*
* As we made a migration to the new way of handling enter keys (VIM-2974), we may face several issues around that
* One of the possible issues is that some plugin may also register a shortcut for this key and do not pass
* the control to the next handler. In this way, the esc won't work, but there will be no exceptions.
* This handler, that should stand in front of handlers change, just logs the event of pressing the key
* and passes the execution.
*/
internal class VimEnterLoggerHandler(private val nextHandler: EditorActionHandler) : EditorActionHandler() {
override fun doExecute(editor: Editor, caret: Caret?, dataContext: DataContext?) {
LOG.info("Enter pressed")
nextHandler.execute(editor, caret, dataContext)
}

override fun isEnabledForCaret(editor: Editor, caret: Caret, dataContext: DataContext?): Boolean {
return nextHandler.isEnabled(editor, caret, dataContext)
}

companion object {
val LOG = logger<VimEnterLoggerHandler>()
}
}

internal abstract class VimKeyHandler(nextHandler: EditorActionHandler?) : OctopusHandler(nextHandler) {

abstract val key: String
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
which (at least for 2020.1) has some long running activities that block other startup extensions. None of the
core platform activities have IDs, so we can't use "before ID". We have to use "first" -->
<postStartupActivity implementation="com.maddyhome.idea.vim.PluginStartup" order="first"/>
<postStartupActivity implementation="com.maddyhome.idea.vim.handler.EditorHandlersChainLogger"/>

<editorFloatingToolbarProvider implementation="com.maddyhome.idea.vim.ui.ReloadFloatingToolbar"/>

Expand Down Expand Up @@ -117,6 +118,12 @@
<editorActionHandler action="EditorEscape" implementationClass="com.maddyhome.idea.vim.handler.VimEscHandler"
id="ideavim-esc"
order="after smart-step-into-escape, after AceHandlerEscape, before jupyterCommandModeEscKeyHandler, before templateEscape, before backend.escape"/>
<editorActionHandler action="EditorEscape" implementationClass="com.maddyhome.idea.vim.handler.VimEscLoggerHandler"
id="ideavim-esc-logger"
order="first"/>
<editorActionHandler action="EditorEnter" implementationClass="com.maddyhome.idea.vim.handler.VimEnterLoggerHandler"
id="ideavim-enter-logger"
order="first"/>
</extensions>

<xi:include href="/META-INF/includes/ApplicationServices.xml" xpointer="xpointer(/idea-plugin/*)"/>
Expand Down

0 comments on commit efd9ed0

Please sign in to comment.