Search in sources :

Example 1 with PyStackFrame

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;
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) PyStackFrame(org.python.pydev.debug.model.PyStackFrame) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) SmartStepIntoVariant(org.python.pydev.debug.model.SmartStepIntoVariant) ArrayList(java.util.ArrayList) ICoreTextSelection(org.python.pydev.shared_core.string.ICoreTextSelection) IRegion(org.eclipse.jface.text.IRegion) DebugException(org.eclipse.debug.core.DebugException) BadLocationException(org.eclipse.jface.text.BadLocationException) TextSelectionUtils(org.python.pydev.shared_core.string.TextSelectionUtils) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) PyEdit(org.python.pydev.editor.PyEdit) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with PyStackFrame

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;
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) PyStackFrame(org.python.pydev.debug.model.PyStackFrame) AbstractDebugTarget(org.python.pydev.debug.model.AbstractDebugTarget) IWatchExpression(org.eclipse.debug.core.model.IWatchExpression) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IDebugTarget(org.eclipse.debug.core.model.IDebugTarget) IVariableLocator(org.python.pydev.debug.model.IVariableLocator) PyVariable(org.python.pydev.debug.model.PyVariable) Tuple(org.python.pydev.shared_core.structure.Tuple)

Example 3 with PyStackFrame

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;
}
Also used : PyStackFrame(org.python.pydev.debug.model.PyStackFrame) ArrayList(java.util.ArrayList) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle)

Example 4 with PyStackFrame

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();
    }
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) PyStackFrame(org.python.pydev.debug.model.PyStackFrame) EvaluateExpressionCommand(org.python.pydev.debug.model.remote.EvaluateExpressionCommand)

Example 5 with PyStackFrame

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;
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) PyStackFrame(org.python.pydev.debug.model.PyStackFrame) IStackFrame(org.eclipse.debug.core.model.IStackFrame) IPath(org.eclipse.core.runtime.IPath) ISelection(org.eclipse.jface.viewers.ISelection) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) DebugException(org.eclipse.debug.core.DebugException)

Aggregations

PyStackFrame (org.python.pydev.debug.model.PyStackFrame)9 IAdaptable (org.eclipse.core.runtime.IAdaptable)4 ArrayList (java.util.ArrayList)2 DebugException (org.eclipse.debug.core.DebugException)2 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)2 PyEdit (org.python.pydev.editor.PyEdit)2 Tuple (org.python.pydev.shared_core.structure.Tuple)2 Collection (java.util.Collection)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Job (org.eclipse.core.runtime.jobs.Job)1 IDebugTarget (org.eclipse.debug.core.model.IDebugTarget)1 IStackFrame (org.eclipse.debug.core.model.IStackFrame)1 IWatchExpression (org.eclipse.debug.core.model.IWatchExpression)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 IRegion (org.eclipse.jface.text.IRegion)1 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)1 ISelection (org.eclipse.jface.viewers.ISelection)1