Skip to content

Commit

Permalink
Cancel pending src update tasks when they become outdated
Browse files Browse the repository at this point in the history
  • Loading branch information
sfPlayer1 committed Nov 6, 2024
1 parent 7c8402f commit 53531e7
Showing 1 changed file with 55 additions and 36 deletions.
91 changes: 55 additions & 36 deletions src/main/java/matcher/gui/tab/SourcecodeTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Set;
import java.util.concurrent.Future;

import matcher.NameType;
import matcher.gui.Gui;
import matcher.gui.ISelectionProvider;
import matcher.srcprocess.Decompiler;
import matcher.srcprocess.HtmlUtil;
import matcher.srcprocess.SrcDecorator;
import matcher.srcprocess.SrcDecorator.SrcParseException;
Expand All @@ -32,7 +34,7 @@ public void onSelectStateChange(boolean tabSelected) {
this.tabSelected = tabSelected;
if (!tabSelected) return;

if (updateNeeded > 0) update();
if (updateNeeded != UPDATE_NONE) update();

if (selectedMember instanceof MethodInstance) {
onMethodSelect((MethodInstance) selectedMember);
Expand All @@ -44,14 +46,14 @@ public void onSelectStateChange(boolean tabSelected) {
@Override
public void onClassSelect(ClassInstance cls) {
selectedClass = cls;
if (updateNeeded == 0) updateNeeded = 1;
if (updateNeeded == UPDATE_NONE) updateNeeded = UPDATE_RESET;
if (tabSelected) update();
}

@Override
public void onMatchChange(Set<MatchType> types) {
selectedClass = selectionProvider.getSelectedClass();
updateNeeded = 2;
updateNeeded = UPDATE_REFRESH;

if (tabSelected && selectedClass != null) {
update();
Expand All @@ -67,8 +69,8 @@ public void onViewChange(ViewChangeCause cause) {
update();
} else if (selectedClass != null
&& (cause == ViewChangeCause.NAME_TYPE_CHANGED
|| cause == ViewChangeCause.DECOMPILER_CHANGED)) {
updateNeeded = 2;
|| cause == ViewChangeCause.DECOMPILER_CHANGED)) {
updateNeeded = UPDATE_REFRESH;
if (tabSelected) update();
}
}
Expand All @@ -78,6 +80,11 @@ private void update() {

final int cDecompId = ++decompId;

if (pendingUpdateTask != null) {
pendingUpdateTask.cancel(true);
pendingUpdateTask = null;
}

if (selectedClass == null) {
displayText("no class selected");
return;
Expand All @@ -86,38 +93,45 @@ private void update() {
displayText("decompiling...");

NameType nameType = gui.getNameType().withUnmatchedTmp(unmatchedTmp);
Decompiler decompiler = gui.getDecompiler().get();

//Gui.runAsyncTask(() -> gui.getEnv().decompile(selectedClass, true))
Gui.runAsyncTask(() -> SrcDecorator.decorate(gui.getEnv().decompile(gui.getDecompiler().get(), selectedClass, nameType), selectedClass, nameType))
.whenComplete((res, exc) -> {
if (cDecompId == decompId) {
if (exc != null) {
exc.printStackTrace();

StringWriter sw = new StringWriter();
exc.printStackTrace(new PrintWriter(sw));

if (exc instanceof SrcParseException) {
SrcParseException parseExc = (SrcParseException) exc;
displayText("parse error: "+parseExc.problems+"\ndecompiled source:\n"+parseExc.source);
} else {
displayText("decompile error: "+sw.toString());
}
} else {
double prevScroll = updateNeeded == 2 ? getScrollTop() : 0;

displayHtml(res);

if (updateNeeded == 2 && prevScroll > 0) {
setScrollTop(prevScroll);
}
}
} else if (exc != null) {
exc.printStackTrace();
}

updateNeeded = 0;
});
pendingUpdateTask = Gui.runAsyncTask(() -> SrcDecorator.decorate(gui.getEnv().decompile(decompiler, selectedClass, nameType), selectedClass, nameType))
.whenComplete((res, exc) -> applyDecompilerResult(res, exc, cDecompId));
}

private void applyDecompilerResult(String res, Throwable exc, int cDecompId) {
if (cDecompId != decompId) {
if (exc != null) {
exc.printStackTrace();
}

return;
}

if (exc != null) {
exc.printStackTrace();

StringWriter sw = new StringWriter();
exc.printStackTrace(new PrintWriter(sw));

if (exc instanceof SrcParseException) {
SrcParseException parseExc = (SrcParseException) exc;
displayText("parse error: "+parseExc.problems+"\ndecompiled source:\n"+parseExc.source);
} else {
displayText("decompile error: "+sw.toString());
}
} else {
double prevScroll = updateNeeded == UPDATE_REFRESH ? getScrollTop() : 0;

displayHtml(res);

if (updateNeeded == UPDATE_REFRESH && prevScroll > 0) {
setScrollTop(prevScroll);
}
}

updateNeeded = UPDATE_NONE;
}

@Override
Expand All @@ -138,13 +152,18 @@ public void onFieldSelect(FieldInstance field) {
}
}

private static final int UPDATE_NONE = 0;
private static final int UPDATE_RESET = 1;
private static final int UPDATE_REFRESH = 2; // tries to keep scroll position

private final Gui gui;
private final ISelectionProvider selectionProvider;
private final boolean unmatchedTmp;

private int decompId;
private int updateNeeded;
private int updateNeeded = UPDATE_NONE;
private boolean tabSelected;
private ClassInstance selectedClass;
private MemberInstance<?> selectedMember;
private Future<?> pendingUpdateTask;
}

0 comments on commit 53531e7

Please sign in to comment.