mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Enabled folder exclusion, other improvements for "Exclude from Project" action
This commit is contained in:
+23
-14
@@ -27,25 +27,24 @@ import com.intellij.openapi.vfs.VfsUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.VirtualFileManager;
|
||||
|
||||
import com.intellij.util.containers.HashSet;
|
||||
import com.intellij.util.indexing.FileBasedIndex;
|
||||
import org.jdom.Attribute;
|
||||
import org.jdom.Element;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author Rustam Vishnyakov
|
||||
*/
|
||||
@State(name = "ExcludedFiles", storages = {@Storage(id = "default", file = "$PROJECT_FILE$")})
|
||||
@State(name = "ProjectFileExclusionManager", storages = {@Storage(id = "default", file = "$PROJECT_FILE$")})
|
||||
public class ProjectFileExclusionManager implements PersistentStateComponent<Element> {
|
||||
|
||||
private final static String FILE_ELEMENT = "file";
|
||||
private final static String PATH_ATTR = "path";
|
||||
private final static String PATH_ATTR = "url";
|
||||
|
||||
private final Map<String,VirtualFile> myExcludedFiles = new TreeMap<String,VirtualFile>();
|
||||
private final Set<VirtualFile> myExcludedFiles = new HashSet<VirtualFile>();
|
||||
private final Project myProject;
|
||||
|
||||
public ProjectFileExclusionManager(Project project) {
|
||||
@@ -54,14 +53,14 @@ public class ProjectFileExclusionManager implements PersistentStateComponent<Ele
|
||||
|
||||
public void addExclusion(VirtualFile file) {
|
||||
if (file.isDirectory()) return;
|
||||
myExcludedFiles.put(file.getPath(), file);
|
||||
myExcludedFiles.add(file);
|
||||
FileBasedIndex.getInstance().requestReindexExcluded(file);
|
||||
fireRootsChange(myProject);
|
||||
}
|
||||
|
||||
public void removeExclusion(VirtualFile file) {
|
||||
if (myExcludedFiles.containsValue(file)) {
|
||||
myExcludedFiles.remove(file.getPath());
|
||||
if (myExcludedFiles.contains(file)) {
|
||||
myExcludedFiles.remove(file);
|
||||
FileBasedIndex.getInstance().requestReindex(file);
|
||||
fireRootsChange(myProject);
|
||||
}
|
||||
@@ -77,18 +76,28 @@ public class ProjectFileExclusionManager implements PersistentStateComponent<Ele
|
||||
}
|
||||
|
||||
public boolean isExcluded(VirtualFile file) {
|
||||
return myExcludedFiles.containsKey(file.getPath());
|
||||
return myExcludedFiles.contains(file);
|
||||
}
|
||||
|
||||
public Collection<VirtualFile> getExcludedFiles() {
|
||||
return myExcludedFiles.values();
|
||||
return myExcludedFiles;
|
||||
}
|
||||
|
||||
public Collection<VirtualFile> getSortedExcludedFiles() {
|
||||
List<VirtualFile> excludedFiles = new ArrayList<VirtualFile>();
|
||||
excludedFiles.addAll(myExcludedFiles);
|
||||
Collections.sort(excludedFiles, new Comparator<VirtualFile>() {
|
||||
public int compare(final VirtualFile file1, final VirtualFile file2) {
|
||||
return file1.getPath().toLowerCase().compareTo(file2.getPath().toLowerCase());
|
||||
}
|
||||
});
|
||||
return excludedFiles;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Element getState() {
|
||||
final Element root = new Element("root");
|
||||
for (String key : myExcludedFiles.keySet()) {
|
||||
VirtualFile vf = myExcludedFiles.get(key);
|
||||
for (VirtualFile vf : getSortedExcludedFiles()) {
|
||||
final Element vfElement = new Element(FILE_ELEMENT);
|
||||
final Attribute filePathAttr = new Attribute(PATH_ATTR, VfsUtil.pathToUrl(vf.getPath()));
|
||||
vfElement.setAttribute(filePathAttr);
|
||||
@@ -108,7 +117,7 @@ public class ProjectFileExclusionManager implements PersistentStateComponent<Ele
|
||||
final String filePath = filePathAttr.getValue();
|
||||
VirtualFile vf = vfManager.findFileByUrl(filePath);
|
||||
if (vf != null) {
|
||||
myExcludedFiles.put(vf.getPath(), vf);
|
||||
myExcludedFiles.add(vf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+54
-8
@@ -15,12 +15,14 @@
|
||||
*/
|
||||
package com.intellij.openapi.file.exclude.ui;
|
||||
|
||||
import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.actionSystem.PlatformDataKeys;
|
||||
import com.intellij.openapi.actionSystem.*;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.file.exclude.ProjectFileExclusionManager;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.module.ModuleUtil;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.roots.*;
|
||||
import com.intellij.openapi.vfs.VfsUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
|
||||
/**
|
||||
@@ -31,11 +33,55 @@ public class ExcludeFromProjectAction extends AnAction {
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
DataContext dataContext = e.getDataContext();
|
||||
final Project project = PlatformDataKeys.PROJECT.getData(dataContext);
|
||||
if (project == null) return;
|
||||
if (project == null) return;
|
||||
final VirtualFile file = PlatformDataKeys.VIRTUAL_FILE.getData(dataContext);
|
||||
if (file == null) return;
|
||||
ProjectFileExclusionManager fileExManager = ProjectFileExclusionManager.getInstance(project);
|
||||
if (fileExManager == null) return;
|
||||
fileExManager.addExclusion(file);
|
||||
if (file.isDirectory()) {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
addExcludedFolder(project, file);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
ProjectFileExclusionManager fileExManager = ProjectFileExclusionManager.getInstance(project);
|
||||
if (fileExManager == null) return;
|
||||
fileExManager.addExclusion(file);
|
||||
}
|
||||
}
|
||||
|
||||
private static void addExcludedFolder(Project project, VirtualFile folder) {
|
||||
Module module = ModuleUtil.findModuleForFile(folder, project);
|
||||
if (module == null) return;
|
||||
ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();
|
||||
for (ContentEntry entry : model.getContentEntries()) {
|
||||
VirtualFile entryFile = entry.getFile();
|
||||
if (entryFile != null) {
|
||||
if (VfsUtil.isAncestor(entryFile, folder, true)) {
|
||||
entry.addExcludeFolder(folder);
|
||||
model.commit();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
model.dispose();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(AnActionEvent e) {
|
||||
DataContext dataContext = e.getDataContext();
|
||||
final VirtualFile file = PlatformDataKeys.VIRTUAL_FILE.getData(dataContext);
|
||||
final Presentation presentation = e.getPresentation();
|
||||
if (file == null) {
|
||||
presentation.setVisible(false);
|
||||
return;
|
||||
}
|
||||
final Project project = PlatformDataKeys.PROJECT.getData(dataContext);
|
||||
if (project != null) {
|
||||
if (file.equals(project.getBaseDir()) || !file.isWritable()) {
|
||||
presentation.setEnabled(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -38,7 +38,7 @@ public class ExcludedFilesConfigurable implements SearchableConfigurable {
|
||||
|
||||
public ExcludedFilesConfigurable(Project project) {
|
||||
myExclusionManager = ProjectFileExclusionManager.getInstance(project);
|
||||
myExcludedFilesPanel = new ExcludedFilesPanel(myExclusionManager != null ? myExclusionManager.getExcludedFiles() : null);
|
||||
myExcludedFilesPanel = new ExcludedFilesPanel(myExclusionManager != null ? myExclusionManager.getSortedExcludedFiles() : null);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -104,11 +104,11 @@ public class ExcludedFilesConfigurable implements SearchableConfigurable {
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
myExcludedFilesPanel.resetFiles(myExclusionManager != null ? myExclusionManager.getExcludedFiles() : null);
|
||||
myExcludedFilesPanel.resetFiles(myExclusionManager != null ? myExclusionManager.getSortedExcludedFiles() : null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disposeUIResources() {
|
||||
//To change body of implemented methods use File | Settings | File Templates.
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user