2 * Copyright 2000-2009 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com
.intellij
.psi
.search
;
19 import com
.intellij
.ide
.impl
.ProjectUtil
;
20 import com
.intellij
.openapi
.application
.ApplicationManager
;
21 import com
.intellij
.openapi
.application
.ex
.ApplicationEx
;
22 import com
.intellij
.openapi
.project
.Project
;
23 import com
.intellij
.openapi
.vfs
.VirtualFile
;
24 import com
.intellij
.psi
.PsiFile
;
25 import com
.intellij
.psi
.PsiManager
;
26 import com
.intellij
.util
.ArrayUtil
;
27 import com
.intellij
.util
.indexing
.*;
28 import com
.intellij
.util
.io
.EnumeratorStringDescriptor
;
29 import com
.intellij
.util
.io
.KeyDescriptor
;
30 import org
.jetbrains
.annotations
.NonNls
;
31 import org
.jetbrains
.annotations
.NotNull
;
38 public class FilenameIndex
extends ScalarIndexExtension
<String
> {
39 @NonNls public static final ID
<String
, Void
> NAME
= ID
.create("FilenameIndex");
40 private final MyDataIndexer myDataIndexer
= new MyDataIndexer();
41 private final MyInputFilter myInputFilter
= new MyInputFilter();
42 private final EnumeratorStringDescriptor myKeyDescriptor
= new EnumeratorStringDescriptor();
44 public ID
<String
,Void
> getName() {
48 public DataIndexer
<String
, Void
, FileContent
> getIndexer() {
52 public KeyDescriptor
<String
> getKeyDescriptor() {
53 return myKeyDescriptor
;
56 public FileBasedIndex
.InputFilter
getInputFilter() {
60 public boolean dependsOnFileContent() {
64 public int getVersion() {
68 public static String
[] getAllFilenames(Project project
) {
69 final Collection
<String
> allKeys
= FileBasedIndex
.getInstance().getAllKeys(NAME
, project
);
70 return ArrayUtil
.toStringArray(allKeys
);
73 public static Collection
<VirtualFile
> getVirtualFilesByName(final Project project
, final String name
, final GlobalSearchScope scope
) {
74 return FileBasedIndex
.getInstance().getContainingFiles(NAME
, name
, scope
);
77 public static PsiFile
[] getFilesByName(final Project project
, final String name
, final GlobalSearchScope scope
) {
78 final Collection
<VirtualFile
> files
= FileBasedIndex
.getInstance().getContainingFiles(NAME
, name
, scope
);
79 if (files
.isEmpty()) return PsiFile
.EMPTY_ARRAY
;
80 List
<PsiFile
> result
= new ArrayList
<PsiFile
>();
81 for(VirtualFile file
: files
) {
82 if (!file
.isValid()) continue;
83 PsiFile psiFile
= PsiManager
.getInstance(project
).findFile(file
);
84 if (psiFile
!= null) {
88 return result
.toArray(new PsiFile
[result
.size()]);
91 private static class MyDataIndexer
implements DataIndexer
<String
, Void
, FileContent
> {
93 public Map
<String
, Void
> map(final FileContent inputData
) {
94 return Collections
.singletonMap(inputData
.getFileName(), null);
98 private static class MyInputFilter
implements FileBasedIndex
.InputFilter
{
99 public boolean acceptInput(final VirtualFile file
) {
100 if (((ApplicationEx
)ApplicationManager
.getApplication()).isInternal()) {
104 return !ProjectUtil
.isProjectOrWorkspaceFile(file
);