Better variable display in debugger

This commit is contained in:
konsoletyper 2014-10-05 17:45:09 +04:00
parent 0c4aeec667
commit 2d583dc7f4
12 changed files with 111 additions and 23 deletions

View File

@ -0,0 +1,41 @@
package org.teavm.eclipse.debugger;
import java.util.Comparator;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
abstract class PropertyNameComparator<T> implements Comparator<T> {
abstract String getName(T value);
@Override
public int compare(T o1, T o2) {
String s1 = getName(o1);
String s2 = getName(o2);
boolean n1 = isNumber(s1);
boolean n2 = isNumber(s2);
if (n1 && n2) {
return Integer.compare(Integer.parseInt(s1), Integer.parseInt(s2));
} else if (n1) {
return -1;
} else if (n2) {
return 1;
} else {
return s1.compareTo(s2);
}
}
private boolean isNumber(String str) {
if (str.length() > 9) {
return false;
}
for (int i = 0; i < str.length(); ++i) {
char c = str.charAt(i);
if (c < '0' || c > '9') {
return false;
}
}
return true;
}
}

View File

@ -27,7 +27,7 @@ public class TeaVMJSScope extends TeaVMVariable {
private JavaScriptValue value; private JavaScriptValue value;
public TeaVMJSScope(TeaVMDebugTarget debugTarget, String name, JavaScriptValue value) { public TeaVMJSScope(TeaVMDebugTarget debugTarget, String name, JavaScriptValue value) {
super(debugTarget, new TeaVMJSValue(debugTarget, value)); super(name, debugTarget, new TeaVMJSValue(name, debugTarget, value));
this.name = name; this.name = name;
} }

View File

@ -33,7 +33,7 @@ public class TeaVMJSStackFrame extends TeaVMStackFrame {
super(thread); super(thread);
this.callFrame = callFrame; this.callFrame = callFrame;
this.jsDebugger = jsDebugger; this.jsDebugger = jsDebugger;
this.variablesHolder = new TeaVMJSVariablesHolder(thread.debugTarget, callFrame.getVariables().values(), this.variablesHolder = new TeaVMJSVariablesHolder("", thread.debugTarget, callFrame.getVariables().values(),
callFrame.getThisVariable(), callFrame.getClosureVariable()); callFrame.getThisVariable(), callFrame.getClosureVariable());
} }

View File

@ -26,8 +26,9 @@ public class TeaVMJSValue extends TeaVMValue {
private JavaScriptValue jsValue; private JavaScriptValue jsValue;
private boolean innerStructure; private boolean innerStructure;
public TeaVMJSValue(TeaVMDebugTarget debugTarget, JavaScriptValue teavmValue) { public TeaVMJSValue(String id, TeaVMDebugTarget debugTarget, JavaScriptValue teavmValue) {
super(debugTarget, new TeaVMJSVariablesHolder(debugTarget, teavmValue.getProperties().values(), null, null)); super(id, debugTarget, new TeaVMJSVariablesHolder(id, debugTarget, teavmValue.getProperties().values(),
null, null));
this.jsValue = teavmValue; this.jsValue = teavmValue;
this.innerStructure = teavmValue.hasInnerStructure(); this.innerStructure = teavmValue.hasInnerStructure();
} }

View File

@ -25,8 +25,8 @@ import org.teavm.debugging.javascript.JavaScriptVariable;
public class TeaVMJSVariable extends TeaVMVariable { public class TeaVMJSVariable extends TeaVMVariable {
private JavaScriptVariable var; private JavaScriptVariable var;
public TeaVMJSVariable(TeaVMDebugTarget debugTarget, JavaScriptVariable var) { public TeaVMJSVariable(String id, TeaVMDebugTarget debugTarget, JavaScriptVariable var) {
super(debugTarget, new TeaVMJSValue(debugTarget, var.getValue())); super(id, debugTarget, new TeaVMJSValue(id, debugTarget, var.getValue()));
this.var = var; this.var = var;
} }

View File

@ -24,13 +24,16 @@ import org.teavm.debugging.javascript.JavaScriptVariable;
* @author Alexey Andreev <konsoletyper@gmail.com> * @author Alexey Andreev <konsoletyper@gmail.com>
*/ */
public class TeaVMJSVariablesHolder extends TeaVMVariablesHolder { public class TeaVMJSVariablesHolder extends TeaVMVariablesHolder {
private String idPrefix;
private TeaVMDebugTarget debugTarget; private TeaVMDebugTarget debugTarget;
private Collection<JavaScriptVariable> teavmVariables; private Collection<JavaScriptVariable> teavmVariables;
private JavaScriptValue thisScope; private JavaScriptValue thisScope;
private JavaScriptValue closureScope; private JavaScriptValue closureScope;
public TeaVMJSVariablesHolder(TeaVMDebugTarget debugTarget, Collection<JavaScriptVariable> teavmVariables, public TeaVMJSVariablesHolder(String idPrefix, TeaVMDebugTarget debugTarget,
Collection<JavaScriptVariable> teavmVariables,
JavaScriptValue thisScope, JavaScriptValue closureScope) { JavaScriptValue thisScope, JavaScriptValue closureScope) {
this.idPrefix = idPrefix;
this.debugTarget = debugTarget; this.debugTarget = debugTarget;
this.teavmVariables = teavmVariables; this.teavmVariables = teavmVariables;
this.thisScope = thisScope; this.thisScope = thisScope;
@ -47,13 +50,14 @@ public class TeaVMJSVariablesHolder extends TeaVMVariablesHolder {
variables.add(new TeaVMJSScope(debugTarget, "<closure>", closureScope)); variables.add(new TeaVMJSScope(debugTarget, "<closure>", closureScope));
} }
List<JavaScriptVariable> teavmVarList = new ArrayList<>(teavmVariables); List<JavaScriptVariable> teavmVarList = new ArrayList<>(teavmVariables);
Collections.sort(teavmVarList, new Comparator<JavaScriptVariable>() { Collections.sort(teavmVarList, new PropertyNameComparator<JavaScriptVariable>() {
@Override public int compare(JavaScriptVariable o1, JavaScriptVariable o2) { @Override String getName(JavaScriptVariable value) {
return o1.getName().compareTo(o2.getName()); return value.getName();
} }
}); });
for (int i = 0; i < teavmVarList.size(); ++i) { for (int i = 0; i < teavmVarList.size(); ++i) {
variables.add(new TeaVMJSVariable(debugTarget, teavmVarList.get(i))); JavaScriptVariable var = teavmVarList.get(i);
variables.add(new TeaVMJSVariable(idPrefix + "." + var.getName(), debugTarget, var));
} }
return variables.toArray(new TeaVMVariable[0]); return variables.toArray(new TeaVMVariable[0]);
} }

View File

@ -33,7 +33,7 @@ public class TeaVMJavaStackFrame extends TeaVMStackFrame {
super(thread); super(thread);
this.callFrame = callFrame; this.callFrame = callFrame;
this.teavmDebugger = teavmDebugger; this.teavmDebugger = teavmDebugger;
this.variablesHolder = new TeaVMJavaVariablesHolder(thread.debugTarget, callFrame.getVariables().values()); this.variablesHolder = new TeaVMJavaVariablesHolder("", thread.debugTarget, callFrame.getVariables().values());
} }
public CallFrame getCallFrame() { public CallFrame getCallFrame() {

View File

@ -26,8 +26,8 @@ public class TeaVMJavaValue extends TeaVMValue {
private Value teavmValue; private Value teavmValue;
private boolean innerStructure; private boolean innerStructure;
public TeaVMJavaValue(TeaVMDebugTarget debugTarget, Value teavmValue) { public TeaVMJavaValue(String id, TeaVMDebugTarget debugTarget, Value teavmValue) {
super(debugTarget, new TeaVMJavaVariablesHolder(debugTarget, teavmValue.getProperties().values())); super(id, debugTarget, new TeaVMJavaVariablesHolder(id, debugTarget, teavmValue.getProperties().values()));
this.teavmValue = teavmValue; this.teavmValue = teavmValue;
this.innerStructure = teavmValue.hasInnerStructure(); this.innerStructure = teavmValue.hasInnerStructure();
} }

View File

@ -25,8 +25,8 @@ import org.teavm.debugging.Variable;
public class TeaVMJavaVariable extends TeaVMVariable { public class TeaVMJavaVariable extends TeaVMVariable {
private Variable var; private Variable var;
public TeaVMJavaVariable(TeaVMDebugTarget debugTarget, Variable var) { public TeaVMJavaVariable(String id, TeaVMDebugTarget debugTarget, Variable var) {
super(debugTarget, new TeaVMJavaValue(debugTarget, var.getValue())); super(id, debugTarget, new TeaVMJavaValue(id, debugTarget, var.getValue()));
this.var = var; this.var = var;
} }

View File

@ -23,10 +23,13 @@ import org.teavm.debugging.Variable;
* @author Alexey Andreev * @author Alexey Andreev
*/ */
public class TeaVMJavaVariablesHolder extends TeaVMVariablesHolder { public class TeaVMJavaVariablesHolder extends TeaVMVariablesHolder {
private String idPrefix;
private TeaVMDebugTarget debugTarget; private TeaVMDebugTarget debugTarget;
private Collection<Variable> teavmVariables; private Collection<Variable> teavmVariables;
public TeaVMJavaVariablesHolder(TeaVMDebugTarget debugTarget, Collection<Variable> teavmVariables) { public TeaVMJavaVariablesHolder(String idPrefix, TeaVMDebugTarget debugTarget,
Collection<Variable> teavmVariables) {
this.idPrefix = idPrefix;
this.debugTarget = debugTarget; this.debugTarget = debugTarget;
this.teavmVariables = teavmVariables; this.teavmVariables = teavmVariables;
} }
@ -35,13 +38,14 @@ public class TeaVMJavaVariablesHolder extends TeaVMVariablesHolder {
protected TeaVMVariable[] createVariables() { protected TeaVMVariable[] createVariables() {
TeaVMJavaVariable[] newVariables = new TeaVMJavaVariable[teavmVariables.size()]; TeaVMJavaVariable[] newVariables = new TeaVMJavaVariable[teavmVariables.size()];
List<Variable> teavmVarList = new ArrayList<>(teavmVariables); List<Variable> teavmVarList = new ArrayList<>(teavmVariables);
Collections.sort(teavmVarList, new Comparator<Variable>() { Collections.sort(teavmVarList, new PropertyNameComparator<Variable>() {
@Override public int compare(Variable o1, Variable o2) { @Override String getName(Variable value) {
return o1.getName().compareTo(o2.getName()); return value.getName();
} }
}); });
for (int i = 0; i < teavmVarList.size(); ++i) { for (int i = 0; i < teavmVarList.size(); ++i) {
newVariables[i] = new TeaVMJavaVariable(debugTarget, teavmVarList.get(i)); Variable var = teavmVarList.get(i);
newVariables[i] = new TeaVMJavaVariable(idPrefix + "." + var.getName(), debugTarget, var);
} }
return newVariables; return newVariables;
} }

View File

@ -24,10 +24,12 @@ import org.eclipse.debug.core.model.IVariable;
* @author Alexey Andreev <konsoletyper@gmail.com> * @author Alexey Andreev <konsoletyper@gmail.com>
*/ */
public abstract class TeaVMValue extends TeaVMDebugElement implements IValue { public abstract class TeaVMValue extends TeaVMDebugElement implements IValue {
private String id;
private TeaVMVariablesHolder variablesHolder; private TeaVMVariablesHolder variablesHolder;
public TeaVMValue(TeaVMDebugTarget debugTarget, TeaVMVariablesHolder variablesHolder) { public TeaVMValue(String id, TeaVMDebugTarget debugTarget, TeaVMVariablesHolder variablesHolder) {
super(debugTarget); super(debugTarget);
this.id = id;
this.variablesHolder = variablesHolder; this.variablesHolder = variablesHolder;
} }
@ -42,4 +44,21 @@ public abstract class TeaVMValue extends TeaVMDebugElement implements IValue {
} }
public abstract String getDescription(); public abstract String getDescription();
@Override
public int hashCode() {
return 31 * id.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof TeaVMValue)) {
return false;
}
TeaVMValue other = (TeaVMValue)obj;
return id.equals(other.id);
}
} }

View File

@ -26,10 +26,12 @@ import org.teavm.eclipse.TeaVMEclipsePlugin;
* @author Alexey Andreev <konsoletyper@gmail.com> * @author Alexey Andreev <konsoletyper@gmail.com>
*/ */
public abstract class TeaVMVariable extends TeaVMDebugElement implements IVariable { public abstract class TeaVMVariable extends TeaVMDebugElement implements IVariable {
private String id;
private TeaVMValue value; private TeaVMValue value;
public TeaVMVariable(TeaVMDebugTarget debugTarget, TeaVMValue value) { public TeaVMVariable(String id, TeaVMDebugTarget debugTarget, TeaVMValue value) {
super(debugTarget); super(debugTarget);
this.id = id;
this.value = value; this.value = value;
} }
@ -67,4 +69,21 @@ public abstract class TeaVMVariable extends TeaVMDebugElement implements IVariab
public boolean hasValueChanged() throws DebugException { public boolean hasValueChanged() throws DebugException {
return false; return false;
} }
@Override
public int hashCode() {
return 31 * id.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof TeaVMVariable)) {
return false;
}
TeaVMVariable other = (TeaVMVariable)obj;
return id.equals(other.id);
}
} }