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;
}
Aggregations