Search in sources :

Example 1 with SmartStepIntoVariant

use of org.python.pydev.debug.model.SmartStepIntoVariant 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)

Aggregations

ArrayList (java.util.ArrayList)1 IAdaptable (org.eclipse.core.runtime.IAdaptable)1 DebugException (org.eclipse.debug.core.DebugException)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 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)1 PyStackFrame (org.python.pydev.debug.model.PyStackFrame)1 SmartStepIntoVariant (org.python.pydev.debug.model.SmartStepIntoVariant)1 PyEdit (org.python.pydev.editor.PyEdit)1 ICoreTextSelection (org.python.pydev.shared_core.string.ICoreTextSelection)1 TextSelectionUtils (org.python.pydev.shared_core.string.TextSelectionUtils)1