Search in sources :

Example 41 with CodeStyleSettings

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

the class EclipseSettingsImportTest method testImportCodeStyleSettingsFromXmlProfile.

public void testImportCodeStyleSettingsFromXmlProfile() throws Exception {
    File input = new File(getTestDataPath() + "eclipse_exported.xml");
    CodeStyleSchemes schemes = CodeStyleSchemes.getInstance();
    CodeStyleScheme scheme = schemes.createNewScheme(getTestName(false), null);
    CodeStyleSettings settings = scheme.getCodeStyleSettings();
    CommonCodeStyleSettings javaSettings = settings.getCommonSettings("Java");
    CommonCodeStyleSettings.IndentOptions indentOptions = javaSettings.getIndentOptions();
    assertNotNull(indentOptions);
    javaSettings.SPACE_AFTER_COMMA_IN_TYPE_ARGUMENTS = false;
    javaSettings.SPACE_WITHIN_ARRAY_INITIALIZER_BRACES = false;
    javaSettings.ARRAY_INITIALIZER_RBRACE_ON_NEXT_LINE = true;
    javaSettings.SPACE_WITHIN_ANNOTATION_PARENTHESES = true;
    javaSettings.BLANK_LINES_AROUND_FIELD = -1;
    javaSettings.SPACE_WITHIN_WHILE_PARENTHESES = true;
    javaSettings.ELSE_ON_NEW_LINE = true;
    javaSettings.ALIGN_GROUP_FIELD_DECLARATIONS = true;
    javaSettings.SPACE_BEFORE_FOR_PARENTHESES = false;
    javaSettings.SPACE_AROUND_ADDITIVE_OPERATORS = false;
    javaSettings.SPACE_AROUND_BITWISE_OPERATORS = false;
    javaSettings.SPACE_AROUND_EQUALITY_OPERATORS = false;
    javaSettings.SPACE_AROUND_LOGICAL_OPERATORS = false;
    javaSettings.FINALLY_ON_NEW_LINE = true;
    javaSettings.CATCH_ON_NEW_LINE = true;
    javaSettings.SPACE_BEFORE_WHILE_PARENTHESES = false;
    javaSettings.BLANK_LINES_AFTER_PACKAGE = -1;
    javaSettings.getIndentOptions().CONTINUATION_INDENT_SIZE = 0;
    javaSettings.ALIGN_MULTILINE_PARAMETERS_IN_CALLS = true;
    javaSettings.BLANK_LINES_BEFORE_PACKAGE = -1;
    javaSettings.SPACE_WITHIN_FOR_PARENTHESES = true;
    javaSettings.ALIGN_MULTILINE_ASSIGNMENT = true;
    javaSettings.SPACE_BEFORE_METHOD_PARENTHESES = true;
    javaSettings.SPACE_WITHIN_CATCH_PARENTHESES = true;
    javaSettings.SPACE_BEFORE_METHOD_CALL_PARENTHESES = true;
    javaSettings.SPACE_WITHIN_CAST_PARENTHESES = true;
    javaSettings.SPACE_AROUND_UNARY_OPERATOR = true;
    javaSettings.KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = false;
    EditorSettingsExternalizable editorSettings = EditorSettingsExternalizable.getInstance();
    boolean currAddLineFeed = editorSettings.isEnsureNewLineAtEOF();
    editorSettings.setEnsureNewLineAtEOF(true);
    javaSettings.SPACE_WITHIN_TRY_PARENTHESES = true;
    javaSettings.SPACE_WITHIN_EMPTY_METHOD_CALL_PARENTHESES = true;
    javaSettings.WHILE_ON_NEW_LINE = true;
    settings.ENABLE_JAVADOC_FORMATTING = false;
    javaSettings.SPACE_BEFORE_SEMICOLON = true;
    javaSettings.BLANK_LINES_BEFORE_METHOD_BODY = -1;
    javaSettings.SPACE_BEFORE_COLON = false;
    javaSettings.DO_NOT_INDENT_TOP_LEVEL_CLASS_MEMBERS = true;
    javaSettings.BINARY_OPERATION_SIGN_ON_NEXT_LINE = false;
    javaSettings.SPACE_WITHIN_SYNCHRONIZED_PARENTHESES = true;
    javaSettings.SPACE_BEFORE_QUEST = false;
    javaSettings.BLANK_LINES_BEFORE_IMPORTS = 0;
    javaSettings.SPACE_AFTER_COLON = false;
    javaSettings.SPACE_WITHIN_FOR_PARENTHESES = true;
    javaSettings.SPACE_BEFORE_SYNCHRONIZED_PARENTHESES = false;
    javaSettings.SPACE_BEFORE_SWITCH_PARENTHESES = false;
    javaSettings.SPACE_WITHIN_METHOD_CALL_PARENTHESES = true;
    javaSettings.CLASS_BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;
    javaSettings.SPACE_BEFORE_ARRAY_INITIALIZER_LBRACE = false;
    javaSettings.SPACE_WITHIN_METHOD_PARENTHESES = true;
    javaSettings.SPACE_BEFORE_CATCH_PARENTHESES = false;
    javaSettings.SPACE_WITHIN_ANNOTATION_PARENTHESES = true;
    javaSettings.BLANK_LINES_AFTER_IMPORTS = -1;
    javaSettings.KEEP_FIRST_COLUMN_COMMENT = true;
    javaSettings.KEEP_CONTROL_STATEMENT_IN_ONE_LINE = true;
    javaSettings.FIELD_ANNOTATION_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
    javaSettings.METHOD_ANNOTATION_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
    javaSettings.PARAMETER_ANNOTATION_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
    javaSettings.VARIABLE_ANNOTATION_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
    javaSettings.CLASS_ANNOTATION_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
    javaSettings.ALIGN_MULTILINE_ARRAY_INITIALIZER_EXPRESSION = false;
    javaSettings.ARRAY_INITIALIZER_WRAP = CommonCodeStyleSettings.WRAP_ALWAYS;
    javaSettings.ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE = false;
    javaSettings.ALIGN_MULTILINE_PARAMETERS_IN_CALLS = true;
    javaSettings.CALL_PARAMETERS_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
    javaSettings.CALL_PARAMETERS_LPAREN_ON_NEXT_LINE = true;
    javaSettings.ALIGN_MULTILINE_EXTENDS_LIST = false;
    javaSettings.EXTENDS_KEYWORD_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
    javaSettings.EXTENDS_LIST_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
    javaSettings.ALIGN_MULTILINE_ASSIGNMENT = false;
    javaSettings.ASSIGNMENT_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
    javaSettings.ALIGN_MULTILINE_PARAMETERS = true;
    javaSettings.METHOD_PARAMETERS_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
    javaSettings.METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE = false;
    javaSettings.ALIGN_MULTILINE_BINARY_OPERATION = false;
    javaSettings.BINARY_OPERATION_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
    javaSettings.ALIGN_MULTILINE_THROWS_LIST = false;
    javaSettings.THROWS_KEYWORD_WRAP = CommonCodeStyleSettings.WRAP_ALWAYS;
    javaSettings.THROWS_LIST_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
    javaSettings.ALIGN_MULTILINE_RESOURCES = true;
    javaSettings.RESOURCE_LIST_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
    javaSettings.RESOURCE_LIST_LPAREN_ON_NEXT_LINE = true;
    javaSettings.METHOD_CALL_CHAIN_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
    javaSettings.ALIGN_MULTILINE_CHAINED_METHODS = false;
    javaSettings.TERNARY_OPERATION_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
    javaSettings.ALIGN_MULTILINE_TERNARY_OPERATION = false;
    javaSettings.BLANK_LINES_AFTER_CLASS_HEADER = -1;
    javaSettings.BLANK_LINES_AFTER_ANONYMOUS_CLASS_HEADER = -1;
    javaSettings.BLANK_LINES_AROUND_CLASS = -1;
    javaSettings.KEEP_BLANK_LINES_IN_CODE = -1;
    javaSettings.KEEP_BLANK_LINES_BEFORE_RBRACE = -1;
    javaSettings.KEEP_BLANK_LINES_IN_DECLARATIONS = -1;
    indentOptions.USE_TAB_CHARACTER = false;
    indentOptions.SMART_TABS = false;
    indentOptions.TAB_SIZE = 3;
    settings.FORMATTER_TAGS_ENABLED = false;
    javaSettings.SPACE_BEFORE_ELSE_KEYWORD = false;
    javaSettings.SPACE_BEFORE_FINALLY_KEYWORD = false;
    javaSettings.SPACE_BEFORE_CATCH_KEYWORD = false;
    javaSettings.SPACE_BEFORE_IF_LBRACE = false;
    javaSettings.SPACE_BEFORE_FOR_LBRACE = false;
    javaSettings.SPACE_BEFORE_WHILE_LBRACE = false;
    javaSettings.SPACE_BEFORE_DO_LBRACE = false;
    javaSettings.SPACE_BEFORE_TRY_LBRACE = false;
    javaSettings.SPACE_BEFORE_CATCH_LBRACE = false;
    javaSettings.SPACE_BEFORE_FINALLY_LBRACE = false;
    javaSettings.SPACE_BEFORE_SYNCHRONIZED_LBRACE = false;
    javaSettings.SPACE_BEFORE_METHOD_LBRACE = false;
    javaSettings.SPACE_BEFORE_CLASS_LBRACE = false;
    javaSettings.SPACE_BEFORE_ANOTATION_PARAMETER_LIST = true;
    javaSettings.KEEP_LINE_BREAKS = true;
    InputStream inputStream = new FileInputStream(input);
    try {
        new EclipseCodeStyleImportWorker().importScheme(inputStream, null, scheme);
        assertTrue(javaSettings.SPACE_AFTER_COMMA_IN_TYPE_ARGUMENTS);
        assertTrue(javaSettings.SPACE_WITHIN_ARRAY_INITIALIZER_BRACES);
        assertFalse(javaSettings.ARRAY_INITIALIZER_RBRACE_ON_NEXT_LINE);
        assertFalse(javaSettings.SPACE_WITHIN_ANNOTATION_PARENTHESES);
        assertEquals(0, javaSettings.BLANK_LINES_AROUND_FIELD);
        assertFalse(javaSettings.SPACE_WITHIN_WHILE_PARENTHESES);
        assertFalse(javaSettings.ELSE_ON_NEW_LINE);
        assertFalse(javaSettings.ALIGN_GROUP_FIELD_DECLARATIONS);
        assertTrue(javaSettings.SPACE_BEFORE_FOR_PARENTHESES);
        assertTrue(javaSettings.SPACE_AROUND_ADDITIVE_OPERATORS);
        assertTrue(javaSettings.SPACE_AROUND_BITWISE_OPERATORS);
        assertTrue(javaSettings.SPACE_AROUND_EQUALITY_OPERATORS);
        assertTrue(javaSettings.SPACE_AROUND_LOGICAL_OPERATORS);
        assertFalse(javaSettings.FINALLY_ON_NEW_LINE);
        assertFalse(javaSettings.CATCH_ON_NEW_LINE);
        assertTrue(javaSettings.SPACE_BEFORE_WHILE_PARENTHESES);
        assertEquals(1, javaSettings.BLANK_LINES_AFTER_PACKAGE);
        assertEquals(8, javaSettings.getIndentOptions().CONTINUATION_INDENT_SIZE);
        assertFalse(javaSettings.ALIGN_MULTILINE_PARAMETERS_IN_CALLS);
        assertEquals(0, javaSettings.BLANK_LINES_BEFORE_PACKAGE);
        assertFalse(javaSettings.SPACE_WITHIN_FOR_PARENTHESES);
        assertFalse(javaSettings.SPACE_BEFORE_METHOD_PARENTHESES);
        assertFalse(javaSettings.SPACE_WITHIN_CATCH_PARENTHESES);
        assertFalse(javaSettings.SPACE_BEFORE_METHOD_CALL_PARENTHESES);
        assertFalse(javaSettings.SPACE_WITHIN_CATCH_PARENTHESES);
        assertFalse(javaSettings.SPACE_AROUND_UNARY_OPERATOR);
        assertTrue(javaSettings.KEEP_SIMPLE_BLOCKS_IN_ONE_LINE);
        assertFalse(editorSettings.isEnsureNewLineAtEOF());
        assertFalse(javaSettings.SPACE_WITHIN_TRY_PARENTHESES);
        assertFalse(javaSettings.SPACE_WITHIN_EMPTY_METHOD_CALL_PARENTHESES);
        assertFalse(javaSettings.WHILE_ON_NEW_LINE);
        assertTrue(settings.ENABLE_JAVADOC_FORMATTING);
        assertFalse(javaSettings.SPACE_BEFORE_SEMICOLON);
        assertEquals(0, javaSettings.BLANK_LINES_BEFORE_METHOD_BODY);
        assertTrue(javaSettings.SPACE_BEFORE_COLON);
        assertFalse(javaSettings.DO_NOT_INDENT_TOP_LEVEL_CLASS_MEMBERS);
        assertTrue(javaSettings.BINARY_OPERATION_SIGN_ON_NEXT_LINE);
        assertFalse(javaSettings.SPACE_WITHIN_SYNCHRONIZED_PARENTHESES);
        assertTrue(javaSettings.SPACE_BEFORE_QUEST);
        assertEquals(1, javaSettings.BLANK_LINES_BEFORE_IMPORTS);
        assertTrue(javaSettings.SPACE_AFTER_COLON);
        assertFalse(javaSettings.SPACE_WITHIN_FOR_PARENTHESES);
        assertTrue(javaSettings.SPACE_BEFORE_SYNCHRONIZED_PARENTHESES);
        assertTrue(javaSettings.SPACE_BEFORE_SWITCH_PARENTHESES);
        assertFalse(javaSettings.SPACE_WITHIN_METHOD_CALL_PARENTHESES);
        assertEquals(CommonCodeStyleSettings.END_OF_LINE, javaSettings.CLASS_BRACE_STYLE);
        assertTrue(javaSettings.SPACE_BEFORE_ARRAY_INITIALIZER_LBRACE);
        assertFalse(javaSettings.SPACE_WITHIN_METHOD_PARENTHESES);
        assertTrue(javaSettings.SPACE_BEFORE_CATCH_PARENTHESES);
        assertFalse(javaSettings.SPACE_WITHIN_ANNOTATION_PARENTHESES);
        assertEquals(1, javaSettings.BLANK_LINES_AFTER_IMPORTS);
        assertFalse(javaSettings.KEEP_FIRST_COLUMN_COMMENT);
        assertFalse(javaSettings.KEEP_CONTROL_STATEMENT_IN_ONE_LINE);
        assertTrue(indentOptions.USE_TAB_CHARACTER);
        assertTrue(indentOptions.SMART_TABS);
        assertTrue(settings.FORMATTER_TAGS_ENABLED);
        assertEquals("@off_tag", settings.FORMATTER_OFF_TAG);
        assertEquals("@on_tag", settings.FORMATTER_ON_TAG);
        assertEquals(CommonCodeStyleSettings.WRAP_ON_EVERY_ITEM | CommonCodeStyleSettings.WRAP_AS_NEEDED, javaSettings.FIELD_ANNOTATION_WRAP);
        assertEquals(CommonCodeStyleSettings.WRAP_ON_EVERY_ITEM | CommonCodeStyleSettings.WRAP_AS_NEEDED, javaSettings.METHOD_ANNOTATION_WRAP);
        assertEquals(CommonCodeStyleSettings.WRAP_ON_EVERY_ITEM | CommonCodeStyleSettings.WRAP_AS_NEEDED, javaSettings.VARIABLE_ANNOTATION_WRAP);
        assertEquals(CommonCodeStyleSettings.WRAP_ON_EVERY_ITEM | CommonCodeStyleSettings.WRAP_AS_NEEDED, javaSettings.PARAMETER_ANNOTATION_WRAP);
        assertEquals(CommonCodeStyleSettings.WRAP_ON_EVERY_ITEM | CommonCodeStyleSettings.WRAP_AS_NEEDED, javaSettings.CLASS_ANNOTATION_WRAP);
        assertTrue(javaSettings.ALIGN_MULTILINE_ARRAY_INITIALIZER_EXPRESSION);
        assertEquals(CommonCodeStyleSettings.WRAP_AS_NEEDED, javaSettings.ARRAY_INITIALIZER_WRAP);
        assertTrue(javaSettings.ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE);
        assertFalse(javaSettings.ALIGN_MULTILINE_PARAMETERS_IN_CALLS);
        assertEquals(CommonCodeStyleSettings.WRAP_ON_EVERY_ITEM | CommonCodeStyleSettings.WRAP_AS_NEEDED, javaSettings.CALL_PARAMETERS_WRAP);
        assertFalse(javaSettings.CALL_PARAMETERS_LPAREN_ON_NEXT_LINE);
        assertTrue(javaSettings.ALIGN_MULTILINE_EXTENDS_LIST);
        assertEquals(CommonCodeStyleSettings.WRAP_AS_NEEDED, javaSettings.EXTENDS_KEYWORD_WRAP);
        assertEquals(CommonCodeStyleSettings.WRAP_ON_EVERY_ITEM | CommonCodeStyleSettings.WRAP_AS_NEEDED, javaSettings.EXTENDS_LIST_WRAP);
        assertTrue(javaSettings.ALIGN_MULTILINE_ASSIGNMENT);
        assertEquals(CommonCodeStyleSettings.WRAP_AS_NEEDED, javaSettings.ASSIGNMENT_WRAP);
        assertFalse(javaSettings.ALIGN_MULTILINE_PARAMETERS);
        assertEquals(CommonCodeStyleSettings.WRAP_AS_NEEDED, javaSettings.METHOD_PARAMETERS_WRAP);
        assertTrue(javaSettings.METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE);
        assertTrue(javaSettings.ALIGN_MULTILINE_BINARY_OPERATION);
        assertEquals(CommonCodeStyleSettings.WRAP_ON_EVERY_ITEM | CommonCodeStyleSettings.WRAP_AS_NEEDED, javaSettings.BINARY_OPERATION_WRAP);
        assertTrue(javaSettings.ALIGN_MULTILINE_THROWS_LIST);
        assertEquals(CommonCodeStyleSettings.WRAP_AS_NEEDED, javaSettings.THROWS_KEYWORD_WRAP);
        assertEquals(CommonCodeStyleSettings.WRAP_AS_NEEDED, javaSettings.THROWS_LIST_WRAP);
        assertFalse(javaSettings.ALIGN_MULTILINE_RESOURCES);
        assertEquals(CommonCodeStyleSettings.WRAP_AS_NEEDED, javaSettings.RESOURCE_LIST_WRAP);
        assertFalse(javaSettings.RESOURCE_LIST_LPAREN_ON_NEXT_LINE);
        assertEquals(CommonCodeStyleSettings.WRAP_ON_EVERY_ITEM | CommonCodeStyleSettings.WRAP_AS_NEEDED, javaSettings.METHOD_CALL_CHAIN_WRAP);
        assertTrue(javaSettings.ALIGN_MULTILINE_CHAINED_METHODS);
        assertEquals(CommonCodeStyleSettings.WRAP_AS_NEEDED, javaSettings.TERNARY_OPERATION_WRAP);
        assertTrue(javaSettings.ALIGN_MULTILINE_TERNARY_OPERATION);
        assertEquals(2, javaSettings.BLANK_LINES_AFTER_CLASS_HEADER);
        assertEquals(2, javaSettings.BLANK_LINES_AFTER_ANONYMOUS_CLASS_HEADER);
        assertEquals(3, javaSettings.BLANK_LINES_AROUND_CLASS);
        assertEquals(5, javaSettings.KEEP_BLANK_LINES_IN_CODE);
        assertEquals(5, javaSettings.KEEP_BLANK_LINES_IN_DECLARATIONS);
        assertEquals(5, javaSettings.KEEP_BLANK_LINES_BEFORE_RBRACE);
        assertTrue(javaSettings.SPACE_BEFORE_ELSE_KEYWORD);
        assertTrue(javaSettings.SPACE_BEFORE_FINALLY_KEYWORD);
        assertTrue(javaSettings.SPACE_BEFORE_CATCH_KEYWORD);
        assertTrue(javaSettings.SPACE_BEFORE_IF_LBRACE);
        assertTrue(javaSettings.SPACE_BEFORE_FOR_LBRACE);
        assertTrue(javaSettings.SPACE_BEFORE_WHILE_LBRACE);
        assertTrue(javaSettings.SPACE_BEFORE_DO_LBRACE);
        assertTrue(javaSettings.SPACE_BEFORE_TRY_LBRACE);
        assertTrue(javaSettings.SPACE_BEFORE_CATCH_LBRACE);
        assertTrue(javaSettings.SPACE_BEFORE_FINALLY_LBRACE);
        assertTrue(javaSettings.SPACE_BEFORE_SYNCHRONIZED_LBRACE);
        assertTrue(javaSettings.SPACE_BEFORE_METHOD_LBRACE);
        assertTrue(javaSettings.SPACE_BEFORE_CLASS_LBRACE);
        assertFalse(javaSettings.SPACE_BEFORE_ANOTATION_PARAMETER_LIST);
        assertFalse(javaSettings.KEEP_LINE_BREAKS);
    } finally {
        inputStream.close();
        schemes.deleteScheme(scheme);
        editorSettings.setEnsureNewLineAtEOF(currAddLineFeed);
    }
}
Also used : CodeStyleSchemes(com.intellij.psi.codeStyle.CodeStyleSchemes) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) EclipseCodeStyleImportWorker(org.jetbrains.idea.eclipse.importer.EclipseCodeStyleImportWorker) CodeStyleScheme(com.intellij.psi.codeStyle.CodeStyleScheme) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) File(java.io.File) FileInputStream(java.io.FileInputStream) EditorSettingsExternalizable(com.intellij.openapi.editor.ex.EditorSettingsExternalizable)

Example 42 with CodeStyleSettings

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

the class MakeCallChainIntoCallSequenceIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
    final List<String> callTexts = new ArrayList<>();
    PsiExpression root = (PsiExpression) element;
    while (root instanceof PsiMethodCallExpression) {
        final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) root;
        final PsiExpressionList arguments = methodCallExpression.getArgumentList();
        final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
        callTexts.add(methodExpression.getReferenceName() + arguments.getText());
        root = methodExpression.getQualifierExpression();
        if (root == null) {
            return;
        }
    }
    final PsiType rootType = root.getType();
    if (rootType == null) {
        return;
    }
    final String targetText;
    final PsiStatement appendStatement;
    @NonNls final String firstStatement;
    final String variableDeclaration;
    final boolean showRenameTemplate;
    final PsiElement parent = element.getParent();
    if (parent instanceof PsiExpressionStatement) {
        targetText = root.getText();
        appendStatement = (PsiStatement) parent;
        firstStatement = null;
        variableDeclaration = null;
        showRenameTemplate = false;
    } else {
        final PsiElement grandParent = parent.getParent();
        appendStatement = (PsiStatement) grandParent;
        if (parent instanceof PsiAssignmentExpression && grandParent instanceof PsiExpressionStatement) {
            final PsiAssignmentExpression assignment = (PsiAssignmentExpression) parent;
            final PsiExpression lhs = assignment.getLExpression();
            if (!(lhs instanceof PsiReferenceExpression)) {
                return;
            }
            final PsiReferenceExpression expression = (PsiReferenceExpression) lhs;
            final PsiElement target = expression.resolve();
            if (!(target instanceof PsiVariable)) {
                return;
            }
            final PsiVariable variable = (PsiVariable) target;
            final PsiType variableType = variable.getType();
            if (variableType.equals(rootType)) {
                targetText = lhs.getText();
                final PsiJavaToken token = assignment.getOperationSign();
                firstStatement = targetText + token.getText() + root.getText() + ';';
                showRenameTemplate = false;
            } else {
                targetText = "x";
                showRenameTemplate = true;
                final Project project = element.getProject();
                final CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(project);
                if (codeStyleSettings.GENERATE_FINAL_LOCALS) {
                    firstStatement = "final " + rootType.getCanonicalText() + ' ' + targetText + '=' + root.getText() + ';';
                } else {
                    firstStatement = rootType.getCanonicalText() + ' ' + targetText + '=' + root.getText() + ';';
                }
            }
            variableDeclaration = null;
        } else {
            final PsiDeclarationStatement declaration = (PsiDeclarationStatement) appendStatement;
            final PsiVariable variable = (PsiVariable) declaration.getDeclaredElements()[0];
            final PsiType variableType = variable.getType();
            if (variableType.equals(rootType)) {
                targetText = variable.getName();
                if (variable.hasModifierProperty(PsiModifier.FINAL)) {
                    firstStatement = "final " + variableType.getCanonicalText() + ' ' + variable.getName() + '=' + root.getText() + ';';
                } else {
                    firstStatement = variableType.getCanonicalText() + ' ' + variable.getName() + '=' + root.getText() + ';';
                }
                variableDeclaration = null;
                showRenameTemplate = false;
            } else {
                if (variable.hasModifierProperty(PsiModifier.FINAL)) {
                    variableDeclaration = "final " + variableType.getCanonicalText() + ' ' + variable.getName() + '=';
                } else {
                    variableDeclaration = variableType.getCanonicalText() + ' ' + variable.getName() + '=';
                }
                targetText = "x";
                showRenameTemplate = true;
                final Project project = element.getProject();
                final CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(project);
                if (codeStyleSettings.GENERATE_FINAL_LOCALS) {
                    firstStatement = "final " + rootType.getCanonicalText() + " x=" + root.getText() + ';';
                } else {
                    firstStatement = rootType.getCanonicalText() + " x=" + root.getText() + ';';
                }
            }
        }
    }
    final StringBuilder builder = new StringBuilder("{\n");
    if (firstStatement != null) {
        builder.append(firstStatement);
    }
    Collections.reverse(callTexts);
    for (int i = 0, size = callTexts.size(); i < size; i++) {
        final String callText = callTexts.get(i);
        if (i == size - 1 && variableDeclaration != null) {
            builder.append(variableDeclaration);
        }
        builder.append(targetText).append('.').append(callText).append(";\n");
    }
    builder.append('}');
    final PsiManager manager = element.getManager();
    final Project project = manager.getProject();
    final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
    final PsiElement appendStatementParent = appendStatement.getParent();
    final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
    final PsiCodeBlock codeBlock = factory.createCodeBlockFromText(builder.toString(), appendStatement);
    if (appendStatementParent instanceof PsiLoopStatement || appendStatementParent instanceof PsiIfStatement) {
        final PsiElement insertedCodeBlock = appendStatement.replace(codeBlock);
        final PsiCodeBlock reformattedCodeBlock = (PsiCodeBlock) codeStyleManager.reformat(insertedCodeBlock);
        if (showRenameTemplate) {
            final PsiStatement[] statements = reformattedCodeBlock.getStatements();
            final PsiVariable variable = (PsiVariable) ((PsiDeclarationStatement) statements[0]).getDeclaredElements()[0];
            HighlightUtil.showRenameTemplate(appendStatementParent, variable);
        }
    } else {
        final PsiStatement[] statements = codeBlock.getStatements();
        PsiVariable variable = null;
        for (int i = 0, length = statements.length; i < length; i++) {
            final PsiElement insertedStatement = appendStatementParent.addBefore(statements[i], appendStatement);
            if (i == 0 && showRenameTemplate) {
                variable = (PsiVariable) ((PsiDeclarationStatement) insertedStatement).getDeclaredElements()[0];
            }
            codeStyleManager.reformat(insertedStatement);
        }
        appendStatement.delete();
        if (variable != null) {
            HighlightUtil.showRenameTemplate(appendStatementParent, variable);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) NonNls(org.jetbrains.annotations.NonNls) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) Project(com.intellij.openapi.project.Project) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings)

Example 43 with CodeStyleSettings

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

the class FullyQualifiedNamePredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof PsiJavaCodeReferenceElement)) {
        return false;
    }
    final PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement) element;
    if (!referenceElement.isQualified()) {
        return false;
    }
    final PsiElement parent = referenceElement.getParent();
    if (parent instanceof PsiMethodCallExpression || parent instanceof PsiAssignmentExpression || parent instanceof PsiVariable) {
        return false;
    }
    if (PsiTreeUtil.getParentOfType(element, PsiImportStatementBase.class, PsiPackageStatement.class, JavaCodeFragment.class) != null) {
        return false;
    }
    final Project project = element.getProject();
    final CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(project);
    if (isInsideCommentInPackageInfo(referenceElement)) {
        return false;
    }
    final PsiFile file = element.getContainingFile();
    if ("module-info.java".equals(file.getName())) {
        return false;
    }
    final PsiElement qualifier = referenceElement.getQualifier();
    if (!(qualifier instanceof PsiJavaCodeReferenceElement)) {
        return false;
    }
    final PsiJavaCodeReferenceElement qualifierReferenceElement = (PsiJavaCodeReferenceElement) qualifier;
    final PsiElement resolved = qualifierReferenceElement.resolve();
    if (!(resolved instanceof PsiPackage)) {
        if (!(resolved instanceof PsiClass)) {
            return false;
        }
        if (!codeStyleSettings.INSERT_INNER_CLASS_IMPORTS) {
            return false;
        }
    }
    final PsiElement target = referenceElement.resolve();
    if (!(target instanceof PsiClass)) {
        return false;
    }
    final PsiClass aClass = (PsiClass) target;
    final String fqName = aClass.getQualifiedName();
    return fqName != null && ImportUtils.nameCanBeImported(fqName, element);
}
Also used : Project(com.intellij.openapi.project.Project) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings)

Example 44 with CodeStyleSettings

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

the class DataPointHolderConversionIntentionTest method tearDown.

@Override
protected void tearDown() throws Exception {
    final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
    settings.STATIC_FIELD_NAME_PREFIX = "";
    settings.STATIC_FIELD_NAME_SUFFIX = "";
    super.tearDown();
}
Also used : CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings)

Example 45 with CodeStyleSettings

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

the class ExtractMethodTest method doTestReturnTypeChanged.

private void doTestReturnTypeChanged(PsiType type) throws PrepareFailedException {
    final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
    settings.ELSE_ON_NEW_LINE = true;
    settings.CATCH_ON_NEW_LINE = myCatchOnNewLine;
    configureByFile(BASE_PATH + getTestName(false) + ".java");
    boolean success = performExtractMethod(true, true, getEditor(), getFile(), getProject(), false, type, false, null);
    assertTrue(success);
    checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
Also used : CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings)

Aggregations

CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)194 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)85 Project (com.intellij.openapi.project.Project)13 XmlCodeStyleSettings (com.intellij.psi.formatter.xml.XmlCodeStyleSettings)12 Document (com.intellij.openapi.editor.Document)10 ECMA4CodeStyleSettings (com.intellij.lang.javascript.formatter.ECMA4CodeStyleSettings)9 JSCodeStyleSettings (com.intellij.lang.javascript.formatter.JSCodeStyleSettings)9 JavaCodeStyleSettings (com.intellij.psi.codeStyle.JavaCodeStyleSettings)9 NotNull (org.jetbrains.annotations.NotNull)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 PsiFile (com.intellij.psi.PsiFile)5 Nullable (org.jetbrains.annotations.Nullable)5 FormattingModel (com.intellij.formatting.FormattingModel)4 PsiElement (com.intellij.psi.PsiElement)4 CodeStyleSettingsManager (com.intellij.psi.codeStyle.CodeStyleSettingsManager)4 File (java.io.File)4 IOException (java.io.IOException)4 CodeStyleAbstractConfigurable (com.intellij.application.options.CodeStyleAbstractConfigurable)3 TabbedLanguageCodeStylePanel (com.intellij.application.options.TabbedLanguageCodeStylePanel)3 FormattingModelBuilder (com.intellij.formatting.FormattingModelBuilder)3