mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
create Java field from usage in Groovy code
This commit is contained in:
+2
-17
@@ -25,7 +25,6 @@ import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.impl.source.codeStyle.CodeEditUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -81,22 +80,8 @@ public class CreateFieldFromUsageFix extends CreateVarFromUsageFix {
|
||||
PsiUtil.setModifierProperty(field, PsiModifier.FINAL, true);
|
||||
}
|
||||
|
||||
if (enclosingContext != null &&
|
||||
enclosingContext.getParent() == parentClass &&
|
||||
targetClass == parentClass &&
|
||||
enclosingContext instanceof PsiField) {
|
||||
field = (PsiField)targetClass.addBefore(field, enclosingContext);
|
||||
}
|
||||
else if (enclosingContext != null &&
|
||||
enclosingContext.getParent() == parentClass &&
|
||||
targetClass == parentClass &&
|
||||
enclosingContext instanceof PsiClassInitializer) {
|
||||
field = (PsiField)targetClass.addBefore(field, enclosingContext);
|
||||
targetClass.addBefore(CodeEditUtil.createLineFeed(field.getManager()), enclosingContext);
|
||||
}
|
||||
else {
|
||||
field = (PsiField)targetClass.add(field);
|
||||
}
|
||||
|
||||
field = CreateFieldFromUsageHelper.insertField(targetClass, field, myReferenceExpression);
|
||||
|
||||
setupVisibility(parentClass, targetClass, field.getModifierList());
|
||||
|
||||
|
||||
+8
@@ -41,6 +41,14 @@ public abstract class CreateFieldFromUsageHelper {
|
||||
CreateFromUsageBaseFix.getTargetSubstitutor(context));
|
||||
}
|
||||
|
||||
public static PsiField insertField(PsiClass targetClass, PsiField field, PsiElement place) {
|
||||
CreateFieldFromUsageHelper helper = EP_NAME.forLanguage(field.getLanguage());
|
||||
if (helper == null) return null;
|
||||
return helper.insertFieldImpl(targetClass, field, place);
|
||||
}
|
||||
|
||||
public abstract PsiField insertFieldImpl(PsiClass targetClass, PsiField field, PsiElement place);
|
||||
|
||||
public abstract Template setupTemplateImpl(PsiField field,
|
||||
Object expectedTypes,
|
||||
PsiClass targetClass,
|
||||
|
||||
+32
@@ -22,6 +22,8 @@ import com.intellij.codeInsight.template.TemplateBuilderImpl;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.source.codeStyle.CodeEditUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
|
||||
/**
|
||||
* @author Max Medvedev
|
||||
@@ -60,4 +62,34 @@ public class JavaCreateFieldFromUsageHelper extends CreateFieldFromUsageHelper {
|
||||
if (((ExpectedTypeInfo[])expectedTypes).length > 1) template.setToShortenLongNames(false);
|
||||
return template;
|
||||
}
|
||||
|
||||
public PsiField insertFieldImpl(PsiClass targetClass, PsiField field, PsiElement place) {
|
||||
PsiMember enclosingContext = null;
|
||||
PsiClass parentClass;
|
||||
do {
|
||||
enclosingContext = PsiTreeUtil.getParentOfType(enclosingContext == null ? place : enclosingContext, PsiMethod.class, PsiField.class, PsiClassInitializer.class);
|
||||
parentClass = enclosingContext == null ? null : enclosingContext.getContainingClass();
|
||||
}
|
||||
while (parentClass instanceof PsiAnonymousClass);
|
||||
|
||||
if (enclosingContext != null &&
|
||||
enclosingContext.getParent() == parentClass &&
|
||||
targetClass == parentClass &&
|
||||
enclosingContext instanceof PsiField) {
|
||||
field = (PsiField)targetClass.addBefore(field, enclosingContext);
|
||||
}
|
||||
else if (enclosingContext != null &&
|
||||
enclosingContext.getParent() == parentClass &&
|
||||
targetClass == parentClass &&
|
||||
enclosingContext instanceof PsiClassInitializer) {
|
||||
field = (PsiField)targetClass.addBefore(field, enclosingContext);
|
||||
targetClass.addBefore(CodeEditUtil.createLineFeed(field.getManager()), enclosingContext);
|
||||
}
|
||||
else {
|
||||
field = (PsiField)targetClass.add(field);
|
||||
}
|
||||
|
||||
return field;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user