Search in sources :

Example 1 with JavaCodeStyleManager

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());
    }
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 2 with JavaCodeStyleManager

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);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) AlienFormFileException(com.intellij.uiDesigner.compiler.AlienFormFileException) LwRootContainer(com.intellij.uiDesigner.lw.LwRootContainer) IncorrectOperationException(com.intellij.util.IncorrectOperationException) AlienFormFileException(com.intellij.uiDesigner.compiler.AlienFormFileException) Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) List(java.util.List) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) PsiGenerationInfo(com.intellij.codeInsight.generation.PsiGenerationInfo)

Example 3 with JavaCodeStyleManager

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());
}
Also used : Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) PsiFile(com.intellij.psi.PsiFile) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with JavaCodeStyleManager

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);
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 5 with JavaCodeStyleManager

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);
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo)

Aggregations

JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)96 Project (com.intellij.openapi.project.Project)27 SuggestedNameInfo (com.intellij.psi.codeStyle.SuggestedNameInfo)21 VariableKind (com.intellij.psi.codeStyle.VariableKind)19 IncorrectOperationException (com.intellij.util.IncorrectOperationException)15 NotNull (org.jetbrains.annotations.NotNull)13 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)12 Nullable (org.jetbrains.annotations.Nullable)12 ArrayList (java.util.ArrayList)10 NonNls (org.jetbrains.annotations.NonNls)8 StringUtil (com.intellij.openapi.util.text.StringUtil)3 UniqueNameGenerator (com.intellij.util.text.UniqueNameGenerator)3 HashSet (java.util.HashSet)3 JavaLanguage (com.intellij.lang.java.JavaLanguage)2 Logger (com.intellij.openapi.diagnostic.Logger)2 Editor (com.intellij.openapi.editor.Editor)2 Comparing (com.intellij.openapi.util.Comparing)2 com.intellij.psi (com.intellij.psi)2 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)2 FunctionalInterfaceParameterizationUtil (com.intellij.psi.impl.source.resolve.graphInference.FunctionalInterfaceParameterizationUtil)2