Skip to content

Commit

Permalink
feat: allow to exclude symlinked packages
Browse files Browse the repository at this point in the history
  • Loading branch information
cvette committed Jul 19, 2023
1 parent 0825924 commit fc0e69c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## Unreleased

### Added
- Allow to exclude symlinked packages automatically
- Compatibility with 2023.2 (eap)

## 1.18.0 - 2023-06-12
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/de/vette/idea/neos/ComposerUpdateListener.java
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}
});
}
}
1 change: 1 addition & 0 deletions src/main/java/de/vette/idea/neos/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class Settings implements PersistentStateComponent<Settings> {

public boolean pluginEnabled = false;
public boolean dismissEnableNotification = false;
public boolean excludePackageSymlinks = false;

public static Settings getInstance(@NotNull Project project) {
return project.getService(Settings.class);
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/de/vette/idea/neos/SettingsForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -92,6 +98,7 @@ public void reset() {

private void updateUIFromSettings() {
pluginEnabled.setSelected(getSettings().pluginEnabled);
excludePackageSymlinks.setSelected(getSettings().excludePackageSymlinks);
}

private Settings getSettings() {
Expand All @@ -101,7 +108,4 @@ private Settings getSettings() {
public static void show(@NotNull Project project) {
ShowSettingsUtilImpl.showSettingsDialog(project, "Neos.SettingsForm", null);
}

@Override
public void disposeUIResources() {}
}
4 changes: 4 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<depends>org.intellij.intelliLang</depends>
<depends>org.jetbrains.plugins.yaml</depends>

<projectListeners>
<listener class="de.vette.idea.neos.ComposerUpdateListener" topic="com.jetbrains.php.composer.actions.update.ComposerInstalledPackagesService$ComposerUpdateListener" />
</projectListeners>

<extensions defaultExtensionNs="com.jetbrains.php">
<libraryRoot id="doctrine_meta" path="doctrine-phpstorm-meta/" runtime="false"/>
<frameworkUsageProvider implementation="de.vette.idea.neos.FrameworkUsageProvider"/>
Expand Down

0 comments on commit fc0e69c

Please sign in to comment.