Extracted abstract class from PydevConsoleExecuteActionHandler

This commit is contained in:
Kiryl Chetyrbak
2017-04-12 15:35:52 -04:00
parent f849cad4b4
commit 8c9a382ab1
8 changed files with 57 additions and 43 deletions
@@ -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);
}
}
}
}
@@ -20,7 +20,6 @@ public interface ConsoleCommunication {
boolean needsMore();
void execInterpreter(ConsoleCodeFragment code, Function<InterpreterResponse, Object> 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;
@@ -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
@@ -230,7 +230,7 @@ public interface PydevConsoleRunner {
void addConsoleListener(PydevConsoleRunnerImpl.ConsoleListener consoleListener);
PydevConsoleExecuteActionHandler getConsoleExecuteActionHandler();
PythonConsoleExecuteActionHandler getConsoleExecuteActionHandler();
PyConsoleProcessHandler getProcessHandler();
@@ -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<ConsoleListener> myConsoleListeners = ContainerUtil.createLockFreeCopyOnWriteList();
private final PyConsoleType myConsoleType;
private Map<String, String> 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;
}
@@ -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
}
@@ -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();
}
}
@@ -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<RunContentDescriptor> myContentDescriptorRef = Ref.create();