/* * Copyright 2000-2015 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.jetbrains.plugins.groovy.util; import com.intellij.openapi.project.DumbService; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.FileIndexFacade; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; import com.intellij.psi.impl.cache.impl.id.IdIndex; import com.intellij.psi.impl.cache.impl.id.IdIndexEntry; import com.intellij.psi.search.GlobalSearchScope; import com.intellij.testFramework.LightVirtualFile; import com.intellij.util.indexing.FileBasedIndex; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase; public class GrFileIndexUtil { public static boolean isGroovySourceFile(@NotNull PsiFile file) { return file instanceof GroovyFileBase && isGroovySourceFile((GroovyFileBase)file); } public static boolean isGroovySourceFile(@NotNull GroovyFileBase file) { return isInSourceFiles(file.getVirtualFile(), file.getProject()); } private static boolean isInSourceFiles(@Nullable VirtualFile file, @NotNull Project project) { if (file != null && !(file instanceof LightVirtualFile)) { final FileIndexFacade index = FileIndexFacade.getInstance(project); if (index.isInSource(file) || index.isInLibraryClasses(file)) { return true; } } return false; } public static boolean hasNameInFile(@NotNull PsiFile file, @NotNull String name) { if (file.getVirtualFile() == null || DumbService.isDumb(file.getProject())) { return StringUtil.contains(file.getViewProvider().getContents(), name); } GlobalSearchScope scope = GlobalSearchScope.fileScope(file); return !FileBasedIndex.getInstance().getContainingFiles(IdIndex.NAME, new IdIndexEntry(name, true), scope).isEmpty(); } }