use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class GenerationUtil method getVarTypes.
static Set<String> getVarTypes(GrVariableDeclaration variableDeclaration) {
GrVariable[] variables = variableDeclaration.getVariables();
final GrTypeElement typeElement = variableDeclaration.getTypeElementGroovy();
Set<String> types = new HashSet<>(variables.length);
if (typeElement == null) {
if (variables.length > 1) {
for (GrVariable variable : variables) {
final GrExpression initializer = variable.getInitializerGroovy();
if (initializer != null) {
final PsiType varType = initializer.getType();
if (varType != null) {
types.add(getTypeText(varType, variableDeclaration));
}
}
}
}
}
return types;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class StubGenerator method writeVariableDeclarations.
@Override
public void writeVariableDeclarations(StringBuilder text, GrVariableDeclaration variableDeclaration) {
GrTypeElement typeElement = variableDeclaration.getTypeElementGroovy();
final GrModifierList modifierList = variableDeclaration.getModifierList();
final PsiNameHelper nameHelper = PsiNameHelper.getInstance(variableDeclaration.getProject());
for (final GrVariable variable : variableDeclaration.getVariables()) {
String name = variable.getName();
if (!nameHelper.isIdentifier(name)) {
//does not have a java image
continue;
}
ModifierListGenerator.writeModifiers(text, modifierList, STUB_FIELD_MODIFIERS, false);
//type
PsiType declaredType = typeElement == null ? PsiType.getJavaLangObject(variable.getManager(), variable.getResolveScope()) : typeElement.getType();
TypeWriter.writeType(text, declaredType, variableDeclaration, classNameProvider);
text.append(' ').append(name).append(" = ").append(getVariableInitializer(variable, declaredType));
text.append(";\n");
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class AddReturnTypeFix method findMethod.
@Nullable
private static GrMethod findMethod(PsiFile file, final int offset) {
final PsiElement at = file.findElementAt(offset);
if (at == null)
return null;
if (at.getParent() instanceof GrReturnStatement) {
final GrReturnStatement returnStatement = ((GrReturnStatement) at.getParent());
final PsiElement word = returnStatement.getReturnWord();
if (!word.getTextRange().contains(offset))
return null;
final GroovyPsiElement returnOwner = PsiTreeUtil.getParentOfType(returnStatement, GrClosableBlock.class, GrMethod.class);
if (returnOwner instanceof GrMethod) {
final GrTypeElement returnTypeElement = ((GrMethod) returnOwner).getReturnTypeElementGroovy();
if (returnTypeElement == null) {
return (GrMethod) returnOwner;
}
}
return null;
}
final GrMethod method = PsiTreeUtil.getParentOfType(at, GrMethod.class, false, GrTypeDefinition.class, GrClosableBlock.class);
if (method != null && GrHighlightUtil.getMethodHeaderTextRange(method).contains(offset)) {
if (method.getReturnTypeElementGroovy() == null) {
return method;
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class ConvertSimpleGetterToPropertyIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
GrMethod method = (GrMethod) element.getParent();
GrOpenBlock block = method.getBlock();
if (block == null)
return;
GrStatement statement = block.getStatements()[0];
GrExpression value;
if (statement instanceof GrReturnStatement) {
value = ((GrReturnStatement) statement).getReturnValue();
} else {
value = (GrExpression) statement;
}
String fieldName = GroovyPropertyUtils.getPropertyNameByGetter(method);
if (fieldName == null)
return;
PsiClass aClass = method.getContainingClass();
if (aClass == null)
return;
List<String> modifiers = ContainerUtil.newArrayList();
for (String modifier : MODIFIERS_TO_CHECK) {
if (method.hasModifierProperty(modifier))
modifiers.add(modifier);
}
modifiers.add(PsiModifier.FINAL);
GrTypeElement returnTypeElement = method.getReturnTypeElementGroovy();
PsiType returnType = returnTypeElement == null ? null : returnTypeElement.getType();
GrVariableDeclaration declaration = GroovyPsiElementFactory.getInstance(project).createFieldDeclaration(ArrayUtil.toStringArray(modifiers), fieldName, value, returnType);
PsiElement replaced = method.replace(declaration);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(replaced);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement in project intellij-community by JetBrains.
the class GrFinalListener method perform.
public void perform(final boolean generateFinal, final String modifier, final GrVariable variable) {
final Document document = myEditor.getDocument();
LOG.assertTrue(variable != null);
final GrModifierList modifierList = variable.getModifierList();
LOG.assertTrue(modifierList != null);
final int textOffset = modifierList.getTextOffset();
final Runnable runnable = () -> {
if (generateFinal) {
final GrTypeElement typeElement = variable.getTypeElementGroovy();
final int typeOffset = typeElement != null ? typeElement.getTextOffset() : textOffset;
document.insertString(typeOffset, modifier + " ");
} else {
final int idx = modifierList.getText().indexOf(modifier);
document.deleteString(textOffset + idx, textOffset + idx + modifier.length() + 1);
}
};
final LookupImpl lookup = (LookupImpl) LookupManager.getActiveLookup(myEditor);
if (lookup != null) {
lookup.performGuardedChange(runnable);
} else {
runnable.run();
}
PsiDocumentManager.getInstance(variable.getProject()).commitDocument(document);
}
Aggregations