Skip to content

Commit

Permalink
Close thread pools used in code editor services when respective tabs …
Browse files Browse the repository at this point in the history
…are closed

The 'Editor' type spawns some threads for syntax highlighting and bracket matching. Since it is not a Navigable type by itself it now implements Closing, and is called in areas where editors are created. The close handling shuts these thread pools down.
  • Loading branch information
Col-E committed Dec 11, 2024
1 parent 401141d commit 483b800
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import software.coley.collections.Lists;
import software.coley.collections.Unchecked;
import software.coley.recaf.analytics.logging.Logging;
import software.coley.recaf.behavior.Closing;
import software.coley.recaf.ui.control.VirtualizedScrollPaneWrapper;
import software.coley.recaf.ui.control.richtext.bracket.SelectedBracketTracking;
import software.coley.recaf.ui.control.richtext.linegraphics.RootLineGraphicFactory;
Expand Down Expand Up @@ -64,7 +65,7 @@
*
* @author Matt Coley
*/
public class Editor extends BorderPane {
public class Editor extends BorderPane implements Closing {
private static final Logger logger = Logging.get(Editor.class);
public static final int SHORTER_DELAY_MS = 25;
public static final int SHORT_DELAY_MS = 150;
Expand Down Expand Up @@ -179,6 +180,14 @@ else if (e.getCode() == KeyCode.ENTER)
lastDocumentSnapshot = ReadOnlyStyledDocument.from(codeArea.getDocument());
}

@Override
public void close() {
if (selectedBracketTracking != null)
selectedBracketTracking.close();
if (!syntaxPool.isShutdown())
syntaxPool.shutdownNow();
}

/**
* @return {@code true} to indicate the contents of this editor are editable. {@code false} for read-only content.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.reactfx.EventStream;
import org.slf4j.Logger;
import software.coley.recaf.analytics.logging.Logging;
import software.coley.recaf.behavior.Closing;
import software.coley.recaf.ui.control.richtext.Editor;
import software.coley.recaf.ui.control.richtext.EditorComponent;
import software.coley.recaf.util.FxThreadUtil;
Expand All @@ -28,7 +29,7 @@
* @see BracketMatchGraphicFactory Adds a line indicator to an {@link Editor} for lines covering the {@link #getRange() range}.
* @see Editor#setSelectedBracketTracking(SelectedBracketTracking) Call to install into an {@link Editor}.
*/
public class SelectedBracketTracking implements EditorComponent, Consumer<Change<Integer>> {
public class SelectedBracketTracking implements EditorComponent, Closing, Consumer<Change<Integer>> {
private static final Logger logger = Logging.get(SelectedBracketTracking.class);
private final ExecutorService service = ThreadPoolFactory.newSingleThreadExecutor("brackets");
private EventStream<Change<Integer>> lastEventStream;
Expand Down Expand Up @@ -241,4 +242,10 @@ else if (close != -1 && close < pos)
// Start found? Create range.
return (start >= 0) ? new IntRange(start, end) : null;
}

@Override
public void close() {
if (!service.isShutdown())
service.shutdownNow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openrewrite.tree.ParseError;
import software.coley.recaf.analytics.logging.DebuggingLogger;
import software.coley.recaf.analytics.logging.Logging;
import software.coley.recaf.behavior.Closing;
import software.coley.recaf.info.AndroidClassInfo;
import software.coley.recaf.info.ClassInfo;
import software.coley.recaf.info.JvmClassInfo;
Expand Down Expand Up @@ -67,7 +68,7 @@
* @see AssemblerContextActionSupport Alternative for context actions on assembly sources.
*/
@Dependent
public class JavaContextActionSupport implements EditorComponent, UpdatableNavigable {
public class JavaContextActionSupport implements EditorComponent, UpdatableNavigable, Closing {
private static final DebuggingLogger logger = Logging.get(JavaContextActionSupport.class);
private static final long REPARSE_ELAPSED_TIME = 2_000L;
private final ExecutorService parseThreadPool = ThreadPoolFactory.newSingleThreadExecutor("java-parse");
Expand Down Expand Up @@ -452,7 +453,13 @@ public void requestFocus() {

@Override
public void disable() {
// no-op
close();
}

@Override
public void close() {
if (!parseThreadPool.isShutdown())
parseThreadPool.shutdownNow();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ public Collection<Navigable> getNavigableChildren() {
public void disable() {
setDisable(true);
setOnKeyPressed(null);
editor.close();
contextActionSupport.close();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ protected void generateDisplay() {
}
}

@Override
public void disable() {
super.disable();
editor.close();
}

@Nonnull
@Override
public PathNode<?> getPath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import me.darknet.assembler.ast.ASTElement;
import me.darknet.assembler.compiler.ClassResult;
import org.kordamp.ikonli.carbonicons.CarbonIcons;
import software.coley.recaf.behavior.Closing;
import software.coley.recaf.info.ClassInfo;
import software.coley.recaf.path.PathNode;
import software.coley.recaf.services.navigation.Navigable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ protected void onPipelineOutputUpdate() {
// no-op
}

@Override
public void disable() {
javaEditor.close();
jasmEditor.close();
}

/**
* Populates the initial text of the expression compiler pane.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ private void removeAttention() {
@Override
public void disable() {
snippetManager.removeSnippetListener(this);
editor.close();
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public Collection<Navigable> getNavigableChildren() {
public void disable() {
setDisable(true);
setOnKeyPressed(null);
editor.close();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public Collection<Navigable> getNavigableChildren() {
public void disable() {
setDisable(true);
setOnKeyPressed(null);
editor.close();
}

@Override
Expand Down

0 comments on commit 483b800

Please sign in to comment.