Added the "Go-To" action to bring the caret to the variables declaration. Also:

- BUG: Tokens with only one character would not get shown if the caret was before the character
- ADD: Made the caret visible
This commit is contained in:
Cody 2024-09-07 19:22:10 -06:00
parent a24c4c0fb6
commit 8dc70a581c

AI 샘플 코드 생성 중입니다

Loading...
3 changed files with 95 additions and 11 deletions

View File

@ -0,0 +1,78 @@
package the.bytecode.club.bytecodeviewer.gui.components.actions;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.Token;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.ClassFileContainer;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.locations.ClassFieldLocation;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.TokenUtil;
import javax.swing.*;
import javax.swing.text.Element;
import java.awt.event.ActionEvent;
/**
* Created by Bl3nd.
* Date: 9/7/2024
*/
public class GoToAction extends AbstractAction
{
private final ClassFileContainer container;
public GoToAction(ClassFileContainer classFileContainer)
{
this.container = classFileContainer;
}
@Override
public void actionPerformed(ActionEvent e)
{
RSyntaxTextArea textArea = (RSyntaxTextArea) e.getSource();
int line = textArea.getCaretLineNumber() + 1;
int column = textArea.getCaretOffsetFromLineStart();
container.fieldMembers.values().forEach(fields -> fields.forEach(field -> {
if (field.line == line && field.columnStart - 1 <= column && field.columnEnd >= column) {
Element root = textArea.getDocument().getDefaultRootElement();
ClassFieldLocation first = fields.get(0);
int startOffset = root.getElement(first.line - 1).getStartOffset() + (first.columnStart - 1);
textArea.setCaretPosition(startOffset);
}
}));
container.methodParameterMembers.values().forEach(parameters -> parameters.forEach(parameter -> {
if (parameter.line == line && parameter.columnStart - 1 <= column && parameter.columnEnd >= column) {
Element root = textArea.getDocument().getDefaultRootElement();
if (parameter.decRef.equalsIgnoreCase("declaration")) {
int startOffset = root.getElement(parameter.line - 1).getStartOffset() + (parameter.columnStart - 1);
textArea.setCaretPosition(startOffset);
} else {
String method = parameter.method;
parameters.stream().filter(classParameterLocation -> classParameterLocation.method.equals(method)).forEach(classParameterLocation -> {
if (classParameterLocation.decRef.equalsIgnoreCase("declaration")) {
int startOffset = root.getElement(classParameterLocation.line - 1).getStartOffset() + (classParameterLocation.columnStart - 1);
textArea.setCaretPosition(startOffset);
}
});
}
}
}));
container.methodLocalMembers.values().forEach(localMembers -> localMembers.forEach(localMember -> {
if (localMember.line == line && localMember.columnStart - 1 <= column && localMember.columnEnd >= column) {
Element root = textArea.getDocument().getDefaultRootElement();
if (localMember.decRef.equals("declaration")) {
int startOffset = root.getElement(localMember.line - 1).getStartOffset() + (localMember.columnStart - 1);
textArea.setCaretPosition(startOffset);
} else {
String method = localMember.method;
localMembers.stream().filter(classLocalVariableLocation -> classLocalVariableLocation.method.equals(method)).forEach(classLocalVariableLocation -> {
if (classLocalVariableLocation.decRef.equalsIgnoreCase("declaration")) {
int startOffset = root.getElement(classLocalVariableLocation.line - 1).getStartOffset() + (classLocalVariableLocation.columnStart - 1);
textArea.setCaretPosition(startOffset);
}
});
}
}
}));
}
}

View File

@ -19,13 +19,11 @@
package the.bytecode.club.bytecodeviewer.gui.util;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Objects;
import java.util.regex.Matcher;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.ChangeEvent;
@ -45,6 +43,7 @@ import the.bytecode.club.bytecodeviewer.gui.components.MethodsRenderer;
import the.bytecode.club.bytecodeviewer.gui.components.MyErrorStripe;
import the.bytecode.club.bytecodeviewer.gui.components.RSyntaxTextAreaHighlighterEx;
import the.bytecode.club.bytecodeviewer.gui.components.SearchableRSyntaxTextArea;
import the.bytecode.club.bytecodeviewer.gui.components.actions.GoToAction;
import the.bytecode.club.bytecodeviewer.gui.hexviewer.HexViewer;
import the.bytecode.club.bytecodeviewer.gui.resourceviewer.BytecodeViewPanel;
import the.bytecode.club.bytecodeviewer.gui.resourceviewer.viewer.ClassViewer;
@ -401,6 +400,8 @@ public class BytecodeViewPanelUpdater implements Runnable
bytecodeViewPanel.textArea.setMarkOccurrencesColor(Color.ORANGE);
bytecodeViewPanel.textArea.setCursor(new Cursor(Cursor.TEXT_CURSOR));
bytecodeViewPanel.textArea.getCaret().setVisible(true);
bytecodeViewPanel.textArea.getCaret().setBlinkRate(0);
bytecodeViewPanel.textArea.setHighlighter(new RSyntaxTextAreaHighlighterEx());
if (bytecodeViewPanel.decompiler != Decompiler.BYTECODE_DISASSEMBLER)
@ -413,7 +414,6 @@ public class BytecodeViewPanelUpdater implements Runnable
bytecodeViewPanel.textArea.setSyntaxEditingStyle("text/javaBytecode");
}
bytecodeViewPanel.textArea.setCodeFoldingEnabled(true);
bytecodeViewPanel.textArea.setAntiAliasingEnabled(true);
bytecodeViewPanel.textArea.setText(decompiledSource);
bytecodeViewPanel.textArea.setCaretPosition(0);
bytecodeViewPanel.textArea.setEditable(isPanelEditable);
@ -432,14 +432,18 @@ public class BytecodeViewPanelUpdater implements Runnable
bytecodeViewPanel.revalidate();
bytecodeViewPanel.repaint();
String classContainerName = viewer.resource.workingName + "-" + decompiler.getDecompilerName();
ClassFileContainer classFileContainer = viewer.classFiles.get(classContainerName);
bytecodeViewPanel.textArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_B, InputEvent.CTRL_DOWN_MASK), "goToAction");
bytecodeViewPanel.textArea.getActionMap().put("goToAction", new GoToAction(classFileContainer));
bytecodeViewPanel.textArea.addCaretListener(e -> {
if (bytecodeViewPanel.textArea.isFocusOwner())
{
RSyntaxTextAreaHighlighterEx highlighterEx = (RSyntaxTextAreaHighlighterEx) bytecodeViewPanel.textArea.getHighlighter();
highlighterEx.clearMarkOccurrencesHighlights();
RSyntaxTextArea textArea = (RSyntaxTextArea) e.getSource();
String workingName = viewer.resource.workingName + "-" + decompiler.getDecompilerName();
markOccurrences(textArea, viewer.classFiles.get(workingName), errorStripe);
markOccurrences(textArea, viewer.classFiles.get(classContainerName), errorStripe);
}
});
}
@ -447,8 +451,8 @@ public class BytecodeViewPanelUpdater implements Runnable
private void markOccurrences(RSyntaxTextArea textArea, ClassFileContainer classFileContainer, MyErrorStripe errorStripe)
{
RSyntaxTextAreaHighlighterEx highlighterEx = (RSyntaxTextAreaHighlighterEx) textArea.getHighlighter();
Token token = textArea.modelToToken(textArea.getCaretPosition() - 1);
if (token == null || token.getLexeme().equals(";"))
Token token = textArea.modelToToken(textArea.getCaretPosition());
if (token == null)
{
highlighterEx.clearMarkOccurrencesHighlights();
errorStripe.refreshMarkers();

View File

@ -22,7 +22,9 @@ public class TokenUtil
|| lexeme.equals("-")
|| lexeme.equals("+")
|| lexeme.equals(" ")
? textArea.modelToToken(textArea.getCaretPosition() + 1)
|| lexeme.equals(";")
|| lexeme.equals(",")
? textArea.modelToToken(textArea.getCaretPosition() - 1)
: token;
}
}