How to know if user is typing in Console of PyCharm
Answered
I have a function that's called in typedHandler but I don't want to call it if the user is typing in console. How to achieve this?
Please sign in to leave a comment.
Please clarify the issue. What console do you mean? A screenshot would be nice. What is the implementation, registration in plugin.xml, and what method is called?
Hi,
There is no open API for this, but you can try the following approach.
You can use com.jetbrains.python.console.completion.PythonConsoleAutopopupBlockingHandler#REPL_KEY to check whether the current context is PythonConsole, like PythonConsoleAutopopupBlockingHandler does it:
Please keep in mind that this is a part of the plugin implementation, not open API, and it may stop working in the future.
An alternative could be checking whether the current virtual file is LightVirtualFile with “Python Console” name. It also may stop working in the future.
Hello, shouldn't this be solvable by editor.KIND == CONSOLE(the enum that editor class had) but I tried it and it wasn't working. Is this not a Console kind of editor?
Hi,
I asked a responsible person about it. Due to vacation period, the answer may be significantly delayed. Please be patient.
Okay okay, no problem. It's not urgent. Thank you!
That was quick :) The possibility of doing it has been implemented and will be available starting with PyCharm 2023.3 EAP 1.
If
editor.getVirtualFile().getUserData(CONSOLE_KEY) == true
then an editor from the Python Console.Regarding the solution with
REPL_KEY
- it is unreliable because it works only if runtime completion in the console is enabled, and it will be disabled by default in 2023.3.