Fix potential leak of ScanContent in user data of library file

Todo index isn't calculated for library files anymore, but BaseFilterLexerUtil
adds ScanContent to the user data only if it will be needed for both Id
and Todo indexes and it is expected to be removed by the second.
This commit is contained in:
Dmitry Trofimov
2017-10-10 00:21:55 +02:00
parent d484054fe7
commit 4c92dc7d45
3 changed files with 30 additions and 23 deletions
@@ -23,9 +23,6 @@ import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeRegistry;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.impl.cache.impl.id.PlatformIdTableBuilding;
@@ -50,7 +47,7 @@ import java.util.Map;
/**
* @author Eugene Zhuravlev
* Date: Jan 20, 2008
* Date: Jan 20, 2008
*/
public class TodoIndex extends FileBasedIndexExtension<TodoIndexEntry, Integer> {
@NonNls public static final ID<TodoIndexEntry, Integer> NAME = ID.create("TodoIndex");
@@ -102,7 +99,7 @@ public class TodoIndex extends FileBasedIndexExtension<TodoIndexEntry, Integer>
private final DataIndexer<TodoIndexEntry, Integer, FileContent> myIndexer = new DataIndexer<TodoIndexEntry, Integer, FileContent>() {
@Override
@NotNull
public Map<TodoIndexEntry,Integer> map(@NotNull final FileContent inputData) {
public Map<TodoIndexEntry, Integer> map(@NotNull final FileContent inputData) {
final VirtualFile file = inputData.getFile();
final DataIndexer<TodoIndexEntry, Integer, FileContent> indexer = PlatformIdTableBuilding
.getTodoIndexer(inputData.getFileType(), file);
@@ -114,13 +111,7 @@ public class TodoIndex extends FileBasedIndexExtension<TodoIndexEntry, Integer>
};
protected final FileBasedIndex.InputFilter myInputFilter = file -> {
if (!file.isInLocalFileSystem()) {
return false; // do not index TODOs in library sources
}
if(!isInContentOfAnyProject(file)) {
return false;
}
if (!TodoIndexers.needsTodoIndex(file)) return false;
final FileType fileType = file.getFileType();
@@ -135,22 +126,13 @@ public class TodoIndex extends FileBasedIndexExtension<TodoIndexEntry, Integer>
fileType instanceof CustomSyntaxTableFileType;
};
private static boolean isInContentOfAnyProject(@NotNull VirtualFile file) {
for (Project project : ProjectManager.getInstance().getOpenProjects()) {
if (ProjectFileIndex.getInstance(project).isInContent(file)) {
return true;
}
}
return false;
}
@Override
public int getVersion() {
int version = 10;
FileType[] types = myFileTypeManager.getRegisteredFileTypes();
Arrays.sort(types, (o1, o2) -> Comparing.compare(o1.getName(), o2.getName()));
for(FileType fileType:types) {
for (FileType fileType : types) {
DataIndexer<TodoIndexEntry, Integer, FileContent> indexer = TodoIndexers.INSTANCE.forFileType(fileType);
if (indexer == null) continue;
@@ -21,6 +21,7 @@ import com.intellij.psi.impl.cache.impl.id.IdIndexEntry;
import com.intellij.psi.impl.cache.impl.id.IdTableBuilding;
import com.intellij.psi.impl.cache.impl.id.LexingIdIndexer;
import com.intellij.psi.impl.cache.impl.todo.TodoIndexEntry;
import com.intellij.psi.impl.cache.impl.todo.TodoIndexers;
import com.intellij.psi.search.IndexPattern;
import com.intellij.util.indexing.FileContent;
import com.intellij.util.indexing.IdDataConsumer;
@@ -39,7 +40,7 @@ public class BaseFilterLexerUtil {
return data;
}
final boolean needTodo = content.getFile().isInLocalFileSystem(); // same as TodoIndex.getFilter().isAcceptable
final boolean needTodo = TodoIndexers.needsTodoIndex(content.getFile());
final boolean needIdIndex = IdTableBuilding.getFileTypeIndexer(content.getFileType()) instanceof LexingIdIndexer;
final IdDataConsumer consumer = needIdIndex? new IdDataConsumer():null;
@@ -17,8 +17,13 @@
package com.intellij.psi.impl.cache.impl.todo;
import com.intellij.openapi.fileTypes.FileTypeExtension;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.indexing.DataIndexer;
import com.intellij.util.indexing.FileContent;
import org.jetbrains.annotations.NotNull;
/**
* @author yole
@@ -29,4 +34,23 @@ public class TodoIndexers extends FileTypeExtension<DataIndexer<TodoIndexEntry,
private TodoIndexers() {
super("com.intellij.todoIndexer");
}
public static boolean needsTodoIndex(@NotNull VirtualFile file) {
if (!file.isInLocalFileSystem()) {
return false;
}
if (!isInContentOfAnyProject(file)) {
return false;
}
return true;
}
private static boolean isInContentOfAnyProject(@NotNull VirtualFile file) {
for (Project project : ProjectManager.getInstance().getOpenProjects()) {
if (ProjectFileIndex.getInstance(project).isInContent(file)) {
return true;
}
}
return false;
}
}