use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList in project intellij-community by JetBrains.
the class GrSetStrongTypeIntention method getOrCreateTypeElement.
@NotNull
private static TypeInfo getOrCreateTypeElement(@NotNull PsiElement parent, @NotNull PsiElement elementToBuildTemplateOn) {
GrModifierList modifierList = getModifierList(parent);
if (modifierList != null && modifierList.hasModifierProperty(GrModifier.DEF) && modifierList.getModifiers().length == 1) {
PsiElement modifier = modifierList.getModifier(GrModifier.DEF);
LOG.assertTrue(modifier != null);
int modifierOffset = modifier.getTextRange().getEndOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
return new TypeInfo(modifier, modifierOffset);
} else {
int nameElementOffset;
final PsiClassType typeToUse = TypesUtil.createType("Abc", parent);
if (elementToBuildTemplateOn instanceof GrVariableDeclaration) {
GrVariableDeclaration decl = (GrVariableDeclaration) elementToBuildTemplateOn;
decl.setType(typeToUse);
nameElementOffset = decl.getModifierList().getTextRange().getEndOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
} else {
GrVariable var = (GrVariable) parent;
var.setType(typeToUse);
nameElementOffset = var.getNameIdentifierGroovy().getTextRange().getStartOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
}
return new TypeInfo(getTypeElement(parent), nameElementOffset);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList in project intellij-community by JetBrains.
the class GrInplaceVariableIntroducer method getTypeELementOrDef.
@Nullable
private static PsiElement getTypeELementOrDef(@NotNull GrVariable variable) {
GrTypeElement typeElement = variable.getTypeElementGroovy();
if (typeElement != null)
return typeElement;
GrModifierList modifierList = variable.getModifierList();
if (modifierList != null)
return modifierList.getModifier(GrModifier.DEF);
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList in project intellij-community by JetBrains.
the class GroovyOverrideImplementUtil method setupAnnotations.
private static void setupAnnotations(@NotNull GrTypeDefinition aClass, @NotNull PsiMethod method, @NotNull GrMethod result) {
if (OverrideImplementUtil.isInsertOverride(method, aClass)) {
result.getModifierList().addAnnotation(CommonClassNames.JAVA_LANG_OVERRIDE);
}
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
final PsiParameter[] originalParams = method.getParameterList().getParameters();
GrParameter[] parameters = result.getParameters();
for (int i = 0; i < parameters.length; i++) {
GrParameter parameter = parameters[i];
PsiParameter original = originalParams[i];
for (PsiAnnotation annotation : original.getModifierList().getAnnotations()) {
final GrModifierList modifierList = parameter.getModifierList();
String qname = annotation.getQualifiedName();
if (qname != null && modifierList.findAnnotation(qname) == null) {
if (annotation instanceof GrAnnotation) {
modifierList.add(annotation);
} else {
modifierList.add(factory.createAnnotationFromText(annotation.getText()));
}
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList 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);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList in project intellij-community by JetBrains.
the class GrChangeSignatureUsageProcessor method processPrimaryMethodInner.
private static boolean processPrimaryMethodInner(JavaChangeInfo changeInfo, GrMethod method, @Nullable PsiMethod baseMethod) {
if (changeInfo.isNameChanged()) {
String newName = baseMethod == null ? changeInfo.getNewName() : RefactoringUtil.suggestNewOverriderName(method.getName(), baseMethod.getName(), changeInfo.getNewName());
if (newName != null && !newName.equals(method.getName())) {
method.setName(changeInfo.getNewName());
}
}
final GrModifierList modifierList = method.getModifierList();
if (changeInfo.isVisibilityChanged()) {
modifierList.setModifierProperty(changeInfo.getNewVisibility(), true);
}
PsiSubstitutor substitutor = baseMethod != null ? calculateSubstitutor(method, baseMethod) : PsiSubstitutor.EMPTY;
final PsiMethod context = changeInfo.getMethod();
GrTypeElement oldReturnTypeElement = method.getReturnTypeElementGroovy();
if (changeInfo.isReturnTypeChanged()) {
CanonicalTypes.Type newReturnType = changeInfo.getNewReturnType();
if (newReturnType == null) {
if (oldReturnTypeElement != null) {
oldReturnTypeElement.delete();
if (modifierList.getModifiers().length == 0) {
modifierList.setModifierProperty(GrModifier.DEF, true);
}
}
} else {
PsiType type = newReturnType.getType(context, method.getManager());
GrReferenceAdjuster.shortenAllReferencesIn(method.setReturnType(substitutor.substitute(type)));
if (oldReturnTypeElement == null) {
modifierList.setModifierProperty(GrModifier.DEF, false);
}
}
}
JavaParameterInfo[] newParameters = changeInfo.getNewParameters();
final GrParameterList parameterList = method.getParameterList();
GrParameter[] oldParameters = parameterList.getParameters();
final PsiParameter[] oldBaseParams = baseMethod != null ? baseMethod.getParameterList().getParameters() : null;
Set<GrParameter> toRemove = new HashSet<>(oldParameters.length);
ContainerUtil.addAll(toRemove, oldParameters);
GrParameter anchor = null;
final GrDocComment docComment = method.getDocComment();
final GrDocTag[] tags = docComment == null ? null : docComment.getTags();
int newParamIndex = 0;
for (JavaParameterInfo newParameter : newParameters) {
//if old parameter name differs from base method parameter name we don't change it
final String newName;
final int oldIndex = newParameter.getOldIndex();
if (oldIndex >= 0 && oldBaseParams != null) {
final String oldName = oldParameters[oldIndex].getName();
if (oldName.equals(oldBaseParams[oldIndex].getName())) {
newName = newParameter.getName();
} else {
newName = oldName;
}
} else {
newName = newParameter.getName();
}
final GrParameter oldParameter = oldIndex >= 0 ? oldParameters[oldIndex] : null;
if (docComment != null && oldParameter != null) {
final String oldName = oldParameter.getName();
for (GrDocTag tag : tags) {
if ("@param".equals(tag.getName())) {
final GrDocParameterReference parameterReference = tag.getDocParameterReference();
if (parameterReference != null && oldName.equals(parameterReference.getText())) {
parameterReference.handleElementRename(newName);
}
}
}
}
GrParameter grParameter = createNewParameter(substitutor, context, parameterList, newParameter, newName);
if (oldParameter != null) {
grParameter.getModifierList().replace(oldParameter.getModifierList());
}
if ("def".equals(newParameter.getTypeText())) {
grParameter.getModifierList().setModifierProperty(GrModifier.DEF, true);
} else if (StringUtil.isEmpty(newParameter.getTypeText())) {
grParameter.getModifierList().setModifierProperty(GrModifier.DEF, false);
}
anchor = (GrParameter) parameterList.addAfter(grParameter, anchor);
if (newParamIndex < oldParameters.length) {
GrParameter oldParam = oldParameters[newParamIndex];
PsiElement prev = oldParam.getPrevSibling();
if (prev instanceof PsiWhiteSpace) {
parameterList.addBefore(prev, anchor);
}
}
newParamIndex++;
}
for (GrParameter oldParameter : toRemove) {
oldParameter.delete();
}
JavaCodeStyleManager.getInstance(parameterList.getProject()).shortenClassReferences(parameterList);
CodeStyleManager.getInstance(parameterList.getProject()).reformat(parameterList);
if (changeInfo.isExceptionSetOrOrderChanged()) {
final ThrownExceptionInfo[] infos = changeInfo.getNewExceptions();
PsiClassType[] exceptionTypes = new PsiClassType[infos.length];
for (int i = 0; i < infos.length; i++) {
ThrownExceptionInfo info = infos[i];
exceptionTypes[i] = (PsiClassType) info.createType(method, method.getManager());
}
PsiReferenceList thrownList = GroovyPsiElementFactory.getInstance(method.getProject()).createThrownList(exceptionTypes);
thrownList = (PsiReferenceList) method.getThrowsList().replace(thrownList);
JavaCodeStyleManager.getInstance(thrownList.getProject()).shortenClassReferences(thrownList);
CodeStyleManager.getInstance(method.getProject()).reformat(method.getThrowsList());
}
return true;
}
Aggregations