migrate to DataSink.lazy

GitOrigin-RevId: 30402500d00996ce83f7f46ff5763c5b5442275b
This commit is contained in:
Gregory.Shrago
2024-06-27 15:00:47 +04:00
committed by intellij-monorepo-bot
parent e0045e4e0e
commit 69cb70983f
2 changed files with 99 additions and 107 deletions

View File

@@ -512,11 +512,7 @@ public final class AntExplorer extends SimpleToolWindowPanel implements DataProv
TreePath[] paths = tree.getSelectionPaths();
TreePath leadPath = tree.getLeadSelectionPath();
AntBuildFile currentBuildFile = getCurrentBuildFile();
sink.set(PlatformCoreDataKeys.BGT_DATA_PROVIDER, id -> getSlowData(id, paths, leadPath, currentBuildFile));
}
private Object getSlowData(@NotNull @NonNls String dataId, final TreePath @Nullable [] paths, @Nullable TreePath leadPath, @Nullable AntBuildFile currentBuildFile) {
if (CommonDataKeys.VIRTUAL_FILE_ARRAY.is(dataId)) {
sink.lazy(CommonDataKeys.VIRTUAL_FILE_ARRAY, () -> {
final List<VirtualFile> virtualFiles = collectAntFiles(buildFile -> {
final VirtualFile virtualFile = buildFile.getVirtualFile();
if (virtualFile != null && virtualFile.isValid()) {
@@ -525,12 +521,12 @@ public final class AntExplorer extends SimpleToolWindowPanel implements DataProv
return null;
}, paths);
return virtualFiles == null? null : virtualFiles.toArray(VirtualFile.EMPTY_ARRAY);
}
else if (PlatformCoreDataKeys.PSI_ELEMENT_ARRAY.is(dataId)) {
});
sink.lazy(PlatformCoreDataKeys.PSI_ELEMENT_ARRAY, () -> {
final List<PsiElement> elements = collectAntFiles(AntBuildFile::getAntFile, paths);
return elements == null? null : elements.toArray(PsiElement.EMPTY_ARRAY);
}
else if (CommonDataKeys.NAVIGATABLE.is(dataId)) {
return elements == null ? null : elements.toArray(PsiElement.EMPTY_ARRAY);
});
sink.lazy(CommonDataKeys.NAVIGATABLE, () -> {
if (leadPath != null) {
final DefaultMutableTreeNode node = (DefaultMutableTreeNode)leadPath.getLastPathComponent();
if (node != null) {
@@ -548,8 +544,8 @@ public final class AntExplorer extends SimpleToolWindowPanel implements DataProv
return new OpenFileDescriptor(myProject, file);
}
}
}
return null;
return null;
});
}
private static <T> List<T> collectAntFiles(final Function<? super AntBuildFile, ? extends T> function, final TreePath @Nullable [] paths) {

View File

@@ -30,7 +30,6 @@ import com.intellij.util.containers.JBIterable;
import com.intellij.util.ui.tree.TreeUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.datatransfer.StringSelection;
import java.util.ArrayList;
@@ -106,108 +105,41 @@ public class ResourceBundleStructureViewComponent extends PropertiesGroupingStru
sink.set(ResourceBundle.ARRAY_DATA_KEY, new ResourceBundle[]{myResourceBundle});
JBIterable<Object> selection = JBIterable.of(getTree().getSelectionPaths()).map(TreeUtil::getLastUserObject);
sink.set(PlatformCoreDataKeys.BGT_DATA_PROVIDER,
slowId -> getSlowData(slowId, selection, myResourceBundle));
sink.set(PlatformDataKeys.DELETE_ELEMENT_PROVIDER, new DeleteProvider() {
@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
@Override
public void deleteElement(@NotNull DataContext dataContext) {
PsiElement[] selectedPsiElements = PlatformCoreDataKeys.PSI_ELEMENT_ARRAY.getData(dataContext);
if (selectedPsiElements == null) return;
List<PsiFile> psiFiles = ContainerUtil.findAll(selectedPsiElements, PsiFile.class);
DeleteProvider delegate;
if (!psiFiles.isEmpty()) {
delegate = new ResourceBundleDeleteProvider();
}
else {
IProperty[] properties = IProperty.ARRAY_KEY.getData(dataContext);
if (properties != null && properties.length != 0) {
delegate = new PropertiesDeleteProvider(((ResourceBundleEditor)getFileEditor()).getPropertiesInsertDeleteManager(), properties);
}
else {
return;
}
}
delegate.deleteElement(dataContext);
}
@Override
public boolean canDeleteElement(@NotNull DataContext dataContext) {
return CommonDataKeys.PROJECT.getData(dataContext) != null;
}
sink.set(PlatformDataKeys.DELETE_ELEMENT_PROVIDER, new MyDeleteProvider());
sink.set(PlatformDataKeys.COPY_PROVIDER, new MyCopyProvider());
sink.lazy(CommonDataKeys.VIRTUAL_FILE, () -> {
return new ResourceBundleAsVirtualFile(myResourceBundle);
});
sink.set(PlatformDataKeys.COPY_PROVIDER, new CopyProvider() {
@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
@Override
public void performCopy(@NotNull DataContext dataContext) {
PsiElement[] selectedPsiElements = PlatformCoreDataKeys.PSI_ELEMENT_ARRAY.getData(dataContext);
if (selectedPsiElements != null) {
List<String> names = new ArrayList<>(selectedPsiElements.length);
for (PsiElement element : selectedPsiElements) {
if (element instanceof PsiNamedElement) {
names.add(((PsiNamedElement)element).getName());
}
}
CopyPasteManager.getInstance().setContents(new StringSelection(StringUtil.join(names, "\n")));
}
}
@Override
public boolean isCopyEnabled(@NotNull DataContext dataContext) {
return true;
}
@Override
public boolean isCopyVisible(@NotNull DataContext dataContext) {
return true;
}
});
}
private static @Nullable Object getSlowData(@NotNull String dataId,
@NotNull JBIterable<Object> selection,
@NotNull ResourceBundle resourceBundle) {
if (CommonDataKeys.VIRTUAL_FILE.is(dataId)) {
return new ResourceBundleAsVirtualFile(resourceBundle);
}
else if (IProperty.ARRAY_KEY.is(dataId)) {
sink.lazy(IProperty.ARRAY_KEY, () -> {
List<IProperty> list = selection
.filterMap(StructureViewComponent::unwrapWrapper)
.filter(ResourceBundleEditorViewElement.class)
.flatten(o -> JBIterable.of(o.getProperties()))
.toList();
return list.isEmpty() ? null : list.toArray(IProperty.EMPTY_ARRAY);
}
else if (PlatformCoreDataKeys.PSI_ELEMENT_ARRAY.is(dataId)) {
List<PsiElement> list = selection
.filterMap(StructureViewComponent::unwrapWrapper)
.filter(ResourceBundleEditorViewElement.class)
.flatten(o -> JBIterable.<PsiElement>of(o.getFiles())
.append(JBIterable.of(o.getProperties())
.map(IProperty::getPsiElement)
.filter(PsiElement::isValid)))
.toList();
});
sink.lazy(PlatformCoreDataKeys.PSI_ELEMENT_ARRAY, () -> {
List<PsiElement> list = getPsiElements(selection);
return list.toArray(PsiElement.EMPTY_ARRAY);
}
else if (UsageView.USAGE_TARGETS_KEY.is(dataId)) {
PsiElement[] chosenElements = (PsiElement[])getSlowData(PlatformCoreDataKeys.PSI_ELEMENT_ARRAY.getName(), selection, resourceBundle);
if (chosenElements != null) {
UsageTarget[] usageTargets = new UsageTarget[chosenElements.length];
for (int i = 0; i < chosenElements.length; i++) {
usageTargets[i] = new PsiElement2UsageTargetAdapter(chosenElements[i], true);
}
return usageTargets;
}
}
return null;
});
sink.lazy(UsageView.USAGE_TARGETS_KEY, () -> {
List<PsiElement> chosenElements = getPsiElements(selection);
if (chosenElements.isEmpty()) return null;
return ContainerUtil.map2Array(
chosenElements, UsageTarget.EMPTY_ARRAY,
o -> new PsiElement2UsageTargetAdapter(o, true));
});
}
private static @NotNull List<PsiElement> getPsiElements(JBIterable<Object> selection) {
return selection
.filterMap(StructureViewComponent::unwrapWrapper)
.filter(ResourceBundleEditorViewElement.class)
.flatten(o -> JBIterable.<PsiElement>of(o.getFiles())
.append(JBIterable.of(o.getProperties())
.map(IProperty::getPsiElement)
.filter(PsiElement::isValid)))
.toList();
}
@Override
@@ -215,6 +147,70 @@ public class ResourceBundleStructureViewComponent extends PropertiesGroupingStru
return false;
}
private static class MyCopyProvider implements CopyProvider {
@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
@Override
public void performCopy(@NotNull DataContext dataContext) {
PsiElement[] selectedPsiElements = PlatformCoreDataKeys.PSI_ELEMENT_ARRAY.getData(dataContext);
if (selectedPsiElements != null) {
List<String> names = new ArrayList<>(selectedPsiElements.length);
for (PsiElement element : selectedPsiElements) {
if (element instanceof PsiNamedElement) {
names.add(((PsiNamedElement)element).getName());
}
}
CopyPasteManager.getInstance().setContents(new StringSelection(StringUtil.join(names, "\n")));
}
}
@Override
public boolean isCopyEnabled(@NotNull DataContext dataContext) {
return true;
}
@Override
public boolean isCopyVisible(@NotNull DataContext dataContext) {
return true;
}
}
private class MyDeleteProvider implements DeleteProvider {
@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
@Override
public void deleteElement(@NotNull DataContext dataContext) {
PsiElement[] selectedPsiElements = PlatformCoreDataKeys.PSI_ELEMENT_ARRAY.getData(dataContext);
if (selectedPsiElements == null) return;
List<PsiFile> psiFiles = ContainerUtil.findAll(selectedPsiElements, PsiFile.class);
DeleteProvider delegate;
if (!psiFiles.isEmpty()) {
delegate = new ResourceBundleDeleteProvider();
}
else {
IProperty[] properties = IProperty.ARRAY_KEY.getData(dataContext);
if (properties != null && properties.length != 0) {
delegate = new PropertiesDeleteProvider(((ResourceBundleEditor)getFileEditor()).getPropertiesInsertDeleteManager(), properties);
}
else {
return;
}
}
delegate.deleteElement(dataContext);
}
@Override
public boolean canDeleteElement(@NotNull DataContext dataContext) {
return CommonDataKeys.PROJECT.getData(dataContext) != null;
}
}
private final class PropertiesDeleteProvider implements DeleteProvider {
private final ResourceBundlePropertiesUpdateManager myInsertDeleteManager;
private final IProperty[] myProperties;