From 383a2d73157124a8b9c2de9aff1a2ce8fdbca909 Mon Sep 17 00:00:00 2001 From: Sergei Tachenov Date: Mon, 5 Dec 2022 15:39:50 +0200 Subject: [PATCH] [UI API] IDEA-307083, IDEA-307615 Clean up SimpleNode.update() mess Now that doUpdate() implementations only touch the presentation, we can try to clean up the surrounding mess in SimpleNode.update(): 1. Remove equality checks (they were broken anyway, and we do a similar check in PresentableNodeDescriptor.apply() anyway. 2. Instead of passing values back and forth between fields and the presentation, we don't mutate fields anymore (this isn't allowed now anyway), and instead use them as fall back if the presentation isn't completely filled by doUpdate(). GitOrigin-RevId: aa3d471883c3df14b75f3a9efd1f5b353b67755d --- .../treeView/PresentableNodeDescriptor.java | 17 +++++-- .../intellij/ui/treeStructure/SimpleNode.java | 44 ++++++++++++------- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/platform/editor-ui-api/src/com/intellij/ide/util/treeView/PresentableNodeDescriptor.java b/platform/editor-ui-api/src/com/intellij/ide/util/treeView/PresentableNodeDescriptor.java index 1f45eb33542c..6b264f157cfa 100644 --- a/platform/editor-ui-api/src/com/intellij/ide/util/treeView/PresentableNodeDescriptor.java +++ b/platform/editor-ui-api/src/com/intellij/ide/util/treeView/PresentableNodeDescriptor.java @@ -10,6 +10,7 @@ import com.intellij.util.concurrency.annotations.RequiresBackgroundThread; import com.intellij.util.concurrency.annotations.RequiresReadLock; import com.intellij.util.ui.StartupUiUtil; import com.intellij.util.ui.UIUtil; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -222,13 +223,23 @@ public abstract class PresentableNodeDescriptor extends NodeDescriptor { } public @NlsSafe String getName() { - if (!getPresentation().getColoredText().isEmpty()) { + String result = getColoredTextAsPlainText(getPresentation()); + if (result != null) { + return result; + } + return myName; + } + + @ApiStatus.Internal + @Nullable + protected static String getColoredTextAsPlainText(PresentationData presentation) { + if (!presentation.getColoredText().isEmpty()) { StringBuilder result = new StringBuilder(); - for (ColoredFragment each : getPresentation().getColoredText()) { + for (ColoredFragment each : presentation.getColoredText()) { result.append(each.getText()); } return result.toString(); } - return myName; + return null; } } diff --git a/platform/platform-api/src/com/intellij/ui/treeStructure/SimpleNode.java b/platform/platform-api/src/com/intellij/ui/treeStructure/SimpleNode.java index ff90e21056f4..36629679edf6 100644 --- a/platform/platform-api/src/com/intellij/ui/treeStructure/SimpleNode.java +++ b/platform/platform-api/src/com/intellij/ui/treeStructure/SimpleNode.java @@ -17,9 +17,6 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.*; import java.awt.event.InputEvent; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; public abstract class SimpleNode extends PresentableNodeDescriptor implements ComparableObject, LeafState.Supplier { @@ -79,23 +76,38 @@ public abstract class SimpleNode extends PresentableNodeDescriptor imple } if (newElement == null) return; - Color oldColor = myColor; - String oldName = myName; - Icon oldIcon = getIcon(); - List oldFragments = new ArrayList<>(presentation.getColoredText()); - - myColor = UIUtil.getTreeForeground(); - doUpdate(presentation); - myName = getName(); - presentation.setPresentableText(myName); + fillFallbackProperties(presentation); + } - presentation.setChanged(!Arrays.equals(new Object[]{getIcon(), myName, oldFragments, myColor}, - new Object[]{oldIcon, oldName, oldFragments, oldColor})); + private void fillFallbackProperties(PresentationData presentation) { + fillFallbackText(presentation); + fillFallbackIcon(presentation); + fillFallbackColor(presentation); + } - presentation.setForcedTextForeground(myColor); - presentation.setIcon(getIcon()); + private void fillFallbackText(PresentationData presentation) { + var text = getColoredTextAsPlainText(presentation); + if (text == null) { + text = presentation.getPresentableText(); + } + if (text == null) { + text = myName; + } + presentation.setPresentableText(text); + } + + private void fillFallbackIcon(PresentationData presentation) { + if (presentation.getIcon(false) == null) { + presentation.setIcon(myClosedIcon); + } + } + + private void fillFallbackColor(PresentationData presentation) { + if (presentation.getForcedTextForeground() == null) { + presentation.setForcedTextForeground(myColor == null ? UIUtil.getTreeForeground() : myColor); + } } /**