use of com.intellij.psi.search.LocalSearchScope in project intellij-plugins by JetBrains.
the class FlexMoveInnerClassProcessor method findUsages.
@NotNull
@Override
protected UsageInfo[] findUsages() {
final Collection<UsageInfo> result = Collections.synchronizedCollection(new ArrayList<UsageInfo>());
ReferencesSearch.search(myElement, new LocalSearchScope(myElement.getContainingFile())).forEach(reference -> {
final PsiElement element = reference.getElement();
if (!(element instanceof JSReferenceExpression)) {
return true;
}
if (JSResolveUtil.isSelfReference(element)) {
return true;
}
result.add(new UsageInfo(element));
return true;
});
if (myElement instanceof JSClass) {
final JSFunction constructor = ((JSClass) myElement).getConstructor();
if (constructor != null) {
result.add(new UsageInfo(constructor));
JSRefactoringUtil.addConstructorUsages((JSClass) myElement, result);
}
}
TextOccurrencesUtil.findNonCodeUsages(myElement, myElement.getName(), mySearchInComments, mySearchTextOccurences, StringUtil.getQualifiedName(myPackageName, myClassName), result);
return UsageViewUtil.removeDuplicatedUsages(result.toArray(new UsageInfo[result.size()]));
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class AutoUnboxingInspection method isFixApplicable.
private static boolean isFixApplicable(PsiExpression location) {
// conservative check to see if the result value of the postfix
// expression is used later in the same expression statement.
// Applying the quick fix in such a case would break the code
// because the explicit unboxing code would split the expression in
// multiple statements.
final PsiElement parent = location.getParent();
if (!(parent instanceof PsiPostfixExpression)) {
return true;
}
final PsiReferenceExpression reference;
if (location instanceof PsiReferenceExpression) {
reference = (PsiReferenceExpression) location;
} else if (location instanceof PsiArrayAccessExpression) {
final PsiArrayAccessExpression arrayAccessExpression = (PsiArrayAccessExpression) location;
final PsiExpression expression = arrayAccessExpression.getArrayExpression();
if (!(expression instanceof PsiReferenceExpression)) {
return true;
}
reference = (PsiReferenceExpression) expression;
} else {
return true;
}
final PsiElement element = reference.resolve();
if (element == null) {
return true;
}
final PsiStatement statement = PsiTreeUtil.getParentOfType(parent, PsiStatement.class);
final LocalSearchScope scope = new LocalSearchScope(statement);
final Query<PsiReference> query = ReferencesSearch.search(element, scope);
final Collection<PsiReference> references = query.findAll();
return references.size() <= 1;
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class InspectionDescriptionInfo method doFindExtension.
@Nullable
private static Extension doFindExtension(Module module, PsiClass psiClass) {
// Try search in narrow scopes first
Project project = module.getProject();
Set<DomFileElement<IdeaPlugin>> processed = new HashSet<>();
for (GlobalSearchScope scope : DescriptionCheckerUtil.searchScopes(module)) {
List<DomFileElement<IdeaPlugin>> origElements = DomService.getInstance().getFileElements(IdeaPlugin.class, project, scope);
origElements.removeAll(processed);
List<DomFileElement<IdeaPlugin>> elements = PluginDescriptorChooser.findAppropriateIntelliJModule(module.getName(), origElements);
Query<PsiReference> query = ReferencesSearch.search(psiClass, new LocalSearchScope(elements.stream().map(DomFileElement::getFile).toArray(PsiElement[]::new)));
Ref<Extension> result = Ref.create(null);
query.forEach(ref -> {
PsiElement element = ref.getElement();
if (element instanceof XmlAttributeValue) {
PsiElement parent = element.getParent();
if (parent instanceof XmlAttribute && "implementationClass".equals(((XmlAttribute) parent).getName())) {
DomElement domElement = DomUtil.getDomElement(parent.getParent());
if (domElement instanceof Extension) {
Extension extension = (Extension) domElement;
ExtensionPoint extensionPoint = extension.getExtensionPoint();
if (extensionPoint != null && InheritanceUtil.isInheritor(extensionPoint.getBeanClass().getValue(), InspectionEP.class.getName())) {
result.set(extension);
return false;
}
}
}
}
return true;
});
Extension extension = result.get();
if (extension != null)
return extension;
processed.addAll(origElements);
}
return null;
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class JoinDeclarationAndAssignmentAction method getPair.
private static Pair<PsiLocalVariable, PsiAssignmentExpression> getPair(PsiElement element) {
PsiAssignmentExpression assignmentExpression = PsiTreeUtil.getParentOfType(element, PsiAssignmentExpression.class);
PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(element, PsiDeclarationStatement.class);
if (declarationStatement != null) {
assignmentExpression = getAssignmentStatement(declarationStatement);
} else if (assignmentExpression != null) {
declarationStatement = getDeclarationStatement(assignmentExpression);
}
if (declarationStatement != null && assignmentExpression != null) {
final PsiExpression lExpression = assignmentExpression.getLExpression();
final PsiExpression rExpression = assignmentExpression.getRExpression();
if (lExpression instanceof PsiReferenceExpression && rExpression != null) {
final PsiElement resolve = ((PsiReferenceExpression) lExpression).resolve();
if (resolve instanceof PsiLocalVariable && resolve.getParent() == declarationStatement) {
final PsiLocalVariable variable = (PsiLocalVariable) resolve;
if (ReferencesSearch.search(variable, new LocalSearchScope(rExpression), false).findFirst() != null) {
return null;
}
return Pair.createNonNull(variable, assignmentExpression);
}
}
}
return null;
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class SurroundAutoCloseableAction method processVariable.
private static void processVariable(Project project, Editor editor, PsiLocalVariable variable) {
PsiExpression initializer = ObjectUtils.assertNotNull(variable.getInitializer());
PsiElement declaration = variable.getParent();
PsiElement codeBlock = declaration.getParent();
LocalSearchScope scope = new LocalSearchScope(codeBlock);
PsiElement last = null;
for (PsiReference reference : ReferencesSearch.search(variable, scope).findAll()) {
PsiElement usage = PsiTreeUtil.findPrevParent(codeBlock, reference.getElement());
if ((last == null || usage.getTextOffset() > last.getTextOffset())) {
last = usage;
}
}
String text = "try (" + variable.getTypeElement().getText() + " " + variable.getName() + " = " + initializer.getText() + ") {}";
PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
PsiTryStatement armStatement = (PsiTryStatement) declaration.replace(factory.createStatementFromText(text, codeBlock));
List<PsiElement> toFormat = null;
if (last != null) {
toFormat = moveStatements(last, armStatement);
}
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
PsiElement formattedElement = codeStyleManager.reformat(armStatement);
if (toFormat != null) {
for (PsiElement psiElement : toFormat) {
codeStyleManager.reformat(psiElement);
}
}
if (last == null) {
PsiCodeBlock tryBlock = ((PsiTryStatement) formattedElement).getTryBlock();
if (tryBlock != null) {
PsiJavaToken brace = tryBlock.getLBrace();
if (brace != null) {
editor.getCaretModel().moveToOffset(brace.getTextOffset() + 1);
}
}
}
}
Aggregations