use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GrImportStatementImpl method processSingleClassImport.
private boolean processSingleClassImport(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state) {
if (!ResolveUtil.shouldProcessClasses(processor.getHint(ElementClassHint.KEY)))
return true;
GrCodeReferenceElement ref = getImportReference();
if (ref == null)
return true;
final PsiElement resolved = ref.resolve();
if (!(resolved instanceof PsiClass)) {
return true;
}
if (!isAliasedImport() && isFromSamePackage((PsiClass) resolved)) {
//don't process classes from the same package because such import statements are ignored by compiler
return true;
}
return processor.execute(resolved, state.put(ClassHint.RESOLVE_CONTEXT, this));
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GrCodeReferenceElementImpl method bindWithQualifiedRef.
@Override
protected GrCodeReferenceElement bindWithQualifiedRef(@NotNull String qName) {
final GrCodeReferenceElement qualifiedRef = GroovyPsiElementFactory.getInstance(getProject()).createTypeOrPackageReference(qName);
final PsiElement list = getTypeArgumentList();
if (list != null) {
qualifiedRef.getNode().addChild(list.copy().getNode());
}
getNode().getTreeParent().replaceChild(getNode(), qualifiedRef.getNode());
return qualifiedRef;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project android by JetBrains.
the class GradleDslParser method getExpressionElement.
@Nullable
private static GradleDslExpression getExpressionElement(@NotNull GradleDslElement parentElement, @NotNull GroovyPsiElement psiElement, @NotNull String propertyName, @NotNull GrExpression propertyExpression) {
if (propertyExpression instanceof GrLiteral) {
// ex: compileSdkVersion 23 or compileSdkVersion = "android-23"
return new GradleDslLiteral(parentElement, psiElement, propertyName, (GrLiteral) propertyExpression);
}
if (propertyExpression instanceof GrReferenceExpression) {
// ex: compileSdkVersion SDK_VERSION or sourceCompatibility = VERSION_1_5
return new GradleDslReference(parentElement, psiElement, propertyName, (GrReferenceExpression) propertyExpression);
}
if (propertyExpression instanceof GrMethodCallExpression) {
// ex: compile project("someProject")
GrMethodCallExpression methodCall = (GrMethodCallExpression) propertyExpression;
GrReferenceExpression callReferenceExpression = getChildOfType(methodCall, GrReferenceExpression.class);
if (callReferenceExpression != null) {
String referenceName = callReferenceExpression.getText();
if (!isEmpty(referenceName)) {
GrArgumentList argumentList = methodCall.getArgumentList();
if (argumentList.getAllArguments().length > 0) {
return getMethodCall(parentElement, methodCall, referenceName, argumentList);
} else {
return new GradleDslMethodCall(parentElement, methodCall, referenceName);
}
}
}
}
if (propertyExpression instanceof GrNewExpression) {
GrNewExpression newExpression = (GrNewExpression) propertyExpression;
GrCodeReferenceElement referenceElement = newExpression.getReferenceElement();
if (referenceElement != null) {
String objectName = referenceElement.getText();
if (!isEmpty(objectName)) {
GrArgumentList argumentList = newExpression.getArgumentList();
if (argumentList != null) {
if (argumentList.getAllArguments().length > 0) {
return getNewExpression(parentElement, newExpression, objectName, argumentList);
}
}
}
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class CreateClassFix method createClassFixAction.
public static IntentionAction createClassFixAction(final GrReferenceElement refElement, GrCreateClassKind type) {
return new CreateClassActionBase(type, refElement) {
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final PsiFile file = element.getContainingFile();
if (!(file instanceof GroovyFileBase))
return;
GroovyFileBase groovyFile = (GroovyFileBase) file;
PsiElement qualifier = myRefElement.getQualifier();
if (qualifier == null || qualifier instanceof GrReferenceElement && ((GrReferenceElement) qualifier).resolve() instanceof PsiPackage) {
createTopLevelClass(project, groovyFile);
} else {
createInnerClass(project, editor, qualifier);
}
}
private void createInnerClass(Project project, final Editor editor, PsiElement qualifier) {
PsiElement resolved = resolveQualifier(qualifier);
if (!(resolved instanceof PsiClass))
return;
JVMElementFactory factory = JVMElementFactories.getFactory(resolved.getLanguage(), project);
if (factory == null)
return;
String name = myRefElement.getReferenceName();
PsiClass template = createTemplate(factory, name);
if (template == null) {
ApplicationManager.getApplication().invokeLater(() -> {
if (editor != null && editor.getComponent().isDisplayable()) {
HintManager.getInstance().showErrorHint(editor, GroovyIntentionsBundle.message("cannot.create.class"));
}
});
return;
}
if (!FileModificationService.getInstance().preparePsiElementForWrite(resolved))
return;
WriteAction.run(() -> {
PsiClass added = (PsiClass) resolved.add(template);
PsiModifierList modifierList = added.getModifierList();
if (modifierList != null) {
modifierList.setModifierProperty(PsiModifier.STATIC, true);
}
IntentionUtils.positionCursor(project, added.getContainingFile(), added);
});
}
@Nullable
private PsiElement resolveQualifier(@NotNull PsiElement qualifier) {
if (qualifier instanceof GrCodeReferenceElement) {
return ((GrCodeReferenceElement) qualifier).resolve();
} else if (qualifier instanceof GrExpression) {
PsiType type = ((GrExpression) qualifier).getType();
if (type instanceof PsiClassType) {
return ((PsiClassType) type).resolve();
} else if (qualifier instanceof GrReferenceExpression) {
final PsiElement resolved = ((GrReferenceExpression) qualifier).resolve();
if (resolved instanceof PsiClass || resolved instanceof PsiPackage) {
return resolved;
}
}
}
return null;
}
@Nullable
private PsiClass createTemplate(JVMElementFactory factory, String name) {
switch(getType()) {
case ENUM:
return factory.createEnum(name);
case TRAIT:
if (factory instanceof GroovyPsiElementFactory) {
return ((GroovyPsiElementFactory) factory).createTrait(name);
} else {
return null;
}
case CLASS:
return factory.createClass(name);
case INTERFACE:
return factory.createInterface(name);
case ANNOTATION:
return factory.createAnnotationType(name);
default:
return null;
}
}
private void createTopLevelClass(@NotNull Project project, @NotNull GroovyFileBase file) {
final String pack = getPackage(file);
final PsiManager manager = PsiManager.getInstance(project);
final String name = myRefElement.getReferenceName();
assert name != null;
final Module module = ModuleUtilCore.findModuleForPsiElement(file);
PsiDirectory targetDirectory = getTargetDirectory(project, pack, name, module, getText());
if (targetDirectory == null)
return;
String templateName = getTemplateName(getType());
final PsiClass targetClass = createClassByType(targetDirectory, name, manager, myRefElement, templateName, true);
if (targetClass == null)
return;
bindRef(targetClass, myRefElement);
IntentionUtils.positionCursor(project, targetClass.getContainingFile(), targetClass);
}
@NotNull
private String getPackage(@NotNull PsiClassOwner file) {
final PsiElement qualifier = myRefElement.getQualifier();
if (qualifier instanceof GrReferenceElement) {
final PsiElement resolved = ((GrReferenceElement) qualifier).resolve();
if (resolved instanceof PsiPackage) {
return ((PsiPackage) resolved).getQualifiedName();
}
}
return file instanceof GroovyFile ? file.getPackageName() : "";
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!super.isAvailable(project, editor, file))
return false;
final PsiElement qualifier = myRefElement.getQualifier();
if (qualifier != null && resolveQualifier(qualifier) == null) {
return false;
}
return true;
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class AddClassToExtendsFix method doFix.
@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
GrReferenceList list;
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final PsiClass comparable = JavaPsiFacade.getInstance(project).findClass(CommonClassNames.JAVA_LANG_COMPARABLE, myPsiClass.getResolveScope());
PsiSubstitutor substitutor = PsiSubstitutor.EMPTY;
boolean addTypeParam = false;
if (comparable != null) {
final PsiTypeParameter[] typeParameters = comparable.getTypeParameters();
if (typeParameters.length == 1) {
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
final PsiTypeParameter[] classParams = myPsiClass.getTypeParameters();
PsiSubstitutor innerSubstitutor = PsiSubstitutor.EMPTY;
for (PsiTypeParameter classParam : classParams) {
innerSubstitutor = innerSubstitutor.put(classParam, elementFactory.createType(classParam));
}
substitutor = substitutor.put(typeParameters[0], elementFactory.createType(myPsiClass, innerSubstitutor));
addTypeParam = true;
}
}
if (!InheritanceUtil.isInheritor(myPsiClass, CommonClassNames.JAVA_LANG_COMPARABLE)) {
if (myPsiClass.isInterface()) {
list = myPsiClass.getExtendsClause();
if (list == null) {
list = factory.createExtendsClause();
PsiElement anchor = myPsiClass.getImplementsClause();
if (anchor == null) {
anchor = myPsiClass.getBody();
}
if (anchor == null)
return;
list = (GrReferenceList) myPsiClass.addBefore(list, anchor);
myPsiClass.getNode().addLeaf(TokenType.WHITE_SPACE, " ", anchor.getNode());
myPsiClass.getNode().addLeaf(TokenType.WHITE_SPACE, " ", list.getNode());
}
} else {
list = myPsiClass.getImplementsClause();
if (list == null) {
list = factory.createImplementsClause();
PsiElement anchor = myPsiClass.getBody();
if (anchor == null)
return;
list = (GrReferenceList) myPsiClass.addBefore(list, anchor);
myPsiClass.getNode().addLeaf(TokenType.WHITE_SPACE, " ", list.getNode());
myPsiClass.getNode().addLeaf(TokenType.WHITE_SPACE, " ", anchor.getNode());
}
}
final GrCodeReferenceElement _ref = factory.createReferenceElementFromText(myInterfaceName + (addTypeParam ? "<" + AddMethodFix.generateTypeText(myPsiClass) + ">" : ""));
final GrCodeReferenceElement ref = (GrCodeReferenceElement) list.add(_ref);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(ref);
}
if (comparable != null && !myPsiClass.isInterface()) {
final PsiMethod baseMethod = comparable.getMethods()[0];
OverrideImplementUtil.overrideOrImplement(myPsiClass, baseMethod);
}
}
Aggregations