use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class GrAccessibilityChecker method buildFixes.
static GroovyFix[] buildFixes(PsiElement location, GroovyResolveResult resolveResult) {
final PsiElement element = resolveResult.getElement();
if (!(element instanceof PsiMember))
return GroovyFix.EMPTY_ARRAY;
final PsiMember refElement = (PsiMember) element;
if (refElement instanceof PsiCompiledElement)
return GroovyFix.EMPTY_ARRAY;
PsiModifierList modifierList = refElement.getModifierList();
if (modifierList == null)
return GroovyFix.EMPTY_ARRAY;
List<GroovyFix> fixes = new ArrayList<>();
try {
Project project = refElement.getProject();
JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
PsiModifierList modifierListCopy = facade.getElementFactory().createFieldFromText("int a;", null).getModifierList();
assert modifierListCopy != null;
modifierListCopy.setModifierProperty(PsiModifier.STATIC, modifierList.hasModifierProperty(PsiModifier.STATIC));
String minModifier = PsiModifier.PROTECTED;
if (refElement.hasModifierProperty(PsiModifier.PROTECTED)) {
minModifier = PsiModifier.PUBLIC;
}
String[] modifiers = { PsiModifier.PROTECTED, PsiModifier.PUBLIC, PsiModifier.PACKAGE_LOCAL };
PsiClass accessObjectClass = PsiTreeUtil.getParentOfType(location, PsiClass.class, false);
if (accessObjectClass == null) {
final PsiFile file = location.getContainingFile();
if (!(file instanceof GroovyFile))
return GroovyFix.EMPTY_ARRAY;
accessObjectClass = ((GroovyFile) file).getScriptClass();
}
for (int i = ArrayUtil.indexOf(modifiers, minModifier); i < modifiers.length; i++) {
String modifier = modifiers[i];
modifierListCopy.setModifierProperty(modifier, true);
if (facade.getResolveHelper().isAccessible(refElement, modifierListCopy, location, accessObjectClass, null)) {
fixes.add(new GrModifierFix(refElement, modifier, true, true, GrModifierFix.MODIFIER_LIST_OWNER));
}
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
return fixes.toArray(new GroovyFix[fixes.size()]);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class AnnotatedMembersSearcher method getAnnotatedMemberCandidates.
@NotNull
private static List<PsiModifierListOwner> getAnnotatedMemberCandidates(final PsiClass clazz, final GlobalSearchScope scope) {
final String name = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
@Override
public String compute() {
return clazz.getName();
}
});
if (name == null)
return Collections.emptyList();
final Collection<PsiElement> members = ApplicationManager.getApplication().runReadAction(new Computable<Collection<PsiElement>>() {
@Override
public Collection<PsiElement> compute() {
return StubIndex.getElements(GrAnnotatedMemberIndex.KEY, name, clazz.getProject(), scope, PsiElement.class);
}
});
if (members.isEmpty()) {
return Collections.emptyList();
}
final List<PsiModifierListOwner> result = new ArrayList<>();
for (final PsiElement element : members) {
ApplicationManager.getApplication().runReadAction(() -> {
PsiElement e = element instanceof GroovyFile ? ((GroovyFile) element).getPackageDefinition() : element;
if (e instanceof PsiModifierListOwner) {
result.add((PsiModifierListOwner) e);
}
});
}
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class GroovyFoldingBuilder method isSingleHighLevelClassBody.
private static boolean isSingleHighLevelClassBody(PsiElement element) {
if (!(element instanceof GrTypeDefinitionBody))
return false;
final PsiElement parent = element.getParent();
if (!(parent instanceof GrTypeDefinition))
return false;
final GrTypeDefinition clazz = (GrTypeDefinition) parent;
if (clazz.isAnonymous() || clazz.getContainingClass() != null)
return false;
final PsiFile file = element.getContainingFile();
return file instanceof GroovyFile && ((GroovyFile) file).getClasses().length == 1;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class GroovyFoldingBuilder method appendDescriptors.
private void appendDescriptors(PsiElement element, List<FoldingDescriptor> descriptors, Set<PsiElement> usedComments) {
ASTNode node = element.getNode();
if (node == null)
return;
IElementType type = node.getElementType();
if (TokenSets.BLOCK_SET.contains(type) && !isSingleHighLevelClassBody(element) || type == GroovyElementTypes.CLOSABLE_BLOCK) {
if (isMultiline(element)) {
collapseBlock(descriptors, element);
}
}
// comments
if ((type.equals(GroovyTokenTypes.mML_COMMENT) || type.equals(GroovyDocElementTypes.GROOVY_DOC_COMMENT)) && isMultiline(element) && isWellEndedComment(element)) {
descriptors.add(new FoldingDescriptor(node, node.getTextRange()));
}
if (type.equals(GroovyTokenTypes.mSL_COMMENT) && !usedComments.contains(element)) {
boolean containsCustomRegionMarker = isCustomRegionElement(element);
usedComments.add(element);
PsiElement end = null;
for (PsiElement current = element.getNextSibling(); current != null; current = current.getNextSibling()) {
if (PsiImplUtil.isWhiteSpaceOrNls(current))
continue;
IElementType elementType = current.getNode().getElementType();
if (elementType == GroovyTokenTypes.mSL_COMMENT) {
end = current;
usedComments.add(current);
containsCustomRegionMarker |= isCustomRegionElement(current);
continue;
}
break;
}
if (end != null && !containsCustomRegionMarker) {
final TextRange range = new TextRange(element.getTextRange().getStartOffset(), end.getTextRange().getEndOffset());
descriptors.add(new FoldingDescriptor(element, range));
}
}
//multiline strings
addFoldingForStrings(descriptors, node);
Set<PsiElement> newUsedComments = new HashSet<>();
for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
appendDescriptors(child, descriptors, newUsedComments);
}
if (element instanceof GroovyFile) {
processImports(descriptors, ((GroovyFile) element).getImportStatements());
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class ControlFlowBuilderUtil method findClassByText.
private static boolean findClassByText(GrReferenceExpression ref) {
final String text = ref.getText();
final int i = text.indexOf('<');
String className = i == -1 ? text : text.substring(0, i);
PsiClass[] names = PsiShortNamesCache.getInstance(ref.getProject()).getClassesByName(className, ref.getResolveScope());
if (names.length > 0)
return true;
PsiFile file = ref.getContainingFile();
if (file instanceof GroovyFile) {
GrImportStatement[] imports = ((GroovyFile) file).getImportStatements();
for (GrImportStatement anImport : imports) {
if (className.equals(anImport.getImportedName()))
return true;
}
}
return false;
}
Aggregations