Skip to content

Commit

Permalink
fix(gui): use new RSTA line number formatter API to show source lines
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Feb 16, 2024
1 parent 603ea39 commit 5c0c1da
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 319 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.fife.ui.rsyntaxtextarea.TokenTypes;
import org.fife.ui.rtextarea.SearchContext;
import org.fife.ui.rtextarea.SearchEngine;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -108,7 +107,6 @@ public AbstractCodeArea(ContentPanel contentPanel, JNode node) {
private void applyEditableProperties(JNode node) {
boolean editable = node.isEditable();
setEditable(editable);
setCodeFoldingEnabled(editable);
if (editable) {
setCloseCurlyBraces(true);
setCloseMarkupTags(true);
Expand Down Expand Up @@ -337,7 +335,7 @@ public static boolean isWordToken(@Nullable Token token) {
}
}

public abstract @NotNull ICodeInfo getCodeInfo();
public abstract ICodeInfo getCodeInfo();

/**
* Implement in this method the code that loads and sets the content to be displayed
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/java/jadx/gui/ui/codearea/CodeArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public final class CodeArea extends AbstractCodeArea {
}

setHyperlinksEnabled(true);
setCodeFoldingEnabled(true);
setLinkScanningMask(InputEvent.CTRL_DOWN_MASK);
CodeLinkGenerator codeLinkGenerator = new CodeLinkGenerator(this);
setLinkGenerator(codeLinkGenerator);
Expand Down
74 changes: 45 additions & 29 deletions jadx-gui/src/main/java/jadx/gui/ui/codearea/CodePanel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jadx.gui.ui.codearea;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
Expand All @@ -21,10 +22,9 @@
import javax.swing.border.EmptyBorder;
import javax.swing.event.PopupMenuEvent;

import org.fife.ui.rtextarea.Gutter;
import org.fife.ui.rtextarea.LineNumberFormatter;
import org.fife.ui.rtextarea.LineNumberList;
import org.fife.ui.rtextarea.RTextScrollPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jadx.api.ICodeInfo;
import jadx.core.utils.StringUtils;
Expand All @@ -36,28 +36,32 @@
import jadx.gui.utils.DefaultPopupMenuListener;
import jadx.gui.utils.NLS;
import jadx.gui.utils.UiUtils;
import jadx.gui.utils.ui.MousePressedHandler;

/**
* A panel combining a {@link SearchBar and a scollable {@link CodeArea}
*/
public class CodePanel extends JPanel {
private static final Logger LOG = LoggerFactory.getLogger(CodePanel.class);
private static final long serialVersionUID = 1117721869391885865L;

private final SearchBar searchBar;
private final AbstractCodeArea codeArea;
private final JScrollPane codeScrollPane;
private final RTextScrollPane codeScrollPane;

private boolean useSourceLines;

public CodePanel(AbstractCodeArea codeArea) {
this.codeArea = codeArea;
this.searchBar = new SearchBar(codeArea);
this.codeScrollPane = buildCodeScrollPane(codeArea);
this.codeScrollPane = new RTextScrollPane(codeArea);

setLayout(new BorderLayout());
setBorder(new EmptyBorder(0, 0, 0, 0));
add(searchBar, BorderLayout.NORTH);
add(codeScrollPane, BorderLayout.CENTER);

initLinesModeSwitch();

KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_F, UiUtils.ctrlButton());
UiUtils.addKeyBinding(codeArea, key, "SearchAction", new AbstractAction() {
private static final long serialVersionUID = 71338030532869694L;
Expand Down Expand Up @@ -118,17 +122,26 @@ public void load() {
initLineNumbers();
}

private JScrollPane buildCodeScrollPane(AbstractCodeArea codeArea) {
if (codeArea instanceof SmaliArea) {
return new RTextScrollPane(codeArea);
private synchronized void initLineNumbers() {
codeScrollPane.getGutter().setLineNumberFont(getSettings().getFont());
LineNumbersMode mode = getLineNumbersMode();
if (mode == LineNumbersMode.DISABLE) {
codeScrollPane.setLineNumbersEnabled(false);
return;
}
return new JScrollPane(codeArea);
useSourceLines = mode == LineNumbersMode.DEBUG;
applyLineFormatter();
codeScrollPane.setLineNumbersEnabled(true);
}

private void initLineNumbers() {
if (codeArea instanceof SmaliArea) {
return;
}
private synchronized void applyLineFormatter() {
LineNumberFormatter linesFormatter = useSourceLines
? new SourceLineFormatter(codeArea.getCodeInfo())
: LineNumberList.DEFAULT_LINE_NUMBER_FORMATTER;
codeScrollPane.getGutter().setLineNumberFormatter(linesFormatter);
}

private LineNumbersMode getLineNumbersMode() {
LineNumbersMode mode = getSettings().getLineNumbersMode();
boolean canShowDebugLines = canShowDebugLines();
if (mode == LineNumbersMode.AUTO) {
Expand All @@ -137,24 +150,13 @@ private void initLineNumbers() {
// nothing to show => hide lines view
mode = LineNumbersMode.DISABLE;
}
switch (mode) {
case DISABLE:
codeScrollPane.setRowHeaderView(null);
break;
case NORMAL:
Gutter gutter = new Gutter(codeArea);
gutter.setLineNumberFont(getSettings().getFont());
codeScrollPane.setRowHeaderView(gutter);
break;
case DEBUG:
LineNumbers jadxGutter = new LineNumbers(codeArea);
jadxGutter.setUseSourceLines(true);
codeScrollPane.setRowHeaderView(jadxGutter);
break;
}
return mode;
}

private boolean canShowDebugLines() {
if (codeArea instanceof SmaliArea) {
return false;
}
ICodeInfo codeInfo = codeArea.getCodeInfo();
if (!codeInfo.hasMetadata()) {
return false;
Expand All @@ -167,6 +169,20 @@ private boolean canShowDebugLines() {
return uniqueDebugLines.size() > 3;
}

private void initLinesModeSwitch() {
if (canShowDebugLines()) {
MousePressedHandler lineModeSwitch = new MousePressedHandler(ev -> {
useSourceLines = !useSourceLines;
applyLineFormatter();
});
for (Component gutterComp : codeScrollPane.getGutter().getComponents()) {
if (gutterComp instanceof LineNumberList) {
gutterComp.addMouseListener(lineModeSwitch);
}
}
}
}

public SearchBar getSearchBar() {
return searchBar;
}
Expand Down
Loading

0 comments on commit 5c0c1da

Please sign in to comment.