use of com.intellij.psi.search.LocalSearchScope in project intellij-elixir by KronicDeth.
the class Issue429Test method testUseScope.
/*
* Tests
*/
public void testUseScope() {
myFixture.configureByFiles("get_use_scope.ex");
PsiElement callable = myFixture.getFile().findElementAt(myFixture.getCaretOffset()).getParent().getPrevSibling().getLastChild().getLastChild().getLastChild();
assertInstanceOf(callable, Call.class);
SearchScope useScope = callable.getUseScope();
assertInstanceOf(useScope, LocalSearchScope.class);
LocalSearchScope localSearchScope = (LocalSearchScope) useScope;
PsiElement[] scope = localSearchScope.getScope();
assertEquals(1, scope.length);
PsiElement singleScope = scope[0];
assertTrue("Use Scope is not the surrounding if", singleScope.getText().startsWith("if auth == "));
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-elixir by KronicDeth.
the class Callable method variableUseScope.
private static LocalSearchScope variableUseScope(@NotNull Match match) {
LocalSearchScope useScope;
PsiElement parent = match.getParent();
if (parent instanceof ElixirStabBody) {
@SuppressWarnings("unchecked") PsiElement ancestor = PsiTreeUtil.getContextOfType(parent, ElixirAnonymousFunction.class, ElixirBlockItem.class, ElixirDoBlock.class, ElixirParentheticalStab.class, ElixirStabOperation.class);
if (ancestor instanceof ElixirParentheticalStab) {
useScope = variableUseScope(parent);
} else {
/* all non-ElixirParentheticalStab are block-like and so could have multiple statements after the match
where the match variable is used */
useScope = followingSiblingsSearchScope(match);
}
} else if (parent instanceof PsiFile) {
useScope = followingSiblingsSearchScope(match);
} else {
useScope = variableUseScope(parent);
}
return useScope;
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class ReassignVariableUtil method reassign.
@VisibleForTesting
public static boolean reassign(final Editor editor) {
final SmartPsiElementPointer<PsiDeclarationStatement> pointer = editor.getUserData(DECLARATION_KEY);
final PsiDeclarationStatement declaration = pointer != null ? pointer.getElement() : null;
final PsiType type = getVariableType(declaration);
if (type != null) {
VariablesProcessor proc = findVariablesOfType(declaration, type);
if (proc.size() > 0) {
List<PsiVariable> vars = new ArrayList<>();
for (int i = 0; i < proc.size(); i++) {
final PsiVariable variable = proc.getResult(i);
PsiElement outerCodeBlock = PsiUtil.getVariableCodeBlock(variable, null);
if (outerCodeBlock == null)
continue;
if (ReferencesSearch.search(variable, new LocalSearchScope(outerCodeBlock)).forEach(reference -> {
final PsiElement element = reference.getElement();
if (element != null) {
return HighlightControlFlowUtil.getInnerClassVariableReferencedFrom(variable, element) == null;
}
return true;
})) {
vars.add(variable);
}
}
if (vars.isEmpty()) {
return true;
}
if (vars.size() == 1) {
replaceWithAssignment(declaration, proc.getResult(0), editor);
return true;
}
final DefaultListModel<PsiVariable> model = new DefaultListModel<>();
for (PsiVariable var : vars) {
model.addElement(var);
}
final JBList list = new JBList(model);
list.setCellRenderer(new ListCellRendererWrapper<PsiVariable>() {
@Override
public void customize(JList list, PsiVariable value, int index, boolean selected, boolean hasFocus) {
if (value != null) {
setText(value.getName());
setIcon(value.getIcon(0));
}
}
});
final VisualPosition visualPosition = editor.getCaretModel().getVisualPosition();
final Point point = editor.visualPositionToXY(new VisualPosition(visualPosition.line + 1, visualPosition.column));
JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle("Choose variable to reassign").setRequestFocus(true).setItemChoosenCallback(() -> replaceWithAssignment(declaration, (PsiVariable) list.getSelectedValue(), editor)).createPopup().show(new RelativePoint(editor.getContentComponent(), point));
}
return true;
}
return false;
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class JavaPullUpHelper method buildConstructorsToSubConstructorsMap.
private HashMap<PsiMethod, HashSet<PsiMethod>> buildConstructorsToSubConstructorsMap(final PsiMethod[] constructors) {
final HashMap<PsiMethod, HashSet<PsiMethod>> constructorsToSubConstructors = new HashMap<>();
for (PsiMethod constructor : constructors) {
final HashSet<PsiMethod> referencingSubConstructors = new HashSet<>();
constructorsToSubConstructors.put(constructor, referencingSubConstructors);
if (constructor != null) {
// find references
for (PsiReference reference : ReferencesSearch.search(constructor, new LocalSearchScope(mySourceClass), false)) {
final PsiElement element = reference.getElement();
if (element != null && "super".equals(element.getText())) {
PsiMethod parentMethod = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
if (parentMethod != null && parentMethod.isConstructor()) {
referencingSubConstructors.add(parentMethod);
}
}
}
}
// check default constructor
if (constructor == null || constructor.getParameterList().getParametersCount() == 0) {
RefactoringUtil.visitImplicitSuperConstructorUsages(mySourceClass, new RefactoringUtil.ImplicitConstructorUsageVisitor() {
public void visitConstructor(PsiMethod constructor, PsiMethod baseConstructor) {
referencingSubConstructors.add(constructor);
}
public void visitClassWithoutConstructors(PsiClass aClass) {
}
}, myTargetSuperClass);
}
}
return constructorsToSubConstructors;
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class PsiClassImplUtil method getClassUseScope.
@NotNull
public static SearchScope getClassUseScope(@NotNull PsiClass aClass) {
if (aClass instanceof PsiAnonymousClass) {
return new LocalSearchScope(aClass);
}
final GlobalSearchScope maximalUseScope = ResolveScopeManager.getElementUseScope(aClass);
PsiFile file = aClass.getContainingFile();
if (PsiImplUtil.isInServerPage(file))
return maximalUseScope;
final PsiClass containingClass = aClass.getContainingClass();
if (aClass.hasModifierProperty(PsiModifier.PUBLIC) || aClass.hasModifierProperty(PsiModifier.PROTECTED)) {
return containingClass == null ? maximalUseScope : containingClass.getUseScope();
} else if (aClass.hasModifierProperty(PsiModifier.PRIVATE) || aClass instanceof PsiTypeParameter) {
PsiClass topClass = PsiUtil.getTopLevelClass(aClass);
return new LocalSearchScope(topClass == null ? aClass.getContainingFile() : topClass);
} else {
PsiPackage aPackage = null;
if (file instanceof PsiJavaFile) {
aPackage = JavaPsiFacade.getInstance(aClass.getProject()).findPackage(((PsiJavaFile) file).getPackageName());
}
if (aPackage == null) {
PsiDirectory dir = file.getContainingDirectory();
if (dir != null) {
aPackage = JavaDirectoryService.getInstance().getPackage(dir);
}
}
if (aPackage != null) {
SearchScope scope = PackageScope.packageScope(aPackage, false);
scope = scope.intersectWith(maximalUseScope);
return scope;
}
return new LocalSearchScope(file);
}
}
Aggregations