feat: add origin file info (code comment for classes, tooltip in tree) (PR #1219)
* chore: make escapeHtml also replace close angle brackets * chore: if multiple files are loaded, show their path as tooltip * feat: add comment on classes that contains the dex file name it has been loaded from * fix: expected line numbers in unit test fixed * fix: delete comments from generated code as it may contain a colon * chore: comment removing wasn't able to handle Linux paths with slash
This commit is contained in:
@@ -147,6 +147,7 @@ public class ClassGen {
|
||||
|
||||
annotationGen.addForClass(clsCode);
|
||||
insertRenameInfo(clsCode, cls);
|
||||
CodeGenUtils.addInputFileInfo(clsCode, cls);
|
||||
clsCode.startLineWithNum(cls.getSourceLine()).add(af.makeString());
|
||||
if (af.isInterface()) {
|
||||
if (af.isAnnotation()) {
|
||||
|
||||
@@ -100,6 +100,15 @@ public class CodeGenUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static void addInputFileInfo(ICodeWriter code, ClassNode node) {
|
||||
if (node.getClsData() != null) {
|
||||
String inputFileName = node.getClsData().getInputFileName();
|
||||
if (inputFileName != null) {
|
||||
code.startLine("/* loaded from: ").add(inputFileName).add(" */");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static CodeVar getCodeVar(RegisterArg arg) {
|
||||
SSAVar svar = arg.getSVar();
|
||||
if (svar != null) {
|
||||
|
||||
@@ -39,7 +39,7 @@ public class TestLineNumbers2 extends IntegrationTest {
|
||||
|
||||
ClassNode cls = getClassNode(TestCls.class);
|
||||
Map<Integer, Integer> lineMapping = cls.getCode().getLineMapping();
|
||||
assertEquals("{5=17, 8=18, 11=22, 12=23, 13=24, 14=28, 16=25, 17=26, 18=28, 21=31, 22=32}",
|
||||
assertEquals("{6=17, 9=18, 12=22, 13=23, 14=24, 15=28, 17=25, 18=26, 19=28, 22=31, 23=32}",
|
||||
lineMapping.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,6 +50,10 @@ public class TestArrayForEachNegative extends IntegrationTest {
|
||||
ClassNode cls = getClassNode(TestCls.class);
|
||||
String code = cls.getCode().toString();
|
||||
|
||||
// Remove all comments - as the comment created by CodeGenUtils.addInputFileInfo
|
||||
// always contains a colon
|
||||
code = code.replaceAll("/\\*.*?\\*/", "");
|
||||
|
||||
assertThat(code, not(containsString(":")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,6 +116,10 @@ public abstract class JNode extends DefaultMutableTreeNode {
|
||||
return javaNode.getDefPos();
|
||||
}
|
||||
|
||||
public String getTooltip() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return makeString();
|
||||
|
||||
@@ -152,4 +152,21 @@ public class JRoot extends JNode {
|
||||
}
|
||||
return count + " files";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTooltip() {
|
||||
List<Path> paths = wrapper.getOpenPaths();
|
||||
int count = paths.size();
|
||||
if (count < 2) {
|
||||
return null;
|
||||
}
|
||||
// Show list of loaded files (full path)
|
||||
StringBuilder sb = new StringBuilder("<html>");
|
||||
for (Path p : paths) {
|
||||
sb.append(UiUtils.escapeHtml(p.toString()));
|
||||
sb.append("<br>");
|
||||
}
|
||||
sb.append("</html>");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ import javax.swing.JToggleButton;
|
||||
import javax.swing.JToolBar;
|
||||
import javax.swing.JTree;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.ToolTipManager;
|
||||
import javax.swing.WindowConstants;
|
||||
import javax.swing.event.MenuEvent;
|
||||
import javax.swing.event.MenuListener;
|
||||
@@ -1068,6 +1069,7 @@ public class MainWindow extends JFrame {
|
||||
DefaultMutableTreeNode treeRootNode = new DefaultMutableTreeNode(NLS.str("msg.open_file"));
|
||||
treeModel = new DefaultTreeModel(treeRootNode);
|
||||
tree = new JTree(treeModel);
|
||||
ToolTipManager.sharedInstance().registerComponent(tree);
|
||||
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
|
||||
tree.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
@@ -1094,7 +1096,11 @@ public class MainWindow extends JFrame {
|
||||
boolean isLeaf, int row, boolean focused) {
|
||||
Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, isLeaf, row, focused);
|
||||
if (value instanceof JNode) {
|
||||
setIcon(((JNode) value).getIcon());
|
||||
JNode jNode = (JNode) value;
|
||||
setIcon(jNode.getIcon());
|
||||
setToolTipText(jNode.getTooltip());
|
||||
} else {
|
||||
setToolTipText(null);
|
||||
}
|
||||
if (value instanceof JPackage) {
|
||||
setEnabled(((JPackage) value).isEnabled());
|
||||
|
||||
@@ -94,7 +94,7 @@ public class UiUtils {
|
||||
}
|
||||
|
||||
public static String escapeHtml(String str) {
|
||||
return str.replace("<", "<");
|
||||
return str.replace("<", "<").replace(">", ">");
|
||||
}
|
||||
|
||||
public static String typeStr(ArgType type) {
|
||||
|
||||
Reference in New Issue
Block a user