use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class MatcherImpl method testFindMatches.
/**
* Finds the matches of given pattern starting from given tree element.
* @param source string for search
* @param pattern to be searched
* @return list of matches found
* @throws MalformedPatternException
* @throws UnsupportedPatternException
*/
protected List<MatchResult> testFindMatches(String source, String pattern, MatchOptions options, boolean filePattern, FileType sourceFileType, String sourceExtension, boolean physicalSourceFile) throws MalformedPatternException, UnsupportedPatternException {
CollectingMatchResultSink sink = new CollectingMatchResultSink();
try {
PsiElement[] elements = MatcherImplUtil.createSourceTreeFromText(source, filePattern ? PatternTreeContext.File : PatternTreeContext.Block, sourceFileType, sourceExtension, project, physicalSourceFile);
options.setSearchPattern(pattern);
options.setScope(new LocalSearchScope(elements));
testFindMatches(sink, options);
} catch (IncorrectOperationException e) {
MalformedPatternException exception = new MalformedPatternException();
exception.initCause(e);
throw exception;
} finally {
options.setScope(null);
}
return sink.getMatches();
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class ExtractMethodProcessor method applyChosenClassAndExtract.
private boolean applyChosenClassAndExtract(List<PsiVariable> inputVariables, @Nullable Pass<ExtractMethodProcessor> extractPass) throws PrepareFailedException {
myStatic = shouldBeStatic();
final Set<PsiField> fields = new LinkedHashSet<>();
if (!PsiUtil.isLocalOrAnonymousClass(myTargetClass) && (myTargetClass.getContainingClass() == null || myTargetClass.hasModifierProperty(PsiModifier.STATIC))) {
boolean canBeStatic = true;
if (myTargetClass.isInterface()) {
final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(myCodeFragmentMember, PsiMethod.class, false);
canBeStatic = containingMethod == null || containingMethod.hasModifierProperty(PsiModifier.STATIC);
}
if (canBeStatic) {
ElementNeedsThis needsThis = new ElementNeedsThis(myTargetClass) {
@Override
protected void visitClassMemberReferenceElement(PsiMember classMember, PsiJavaCodeReferenceElement classMemberReference) {
if (classMember instanceof PsiField && !classMember.hasModifierProperty(PsiModifier.STATIC)) {
final PsiExpression expression = PsiTreeUtil.getParentOfType(classMemberReference, PsiExpression.class, false);
if (expression == null || !PsiUtil.isAccessedForWriting(expression)) {
fields.add((PsiField) classMember);
return;
}
}
super.visitClassMemberReferenceElement(classMember, classMemberReference);
}
};
for (int i = 0; i < myElements.length && !needsThis.usesMembers(); i++) {
PsiElement element = myElements[i];
element.accept(needsThis);
}
myCanBeStatic = !needsThis.usesMembers();
} else {
myCanBeStatic = false;
}
} else {
myCanBeStatic = false;
}
myInputVariables = new InputVariables(inputVariables, myProject, new LocalSearchScope(myElements), isFoldingApplicable());
myInputVariables.setUsedInstanceFields(fields);
if (!checkExitPoints()) {
return false;
}
checkCanBeChainedConstructor();
if (extractPass != null) {
extractPass.pass(this);
}
return true;
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class AnonymousToInnerHandler method renameReferences.
private void renameReferences(PsiElement scope) throws IncorrectOperationException {
PsiElementFactory factory = JavaPsiFacade.getInstance(myManager.getProject()).getElementFactory();
for (VariableInfo info : myVariableInfos) {
for (PsiReference reference : ReferencesSearch.search(info.variable, new LocalSearchScope(scope))) {
PsiElement ref = reference.getElement();
PsiIdentifier identifier = (PsiIdentifier) ((PsiJavaCodeReferenceElement) ref).getReferenceNameElement();
assert identifier != null;
boolean renameToFieldName = !isUsedInInitializer(ref);
PsiIdentifier newNameIdentifier = factory.createIdentifier(renameToFieldName ? info.fieldName : info.parameterName);
if (renameToFieldName) {
identifier.replace(newNameIdentifier);
} else {
if (info.passAsParameter) {
identifier.replace(newNameIdentifier);
}
}
}
}
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class ExtractMethodProcessor method doRefactoring.
/**
* Invoked in command and in atomic action
*/
public void doRefactoring() throws IncorrectOperationException {
initDuplicates();
chooseAnchor();
LogicalPosition pos1;
if (myEditor != null) {
int col = myEditor.getCaretModel().getLogicalPosition().column;
int line = myEditor.getCaretModel().getLogicalPosition().line;
pos1 = new LogicalPosition(line, col);
LogicalPosition pos = new LogicalPosition(0, 0);
myEditor.getCaretModel().moveToLogicalPosition(pos);
} else {
pos1 = null;
}
final SearchScope processConflictsScope = myMethodVisibility.equals(PsiModifier.PRIVATE) ? new LocalSearchScope(myTargetClass) : GlobalSearchScope.projectScope(myProject);
final Map<PsiMethodCallExpression, PsiMethod> overloadsResolveMap = new HashMap<>();
final Runnable collectOverloads = () -> ApplicationManager.getApplication().runReadAction(() -> {
Map<PsiMethodCallExpression, PsiMethod> overloads = ExtractMethodUtil.encodeOverloadTargets(myTargetClass, processConflictsScope, myMethodName, myCodeFragmentMember);
overloadsResolveMap.putAll(overloads);
});
final Runnable extract = () -> {
doExtract();
ExtractMethodUtil.decodeOverloadTargets(overloadsResolveMap, myExtractedMethod, myCodeFragmentMember);
};
if (ApplicationManager.getApplication().isWriteAccessAllowed()) {
collectOverloads.run();
extract.run();
} else {
if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(collectOverloads, "Collect overloads...", true, myProject))
return;
ApplicationManager.getApplication().runWriteAction(extract);
}
if (myEditor != null) {
myEditor.getCaretModel().moveToLogicalPosition(pos1);
int offset = myMethodCall.getMethodExpression().getTextRange().getStartOffset();
myEditor.getCaretModel().moveToOffset(offset);
myEditor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
myEditor.getSelectionModel().removeSelection();
}
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class ExtractMethodSignatureSuggester method removeUnusedStongParams.
private void removeUnusedStongParams(int strongParamsCound) {
final PsiExpression[] expressions = myMethodCall.getArgumentList().getExpressions();
final PsiParameter[] parameters = myExtractedMethod.getParameterList().getParameters();
final PsiCodeBlock body = myExtractedMethod.getBody();
if (body != null) {
final LocalSearchScope scope = new LocalSearchScope(body);
for (int i = strongParamsCound - 1; i >= 0; i--) {
final PsiParameter parameter = parameters[i];
if (ReferencesSearch.search(parameter, scope).findFirst() == null) {
parameter.delete();
expressions[i].delete();
}
}
}
}
Aggregations