未验证 提交 2bdde6a5 编写于 作者: L LBJ-the-GOAT 提交者: GitHub

fix(gui): fix variable usage & caret position after rename (#1099) (PR #1103)

Co-authored-by: tobias <tobias.hotmail.com>
上级 c61cb80a
......@@ -448,7 +448,7 @@ public final class JadxDecompiler implements Closeable {
}
if (obj instanceof VariableNode) {
VariableNode varNode = (VariableNode) obj;
return new JavaVariable(getJavaClassByNode(varNode.getClassNode()), varNode);
return new JavaVariable(getJavaClassByNode(varNode.getClassNode().getTopParentClass()), varNode);
}
throw new JadxRuntimeException("Unexpected node type: " + obj);
}
......
......@@ -55,6 +55,9 @@ public class JavaVariable implements JavaNode {
@Override
public boolean equals(Object obj) {
return node.equals(obj);
if (obj instanceof JavaVariable) {
return node.equals(((JavaVariable) obj).getVariableNode());
}
return false;
}
}
......@@ -92,7 +92,6 @@ public class InsnGen {
if (codeVar != null) {
VariableNode node = mth.getVariable(codeVar.getIndex());
if (node != null) {
node.useVar(code, codeVar);
code.attachAnnotation(node);
}
}
......@@ -653,7 +652,6 @@ public class InsnGen {
if (insn.isSuper()) {
code.add("super");
} else if (insn.isThis()) {
code.attachAnnotation(mth.getParentClass());
code.add("this");
} else {
code.add("new ");
......
......@@ -110,15 +110,6 @@ public class MethodNode extends NotificationAttrNode implements IMethodDetails,
return null;
}
public VariableNode getVariable(int index, VarKind varType) {
for (VariableNode variable : variables) {
if (variable.getVarKind() == varType && variable.getIndex() == index) {
return variable;
}
}
return null;
}
public VariableNode declareVar(VisibleVar var, NameGen nameGen, VarKind varKind) {
if (var instanceof CodeVar) {
if (((CodeVar) var).isThis()) {
......@@ -127,14 +118,11 @@ public class MethodNode extends NotificationAttrNode implements IMethodDetails,
}
VariableNode varNode;
int index = var.getIndex();
if (index == -1) {
if (index > -1) {
varNode = getVariable(var.getIndex());
} else {
index = variables.size();
var.setIndex(index);
varNode = null;
} else {
varNode = getVariable(var.getIndex());
}
if (varNode == null) {
String name = mthInfo.getVariableName(VariableNode.makeVarIndex(index, varKind));
if (name != null) {
var.setName(name); // set name with user renamed previously.
......
package jadx.core.dex.nodes;
import jadx.core.codegen.CodeWriter;
import jadx.core.dex.attributes.nodes.LineAttrNode;
import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.instructions.args.CodeVar;
import jadx.core.utils.exceptions.JadxRuntimeException;
public class VariableNode extends LineAttrNode {
......@@ -56,17 +54,6 @@ public class VariableNode extends LineAttrNode {
return index;
}
public void addUsage(int line, int offset, int codeOffset) {
}
public void useVar(CodeWriter code, CodeVar codeVar) {
if (!codeVar.isThis()) { // TODO: add usage
// IdentifierVisitor.VariableNode node = codeVar.getVariableNode();
// node.addUsage(code.getLine(), code.getOffset(), code.bufLength());
// code.attachAnnotation(node);
}
}
public String getRenameKey() {
return mth.getMethodInfo().getRawFullId() + VAR_SEPARATOR + makeVarIndex(index, varKind);
}
......
......@@ -12,6 +12,9 @@ import javax.swing.JPopupMenu.Separator;
import javax.swing.border.EmptyBorder;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.text.BadLocationException;
import org.fife.ui.rsyntaxtextarea.Token;
import jadx.api.ICodeInfo;
import jadx.core.utils.StringUtils;
......@@ -146,12 +149,67 @@ public class CodePanel extends JPanel {
}
public void refresh() {
JViewport viewport = getCodeScrollPane().getViewport();
Point viewPosition = viewport.getViewPosition();
int line;
int lineCount;
int tokenIndex;
int pos = codeArea.getCaretPosition();
try {
// after rename the change of document is undetectable, so
// use Token offset to calculate the new caret position.
line = codeArea.getLineOfOffset(pos);
Token token = codeArea.getTokenListForLine(line);
tokenIndex = getTokenIndexByOffset(token, pos);
} catch (BadLocationException e) {
e.printStackTrace();
tokenIndex = 0;
line = codeArea.getLineCount() - 1;
}
lineCount = codeArea.getLineCount();
codeArea.refresh();
initLineNumbers();
int lineDiff = codeArea.getLineCount() - lineCount;
if (lineDiff > 0) {
lineDiff--;
} else if (lineDiff < 0) {
lineDiff++;
}
Token token = codeArea.getTokenListForLine(line + lineDiff);
int newPos = getOffsetOfTokenByIndex(tokenIndex, token);
SwingUtilities.invokeLater(() -> {
viewport.setViewPosition(viewPosition);
if (newPos != -1) {
codeArea.scrollToPos(newPos);
} else {
codeArea.scrollToLine(codeArea.getLineCount() - 1);
}
});
}
private int getTokenIndexByOffset(Token token, int offset) {
if (token != null) {
int index = 1;
while (token.getEndOffset() < offset) {
token = token.getNextToken();
if (token == null) {
index = 0;
break;
}
index++;
}
return index;
}
return -1;
}
private int getOffsetOfTokenByIndex(int index, Token token) {
if (token != null) {
for (int i = 0; i < index; i++) {
token = token.getNextToken();
if (token == null) {
return -1;
}
}
return token.getOffset();
}
return -1;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册