From 0bdd2b07a0862e1f8e9685d226dda97c877c4ab5 Mon Sep 17 00:00:00 2001 From: Rob Marsal Date: Mon, 23 Nov 2026 12:29:04 +0000 Subject: [PATCH] feat(ENG-3917): light disassembly on any caret move --- .../aidecompiler/AIDecompilationdWindow.java | 39 +++++++++++++------ .../ai/reveng/AIDecompilerComponentTest.java | 13 +++++++ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/main/java/ai/reveng/toolkit/ghidra/binarysimilarity/ui/aidecompiler/AIDecompilationdWindow.java b/src/main/java/ai/reveng/toolkit/ghidra/binarysimilarity/ui/aidecompiler/AIDecompilationdWindow.java index 489a865..d4ff861 100644 --- a/src/main/java/ai/reveng/toolkit/ghidra/binarysimilarity/ui/aidecompiler/AIDecompilationdWindow.java +++ b/src/main/java/ai/reveng/toolkit/ghidra/binarysimilarity/ui/aidecompiler/AIDecompilationdWindow.java @@ -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*"); @@ -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()); } } @@ -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); @@ -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() { @@ -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())) { @@ -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; @@ -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; } diff --git a/src/test/java/ai/reveng/AIDecompilerComponentTest.java b/src/test/java/ai/reveng/AIDecompilerComponentTest.java index 1ece7dc..b088234 100644 --- a/src/test/java/ai/reveng/AIDecompilerComponentTest.java +++ b/src/test/java/ai/reveng/AIDecompilerComponentTest.java @@ -782,6 +782,19 @@ public List 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")