下面列出了怎么用com.intellij.psi.search.EverythingGlobalScope的API类实例代码及写法,或者点击链接到github查看源代码。
public void setSelectedPatch(Patch selectedPatch) {
System.out.println(selectedPatch.asString() + " selected! ");
this.selectedPatch = selectedPatch;
final SourceLocation location = this.selectedPatch.getSourceLocation();
PsiClass classToBeFix = JavaPsiFacade.getInstance(this.project).findClass(this.selectedPatch.getRootClassName(), new EverythingGlobalScope(this.project));
classToBeFix.accept(new JavaRecursiveElementVisitor() {
@Override
public void visitStatement(PsiStatement statement) {
if (location.getBeginSource() == statement.getTextOffset() &&
location.getEndSource() == statement.getTextOffset() + statement.getTextLength() - 1) {
buggyElement = statement;
}
super.visitStatement(statement);
}
});
}
protected void generateInjects(@NotNull IButterKnife butterKnife) {
PsiClass activityClass = JavaPsiFacade.getInstance(mProject).findClass(
"android.app.Activity", new EverythingGlobalScope(mProject));
PsiClass fragmentClass = JavaPsiFacade.getInstance(mProject).findClass(
"android.app.Fragment", new EverythingGlobalScope(mProject));
PsiClass supportFragmentClass = JavaPsiFacade.getInstance(mProject).findClass(
"android.support.v4.app.Fragment", new EverythingGlobalScope(mProject));
// Check for Activity class
if (activityClass != null && mClass.isInheritor(activityClass, true)) {
generateActivityBind(butterKnife);
// Check for Fragment class
} else if ((fragmentClass != null && mClass.isInheritor(fragmentClass, true)) || (supportFragmentClass != null && mClass.isInheritor(supportFragmentClass, true))) {
generateFragmentBindAndUnbind(butterKnife);
}
}
@Nullable
@Override
public GlobalSearchScope getScopeToExclude(PsiElement element) {
if (!enabled.getValue()) {
return null;
}
if (element instanceof PsiFileSystemItem) {
return GlobalSearchScope.getScopeRestrictedByFileTypes(
new EverythingGlobalScope(), BuildFileType.INSTANCE);
}
return null;
}
private static PsiFile resolveLayoutResourceFile(PsiElement element, Project project, String name) {
// restricting the search to the current module - searching the whole project could return wrong layouts
Module module = ModuleUtil.findModuleForPsiElement(element);
PsiFile[] files = null;
if (module != null) {
// first omit libraries, it might cause issues like (#103)
GlobalSearchScope moduleScope = module.getModuleWithDependenciesScope();
files = FilenameIndex.getFilesByName(project, name, moduleScope);
if (files == null || files.length <= 0) {
// now let's do a fallback including the libraries
moduleScope = module.getModuleWithDependenciesAndLibrariesScope(false);
files = FilenameIndex.getFilesByName(project, name, moduleScope);
}
}
if (files == null || files.length <= 0) {
// fallback to search through the whole project
// useful when the project is not properly configured - when the resource directory is not configured
files = FilenameIndex.getFilesByName(project, name, new EverythingGlobalScope(project));
if (files.length <= 0) {
return null; //no matching files
}
}
// TODO - we have a problem here - we still can have multiple layouts (some coming from a dependency)
// we need to resolve R class properly and find the proper layout for the R class
for (PsiFile file : files) {
log.info("Resolved layout resource file for name [" + name + "]: " + file.getVirtualFile());
}
return files[0];
}
@Nonnull
public static GlobalSearchScope searchScopeFor(@Nullable Project project, boolean searchInLibraries) {
GlobalSearchScope baseScope = project == null ? new EverythingGlobalScope() : searchInLibraries ? ProjectScope.getAllScope(project) : ProjectScope.getProjectScope(project);
return baseScope.intersectWith(new EverythingGlobalScope(project) {
@Override
public boolean contains(@Nonnull VirtualFile file) {
return !(file.getFileSystem() instanceof HiddenFileSystem);
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
final Project project = this.parent.getProject();
PsiElement buggyElement = this.parent.getBuggyElement();
final Patch selectedPatch = this.parent.getSelectedPatch();
PsiClass classToBeFix = JavaPsiFacade.getInstance(project).findClass(selectedPatch.getRootClassName(), new EverythingGlobalScope(project));
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, classToBeFix.getContainingFile().getVirtualFile(), buggyElement.getTextOffset());
Editor editor = FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
Document modifiedDocument = editor.getDocument();
final String patch;
if (selectedPatch.getType() == RepairType.CONDITIONAL) {
buggyElement = ((PsiIfStatement) buggyElement).getCondition();
patch = selectedPatch.asString();
} else {
String newline = FileDocumentManager.getInstance().getFile(modifiedDocument).getDetectedLineSeparator();
StringBuilder sb = new StringBuilder();
sb.append("if( ");
sb.append(selectedPatch.asString());
sb.append(" ) {" + newline);
sb.append(buggyElement.getText() + newline);
sb.append("}");
patch = sb.toString();
}
final PsiElement finalBuggyElement = buggyElement;
CommandProcessor.getInstance().executeCommand(project, () -> WriteCommandAction.runWriteCommandAction(project, () -> {
//Apply the patch
modifiedDocument.replaceString(finalBuggyElement.getTextOffset(), finalBuggyElement.getTextOffset() + finalBuggyElement.getTextLength(), patch);
//Move caret to modification
editor.getCaretModel().moveToOffset(finalBuggyElement.getTextOffset());
//Select patch
editor.getSelectionModel().setSelection(finalBuggyElement.getTextOffset(), finalBuggyElement.getTextOffset() +
(selectedPatch.getType() == RepairType.CONDITIONAL ? finalBuggyElement.getTextLength() : patch.length()));
PsiDocumentManager.getInstance(project).commitDocument(modifiedDocument);
CodeStyleManager.getInstance(project).reformat(PsiDocumentManager.getInstance(project).getPsiFile(modifiedDocument), false);
}), "Apply Patch", DocCommandGroupId.noneGroupId(modifiedDocument));
PsiDocumentManager.getInstance(project).commitDocument(modifiedDocument);
parent.close(0);
}
public static boolean isClassAvailableForProject(Project project, String className) {
PsiClass classInModule = JavaPsiFacade.getInstance(project).findClass(className,
new EverythingGlobalScope(project));
return classInModule != null;
}
@Override
public boolean execute(@Nonnull final DirectTypeInheritorsSearch.SearchParameters p, @Nonnull final Processor<? super DotNetTypeDeclaration> consumer)
{
String vmQName = p.getVmQName();
/*if(DotNetTypes.System_Object.equals(qualifiedName))
{
final SearchScope scope = useScope;
return AllClassesSearch.search(scope, aClass.getProject()).forEach(new Processor<DotNetTypeDeclaration>()
{
@Override
public boolean process(final DotNetTypeDeclaration typeDcl)
{
if(typeDcl.isInterface())
{
return consumer.process(typeDcl);
}
final DotNetTypeDeclaration superClass = typeDcl.getSuperClass();
if(superClass != null && DotNetTypes.System_Object.equals(ApplicationManager.getApplication().runReadAction(
new Computable<String>()
{
public String compute()
{
return superClass.getPresentableQName();
}
})))
{
return consumer.process(typeDcl);
}
return true;
}
});
} */
SearchScope useScope = p.getScope();
final GlobalSearchScope scope = useScope instanceof GlobalSearchScope ? (GlobalSearchScope) useScope : new EverythingGlobalScope(p
.getProject());
final String searchKey = MsilHelper.cutGenericMarker(StringUtil.getShortName(vmQName));
if(StringUtil.isEmpty(searchKey))
{
return true;
}
Collection<DotNetTypeList> candidates = ApplicationManager.getApplication().runReadAction((Computable<Collection<DotNetTypeList>>) () -> ExtendsListIndex.getInstance().get(searchKey, p.getProject(), scope));
Map<String, List<DotNetTypeDeclaration>> classes = new HashMap<>();
for(DotNetTypeList referenceList : candidates)
{
ProgressIndicatorProvider.checkCanceled();
final DotNetTypeDeclaration candidate = ReadAction.compute(() -> (DotNetTypeDeclaration) referenceList.getParent());
if(!checkInheritance(p, vmQName, candidate))
{
continue;
}
String fqn = ReadAction.compute(candidate::getPresentableQName);
List<DotNetTypeDeclaration> list = classes.get(fqn);
if(list == null)
{
list = new ArrayList<>();
classes.put(fqn, list);
}
list.add(candidate);
}
for(List<DotNetTypeDeclaration> sameNamedClasses : classes.values())
{
for(DotNetTypeDeclaration sameNamedClass : sameNamedClasses)
{
if(!consumer.process(sameNamedClass))
{
return false;
}
}
}
return true;
}
@Nonnull
@Override
public GlobalSearchScope buildEverythingScope() {
return new EverythingGlobalScope(myProject);
}
@Override
public GlobalSearchScope buildAllScope() {
return new EverythingGlobalScope();
}
@Override
public <K> boolean processAllKeys(@Nonnull final ID<K, ?> indexId, @Nonnull Processor<? super K> processor, @Nullable Project project) {
return processAllKeys(indexId, processor, project == null ? new EverythingGlobalScope() : GlobalSearchScope.allScope(project), null);
}
/**
* Check whether classpath of a the whole project contains given class.
* This is only fallback for wrongly setup projects.
*
* @param project Project
* @param className Class name of the searched class
* @return True if the class is present on the classpath
* @since 1.3.1
*/
public static boolean isClassAvailableForProject(@NotNull Project project, @NotNull String className) {
PsiClass classInModule = JavaPsiFacade.getInstance(project).findClass(className,
new EverythingGlobalScope(project));
return classInModule != null;
}
/**
* Check whether classpath of a the whole project contains given class.
* This is only fallback for wrongly setup projects.
*
* @param project Project
* @param className Class name of the searched class
* @return True if the class is present on the classpath
* @since 1.3.1
*/
public static boolean isClassAvailableForProject(@NotNull Project project, @NotNull String className) {
PsiClass classInModule = JavaPsiFacade.getInstance(project).findClass(className,
new EverythingGlobalScope(project));
return classInModule != null;
}