use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class Generator method generateBean.
// todo: inline
private static void generateBean(final PsiClass aClass, final String[] properties, final HashMap<String, String> property2fqClassName) throws MyException {
final StringBuffer membersBuffer = new StringBuffer();
final StringBuffer methodsBuffer = new StringBuffer();
final CodeStyleManager formatter = CodeStyleManager.getInstance(aClass.getProject());
final JavaCodeStyleManager styler = JavaCodeStyleManager.getInstance(aClass.getProject());
for (final String property : properties) {
LOG.assertTrue(property != null);
final String type = property2fqClassName.get(property);
LOG.assertTrue(type != null);
generateProperty(styler, property, type, membersBuffer, methodsBuffer);
}
final PsiClass fakeClass;
try {
fakeClass = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory().createClassFromText(membersBuffer.toString() + methodsBuffer.toString(), null);
final PsiField[] fields = fakeClass.getFields();
for (final PsiField field : fields) {
aClass.add(field);
}
final PsiMethod[] methods = fakeClass.getMethods();
for (final PsiMethod method : methods) {
aClass.add(method);
}
styler.shortenClassReferences(aClass);
formatter.reformat(aClass);
} catch (IncorrectOperationException e) {
throw new MyException(e.getMessage());
}
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class GenerateMainAction method actionPerformed.
public void actionPerformed(AnActionEvent e) {
final Project project = e.getData(CommonDataKeys.PROJECT);
assert project != null;
final Editor editor = e.getData(CommonDataKeys.EDITOR);
assert editor != null;
final int offset = editor.getCaretModel().getOffset();
final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
PsiClass psiClass = PsiTreeUtil.getParentOfType(file.findElementAt(offset), PsiClass.class);
assert psiClass != null;
if (!PsiUtil.hasDefaultConstructor(psiClass)) {
Messages.showMessageDialog(project, UIDesignerBundle.message("generate.main.no.default.constructor"), UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
return;
}
final List<PsiFile> boundForms = FormClassIndex.findFormsBoundToClass(project, psiClass.getQualifiedName());
final LwRootContainer rootContainer;
try {
rootContainer = Utils.getRootContainer(boundForms.get(0).getText(), null);
} catch (AlienFormFileException ex) {
Messages.showMessageDialog(project, "The form bound to the class is not a valid IntelliJ IDEA form", UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
return;
} catch (Exception ex) {
LOG.error(ex);
return;
}
if (rootContainer.getComponentCount() == 0) {
Messages.showMessageDialog(project, UIDesignerBundle.message("generate.main.empty.form"), UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
return;
}
String rootBinding = rootContainer.getComponent(0).getBinding();
if (rootBinding == null || psiClass.findFieldByName(rootBinding, true) == null) {
Messages.showMessageDialog(project, UIDesignerBundle.message("generate.main.no.root.binding"), UIDesignerBundle.message("generate.main.title"), Messages.getErrorIcon());
return;
}
@NonNls final StringBuilder mainBuilder = new StringBuilder("public static void main(String[] args) { ");
final JavaCodeStyleManager csm = JavaCodeStyleManager.getInstance(project);
SuggestedNameInfo nameInfo = csm.suggestVariableName(VariableKind.LOCAL_VARIABLE, "frame", null, null);
String varName = nameInfo.names[0];
mainBuilder.append(JFrame.class.getName()).append(" ").append(varName).append("= new ").append(JFrame.class.getName());
mainBuilder.append("(\"").append(psiClass.getName()).append("\");");
mainBuilder.append(varName).append(".setContentPane(new ").append(psiClass.getQualifiedName()).append("().").append(rootBinding).append(");");
mainBuilder.append(varName).append(".setDefaultCloseOperation(").append(JFrame.class.getName()).append(".EXIT_ON_CLOSE);");
mainBuilder.append(varName).append(".pack();");
mainBuilder.append(varName).append(".setVisible(true);");
mainBuilder.append("}\n");
CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> {
try {
PsiMethod method = JavaPsiFacade.getInstance(file.getProject()).getElementFactory().createMethodFromText(mainBuilder.toString(), file);
List<PsiGenerationInfo<PsiMethod>> infos = Collections.singletonList(new PsiGenerationInfo<>(method));
List<PsiGenerationInfo<PsiMethod>> resultMembers = GenerateMembersUtil.insertMembersAtOffset(file, offset, infos);
resultMembers.get(0).positionCaret(editor, false);
} catch (IncorrectOperationException e1) {
LOG.error(e1);
}
}), null, null);
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class ParameterNameExpression method getNameInfo.
@Nullable
private SuggestedNameInfo getNameInfo(ExpressionContext context) {
Project project = context.getProject();
PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument());
assert file != null;
PsiElement elementAt = file.findElementAt(context.getStartOffset());
GrParameter parameter = PsiTreeUtil.getParentOfType(elementAt, GrParameter.class);
if (parameter == null)
return null;
JavaCodeStyleManager manager = JavaCodeStyleManager.getInstance(project);
return manager.suggestVariableName(VariableKind.PARAMETER, myDefaultName, null, parameter.getTypeGroovy());
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class GrPullUpHelper method replaceMovedMemberTypeParameters.
public static void replaceMovedMemberTypeParameters(final PsiElement member, final Iterable<PsiTypeParameter> parametersIterable, final PsiSubstitutor substitutor, final GroovyPsiElementFactory factory) {
final Map<PsiElement, PsiElement> replacement = new LinkedHashMap<>();
for (PsiTypeParameter parameter : parametersIterable) {
PsiType substitutedType = substitutor.substitute(parameter);
PsiType type = substitutedType != null ? substitutedType : TypeConversionUtil.erasure(factory.createType(parameter));
PsiElement scopeElement = member instanceof GrField ? member.getParent() : member;
for (PsiReference reference : ReferencesSearch.search(parameter, new LocalSearchScope(scopeElement))) {
final PsiElement element = reference.getElement();
final PsiElement parent = element.getParent();
if (parent instanceof PsiTypeElement) {
replacement.put(parent, factory.createTypeElement(type));
} else if (element instanceof GrCodeReferenceElement && type instanceof PsiClassType) {
replacement.put(element, factory.createReferenceElementByType((PsiClassType) type));
}
}
}
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(member.getProject());
for (PsiElement element : replacement.keySet()) {
if (element.isValid()) {
final PsiElement replaced = element.replace(replacement.get(element));
codeStyleManager.shortenClassReferences(replaced);
}
}
}
use of com.intellij.psi.codeStyle.JavaCodeStyleManager in project intellij-community by JetBrains.
the class IntroduceVariableBase method getSuggestedName.
public static SuggestedNameInfo getSuggestedName(@Nullable PsiType type, @NotNull final PsiExpression expression, final PsiElement anchor) {
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(expression.getProject());
final SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, expression, type);
final String[] strings = JavaCompletionUtil.completeVariableNameForRefactoring(codeStyleManager, type, VariableKind.LOCAL_VARIABLE, nameInfo);
final SuggestedNameInfo.Delegate delegate = new SuggestedNameInfo.Delegate(strings, nameInfo);
return codeStyleManager.suggestUniqueVariableName(delegate, anchor, true);
}
Aggregations