d663258df68203517569eef286cc80026cf525b7
[idea/community.git] / platform / lang-impl / src / com / intellij / ide / actions / GotoFileItemProvider.java
1 /*
2  * Copyright 2000-2014 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.ide.actions;
17
18 import com.intellij.ide.util.gotoByName.ChooseByNameBase;
19 import com.intellij.ide.util.gotoByName.ChooseByNamePopup;
20 import com.intellij.ide.util.gotoByName.DefaultChooseByNameItemProvider;
21 import com.intellij.ide.util.gotoByName.GotoFileModel;
22 import com.intellij.openapi.progress.ProgressIndicator;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.openapi.roots.ProjectFileIndex;
25 import com.intellij.openapi.util.io.FileUtil;
26 import com.intellij.openapi.vfs.LocalFileSystem;
27 import com.intellij.openapi.vfs.VirtualFile;
28 import com.intellij.psi.PsiElement;
29 import com.intellij.psi.PsiFileSystemItem;
30 import com.intellij.psi.PsiManager;
31 import com.intellij.util.Processor;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
34
35 /**
36 * @author peter
37 */
38 public class GotoFileItemProvider extends DefaultChooseByNameItemProvider {
39   private final Project myProject;
40   private final GotoFileModel myModel;
41
42   public GotoFileItemProvider(@NotNull Project project, @Nullable PsiElement context, GotoFileModel model) {
43     super(context);
44     myProject = project;
45     myModel = model;
46   }
47
48   @Override
49   public boolean filterElements(@NotNull ChooseByNameBase base,
50                                 @NotNull String pattern,
51                                 boolean everywhere,
52                                 @NotNull ProgressIndicator indicator,
53                                 @NotNull Processor<Object> consumer) {
54     if (pattern.contains("/") || pattern.contains("\\")) {
55       String path = FileUtil.toSystemIndependentName(ChooseByNamePopup.getTransformedPattern(pattern, myModel));
56       VirtualFile vFile = LocalFileSystem.getInstance().findFileByPathIfCached(path);
57       if (vFile != null) {
58         ProjectFileIndex index = ProjectFileIndex.SERVICE.getInstance(myProject);
59         if (index.isInContent(vFile) || index.isInLibraryClasses(vFile) || index.isInLibrarySource(vFile)) {
60           PsiFileSystemItem fileOrDir = vFile.isDirectory() ?
61                                         PsiManager.getInstance(myProject).findDirectory(vFile) :
62                                         PsiManager.getInstance(myProject).findFile(vFile);
63           if (fileOrDir != null && !consumer.process(fileOrDir)) {
64             return false;
65           }
66         }
67       }
68     }
69
70     return super.filterElements(base, pattern, everywhere, indicator, consumer);
71   }
72 }