Search in sources :

Example 6 with CodeStyleManager

use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.

the class JavaIntroduceParameterObjectClassDescriptor method createClass.

@Override
public PsiClass createClass(PsiMethod method, ReadWriteAccessDetector.Access[] accessors) {
    if (isUseExistingClass()) {
        return getExistingClass();
    }
    final ParameterObjectBuilder beanClassBuilder = new ParameterObjectBuilder();
    beanClassBuilder.setVisibility(isCreateInnerClass() ? PsiModifier.PRIVATE : PsiModifier.PUBLIC);
    beanClassBuilder.setProject(method.getProject());
    beanClassBuilder.setTypeArguments(getTypeParameters());
    beanClassBuilder.setClassName(getClassName());
    beanClassBuilder.setPackageName(getPackageName());
    PsiParameter[] parameters = method.getParameterList().getParameters();
    final ParameterInfoImpl[] parameterInfos = getParamsToMerge();
    for (int i = 0; i < parameterInfos.length; i++) {
        PsiParameter parameter = parameters[parameterInfos[i].getOldIndex()];
        final boolean setterRequired = accessors[i] == ReadWriteAccessDetector.Access.Write;
        final String newName = parameterInfos[i].getName();
        beanClassBuilder.addField(parameter, newName, parameterInfos[i].getTypeWrapper().getType(method), setterRequired);
    }
    final String classString = beanClassBuilder.buildBeanClass();
    try {
        final PsiFileFactory factory = PsiFileFactory.getInstance(method.getProject());
        final PsiJavaFile newFile = (PsiJavaFile) factory.createFileFromText(getClassName() + ".java", JavaFileType.INSTANCE, classString);
        if (isCreateInnerClass()) {
            final PsiClass containingClass = method.getContainingClass();
            final PsiClass[] classes = newFile.getClasses();
            assert classes.length > 0 : classString;
            final PsiClass innerClass = (PsiClass) containingClass.add(classes[0]);
            PsiUtil.setModifierProperty(innerClass, PsiModifier.STATIC, true);
            return (PsiClass) JavaCodeStyleManager.getInstance(newFile.getProject()).shortenClassReferences(innerClass);
        } else {
            final PsiFile containingFile = method.getContainingFile();
            final PsiDirectory containingDirectory = containingFile.getContainingDirectory();
            final PsiDirectory directory;
            final MoveDestination moveDestination = getMoveDestination();
            if (moveDestination != null) {
                directory = moveDestination.getTargetDirectory(containingDirectory);
            } else {
                final Module module = ModuleUtilCore.findModuleForPsiElement(containingFile);
                directory = PackageUtil.findOrCreateDirectoryForPackage(module, getPackageName(), containingDirectory, true, true);
            }
            if (directory != null) {
                PsiFile file = directory.findFile(newFile.getName());
                if (file == null) {
                    final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(method.getManager().getProject());
                    final PsiElement shortenedFile = JavaCodeStyleManager.getInstance(newFile.getProject()).shortenClassReferences(newFile);
                    final PsiElement reformattedFile = codeStyleManager.reformat(shortenedFile);
                    file = (PsiFile) directory.add(reformattedFile);
                }
                return ((PsiJavaFile) file).getClasses()[0];
            }
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return null;
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) MoveDestination(com.intellij.refactoring.MoveDestination) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Module(com.intellij.openapi.module.Module)

Example 7 with CodeStyleManager

use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.

the class MakeMethodStaticProcessor method changeInternalUsage.

protected void changeInternalUsage(InternalUsageInfo usage, PsiElementFactory factory) throws IncorrectOperationException {
    if (!mySettings.isChangeSignature())
        return;
    PsiElement element = usage.getElement();
    if (element instanceof PsiReferenceExpression) {
        PsiReferenceExpression newRef = null;
        if (mySettings.isMakeFieldParameters()) {
            PsiElement resolved = ((PsiReferenceExpression) element).resolve();
            if (resolved instanceof PsiField) {
                String name = mySettings.getNameForField((PsiField) resolved);
                if (name != null) {
                    newRef = (PsiReferenceExpression) factory.createExpressionFromText(name, null);
                }
            }
        }
        if (newRef == null && mySettings.isMakeClassParameter()) {
            newRef = (PsiReferenceExpression) factory.createExpressionFromText(mySettings.getClassParameterName() + "." + element.getText(), null);
        }
        if (newRef != null) {
            CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myProject);
            newRef = (PsiReferenceExpression) codeStyleManager.reformat(newRef);
            element.replace(newRef);
        }
    } else if (element instanceof PsiThisExpression && mySettings.isMakeClassParameter()) {
        element.replace(factory.createExpressionFromText(mySettings.getClassParameterName(), null));
    } else if (element instanceof PsiSuperExpression && mySettings.isMakeClassParameter()) {
        element.replace(factory.createExpressionFromText(mySettings.getClassParameterName(), null));
    } else if (element instanceof PsiNewExpression && mySettings.isMakeClassParameter()) {
        final PsiNewExpression newExpression = ((PsiNewExpression) element);
        LOG.assertTrue(newExpression.getQualifier() == null);
        final String newText = mySettings.getClassParameterName() + "." + newExpression.getText();
        final PsiExpression expr = factory.createExpressionFromText(newText, null);
        element.replace(expr);
    }
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager)

Example 8 with CodeStyleManager

use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.

the class PsiElementFactoryImpl method createParameter.

@NotNull
@Override
public PsiParameter createParameter(@NotNull final String name, @NotNull final PsiType type) throws IncorrectOperationException {
    PsiUtil.checkIsIdentifier(myManager, name);
    if (PsiType.NULL.equals(type)) {
        throw new IncorrectOperationException("Cannot create parameter with type \"null\".");
    }
    final String text = type.getCanonicalText(true) + " " + name;
    PsiParameter parameter = createParameterFromText(text, null);
    final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myManager.getProject());
    PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, JavaCodeStyleSettingsFacade.getInstance(myManager.getProject()).isGenerateFinalParameters());
    GeneratedMarkerVisitor.markGenerated(parameter);
    parameter = (PsiParameter) JavaCodeStyleManager.getInstance(myManager.getProject()).shortenClassReferences(parameter);
    return (PsiParameter) codeStyleManager.reformat(parameter);
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with CodeStyleManager

use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.

the class PsiElementFactoryImpl method createCatchSection.

@NotNull
@Override
public PsiCatchSection createCatchSection(@NotNull final PsiType exceptionType, @NotNull final String exceptionName, @Nullable final PsiElement context) throws IncorrectOperationException {
    if (!(exceptionType instanceof PsiClassType || exceptionType instanceof PsiDisjunctionType)) {
        throw new IncorrectOperationException("Unexpected type:" + exceptionType);
    }
    @NonNls final String text = "catch (" + exceptionType.getCanonicalText(true) + " " + exceptionName + ") {}";
    final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, CATCH_SECTION, level(context)), context);
    final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode());
    if (!(element instanceof PsiCatchSection)) {
        throw new IncorrectOperationException("Incorrect catch section '" + text + "'. Parsed element: " + element);
    }
    final Project project = myManager.getProject();
    final JavaPsiImplementationHelper helper = JavaPsiImplementationHelper.getInstance(project);
    helper.setupCatchBlock(exceptionName, exceptionType, context, (PsiCatchSection) element);
    final CodeStyleManager styleManager = CodeStyleManager.getInstance(project);
    final PsiCatchSection catchSection = (PsiCatchSection) styleManager.reformat(element);
    GeneratedMarkerVisitor.markGenerated(catchSection);
    return catchSection;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with CodeStyleManager

use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.

the class JsonStringPropertyInsertHandler method reformat.

private static void reformat(@NotNull InsertionContext context, int startOffset, int endOffset) {
    PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getDocument());
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(context.getProject());
    codeStyleManager.reformatText(context.getFile(), startOffset, endOffset);
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager)

Aggregations

CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)97 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)29 Project (com.intellij.openapi.project.Project)26 TextRange (com.intellij.openapi.util.TextRange)19 NonNls (org.jetbrains.annotations.NonNls)18 IncorrectOperationException (com.intellij.util.IncorrectOperationException)16 NotNull (org.jetbrains.annotations.NotNull)8 Document (com.intellij.openapi.editor.Document)6 PsiFile (com.intellij.psi.PsiFile)6 Module (com.intellij.openapi.module.Module)5 PsiElement (com.intellij.psi.PsiElement)4 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)4 Nullable (org.jetbrains.annotations.Nullable)4 CaretModel (com.intellij.openapi.editor.CaretModel)3 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)3 JavaLanguage (com.intellij.lang.java.JavaLanguage)2 FileType (com.intellij.openapi.fileTypes.FileType)2 Comparing (com.intellij.openapi.util.Comparing)2 StringUtil (com.intellij.openapi.util.text.StringUtil)2 com.intellij.psi (com.intellij.psi)2