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
.openapi
.roots
.impl
;
19 import com
.intellij
.openapi
.fileTypes
.FileTypeManager
;
20 import com
.intellij
.openapi
.fileTypes
.StdFileTypes
;
21 import com
.intellij
.openapi
.module
.Module
;
22 import com
.intellij
.openapi
.roots
.ContentIterator
;
23 import com
.intellij
.openapi
.roots
.ModuleFileIndex
;
24 import com
.intellij
.openapi
.roots
.ModuleRootManager
;
25 import com
.intellij
.openapi
.roots
.OrderEntry
;
26 import com
.intellij
.openapi
.vfs
.VirtualFile
;
27 import com
.intellij
.openapi
.vfs
.VirtualFileFilter
;
28 import org
.jetbrains
.annotations
.NotNull
;
30 import java
.util
.ArrayList
;
31 import java
.util
.Collections
;
32 import java
.util
.List
;
34 public class ModuleFileIndexImpl
implements ModuleFileIndex
{
35 private final Module myModule
;
36 private final FileTypeManager myFileTypeManager
;
37 private final DirectoryIndex myDirectoryIndex
;
38 private final ContentFilter myContentFilter
;
40 public ModuleFileIndexImpl(Module module
, DirectoryIndex directoryIndex
) {
42 myDirectoryIndex
= directoryIndex
;
43 myFileTypeManager
= FileTypeManager
.getInstance();
45 myContentFilter
= new ContentFilter();
48 public boolean iterateContent(@NotNull ContentIterator iterator
) {
49 VirtualFile
[] contentRoots
= ModuleRootManager
.getInstance(myModule
).getContentRoots();
50 for (VirtualFile contentRoot
: contentRoots
) {
51 VirtualFile parent
= contentRoot
.getParent();
53 DirectoryInfo parentInfo
= myDirectoryIndex
.getInfoForDirectory(parent
);
54 if (parentInfo
!= null && myModule
.equals(parentInfo
.module
)) continue; // inner content - skip it
57 boolean finished
= FileIndexImplUtil
.iterateRecursively(contentRoot
, myContentFilter
, iterator
);
58 if (!finished
) return false;
64 public boolean iterateContentUnderDirectory(@NotNull VirtualFile dir
, @NotNull ContentIterator iterator
) {
65 return FileIndexImplUtil
.iterateRecursively(dir
, myContentFilter
, iterator
);
68 public boolean isContentJavaSourceFile(@NotNull VirtualFile file
) {
69 return !file
.isDirectory()
70 && myFileTypeManager
.getFileTypeByFile(file
) == StdFileTypes
.JAVA
71 && !myFileTypeManager
.isFileIgnored(file
.getName())
72 && isInSourceContent(file
);
75 public boolean isInContent(@NotNull VirtualFile fileOrDir
) {
76 if (fileOrDir
.isDirectory()) {
77 DirectoryInfo info
= myDirectoryIndex
.getInfoForDirectory(fileOrDir
);
78 return info
!= null && myModule
.equals(info
.module
);
81 VirtualFile parent
= fileOrDir
.getParent();
82 return parent
!= null && isInContent(parent
);
86 public boolean isInSourceContent(@NotNull VirtualFile fileOrDir
) {
87 if (fileOrDir
.isDirectory()) {
88 DirectoryInfo info
= myDirectoryIndex
.getInfoForDirectory(fileOrDir
);
89 return info
!= null && info
.isInModuleSource
&& myModule
.equals(info
.module
);
92 VirtualFile parent
= fileOrDir
.getParent();
93 return parent
!= null && isInSourceContent(parent
);
98 public List
<OrderEntry
> getOrderEntriesForFile(@NotNull VirtualFile fileOrDir
) {
99 VirtualFile dir
= fileOrDir
.isDirectory() ? fileOrDir
: fileOrDir
.getParent();
100 if (dir
== null) return Collections
.emptyList();
101 final DirectoryInfo info
= myDirectoryIndex
.getInfoForDirectory(dir
);
102 if (info
== null) return Collections
.emptyList();
103 return new ArrayList
<OrderEntry
>(info
.getOrderEntries(myModule
));
106 public OrderEntry
getOrderEntryForFile(@NotNull VirtualFile fileOrDir
) {
107 VirtualFile dir
= fileOrDir
.isDirectory() ? fileOrDir
: fileOrDir
.getParent();
108 if (dir
== null) return null;
109 final DirectoryInfo info
= myDirectoryIndex
.getInfoForDirectory(dir
);
110 if (info
== null) return null;
111 final List
<OrderEntry
> orderEntries
= info
.getOrderEntries();
112 //noinspection ForLoopReplaceableByForEach
113 for (int i
= 0; i
< orderEntries
.size(); i
++) {
114 final OrderEntry orderEntry
= orderEntries
.get(i
);
115 if (orderEntry
.getOwnerModule() == myModule
) return orderEntry
;
120 public boolean isInTestSourceContent(@NotNull VirtualFile fileOrDir
) {
121 if (fileOrDir
.isDirectory()) {
122 DirectoryInfo info
= myDirectoryIndex
.getInfoForDirectory(fileOrDir
);
123 return info
!= null && info
.isInModuleSource
&& info
.isTestSource
&& myModule
.equals(info
.module
);
126 VirtualFile parent
= fileOrDir
.getParent();
127 return parent
!= null && isInTestSourceContent(parent
);
131 private class ContentFilter
implements VirtualFileFilter
{
132 public boolean accept(@NotNull VirtualFile file
) {
133 if (file
.isDirectory()) {
134 DirectoryInfo info
= myDirectoryIndex
.getInfoForDirectory(file
);
135 return info
!= null && myModule
.equals(info
.module
);
138 return !myFileTypeManager
.isFileIgnored(file
.getName());