1 package com
.intellij
.psi
.impl
.cache
.impl
.todo
;
3 import com
.intellij
.ide
.impl
.ProjectUtil
;
4 import com
.intellij
.ide
.todo
.TodoConfiguration
;
5 import com
.intellij
.lang
.Language
;
6 import com
.intellij
.lang
.LanguageParserDefinitions
;
7 import com
.intellij
.lang
.ParserDefinition
;
8 import com
.intellij
.openapi
.fileTypes
.FileType
;
9 import com
.intellij
.openapi
.fileTypes
.FileTypeManager
;
10 import com
.intellij
.openapi
.fileTypes
.LanguageFileType
;
11 import com
.intellij
.openapi
.fileTypes
.impl
.AbstractFileType
;
12 import com
.intellij
.openapi
.vfs
.LocalFileSystem
;
13 import com
.intellij
.openapi
.vfs
.VirtualFile
;
14 import com
.intellij
.psi
.impl
.cache
.impl
.id
.IdTableBuilding
;
15 import com
.intellij
.psi
.search
.IndexPatternProvider
;
16 import com
.intellij
.psi
.tree
.TokenSet
;
17 import com
.intellij
.util
.indexing
.*;
18 import com
.intellij
.util
.io
.DataExternalizer
;
19 import com
.intellij
.util
.io
.KeyDescriptor
;
20 import org
.jetbrains
.annotations
.NonNls
;
21 import org
.jetbrains
.annotations
.NotNull
;
23 import java
.beans
.PropertyChangeEvent
;
24 import java
.beans
.PropertyChangeListener
;
25 import java
.io
.DataInput
;
26 import java
.io
.DataOutput
;
27 import java
.io
.IOException
;
28 import java
.util
.Collections
;
32 * @author Eugene Zhuravlev
35 public class TodoIndex
extends FileBasedIndexExtension
<TodoIndexEntry
, Integer
> {
36 @NonNls public static final ID
<TodoIndexEntry
, Integer
> NAME
= ID
.create("TodoIndex");
38 public TodoIndex(TodoConfiguration config
) {
39 config
.addPropertyChangeListener(new PropertyChangeListener() {
40 public void propertyChange(final PropertyChangeEvent evt
) {
41 if (IndexPatternProvider
.PROP_INDEX_PATTERNS
.equals(evt
.getPropertyName())) {
42 FileBasedIndex
.getInstance().requestRebuild(NAME
);
48 private final KeyDescriptor
<TodoIndexEntry
> myKeyDescriptor
= new KeyDescriptor
<TodoIndexEntry
>() {
49 public int getHashCode(final TodoIndexEntry value
) {
50 return value
.hashCode();
53 public boolean isEqual(final TodoIndexEntry val1
, final TodoIndexEntry val2
) {
54 return val1
.equals(val2
);
57 public void save(final DataOutput out
, final TodoIndexEntry value
) throws IOException
{
58 out
.writeUTF(value
.pattern
);
59 out
.writeBoolean(value
.caseSensitive
);
62 public TodoIndexEntry
read(final DataInput in
) throws IOException
{
63 final String pattern
= in
.readUTF();
64 final boolean caseSensitive
= in
.readBoolean();
65 return new TodoIndexEntry(pattern
, caseSensitive
);
69 private final DataExternalizer
<Integer
> myValueExternalizer
= new DataExternalizer
<Integer
>() {
70 public void save(final DataOutput out
, final Integer value
) throws IOException
{
71 out
.writeInt(value
.intValue());
74 public Integer
read(final DataInput in
) throws IOException
{
75 return Integer
.valueOf(in
.readInt());
79 private final DataIndexer
<TodoIndexEntry
, Integer
, FileContent
> myIndexer
= new DataIndexer
<TodoIndexEntry
, Integer
, FileContent
>() {
81 public Map
<TodoIndexEntry
,Integer
> map(final FileContent inputData
) {
82 final VirtualFile file
= inputData
.getFile();
83 final DataIndexer
<TodoIndexEntry
, Integer
, FileContent
> indexer
= IdTableBuilding
.getTodoIndexer(inputData
.getFileType(), file
);
84 if (indexer
!= null) {
85 return indexer
.map(inputData
);
87 return Collections
.emptyMap();
91 private final FileBasedIndex
.InputFilter myInputFilter
= new FileBasedIndex
.InputFilter() {
92 private final FileTypeManager myFtManager
= FileTypeManager
.getInstance();
93 public boolean acceptInput(final VirtualFile file
) {
94 if (!(file
.getFileSystem() instanceof LocalFileSystem
)) {
95 return false; // do not index TODOs in library sources
98 final FileType fileType
= myFtManager
.getFileTypeByFile(file
);
99 if (ProjectUtil
.isProjectOrWorkspaceFile(file
, fileType
)) {
103 if (fileType
instanceof LanguageFileType
) {
104 final Language lang
= ((LanguageFileType
)fileType
).getLanguage();
105 final ParserDefinition parserDef
= LanguageParserDefinitions
.INSTANCE
.forLanguage(lang
);
106 final TokenSet commentTokens
= parserDef
!= null ? parserDef
.getCommentTokens() : null;
107 return commentTokens
!= null;
110 return IdTableBuilding
.isTodoIndexerRegistered(fileType
) ||
111 fileType
instanceof AbstractFileType
;
115 public int getVersion() {
119 public boolean dependsOnFileContent() {
123 public ID
<TodoIndexEntry
, Integer
> getName() {
127 public DataIndexer
<TodoIndexEntry
, Integer
, FileContent
> getIndexer() {
131 public KeyDescriptor
<TodoIndexEntry
> getKeyDescriptor() {
132 return myKeyDescriptor
;
135 public DataExternalizer
<Integer
> getValueExternalizer() {
136 return myValueExternalizer
;
139 public FileBasedIndex
.InputFilter
getInputFilter() {
140 return myInputFilter
;