use of org.python.pydev.debug.model.PyStackFrame in project Pydev by fabioz.
the class StepIntoSelectionHyperlinkDetector method detectHyperlinks.
/**
* @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion, boolean)
*/
@Override
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
ITextEditor editor = getAdapter(ITextEditor.class);
if (editor instanceof PyEdit) {
PyEdit pyEdit = (PyEdit) editor;
// should only enable step into selection when the current debug context
// is an instance of IJavaStackFrame
IAdaptable debugContext = DebugUITools.getDebugContext();
if (debugContext == null) {
return null;
}
if (!(debugContext instanceof PyStackFrame)) {
return null;
}
PyStackFrame pyStackFrame = (PyStackFrame) debugContext;
SmartStepIntoVariant[] stepIntoTargets = pyStackFrame.getStepIntoTargets();
if (stepIntoTargets == null) {
Log.log("Unable to get Smart Step Into Targets.");
return null;
}
int offset = region.getOffset();
try {
IDocument document = pyEdit.getDocument();
// see if we can find a word there
IRegion wordRegion = TextSelectionUtils.findWord(document, offset);
if (wordRegion == null || wordRegion.getLength() == 0) {
return null;
}
String selectedWord;
// don't highlight keywords
try {
selectedWord = document.get(wordRegion.getOffset(), wordRegion.getLength());
if (PythonLanguageUtils.isKeyword(selectedWord)) {
return null;
}
} catch (BadLocationException e) {
Log.log(e);
return null;
}
ICoreTextSelection textSelection = pyEdit.getTextSelection();
int line = textSelection.getStartLine();
List<SmartStepIntoVariant> found = new ArrayList<>();
for (SmartStepIntoVariant smartStepIntoVariant : stepIntoTargets) {
if (line == smartStepIntoVariant.line && selectedWord.equals(smartStepIntoVariant.name)) {
found.add(smartStepIntoVariant);
}
}
if (found.size() == 0) {
Log.log("Unable to find step into target with name: " + selectedWord + " at line: " + line + "\nAvailable: " + StringUtils.join("; ", stepIntoTargets));
return null;
}
SmartStepIntoVariant target;
if (found.size() > 1) {
// i.e.: the target is backwards.
Collections.reverse(found);
Iterator<SmartStepIntoVariant> iterator = found.iterator();
target = iterator.next();
TextSelectionUtils ts = new TextSelectionUtils(document, textSelection);
// Let's check if there's more than one occurrence of the same word in the given line.
String lineContents = ts.getLine(line);
List<Integer> wordOffsets = StringUtils.findWordOffsets(lineContents, selectedWord);
if (wordOffsets.size() > 0) {
int offsetInLine = wordRegion.getOffset() - ts.getStartLineOffset();
for (Integer i : wordOffsets) {
if (i >= offsetInLine) {
break;
}
target = iterator.next();
if (!iterator.hasNext()) {
break;
}
}
}
} else {
// size == 1
target = found.get(0);
}
// return a hyperlink even without trying to find the definition (which may be costly)
return new IHyperlink[] { new StepIntoSelectionHyperlink((PyStackFrame) debugContext, pyEdit, wordRegion, line, selectedWord, target) };
} catch (Exception e) {
Log.log(e);
return null;
}
}
return null;
}
use of org.python.pydev.debug.model.PyStackFrame in project Pydev by fabioz.
the class RunCustomOperationCommand method extractContextFromSelection.
/**
* Extracts a debug target and locator from the provided selection.
* <p>
* The Eclipse definition org.python.pydev.debug.model.RunCustomOperationCommand.ExtractableContext can
* be used to filter menu presentation on whether the selection may be extractable.
*
* @param selection
* @return Debug target and locator suitable for passing the the constructor,
* or <code>null</code> if no suitable selection is selected.
*/
public static Tuple<AbstractDebugTarget, IVariableLocator> extractContextFromSelection(ISelection selection) {
if (selection instanceof StructuredSelection) {
StructuredSelection structuredSelection = (StructuredSelection) selection;
Object elem = structuredSelection.getFirstElement();
if (elem instanceof PyVariable) {
PyVariable pyVar = (PyVariable) elem;
AbstractDebugTarget target = (AbstractDebugTarget) pyVar.getDebugTarget();
return new Tuple<AbstractDebugTarget, IVariableLocator>(target, pyVar);
} else if (elem instanceof IWatchExpression) {
IWatchExpression expression = (IWatchExpression) elem;
final String expressionText = expression.getExpressionText();
IDebugTarget debugTarget = expression.getDebugTarget();
if (debugTarget instanceof AbstractDebugTarget) {
AbstractDebugTarget target = (AbstractDebugTarget) debugTarget;
IAdaptable context = DebugUITools.getDebugContext();
final PyStackFrame stackFrame = (PyStackFrame) context.getAdapter(PyStackFrame.class);
if (stackFrame != null) {
return new Tuple<AbstractDebugTarget, IVariableLocator>(target, new IVariableLocator() {
@Override
public String getThreadId() {
return stackFrame.getThreadId();
}
@Override
public String getPyDBLocation() {
String locator = stackFrame.getExpressionLocator().getPyDBLocation();
return locator + "\t" + expressionText;
}
});
}
}
}
}
return null;
}
use of org.python.pydev.debug.model.PyStackFrame in project Pydev by fabioz.
the class PydevDebugConsoleCommunication method getCompletions.
@Override
public ICompletionProposalHandle[] getCompletions(String text, String actTok, int offset, boolean showForTabCompletion) throws Exception {
ICompletionProposalHandle[] receivedCompletions = {};
if (waitingForInput) {
return new ICompletionProposalHandle[0];
}
PyStackFrame frame = consoleFrameProvider.getLastSelectedFrame();
if (frame == null) {
return new ICompletionProposalHandle[0];
}
final EvaluateDebugConsoleExpression evaluateDebugConsoleExpression = new EvaluateDebugConsoleExpression(frame);
String result = evaluateDebugConsoleExpression.getCompletions(actTok, offset);
if (result.length() > 0) {
List<Object[]> fromServer = XMLUtils.convertXMLcompletionsFromConsole(result);
List<ICompletionProposalHandle> ret = new ArrayList<ICompletionProposalHandle>();
PydevConsoleCommunication.convertConsoleCompletionsToICompletions(text, actTok, offset, fromServer, ret, false);
receivedCompletions = ret.toArray(new ICompletionProposalHandle[ret.size()]);
}
return receivedCompletions;
}
use of org.python.pydev.debug.model.PyStackFrame in project Pydev by fabioz.
the class EvaluationConsoleInputListener method newLineReceived.
@Override
public void newLineReceived(String lineReceived, AbstractDebugTarget target) {
boolean evaluateNow = !lineReceived.startsWith(" ") && !lineReceived.startsWith("\t") && !lineReceived.endsWith(":") && !lineReceived.endsWith("\\");
if (DEBUG) {
System.out.println("line: '" + lineReceived + "'");
}
buf.append(lineReceived);
if (lineReceived.length() > 0) {
buf.append("@LINE@");
}
if (evaluateNow) {
final String toEval = buf.toString();
if (toEval.trim().length() > 0) {
IAdaptable context = DebugUITools.getDebugContext();
if (DEBUG) {
System.out.println("Evaluating:\n" + toEval);
}
if (context instanceof PyStackFrame) {
final PyStackFrame frame = (PyStackFrame) context;
target.postCommand(new EvaluateExpressionCommand(target, toEval, frame.getLocalsLocator().getPyDBLocation(), true) {
@Override
public void processOKResponse(int cmdCode, String payload) {
frame.forceGetNewVariables();
super.processOKResponse(cmdCode, payload);
}
});
}
}
buf = new StringBuffer();
}
}
use of org.python.pydev.debug.model.PyStackFrame in project Pydev by fabioz.
the class IgnoreCaughtExceptionCommandHandler method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
ISelection selection = HandlerUtil.getCurrentSelection(event);
if (selection instanceof StructuredSelection) {
StructuredSelection structuredSelection = (StructuredSelection) selection;
Object elem = structuredSelection.getFirstElement();
if (elem instanceof IAdaptable) {
IAdaptable iAdaptable = (IAdaptable) elem;
elem = iAdaptable.getAdapter(IStackFrame.class);
}
if (elem instanceof PyStackFrame) {
try {
PyStackFrame pyStackFrame = (PyStackFrame) elem;
IPath path = pyStackFrame.getPath();
int lineNumber = pyStackFrame.getLineNumber();
PyExceptionBreakPointManager.getInstance().ignoreCaughtExceptionsWhenThrownFrom.addIgnoreThrownExceptionIn(path.toFile(), lineNumber);
} catch (DebugException e) {
Log.log(e);
}
}
}
return null;
}
Aggregations