From fc0e69c4315215354d2904cb970dc5dd188ab2a6 Mon Sep 17 00:00:00 2001 From: Christian Vette <6884391+cvette@users.noreply.github.com> Date: Thu, 20 Jul 2023 01:02:58 +0200 Subject: [PATCH] feat: allow to exclude symlinked packages --- CHANGELOG.md | 1 + .../idea/neos/ComposerUpdateListener.java | 81 +++++++++++++++++++ .../java/de/vette/idea/neos/Settings.java | 1 + .../java/de/vette/idea/neos/SettingsForm.java | 16 ++-- src/main/resources/META-INF/plugin.xml | 4 + 5 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 src/main/java/de/vette/idea/neos/ComposerUpdateListener.java diff --git a/CHANGELOG.md b/CHANGELOG.md index c3f9408d..04bc2105 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ## Unreleased ### Added +- Allow to exclude symlinked packages automatically - Compatibility with 2023.2 (eap) ## 1.18.0 - 2023-06-12 diff --git a/src/main/java/de/vette/idea/neos/ComposerUpdateListener.java b/src/main/java/de/vette/idea/neos/ComposerUpdateListener.java new file mode 100644 index 00000000..b32a448c --- /dev/null +++ b/src/main/java/de/vette/idea/neos/ComposerUpdateListener.java @@ -0,0 +1,81 @@ +package de.vette.idea.neos; + +import com.intellij.openapi.module.Module; +import com.intellij.openapi.module.ModuleUtil; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.ModuleRootManager; +import com.intellij.openapi.roots.ModuleRootModificationUtil; +import com.intellij.openapi.vfs.VfsUtilCore; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.VirtualFileVisitor; +import com.jetbrains.php.composer.ComposerDataService; +import com.jetbrains.php.composer.actions.update.ComposerInstalledPackagesService; +import org.jetbrains.annotations.NotNull; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; + +public class ComposerUpdateListener implements ComposerInstalledPackagesService.ComposerUpdateListener { + static final String PACKAGES_DIRECTORY = "Packages"; + + public void packageRefreshed(@NotNull Project project) { + if (!Settings.getInstance(project).excludePackageSymlinks) { + return; + } + + VirtualFile config = ComposerDataService.getInstance(project).getCurrentConfigFile(); + + if (config == null) { + return; + } + + VirtualFile packagesDir = config.getParent().findChild(PACKAGES_DIRECTORY); + if (packagesDir == null) { + return; + } + + VfsUtilCore.visitChildrenRecursively(packagesDir, new VirtualFileVisitor<>() { + public boolean visitFile(@NotNull VirtualFile file) { + // Limit to the first two levels + if (file.getPath().split("/").length - packagesDir.getPath().split("/").length > 2) { + return false; + } + + // Only process directories + if (!file.isDirectory()) { + return false; + } + + // We need to get the native file to check for symlinks + java.io.File ioFile = VfsUtilCore.virtualToIoFile(file); + Path path = Paths.get(ioFile.getPath()); + if (!Files.isSymbolicLink(path)) { + return true; + } + + Module module = ModuleUtil.findModuleForFile(file, project); + if (module == null) { + return false; + } + + // Exclude directories + String fileUrl = file.getUrl(); + Collection excludeFolders = new ArrayList<>(); + excludeFolders.add(fileUrl); + ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module); + for (VirtualFile sourceRoot : moduleRootManager.getContentRoots()) { + ModuleRootModificationUtil.updateExcludedFolders( + module, + sourceRoot, + Collections.emptyList(), + excludeFolders + ); + } + + return true; + } + }); + } +} diff --git a/src/main/java/de/vette/idea/neos/Settings.java b/src/main/java/de/vette/idea/neos/Settings.java index c1ae71a2..50b954f0 100644 --- a/src/main/java/de/vette/idea/neos/Settings.java +++ b/src/main/java/de/vette/idea/neos/Settings.java @@ -29,6 +29,7 @@ public class Settings implements PersistentStateComponent { public boolean pluginEnabled = false; public boolean dismissEnableNotification = false; + public boolean excludePackageSymlinks = false; public static Settings getInstance(@NotNull Project project) { return project.getService(Settings.class); diff --git a/src/main/java/de/vette/idea/neos/SettingsForm.java b/src/main/java/de/vette/idea/neos/SettingsForm.java index 139906c5..04c2eed8 100644 --- a/src/main/java/de/vette/idea/neos/SettingsForm.java +++ b/src/main/java/de/vette/idea/neos/SettingsForm.java @@ -19,7 +19,6 @@ package de.vette.idea.neos; import com.intellij.ide.actions.ShowSettingsUtilImpl; -import com.intellij.openapi.options.Configurable; import com.intellij.openapi.project.Project; import com.intellij.uiDesigner.core.GridConstraints; import com.intellij.uiDesigner.core.GridLayoutManager; @@ -33,6 +32,7 @@ public class SettingsForm implements PhpFrameworkConfigurable { private JCheckBox pluginEnabled; + private JCheckBox excludePackageSymlinks; private final Project project; public SettingsForm(@NotNull final Project project) { @@ -41,7 +41,7 @@ public SettingsForm(@NotNull final Project project) { @Override public boolean isBeingUsed() { - return this.pluginEnabled.isSelected(); + return this.pluginEnabled.isSelected() || this.excludePackageSymlinks.isSelected(); } @Override @@ -65,24 +65,30 @@ public String getHelpTopic() { @Override public JComponent createComponent() { pluginEnabled = new JCheckBox("Enable plugin for this project"); + excludePackageSymlinks = new JCheckBox("Exclude symlinked packages"); GridLayoutManager layout = new GridLayoutManager(2,1); GridConstraints c = new GridConstraints(); c.setAnchor(GridConstraints.ANCHOR_NORTHWEST); + c.setRow(0); JPanel panel1 = new JPanel(layout); panel1.add(pluginEnabled, c); + + c.setRow(1); + panel1.add(excludePackageSymlinks, c); return panel1; } @Override public boolean isModified() { - return !pluginEnabled.isSelected() == getSettings().pluginEnabled; + return !pluginEnabled.isSelected() == getSettings().pluginEnabled || !excludePackageSymlinks.isSelected() == getSettings().excludePackageSymlinks; } @Override public void apply() { getSettings().pluginEnabled = pluginEnabled.isSelected(); + getSettings().excludePackageSymlinks = excludePackageSymlinks.isSelected(); } @Override @@ -92,6 +98,7 @@ public void reset() { private void updateUIFromSettings() { pluginEnabled.setSelected(getSettings().pluginEnabled); + excludePackageSymlinks.setSelected(getSettings().excludePackageSymlinks); } private Settings getSettings() { @@ -101,7 +108,4 @@ private Settings getSettings() { public static void show(@NotNull Project project) { ShowSettingsUtilImpl.showSettingsDialog(project, "Neos.SettingsForm", null); } - - @Override - public void disposeUIResources() {} } diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 4ece7de2..4c1e997a 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -11,6 +11,10 @@ org.intellij.intelliLang org.jetbrains.plugins.yaml + + + +