From 2d583dc7f46d7430086487f2ec59a5fc2309e104 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Sun, 5 Oct 2014 17:45:09 +0400 Subject: [PATCH] Better variable display in debugger --- .../debugger/PropertyNameComparator.java | 41 +++++++++++++++++++ .../teavm/eclipse/debugger/TeaVMJSScope.java | 2 +- .../eclipse/debugger/TeaVMJSStackFrame.java | 2 +- .../teavm/eclipse/debugger/TeaVMJSValue.java | 5 ++- .../eclipse/debugger/TeaVMJSVariable.java | 4 +- .../debugger/TeaVMJSVariablesHolder.java | 14 ++++--- .../eclipse/debugger/TeaVMJavaStackFrame.java | 2 +- .../eclipse/debugger/TeaVMJavaValue.java | 4 +- .../eclipse/debugger/TeaVMJavaVariable.java | 4 +- .../debugger/TeaVMJavaVariablesHolder.java | 14 ++++--- .../teavm/eclipse/debugger/TeaVMValue.java | 21 +++++++++- .../teavm/eclipse/debugger/TeaVMVariable.java | 21 +++++++++- 12 files changed, 111 insertions(+), 23 deletions(-) create mode 100644 teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/PropertyNameComparator.java diff --git a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/PropertyNameComparator.java b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/PropertyNameComparator.java new file mode 100644 index 000000000..5958a359e --- /dev/null +++ b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/PropertyNameComparator.java @@ -0,0 +1,41 @@ +package org.teavm.eclipse.debugger; + +import java.util.Comparator; + +/** + * + * @author Alexey Andreev + */ +abstract class PropertyNameComparator implements Comparator { + 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; + } +} diff --git a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSScope.java b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSScope.java index 89e9e03cd..5739a82aa 100644 --- a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSScope.java +++ b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSScope.java @@ -27,7 +27,7 @@ public class TeaVMJSScope extends TeaVMVariable { private 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; } diff --git a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSStackFrame.java b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSStackFrame.java index b628d74bb..0f5ebd245 100644 --- a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSStackFrame.java +++ b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSStackFrame.java @@ -33,7 +33,7 @@ public class TeaVMJSStackFrame extends TeaVMStackFrame { super(thread); this.callFrame = callFrame; 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()); } diff --git a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSValue.java b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSValue.java index 465580a1a..5c9ddd66a 100644 --- a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSValue.java +++ b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSValue.java @@ -26,8 +26,9 @@ public class TeaVMJSValue extends TeaVMValue { private JavaScriptValue jsValue; private boolean innerStructure; - public TeaVMJSValue(TeaVMDebugTarget debugTarget, JavaScriptValue teavmValue) { - super(debugTarget, new TeaVMJSVariablesHolder(debugTarget, teavmValue.getProperties().values(), null, null)); + public TeaVMJSValue(String id, TeaVMDebugTarget debugTarget, JavaScriptValue teavmValue) { + super(id, debugTarget, new TeaVMJSVariablesHolder(id, debugTarget, teavmValue.getProperties().values(), + null, null)); this.jsValue = teavmValue; this.innerStructure = teavmValue.hasInnerStructure(); } diff --git a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSVariable.java b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSVariable.java index 047c9fabb..e1298fb8e 100644 --- a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSVariable.java +++ b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSVariable.java @@ -25,8 +25,8 @@ import org.teavm.debugging.javascript.JavaScriptVariable; public class TeaVMJSVariable extends TeaVMVariable { private JavaScriptVariable var; - public TeaVMJSVariable(TeaVMDebugTarget debugTarget, JavaScriptVariable var) { - super(debugTarget, new TeaVMJSValue(debugTarget, var.getValue())); + public TeaVMJSVariable(String id, TeaVMDebugTarget debugTarget, JavaScriptVariable var) { + super(id, debugTarget, new TeaVMJSValue(id, debugTarget, var.getValue())); this.var = var; } diff --git a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSVariablesHolder.java b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSVariablesHolder.java index 63e082110..013286091 100644 --- a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSVariablesHolder.java +++ b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJSVariablesHolder.java @@ -24,13 +24,16 @@ import org.teavm.debugging.javascript.JavaScriptVariable; * @author Alexey Andreev */ public class TeaVMJSVariablesHolder extends TeaVMVariablesHolder { + private String idPrefix; private TeaVMDebugTarget debugTarget; private Collection teavmVariables; private JavaScriptValue thisScope; private JavaScriptValue closureScope; - public TeaVMJSVariablesHolder(TeaVMDebugTarget debugTarget, Collection teavmVariables, + public TeaVMJSVariablesHolder(String idPrefix, TeaVMDebugTarget debugTarget, + Collection teavmVariables, JavaScriptValue thisScope, JavaScriptValue closureScope) { + this.idPrefix = idPrefix; this.debugTarget = debugTarget; this.teavmVariables = teavmVariables; this.thisScope = thisScope; @@ -47,13 +50,14 @@ public class TeaVMJSVariablesHolder extends TeaVMVariablesHolder { variables.add(new TeaVMJSScope(debugTarget, "", closureScope)); } List teavmVarList = new ArrayList<>(teavmVariables); - Collections.sort(teavmVarList, new Comparator() { - @Override public int compare(JavaScriptVariable o1, JavaScriptVariable o2) { - return o1.getName().compareTo(o2.getName()); + Collections.sort(teavmVarList, new PropertyNameComparator() { + @Override String getName(JavaScriptVariable value) { + return value.getName(); } }); 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]); } diff --git a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJavaStackFrame.java b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJavaStackFrame.java index df10e9bff..325f09a9e 100644 --- a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJavaStackFrame.java +++ b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJavaStackFrame.java @@ -33,7 +33,7 @@ public class TeaVMJavaStackFrame extends TeaVMStackFrame { super(thread); this.callFrame = callFrame; this.teavmDebugger = teavmDebugger; - this.variablesHolder = new TeaVMJavaVariablesHolder(thread.debugTarget, callFrame.getVariables().values()); + this.variablesHolder = new TeaVMJavaVariablesHolder("", thread.debugTarget, callFrame.getVariables().values()); } public CallFrame getCallFrame() { diff --git a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJavaValue.java b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJavaValue.java index b056665c7..9e3e6c337 100644 --- a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJavaValue.java +++ b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJavaValue.java @@ -26,8 +26,8 @@ public class TeaVMJavaValue extends TeaVMValue { private Value teavmValue; private boolean innerStructure; - public TeaVMJavaValue(TeaVMDebugTarget debugTarget, Value teavmValue) { - super(debugTarget, new TeaVMJavaVariablesHolder(debugTarget, teavmValue.getProperties().values())); + public TeaVMJavaValue(String id, TeaVMDebugTarget debugTarget, Value teavmValue) { + super(id, debugTarget, new TeaVMJavaVariablesHolder(id, debugTarget, teavmValue.getProperties().values())); this.teavmValue = teavmValue; this.innerStructure = teavmValue.hasInnerStructure(); } diff --git a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJavaVariable.java b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJavaVariable.java index 832a45e15..a8863ae6f 100644 --- a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJavaVariable.java +++ b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJavaVariable.java @@ -25,8 +25,8 @@ import org.teavm.debugging.Variable; public class TeaVMJavaVariable extends TeaVMVariable { private Variable var; - public TeaVMJavaVariable(TeaVMDebugTarget debugTarget, Variable var) { - super(debugTarget, new TeaVMJavaValue(debugTarget, var.getValue())); + public TeaVMJavaVariable(String id, TeaVMDebugTarget debugTarget, Variable var) { + super(id, debugTarget, new TeaVMJavaValue(id, debugTarget, var.getValue())); this.var = var; } diff --git a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJavaVariablesHolder.java b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJavaVariablesHolder.java index de132c179..949936fb1 100644 --- a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJavaVariablesHolder.java +++ b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMJavaVariablesHolder.java @@ -23,10 +23,13 @@ import org.teavm.debugging.Variable; * @author Alexey Andreev */ public class TeaVMJavaVariablesHolder extends TeaVMVariablesHolder { + private String idPrefix; private TeaVMDebugTarget debugTarget; private Collection teavmVariables; - public TeaVMJavaVariablesHolder(TeaVMDebugTarget debugTarget, Collection teavmVariables) { + public TeaVMJavaVariablesHolder(String idPrefix, TeaVMDebugTarget debugTarget, + Collection teavmVariables) { + this.idPrefix = idPrefix; this.debugTarget = debugTarget; this.teavmVariables = teavmVariables; } @@ -35,13 +38,14 @@ public class TeaVMJavaVariablesHolder extends TeaVMVariablesHolder { protected TeaVMVariable[] createVariables() { TeaVMJavaVariable[] newVariables = new TeaVMJavaVariable[teavmVariables.size()]; List teavmVarList = new ArrayList<>(teavmVariables); - Collections.sort(teavmVarList, new Comparator() { - @Override public int compare(Variable o1, Variable o2) { - return o1.getName().compareTo(o2.getName()); + Collections.sort(teavmVarList, new PropertyNameComparator() { + @Override String getName(Variable value) { + return value.getName(); } }); 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; } diff --git a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMValue.java b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMValue.java index 4ae44b1ee..94f421b3d 100644 --- a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMValue.java +++ b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMValue.java @@ -24,10 +24,12 @@ import org.eclipse.debug.core.model.IVariable; * @author Alexey Andreev */ public abstract class TeaVMValue extends TeaVMDebugElement implements IValue { + private String id; private TeaVMVariablesHolder variablesHolder; - public TeaVMValue(TeaVMDebugTarget debugTarget, TeaVMVariablesHolder variablesHolder) { + public TeaVMValue(String id, TeaVMDebugTarget debugTarget, TeaVMVariablesHolder variablesHolder) { super(debugTarget); + this.id = id; this.variablesHolder = variablesHolder; } @@ -42,4 +44,21 @@ public abstract class TeaVMValue extends TeaVMDebugElement implements IValue { } 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); + } } diff --git a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMVariable.java b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMVariable.java index 1ae07cc9c..afe35fa28 100644 --- a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMVariable.java +++ b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/debugger/TeaVMVariable.java @@ -26,10 +26,12 @@ import org.teavm.eclipse.TeaVMEclipsePlugin; * @author Alexey Andreev */ public abstract class TeaVMVariable extends TeaVMDebugElement implements IVariable { + private String id; private TeaVMValue value; - public TeaVMVariable(TeaVMDebugTarget debugTarget, TeaVMValue value) { + public TeaVMVariable(String id, TeaVMDebugTarget debugTarget, TeaVMValue value) { super(debugTarget); + this.id = id; this.value = value; } @@ -67,4 +69,21 @@ public abstract class TeaVMVariable extends TeaVMDebugElement implements IVariab public boolean hasValueChanged() throws DebugException { 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); + } }