Skip to content

Commit

Permalink
debugger (java) updates
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Jan 24, 2024
1 parent f7ee5a0 commit 5f8e6fa
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class StepIntoSelectionHyperlink implements IHyperlink {

/**
* Constructor
* @param debugContext
* @param pyEdit
* @param selectedWord
* @param debugContext
* @param pyEdit
* @param selectedWord
* @param region
*/
public StepIntoSelectionHyperlink(PyStackFrame debugContext, PyEdit pyEdit, IRegion selection, int line,
Expand All @@ -74,7 +74,7 @@ public IRegion getHyperlinkRegion() {
*/
@Override
public String getHyperlinkText() {
return "Step Into Selection";
return "Step Into Selection: " + this.selectedWord;
}

/**
Expand Down Expand Up @@ -122,79 +122,134 @@ public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boo
Log.log("Unable to get Smart Step Into Targets.");
return null;
}
if (stepIntoTargets.length == 0) {
Log.log("No Smart Step Into Targets found.");
return null;
}

int offset = region.getOffset();
ICoreTextSelection textSelection = pyEdit.getTextSelection();
final int line = textSelection.getStartLine();
final int offset = region.getOffset();

try {
boolean smartStepIntoHasOffset = false;
List<SmartStepIntoVariant> foundAtLine = new ArrayList<>();
for (SmartStepIntoVariant smartStepIntoVariant : stepIntoTargets) {
if (smartStepIntoVariant.endlineno >= 0 && smartStepIntoVariant.startcol >= 0
&& smartStepIntoVariant.endcol >= 0) {
// Ok, new approach: use the offsets given by the backend when available (Python 3.11 onwards).
smartStepIntoHasOffset = true;
if (smartStepIntoVariant.line >= line) {
foundAtLine.add(smartStepIntoVariant);
}
}
}

List<SmartStepIntoVariant> found = new ArrayList<>();
IDocument document = pyEdit.getDocument();

IDocument document = pyEdit.getDocument();
//see if we can find a word there
IRegion wordRegion = TextSelectionUtils.findWord(document, offset);
if (wordRegion == null || wordRegion.getLength() == 0) {
if (smartStepIntoHasOffset) {
TextSelectionUtils ts = new TextSelectionUtils(document, offset);

List<StepIntoSelectionHyperlink> hyperlinks = new ArrayList<>();
for (SmartStepIntoVariant smartStepIntoVariant : stepIntoTargets) {
try {
int startOffset = ts.getAbsoluteCursorOffset(smartStepIntoVariant.line,
smartStepIntoVariant.startcol);
int endOffset = ts.getAbsoluteCursorOffset(smartStepIntoVariant.endlineno,
smartStepIntoVariant.endcol);
if (startOffset <= offset && endOffset >= offset) {
IRegion wordRegion = new Region(startOffset, endOffset - startOffset);
final StepIntoSelectionHyperlink link = new StepIntoSelectionHyperlink(
(PyStackFrame) debugContext, pyEdit,
wordRegion, line,
smartStepIntoVariant.name, smartStepIntoVariant);
if (hyperlinks.size() == 0 || canShowMultipleHyperlinks) {
hyperlinks.add(link);
} else {
// Just add it if its size is smaller than the existing one.
StepIntoSelectionHyperlink curr = hyperlinks.get(0);
if (curr.getHyperlinkRegion().getLength() > link.getHyperlinkRegion().getLength()) {
hyperlinks.set(0, link);
}
}
}
} catch (Exception e) {
Log.log(e);
}
}
if (hyperlinks.size() == 0) {
// We can't return empty as it gives an IllegalArgumentException.
return null;
}
return hyperlinks.toArray(new StepIntoSelectionHyperlink[0]);

String selectedWord;
//don't highlight keywords
} else {
try {
selectedWord = document.get(wordRegion.getOffset(), wordRegion.getLength());
if (PythonLanguageUtils.isKeyword(selectedWord)) {
//see if we can find a word there
IRegion wordRegion = TextSelectionUtils.findWord(document, offset);
if (wordRegion == null || wordRegion.getLength() == 0) {
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);
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;
}
}
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) {
Collections.reverse(found); // i.e.: the target is backwards.
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;

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) {
Collections.reverse(found); // i.e.: the target is backwards.
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);
}
} 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 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;
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,11 +677,15 @@ private void processThreadRun(String payload) {
int resumeReason = DebugEvent.UNSPECIFIED;
try {
int raw_reason = Integer.parseInt(threadIdAndReason.o2);
if (raw_reason == AbstractDebuggerCommand.CMD_STEP_OVER) {
if (raw_reason == AbstractDebuggerCommand.CMD_STEP_OVER
|| raw_reason == AbstractDebuggerCommand.CMD_STEP_OVER_MY_CODE) {
resumeReason = DebugEvent.STEP_OVER;
} else if (raw_reason == AbstractDebuggerCommand.CMD_STEP_RETURN) {
} else if (raw_reason == AbstractDebuggerCommand.CMD_STEP_RETURN
|| raw_reason == AbstractDebuggerCommand.CMD_STEP_RETURN_MY_CODE) {
resumeReason = DebugEvent.STEP_RETURN;
} else if (raw_reason == AbstractDebuggerCommand.CMD_STEP_INTO
|| raw_reason == AbstractDebuggerCommand.CMD_STEP_INTO_COROUTINE
|| raw_reason == AbstractDebuggerCommand.CMD_STEP_INTO_MY_CODE
|| raw_reason == AbstractDebuggerCommand.CMD_SMART_STEP_INTO
|| raw_reason == AbstractDebuggerCommand.CMD_STEP_CAUGHT_EXCEPTION) {
resumeReason = DebugEvent.STEP_INTO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public void stepInto() throws DebugException {

public void stepIntoTarget(PyEdit pyEdit, int line, String selectedWord, SmartStepIntoVariant target)
throws DebugException {
thread.stepIntoTarget(pyEdit, line, selectedWord, target);
thread.stepIntoTarget(pyEdit, line, "<unused>", target);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,27 @@ public class SmartStepIntoVariant {
public final int offset;
public final int callOrder;
public final int childOffset;
public final int endlineno;
public final int startcol;
public final int endcol;

public SmartStepIntoVariant(AbstractDebugTarget target, String name, boolean isVisited, int line, int offset,
int childOffset, int callOrder) {
int childOffset, int callOrder, int endlineno, int startcol, int endcol) {
this.target = target;
this.name = name;
this.isVisited = isVisited;
this.line = line;
this.offset = offset;
this.childOffset = childOffset;
this.callOrder = callOrder;
this.endlineno = endlineno;
this.startcol = startcol;
this.endcol = endcol;
}

@Override
public String toString() {
return "Step In: " + this.name + " line: " + this.line;
return "Step In: " + this.name + " line: " + this.line + " col: " + this.startcol + " endline: "
+ this.endlineno + " endcol: " + this.endcol;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,28 @@ public void startElement(String uri, String localName, String qName, Attributes
childOffset = Integer.parseInt(value);
}
int callOrder = Integer.parseInt(attributes.getValue("callOrder"));
int endlineno = -1;
int startcol = -1;
int endcol = -1;

// These are optional
try {
endlineno = Integer.parseInt(attributes.getValue("endlineno"));
} catch (Exception e) {
}
try {
final String startcolVal = attributes.getValue("startcol");
startcol = Integer.parseInt(startcolVal);
} catch (Exception e) {
}
try {
endcol = Integer.parseInt(attributes.getValue("endcol"));
} catch (Exception e) {
}

variants.add(
new SmartStepIntoVariant(target, name, isVisited, line - 1, offset, childOffset, callOrder));
new SmartStepIntoVariant(target, name, isVisited, line - 1, offset, childOffset, callOrder,
endlineno - 1, startcol, endcol));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public abstract class AbstractDebuggerCommand {
static public final int CMD_SIGNATURE_CALL_TRACE = 130;

static public final int CMD_SET_PY_EXCEPTION = 131;
static public final int CMD_STEP_INTO_MY_CODE = 144;
static public final int CMD_SET_PY_EXCEPTION_JSON = 161;
static public final int CMD_SET_PATH_MAPPING_JSON = 162;
static public final int CMD_GET_FILE_CONTENTS = 132;
Expand All @@ -90,8 +91,12 @@ public abstract class AbstractDebuggerCommand {
static public final int CMD_INPUT_REQUESTED = 147;
static public final int CMD_PROCESS_CREATED = 149;

static public final int CMD_STEP_OVER_MY_CODE = 159;
static public final int CMD_STEP_RETURN_MY_CODE = 160;

static public final int CMD_GET_SMART_STEP_INTO_VARIANTS = 163;

static public final int CMD_STEP_INTO_COROUTINE = 206;
static public final int CMD_LOAD_SOURCE_FROM_FRAME_ID = 207;

static public final int CMD_ERROR = 901;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ protected void createFieldEditors() {
c.setToolTipText("When this option is turned on, the debugger will be able to debug GEvent programs.");
addField(editor);

editor = new BooleanFieldEditor(PyDevEditorPreferences.DEBUG_JUST_MY_CODE,
"Debug just my code (i.e.: only source code in source folders in PyDev)?",
BooleanFieldEditor.SEPARATE_LABEL,
p);
c = editor.getDescriptionControl(p);
c.setToolTipText(
"When this option is turned on, the debugger will ignore any files which are not under a PyDev source folder.");
addField(editor);

ComboFieldEditor comboEditor = new ComboFieldEditor(PyDevEditorPreferences.QT_THREADS_DEBUG_MODE, "Qt Threads:",
PyDevEditorPreferences.ENTRIES_VALUES_QT_THREADS_DEBUG_MODE, p);
Label labelControl = comboEditor.getLabelControl(p);
Expand Down Expand Up @@ -136,6 +145,11 @@ public static boolean getGeventDebugging() {
PyDevEditorPreferences.DEFAULT_GEVENT_DEBUGGING);
}

public static boolean getJustMyCode() {
return PydevPrefs.getEclipsePreferences().getBoolean(PyDevEditorPreferences.DEBUG_JUST_MY_CODE,
PyDevEditorPreferences.DEFAULT_DEBUG_JUST_MY_CODE);
}

public static String getQtThreadsDebugMode() {
return PydevPrefs.getEclipsePreferences().get(PyDevEditorPreferences.QT_THREADS_DEBUG_MODE,
PyDevEditorPreferences.DEFAULT_QT_THREADS_DEBUG_MODE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,12 @@ public PythonRunnerConfig(ILaunchConfiguration conf, String mode, String run,
}
envp = StringUtils.addString(envp,
"IDE_PROJECT_ROOTS=" + StringUtils.join(File.pathSeparator, ideProjectRoots));

if (DebugPrefsPage.getJustMyCode()) {
envp = StringUtils.addString(envp,
"PYDEVD_FILTER_LIBRARIES=1");
}

envp = StringUtils.addString(envp,
"PYDEVD_SHOW_COMPILE_CYTHON_COMMAND_LINE=True");
this.pythonpathUsed = p;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ public class PyDevEditorPreferences {
public static final String GEVENT_DEBUGGING = "GEVENT_DEBUGGING";
public static final boolean DEFAULT_GEVENT_DEBUGGING = false;

public static final String DEBUG_JUST_MY_CODE = "DEBUG_JUST_MY_CODE";
public static final boolean DEFAULT_DEBUG_JUST_MY_CODE = false;

//font
public static final String DECORATOR_STYLE = "DECORATOR_STYLE";
public static final int DEFAULT_DECORATOR_STYLE = SWT.ITALIC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public void initializeDefaultPreferences() {
node.putBoolean(PyDevEditorPreferences.KILL_SUBPROCESSES_WHEN_TERMINATING_PROCESS,
PyDevEditorPreferences.DEFAULT_KILL_SUBPROCESSES_WHEN_TERMINATING_PROCESS);
node.putBoolean(PyDevEditorPreferences.GEVENT_DEBUGGING, PyDevEditorPreferences.DEFAULT_GEVENT_DEBUGGING);
node.putBoolean(PyDevEditorPreferences.DEBUG_JUST_MY_CODE, PyDevEditorPreferences.DEFAULT_DEBUG_JUST_MY_CODE);
node.putBoolean(PyDevEditorPreferences.TRACE_DJANGO_TEMPLATE_RENDER_EXCEPTIONS,
PyDevEditorPreferences.DEFAULT_TRACE_DJANGO_TEMPLATE_RENDER_EXCEPTIONS);
node.putBoolean(PyDevEditorPreferences.TRACE_JINJA2_TEMPLATE_RENDER_EXCEPTIONS,
Expand Down

0 comments on commit 5f8e6fa

Please sign in to comment.