Skip to content

Commit

Permalink
fixed JetBrains#35 - ignore files from outside of the project
Browse files Browse the repository at this point in the history
  • Loading branch information
hsz authored and zolotov committed Jul 28, 2014
1 parent c9efceb commit 5578274
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/mobi/hsz/idea/gitignore/GitignoreException.java
@@ -0,0 +1,5 @@
package mobi.hsz.idea.gitignore;

public class GitignoreException extends Exception {

}
13 changes: 10 additions & 3 deletions src/mobi/hsz/idea/gitignore/actions/IgnoreFileGroupAction.java
Expand Up @@ -4,6 +4,7 @@
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import mobi.hsz.idea.gitignore.GitignoreBundle;
import mobi.hsz.idea.gitignore.util.ExternalFileException;
import mobi.hsz.idea.gitignore.util.Icons;
import mobi.hsz.idea.gitignore.util.Utils;
import org.jetbrains.annotations.NotNull;
Expand All @@ -30,9 +31,15 @@ public void update(AnActionEvent e) {
final Project project = e.getData(CommonDataKeys.PROJECT);
files.clear();
if (project != null && file != null) {
files.addAll(Utils.getSuitableGitignoreFiles(project, file));
Collections.reverse(files);
baseDir = project.getBaseDir();
try {
files.addAll(Utils.getSuitableGitignoreFiles(project, file));
e.getPresentation().setVisible(true);
Collections.reverse(files);
baseDir = project.getBaseDir();
} catch (ExternalFileException e1) {
e.getPresentation().setVisible(false);
e1.printStackTrace();
}
}
setPopup(files.size() > 1);
}
Expand Down
6 changes: 6 additions & 0 deletions src/mobi/hsz/idea/gitignore/util/ExternalFileException.java
@@ -0,0 +1,6 @@
package mobi.hsz.idea.gitignore.util;

import mobi.hsz.idea.gitignore.GitignoreException;

public class ExternalFileException extends GitignoreException {
}
6 changes: 5 additions & 1 deletion src/mobi/hsz/idea/gitignore/util/Utils.java
Expand Up @@ -2,6 +2,7 @@

import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
Expand Down Expand Up @@ -73,8 +74,11 @@ public static Collection<VirtualFile> getGitignoreFiles(@NotNull Project project
return FilenameIndex.getVirtualFilesByName(project, GitignoreLanguage.FILENAME, GlobalSearchScope.projectScope(project));
}

public static List<VirtualFile> getSuitableGitignoreFiles(@NotNull Project project, @NotNull VirtualFile file) {
public static List<VirtualFile> getSuitableGitignoreFiles(@NotNull Project project, @NotNull VirtualFile file) throws ExternalFileException {
List<VirtualFile> files = new ArrayList<VirtualFile>();
if (file.getCanonicalPath() == null || !StringUtil.startsWith(file.getCanonicalPath(), project.getBasePath())) {
throw new ExternalFileException();
}
if (!project.getBaseDir().equals(file)) {
do {
file = file.getParent();
Expand Down

0 comments on commit 5578274

Please sign in to comment.