From 8c9a382ab1f5c8cb036a55f67c0567da7cf150bd Mon Sep 17 00:00:00 2001 From: Kiryl Chetyrbak Date: Wed, 12 Apr 2017 15:35:52 -0400 Subject: [PATCH] Extracted abstract class from PydevConsoleExecuteActionHandler --- .../pydev/AbstractConsoleCommunication.java | 9 +++++ .../console/pydev/ConsoleCommunication.java | 3 +- .../PydevConsoleExecuteActionHandler.kt | 33 +++---------------- .../python/console/PydevConsoleRunner.java | 2 +- .../console/PydevConsoleRunnerImpl.java | 11 ++----- .../PythonConsoleExecuteActionHandler.kt | 32 ++++++++++++++++++ .../python/console/PythonConsoleView.java | 8 ++--- .../env/python/console/PyConsoleTask.java | 2 +- 8 files changed, 57 insertions(+), 43 deletions(-) create mode 100644 python/src/com/jetbrains/python/console/PythonConsoleExecuteActionHandler.kt diff --git a/python/pydevSrc/com/jetbrains/python/console/pydev/AbstractConsoleCommunication.java b/python/pydevSrc/com/jetbrains/python/console/pydev/AbstractConsoleCommunication.java index 09e2b519e215..593f9a877de4 100644 --- a/python/pydevSrc/com/jetbrains/python/console/pydev/AbstractConsoleCommunication.java +++ b/python/pydevSrc/com/jetbrains/python/console/pydev/AbstractConsoleCommunication.java @@ -78,4 +78,13 @@ public abstract class AbstractConsoleCommunication implements ConsoleCommunicati public void setConsoleFile(VirtualFile consoleFile) { myConsoleFile = consoleFile; } + + public void notifyInputReceived() { + if (waitingForInput) { + waitingForInput = false; + for (ConsoleCommunicationListener listener : communicationListeners) { + listener.commandExecuted(false); + } + } + } } diff --git a/python/pydevSrc/com/jetbrains/python/console/pydev/ConsoleCommunication.java b/python/pydevSrc/com/jetbrains/python/console/pydev/ConsoleCommunication.java index d348fe6dce04..939409716e2f 100644 --- a/python/pydevSrc/com/jetbrains/python/console/pydev/ConsoleCommunication.java +++ b/python/pydevSrc/com/jetbrains/python/console/pydev/ConsoleCommunication.java @@ -20,7 +20,6 @@ public interface ConsoleCommunication { boolean needsMore(); - void execInterpreter(ConsoleCodeFragment code, Function callback); void interrupt(); @@ -30,6 +29,8 @@ public interface ConsoleCommunication { void notifyCommandExecuted(boolean more); void notifyInputRequested(); + void notifyInputReceived(); + class ConsoleCodeFragment { private final String myText; private final boolean myIsSingleLine; diff --git a/python/src/com/jetbrains/python/console/PydevConsoleExecuteActionHandler.kt b/python/src/com/jetbrains/python/console/PydevConsoleExecuteActionHandler.kt index f89b34902440..9c4fb29eb77e 100644 --- a/python/src/com/jetbrains/python/console/PydevConsoleExecuteActionHandler.kt +++ b/python/src/com/jetbrains/python/console/PydevConsoleExecuteActionHandler.kt @@ -39,13 +39,13 @@ import java.awt.Font */ open class PydevConsoleExecuteActionHandler(private val myConsoleView: LanguageConsoleView, processHandler: ProcessHandler, - val consoleCommunication: ConsoleCommunication) : ProcessBackedConsoleExecuteActionHandler(processHandler, false), ConsoleCommunicationListener { + final override val consoleCommunication: ConsoleCommunication) : PythonConsoleExecuteActionHandler(processHandler, false), ConsoleCommunicationListener { private val project = myConsoleView.project private val myEnterHandler = PyConsoleEnterHandler() private var myIpythonInputPromptCount = 1 - var isEnabled = false + override var isEnabled = false set(value) { field = value updateConsoleState() @@ -55,12 +55,10 @@ open class PydevConsoleExecuteActionHandler(private val myConsoleView: LanguageC this.consoleCommunication.addCommunicationListener(this) } - override fun processLine(text: String) { executeMultiLine(text) } - private fun executeMultiLine(text: String) { val commandText = if (!text.endsWith("\n")) { text + "\n" @@ -71,10 +69,9 @@ open class PydevConsoleExecuteActionHandler(private val myConsoleView: LanguageC sendLineToConsole(ConsoleCommunication.ConsoleCodeFragment(commandText, checkSingleLine(text))) } - fun checkSingleLine(text: String): Boolean { + override fun checkSingleLine(text: String): Boolean { val pyFile: PyFile =PyElementGenerator.getInstance(project).createDummyFile(myConsoleView.virtualFile.getUserData(LanguageLevel.KEY), text) as PyFile return PsiTreeUtil.findChildOfAnyType(pyFile, PyStatementList::class.java) == null && pyFile.statements.size < 2 - } private fun sendLineToConsole(code: ConsoleCommunication.ConsoleCodeFragment) { @@ -90,7 +87,6 @@ open class PydevConsoleExecuteActionHandler(private val myConsoleView: LanguageC consoleComm.execInterpreter(code) {} } - private fun updateConsoleState() { if (!isEnabled) { executingPrompt() @@ -111,18 +107,6 @@ open class PydevConsoleExecuteActionHandler(private val myConsoleView: LanguageC } } - fun inputReceived() { - if (consoleCommunication is PythonDebugConsoleCommunication) { - if (consoleCommunication.waitingForInput) { - consoleCommunication.waitingForInput = false - val console = myConsoleView - if (PyConsoleUtil.INPUT_PROMPT.equals(console.prompt) || PyConsoleUtil.HELP_PROMPT.equals(console.prompt)) { - console.prompt = PyConsoleUtil.ORDINARY_PROMPT - } - } - } - } - private fun inPrompt() { if (ipythonEnabled) { ipythonInPrompt() @@ -137,8 +121,6 @@ open class PydevConsoleExecuteActionHandler(private val myConsoleView: LanguageC myConsoleView.prompt = PyConsoleUtil.ORDINARY_PROMPT PyConsoleUtil.scrollDown(myConsoleView.currentEditor) } - - } private val ipythonEnabled: Boolean @@ -179,8 +161,6 @@ open class PydevConsoleExecuteActionHandler(private val myConsoleView: LanguageC myConsoleView.prompt = prompt PyConsoleUtil.scrollDown(myConsoleView.currentEditor) } - - } override fun commandExecuted(more: Boolean) = updateConsoleState() @@ -192,7 +172,7 @@ open class PydevConsoleExecuteActionHandler(private val myConsoleView: LanguageC val pythonIndent: Int get() = CodeStyleSettingsManager.getSettings(project).getIndentSize(PythonFileType.INSTANCE) - val cantExecuteMessage: String + override val cantExecuteMessage: String get() { if (!isEnabled) { return consoleIsNotEnabledMessage @@ -219,9 +199,7 @@ open class PydevConsoleExecuteActionHandler(private val myConsoleView: LanguageC } } - private fun doRunExecuteAction(console: LanguageConsoleView) { - val doc = myConsoleView.editorDocument val endMarker = doc.createRangeMarker(doc.textLength, doc.textLength) endMarker.isGreedyToLeft = false @@ -243,12 +221,11 @@ open class PydevConsoleExecuteActionHandler(private val myConsoleView: LanguageC processLine(myConsoleView.consoleEditor.document.text) } } - } private fun copyToHistoryAndExecute(console: LanguageConsoleView) = super.runExecuteAction(console) - fun canExecuteNow(): Boolean = !consoleCommunication.isExecuting || consoleCommunication.isWaitingForInput + override fun canExecuteNow(): Boolean = !consoleCommunication.isExecuting || consoleCommunication.isWaitingForInput protected open val consoleIsNotEnabledMessage: String get() = notEnabledMessage diff --git a/python/src/com/jetbrains/python/console/PydevConsoleRunner.java b/python/src/com/jetbrains/python/console/PydevConsoleRunner.java index ed552d30cc9c..05e13edee272 100644 --- a/python/src/com/jetbrains/python/console/PydevConsoleRunner.java +++ b/python/src/com/jetbrains/python/console/PydevConsoleRunner.java @@ -230,7 +230,7 @@ public interface PydevConsoleRunner { void addConsoleListener(PydevConsoleRunnerImpl.ConsoleListener consoleListener); - PydevConsoleExecuteActionHandler getConsoleExecuteActionHandler(); + PythonConsoleExecuteActionHandler getConsoleExecuteActionHandler(); PyConsoleProcessHandler getProcessHandler(); diff --git a/python/src/com/jetbrains/python/console/PydevConsoleRunnerImpl.java b/python/src/com/jetbrains/python/console/PydevConsoleRunnerImpl.java index c95f886f8c89..b9c03a3da941 100644 --- a/python/src/com/jetbrains/python/console/PydevConsoleRunnerImpl.java +++ b/python/src/com/jetbrains/python/console/PydevConsoleRunnerImpl.java @@ -142,7 +142,7 @@ public class PydevConsoleRunnerImpl implements PydevConsoleRunner { protected int[] myPorts; private PydevConsoleCommunication myPydevConsoleCommunication; private PyConsoleProcessHandler myProcessHandler; - protected PydevConsoleExecuteActionHandler myConsoleExecuteActionHandler; + protected PythonConsoleExecuteActionHandler myConsoleExecuteActionHandler; private List myConsoleListeners = ContainerUtil.createLockFreeCopyOnWriteList(); private final PyConsoleType myConsoleType; private Map myEnvironmentVariables; @@ -729,11 +729,6 @@ public class PydevConsoleRunnerImpl implements PydevConsoleRunner { return anAction; } - private boolean isIndentSubstring(String text) { - int indentSize = myConsoleExecuteActionHandler.getPythonIndent(); - return text.length() >= indentSize && CharMatcher.WHITESPACE.matchesAllOf(text.substring(text.length() - indentSize)); - } - private void enableConsoleExecuteAction() { myConsoleExecuteActionHandler.setEnabled(true); } @@ -873,7 +868,7 @@ public class PydevConsoleRunnerImpl implements PydevConsoleRunner { } @NotNull - protected PydevConsoleExecuteActionHandler createExecuteActionHandler() { + protected PythonConsoleExecuteActionHandler createExecuteActionHandler() { myConsoleExecuteActionHandler = new PydevConsoleExecuteActionHandler(myConsoleView, myProcessHandler, myPydevConsoleCommunication); myConsoleExecuteActionHandler.setEnabled(false); @@ -907,7 +902,7 @@ public class PydevConsoleRunnerImpl implements PydevConsoleRunner { } @Override - public PydevConsoleExecuteActionHandler getConsoleExecuteActionHandler() { + public PythonConsoleExecuteActionHandler getConsoleExecuteActionHandler() { return myConsoleExecuteActionHandler; } diff --git a/python/src/com/jetbrains/python/console/PythonConsoleExecuteActionHandler.kt b/python/src/com/jetbrains/python/console/PythonConsoleExecuteActionHandler.kt new file mode 100644 index 000000000000..1aa8565b877a --- /dev/null +++ b/python/src/com/jetbrains/python/console/PythonConsoleExecuteActionHandler.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jetbrains.python.console + +import com.intellij.execution.console.ProcessBackedConsoleExecuteActionHandler +import com.intellij.execution.process.ProcessHandler +import com.jetbrains.python.console.pydev.ConsoleCommunication + +/** + * Created by kirylch on 4/12/2017. + */ +abstract class PythonConsoleExecuteActionHandler(processHandler: ProcessHandler, preserveMarkup: Boolean) : ProcessBackedConsoleExecuteActionHandler(processHandler, preserveMarkup) { + abstract override fun processLine(line: String) + abstract fun checkSingleLine(text: String): Boolean + abstract val cantExecuteMessage: String + abstract fun canExecuteNow(): Boolean + abstract var isEnabled: Boolean + abstract val consoleCommunication: ConsoleCommunication +} \ No newline at end of file diff --git a/python/src/com/jetbrains/python/console/PythonConsoleView.java b/python/src/com/jetbrains/python/console/PythonConsoleView.java index 0668c1cbe9a9..eeef188f4ac8 100644 --- a/python/src/com/jetbrains/python/console/PythonConsoleView.java +++ b/python/src/com/jetbrains/python/console/PythonConsoleView.java @@ -86,7 +86,7 @@ public class PythonConsoleView extends LanguageConsoleImpl implements Observable private static final Logger LOG = Logger.getInstance(PythonConsoleView.class); private final ConsolePromptDecorator myPromptView; - private PydevConsoleExecuteActionHandler myExecuteActionHandler; + private PythonConsoleExecuteActionHandler myExecuteActionHandler; private PyConsoleSourceHighlighter mySourceHighlighter; private boolean myIsIPythonOutput; private final PyHighlighter myPyHighlighter; @@ -142,11 +142,11 @@ public class PythonConsoleView extends LanguageConsoleImpl implements Observable } } - public void setExecutionHandler(@NotNull PydevConsoleExecuteActionHandler consoleExecuteActionHandler) { + public void setExecutionHandler(@NotNull PythonConsoleExecuteActionHandler consoleExecuteActionHandler) { myExecuteActionHandler = consoleExecuteActionHandler; } - public PydevConsoleExecuteActionHandler getExecuteActionHandler() { + public PythonConsoleExecuteActionHandler getExecuteActionHandler() { return myExecuteActionHandler; } @@ -171,7 +171,7 @@ public class PythonConsoleView extends LanguageConsoleImpl implements Observable public void inputReceived() { // If user's input was entered while debug console was turned off, we shouldn't wait for it anymore if (myExecuteActionHandler != null) { - myExecuteActionHandler.inputReceived(); + myExecuteActionHandler.getConsoleCommunication().notifyInputReceived(); } } diff --git a/python/testSrc/com/jetbrains/env/python/console/PyConsoleTask.java b/python/testSrc/com/jetbrains/env/python/console/PyConsoleTask.java index 04ab04486755..44f82ab72ca1 100644 --- a/python/testSrc/com/jetbrains/env/python/console/PyConsoleTask.java +++ b/python/testSrc/com/jetbrains/env/python/console/PyConsoleTask.java @@ -59,7 +59,7 @@ public class PyConsoleTask extends PyExecutionFixtureTestTask { private PythonConsoleView myConsoleView; private Semaphore myCommandSemaphore; private Semaphore myConsoleInitSemaphore; - private PydevConsoleExecuteActionHandler myExecuteHandler; + private PythonConsoleExecuteActionHandler myExecuteHandler; private Ref myContentDescriptorRef = Ref.create();