use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class GrIntroduceHandlerBase method collectVariableUsages.
public static PsiElement[] collectVariableUsages(GrVariable variable, PsiElement scope) {
final List<PsiElement> list = Collections.synchronizedList(new ArrayList<PsiElement>());
if (scope instanceof GroovyScriptClass) {
scope = scope.getContainingFile();
}
ReferencesSearch.search(variable, new LocalSearchScope(scope)).forEach(psiReference -> {
final PsiElement element = psiReference.getElement();
if (element != null) {
list.add(element);
}
return true;
});
return list.toArray(new PsiElement[list.size()]);
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class ListenerNavigateButton method buildNavigateActionGroup.
private static DefaultActionGroup buildNavigateActionGroup(RadComponent component, final PsiField boundField) {
final DefaultActionGroup actionGroup = new DefaultActionGroup();
final EventSetDescriptor[] eventSetDescriptors;
try {
BeanInfo beanInfo = Introspector.getBeanInfo(component.getComponentClass());
eventSetDescriptors = beanInfo.getEventSetDescriptors();
} catch (IntrospectionException e) {
LOG.error(e);
return null;
}
PsiFile boundClassFile = boundField.getContainingFile();
if (boundClassFile == null) {
return null;
}
final LocalSearchScope scope = new LocalSearchScope(boundClassFile);
ReferencesSearch.search(boundField, scope).forEach(ref -> {
final PsiElement element = ref.getElement();
if (element.getParent() instanceof PsiReferenceExpression) {
PsiReferenceExpression refExpr = (PsiReferenceExpression) element.getParent();
if (refExpr.getParent() instanceof PsiMethodCallExpression) {
PsiMethodCallExpression methodCall = (PsiMethodCallExpression) refExpr.getParent();
final PsiElement psiElement = refExpr.resolve();
if (psiElement instanceof PsiMethod) {
PsiMethod method = (PsiMethod) psiElement;
for (EventSetDescriptor eventSetDescriptor : eventSetDescriptors) {
if (Comparing.equal(eventSetDescriptor.getAddListenerMethod().getName(), method.getName())) {
final String eventName = eventSetDescriptor.getName();
final PsiExpression[] args = methodCall.getArgumentList().getExpressions();
if (args.length > 0) {
addListenerRef(actionGroup, eventName, args[0]);
}
}
}
}
}
}
return true;
});
return actionGroup;
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class GrParameterImpl method getUseScope.
@Override
@NotNull
public SearchScope getUseScope() {
if (!isPhysical()) {
final PsiFile file = getContainingFile();
final PsiElement context = file.getContext();
if (context != null)
return new LocalSearchScope(context);
return super.getUseScope();
}
final PsiElement scope = getDeclarationScope();
if (scope instanceof GrDocCommentOwner) {
GrDocCommentOwner owner = (GrDocCommentOwner) scope;
final GrDocComment comment = owner.getDocComment();
if (comment != null) {
return new LocalSearchScope(new PsiElement[] { scope, comment });
}
}
return new LocalSearchScope(scope);
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class OldReferencesResolver method inlineParam.
private PsiElement inlineParam(PsiElement newExpr, GrExpression actualArg, PsiParameter parameter) {
if (myParamsToNotInline.contains(parameter))
return newExpr;
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
if (myExpr instanceof GrClosableBlock) {
int count = 0;
for (PsiReference reference : ReferencesSearch.search(parameter, new LocalSearchScope(myParameterInitializer))) {
count++;
if (count > 1)
break;
}
if (count > 1) {
myParamsToNotInline.add(parameter);
final PsiType type;
if (parameter instanceof GrParameter) {
type = ((GrParameter) parameter).getDeclaredType();
} else {
type = parameter.getType();
}
final GrVariableDeclaration declaration = factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, actualArg, type, parameter.getName());
final GrStatement[] statements = ((GrClosableBlock) myExpr).getStatements();
GrStatement anchor = statements.length > 0 ? statements[0] : null;
return ((GrClosableBlock) myExpr).addStatementBefore(declaration, anchor);
}
}
int copyingSafetyLevel = GroovyRefactoringUtil.verifySafeCopyExpression(actualArg);
if (copyingSafetyLevel == RefactoringUtil.EXPR_COPY_PROHIBITED) {
actualArg = factory.createExpressionFromText(getTempVar(actualArg));
}
newExpr = newExpr.replace(actualArg);
return newExpr;
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class IncrementDecrementUsedAsExpressionInspection method extractPrefixPostfixExpressionToSeparateStatement.
public static void extractPrefixPostfixExpressionToSeparateStatement(PsiElement element) {
final PsiExpression operand;
if (element instanceof PsiPostfixExpression) {
final PsiPostfixExpression postfixExpression = (PsiPostfixExpression) element;
operand = postfixExpression.getOperand();
} else if (element instanceof PsiPrefixExpression) {
final PsiPrefixExpression prefixExpression = (PsiPrefixExpression) element;
operand = prefixExpression.getOperand();
} else {
assert false;
return;
}
if (operand == null) {
return;
}
final PsiStatement statement = PsiTreeUtil.getParentOfType(element, PsiStatement.class);
if (statement == null) {
return;
}
final PsiElement parent = statement.getParent();
if (parent == null) {
return;
}
final Project project = element.getProject();
final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
final String newStatementText = element.getText() + ';';
final String operandText = operand.getText();
if (parent instanceof PsiIfStatement || parent instanceof PsiLoopStatement) {
// need to add braces because
// in/decrement is inside braceless control statement body
final StringBuilder text = new StringBuilder();
text.append('{');
final String elementText = PsiReplacementUtil.getElementText(statement, element, operandText);
if (element instanceof PsiPostfixExpression) {
text.append(elementText);
text.append(newStatementText);
} else {
text.append(newStatementText);
text.append(elementText);
}
text.append('}');
final PsiCodeBlock codeBlock = factory.createCodeBlockFromText(text.toString(), parent);
statement.replace(codeBlock);
return;
}
final PsiStatement newStatement = factory.createStatementFromText(newStatementText, element);
if (statement instanceof PsiReturnStatement) {
if (element instanceof PsiPostfixExpression) {
// special handling of postfix expression in return statement
final PsiReturnStatement returnStatement = (PsiReturnStatement) statement;
final PsiExpression returnValue = returnStatement.getReturnValue();
if (returnValue == null) {
return;
}
final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
final String variableName = javaCodeStyleManager.suggestUniqueVariableName("result", returnValue, true);
final PsiType type = returnValue.getType();
if (type == null) {
return;
}
final String newReturnValueText = PsiReplacementUtil.getElementText(returnValue, element, operandText);
final String declarationStatementText = type.getCanonicalText() + ' ' + variableName + '=' + newReturnValueText + ';';
final PsiStatement declarationStatement = factory.createStatementFromText(declarationStatementText, returnStatement);
parent.addBefore(declarationStatement, statement);
parent.addBefore(newStatement, statement);
final PsiStatement newReturnStatement = factory.createStatementFromText("return " + variableName + ';', returnStatement);
returnStatement.replace(newReturnStatement);
return;
} else {
parent.addBefore(newStatement, statement);
}
} else if (statement instanceof PsiThrowStatement) {
if (element instanceof PsiPostfixExpression) {
// special handling of postfix expression in throw statement
final PsiThrowStatement returnStatement = (PsiThrowStatement) statement;
final PsiExpression exception = returnStatement.getException();
if (exception == null) {
return;
}
final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
final String variableName = javaCodeStyleManager.suggestUniqueVariableName("e", exception, true);
final PsiType type = exception.getType();
if (type == null) {
return;
}
final String newReturnValueText = PsiReplacementUtil.getElementText(exception, element, operandText);
final String declarationStatementText = type.getCanonicalText() + ' ' + variableName + '=' + newReturnValueText + ';';
final PsiStatement declarationStatement = factory.createStatementFromText(declarationStatementText, returnStatement);
parent.addBefore(declarationStatement, statement);
parent.addBefore(newStatement, statement);
final PsiStatement newReturnStatement = factory.createStatementFromText("throw " + variableName + ';', returnStatement);
returnStatement.replace(newReturnStatement);
return;
} else {
parent.addBefore(newStatement, statement);
}
} else if (!(statement instanceof PsiForStatement)) {
if (element instanceof PsiPostfixExpression) {
parent.addAfter(newStatement, statement);
} else {
parent.addBefore(newStatement, statement);
}
} else if (operand instanceof PsiReferenceExpression) {
final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) operand;
final PsiElement target = referenceExpression.resolve();
if (target != null) {
final SearchScope useScope = target.getUseScope();
if (!new LocalSearchScope(statement).equals(useScope)) {
if (element instanceof PsiPostfixExpression) {
parent.addAfter(newStatement, statement);
} else {
parent.addBefore(newStatement, statement);
}
}
}
}
if (statement instanceof PsiLoopStatement) {
// in/decrement inside loop statement condition
final PsiLoopStatement loopStatement = (PsiLoopStatement) statement;
final PsiStatement body = loopStatement.getBody();
if (body instanceof PsiBlockStatement) {
final PsiBlockStatement blockStatement = (PsiBlockStatement) body;
final PsiCodeBlock codeBlock = blockStatement.getCodeBlock();
if (element instanceof PsiPostfixExpression) {
final PsiElement firstElement = codeBlock.getFirstBodyElement();
codeBlock.addBefore(newStatement, firstElement);
} else {
codeBlock.add(newStatement);
}
} else {
final StringBuilder blockText = new StringBuilder();
blockText.append('{');
if (element instanceof PsiPostfixExpression) {
blockText.append(newStatementText);
if (body != null) {
blockText.append(body.getText());
}
} else {
if (body != null) {
blockText.append(body.getText());
}
blockText.append(newStatementText);
}
blockText.append('}');
final PsiStatement blockStatement = factory.createStatementFromText(blockText.toString(), statement);
if (body == null) {
loopStatement.add(blockStatement);
} else {
body.replace(blockStatement);
}
}
}
PsiReplacementUtil.replaceExpression((PsiExpression) element, operandText);
}
Aggregations