Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public class AIDecompilationdWindow extends ComponentProviderAdapter {
private ProgramLocation lastLocation;
/// Program the listing highlight was set on, so only a highlight of ours is cleared.
private Program highlightedProgram;
private boolean settingCode;
private int caretLine = -1;

private static final Pattern IDENTIFIER = Pattern.compile("[A-Za-z_]\\w*");

Expand Down Expand Up @@ -264,8 +266,6 @@ private JComponent buildComponent() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
handleDoubleClick(e.getPoint());
} else if (e.getClickCount() == 1 && SwingUtilities.isLeftMouseButton(e)) {
highlightFromDecompilation(e.getPoint());
}
}

Expand All @@ -285,6 +285,20 @@ private void maybeShowPopup(MouseEvent e) {
}
}
});
textArea.addCaretListener(e -> {
if (settingCode) {
return;
}
try {
int line = textArea.getLineOfOffset(e.getDot());
if (line != caretLine) {
caretLine = line;
highlightFromDecompilation(line);
}
} catch (BadLocationException ignored) {
// The caret outran a re-render; the next move lights its line.
}
});
sp = new RTextScrollPane(textArea);

component.add(sp, BorderLayout.CENTER);
Expand Down Expand Up @@ -471,7 +485,13 @@ private static String leadingWhitespace(String line) {
private void setCode(String code) {
String text = code;
textArea.removeAllLineHighlights();
textArea.setText(text);
settingCode = true;
try {
textArea.setText(text);
} finally {
settingCode = false;
}
caretLine = -1;
}

private void clear() {
Expand Down Expand Up @@ -628,6 +648,7 @@ public void run(TaskMonitor monitor) {
/// Light the decompilation lines the instruction at {@code loc} fed, and bring the first into view.
private void highlightFromListing(ProgramLocation loc) {
textArea.removeAllLineHighlights();
caretLine = -1;
RenderModel model = currentRenderModel;
Function target = this.function;
if (loc == null || model == null || target == null || !target.getBody().contains(loc.getAddress())) {
Expand All @@ -653,10 +674,10 @@ private void highlightFromListing(ProgramLocation loc) {
}
}

/// Light the clicked decompilation line and the instructions that fed it, and scroll the listing
/// Light the caret's decompilation line and the instructions that fed it, and scroll the listing
/// to them. The cursor stays put, so every attributed instruction shows the same colour.
/// A line the model attributed nothing to lights nothing.
private void highlightFromDecompilation(Point point) {
private void highlightFromDecompilation(int displayLine) {
textArea.removeAllLineHighlights();
clearListingHighlight();
RenderModel model = currentRenderModel;
Expand All @@ -665,13 +686,7 @@ private void highlightFromDecompilation(Point point) {
if (model == null || lines == null) {
return;
}
Integer sourceLine;
try {
int offset = textArea.viewToModel2D(point);
sourceLine = offset < 0 ? null : model.sourceLine(textArea.getLineOfOffset(offset));
} catch (BadLocationException e) {
return;
}
Integer sourceLine = model.sourceLine(displayLine);
if (sourceLine == null) {
return;
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/ai/reveng/AIDecompilerComponentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,19 @@ public List<String> getAssembly(FunctionID functionID) {
assertTrue("a distant instruction is scrolled into view", listing.getFieldPanel().getVisibleLayouts()
.stream().anyMatch(layout -> layout.getIndex().equals(farIndex)));
assertNotEquals(indexes.getIndex(func1.getEntryPoint()), listing.getFieldPanel().getViewerPosition().getIndex());

runSwing(() -> {
try {
textArea.setCaretPosition(textArea.getLineStartOffset(1));
} catch (javax.swing.text.BadLocationException e) {
throw new RuntimeException(e);
}
});
waitForSwing();
highlight = codeBrowser.getProvider().getHighlight();
assertTrue("moving the caret by keyboard lights its instructions", highlight.contains(space.getAddress(0x1001)));
assertFalse(highlight.contains(space.getAddress(0x1006)));
assertEquals(List.of(1), highlightedLines(textArea));
}

@SuppressWarnings("unchecked")
Expand Down
Loading