Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Proof of concept to access user input send from eclipse console to pr…
…ocess.

This does not work with current version of eclipse's ProcessConsole.
  • Loading branch information
PPazderski committed Jul 2, 2019
1 parent 8fa736b commit b1fb810
Showing 1 changed file with 42 additions and 71 deletions.
Expand Up @@ -7,6 +7,7 @@
package org.python.pydev.debug.model;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -31,17 +32,15 @@
import org.eclipse.debug.core.model.IDebugTarget;
import org.eclipse.debug.core.model.IMemoryBlock;
import org.eclipse.debug.core.model.IStackFrame;
import org.eclipse.debug.core.model.IStreamMonitor;
import org.eclipse.debug.core.model.IStreamsProxy;
import org.eclipse.debug.core.model.IThread;
import org.eclipse.debug.internal.ui.views.console.ProcessConsole;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.internal.console.IOConsolePartition;
import org.eclipse.ui.console.IOConsole;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.tasklist.ITaskListResourceAdapter;
import org.python.pydev.ast.location.FindWorkspaceFiles;
Expand Down Expand Up @@ -80,7 +79,6 @@
*
* @author Fabio
*/
@SuppressWarnings("restriction")
public abstract class AbstractDebugTarget extends AbstractDebugTargetWithTransmission implements IDebugTarget,
ILaunchListener, IExceptionsBreakpointListener, IPropertyTraceListener {

Expand Down Expand Up @@ -898,84 +896,57 @@ private void addBreakpointsFor(IContainer container) {
* This function adds the input listener extension point, so that plugins that only care about
* the input in the console can know about it.
*/
@SuppressWarnings({ "unchecked" })
public void addConsoleInputListener() {
IConsole console = DebugUITools.getConsole(this.getProcess());
if (console instanceof ProcessConsole) {
final ProcessConsole c = (ProcessConsole) console;
final List<IConsoleInputListener> participants = ExtensionHelper
.getParticipants(ExtensionHelper.PYDEV_DEBUG_CONSOLE_INPUT_LISTENER);
if (console instanceof IOConsole && console instanceof org.eclipse.debug.ui.console.IConsole) {
final org.eclipse.debug.ui.console.IConsole ic = (org.eclipse.debug.ui.console.IConsole) console;
final IOConsole c = (IOConsole) console;
final AbstractDebugTarget target = this;

target.addProcessConsole(c);

//let's listen the doc for the changes
c.getDocument().addDocumentListener(new IDocumentListener() {
IStreamsProxy proxy = new SplittingStreamProxy(target, this.getProcess().getStreamsProxy());
// JavaDoc is not clear if connect is allowed to be called more than once
// reality is: on ProcessConsole (this is one) you cannot call connect
ic.connect(proxy);
}
}

@Override
public void documentAboutToBeChanged(DocumentEvent event) {
if (target.isWaitingForInput()) {
return;
}
private static class SplittingStreamProxy implements IStreamsProxy {

//only report when we have a new line
if (event.fText.indexOf('\r') != -1 || event.fText.indexOf('\n') != -1) {
try {
ITypedRegion partition = event.fDocument.getPartition(event.fOffset);
if (partition instanceof IOConsolePartition) {
IOConsolePartition p = (IOConsolePartition) partition;

//we only communicate about inputs (because we only care about what the user writes)
if (p.getType().equals(IOConsolePartition.INPUT_PARTITION_TYPE)) {
if (event.fText.length() <= 2) {
//the user typed something
final String inputFound = p.getString();
for (IConsoleInputListener listener : participants) {
listener.newLineReceived(inputFound, target);
}
}

}
}
} catch (Exception e) {
Log.log(e);
}
}
@SuppressWarnings({ "unchecked" })
final List<IConsoleInputListener> participants = ExtensionHelper
.getParticipants(ExtensionHelper.PYDEV_DEBUG_CONSOLE_INPUT_LISTENER);
AbstractDebugTarget target;
IStreamsProxy delegateProxy;

}
public SplittingStreamProxy(AbstractDebugTarget target, IStreamsProxy delegateProxy) {
super();
this.target = target;
this.delegateProxy = delegateProxy;
}

@Override
public void documentChanged(DocumentEvent event) {
if (target.isWaitingForInput()) {
return;
}
@Override
public IStreamMonitor getErrorStreamMonitor() {
return delegateProxy.getErrorStreamMonitor();
}

//only report when we have a new line
if (event.fText.indexOf('\r') != -1 || event.fText.indexOf('\n') != -1) {
try {
ITypedRegion partition = event.fDocument.getPartition(event.fOffset);
if (partition instanceof IOConsolePartition) {
IOConsolePartition p = (IOConsolePartition) partition;

//we only communicate about inputs (because we only care about what the user writes)
if (p.getType().equals(IOConsolePartition.INPUT_PARTITION_TYPE)) {
if (event.fText.length() > 2) {
//the user pasted something
for (IConsoleInputListener listener : participants) {
listener.pasteReceived(event.fText, target);
}
}

}
}
} catch (Exception e) {
Log.log(e);
}
}
}
@Override
public IStreamMonitor getOutputStreamMonitor() {
return delegateProxy.getOutputStreamMonitor();
}

});
@Override
public void write(String input) throws IOException {
if (target.isWaitingForInput()) {
delegateProxy.write(input);
} else {
for (IConsoleInputListener participant : participants) {
participant.newLineReceived(input, target);
}
}
}

}

@Override
Expand Down

0 comments on commit b1fb810

Please sign in to comment.