Search in sources :

Example 11 with LanguageLevel

use of com.intellij.pom.java.LanguageLevel in project intellij-community by JetBrains.

the class JavaFormatterTest method test.

public void test() throws Exception {
    getSettings().getRootSettings().getIndentOptions(StdFileTypes.JAVA).INDENT_SIZE = 2;
    getSettings().getRootSettings().getIndentOptions(StdFileTypes.JAVA).CONTINUATION_INDENT_SIZE = 2;
    getSettings().RIGHT_MARGIN = 37;
    getSettings().ALIGN_MULTILINE_EXTENDS_LIST = true;
    getSettings().EXTENDS_KEYWORD_WRAP = CommonCodeStyleSettings.WRAP_AS_NEEDED;
    getSettings().EXTENDS_LIST_WRAP = CommonCodeStyleSettings.WRAP_AS_NEEDED;
    getSettings().ASSERT_STATEMENT_WRAP = CommonCodeStyleSettings.WRAP_AS_NEEDED;
    getSettings().ASSERT_STATEMENT_COLON_ON_NEXT_LINE = false;
    getSettings().ALIGN_MULTILINE_BINARY_OPERATION = true;
    final JavaPsiFacade facade = getJavaFacade();
    final LanguageLevel effectiveLanguageLevel = LanguageLevelProjectExtension.getInstance(facade.getProject()).getLanguageLevel();
    LanguageLevelProjectExtension.getInstance(facade.getProject()).setLanguageLevel(LanguageLevel.JDK_1_5);
    try {
        doTextTest("public class ThisIsASampleClass extends C1 implements I1, I2, I3, I4, I5 {\n" + "  public void longerMethod() {\n" + "    assert i + j + k + l + n+ m <= 2 : \"assert description\";" + "  }\n" + "}", "public class ThisIsASampleClass\n" + "  extends C1\n" + "  implements I1, I2, I3, I4, I5 {\n" + "  public void longerMethod() {\n" + "    assert\n" + "      i + j + k + l + n + m <= 2 :\n" + "      \"assert description\";\n" + "  }\n" + "}");
    } finally {
        LanguageLevelProjectExtension.getInstance(facade.getProject()).setLanguageLevel(effectiveLanguageLevel);
    }
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) LanguageLevel(com.intellij.pom.java.LanguageLevel)

Example 12 with LanguageLevel

use of com.intellij.pom.java.LanguageLevel in project intellij-community by JetBrains.

the class JavaFormatterTest method testSCRIDEADEV_2292.

public void testSCRIDEADEV_2292() throws IncorrectOperationException {
    getSettings().KEEP_CONTROL_STATEMENT_IN_ONE_LINE = false;
    getSettings().WHILE_ON_NEW_LINE = true;
    final JavaPsiFacade facade = getJavaFacade();
    final LanguageLevel stored = LanguageLevelProjectExtension.getInstance(facade.getProject()).getLanguageLevel();
    LanguageLevelProjectExtension.getInstance(facade.getProject()).setLanguageLevel(LanguageLevel.JDK_1_5);
    try {
        doTextTest("class Foo {\n" + "    void foo() {\n" + "        if (a) foo();\n" + "        else bar();\n" + "    }\n" + "}", "class Foo {\n" + "    void foo() {\n" + "        if (a)\n" + "            foo();\n" + "        else\n" + "            bar();\n" + "    }\n" + "}");
        doTextTest("class Foo {\n" + "    void foo() {\n" + "        for (int i = 0; i < 10; i++) foo();\n" + "    }\n" + "}", "class Foo {\n" + "    void foo() {\n" + "        for (int i = 0; i < 10; i++)\n" + "            foo();\n" + "    }\n" + "}");
        doTextTest("class Foo {\n" + "    void foo() {\n" + "        for (int var : vars) foo();\n" + "    }\n" + "}", "class Foo {\n" + "    void foo() {\n" + "        for (int var : vars)\n" + "            foo();\n" + "    }\n" + "}");
        doTextTest("class Foo {\n" + "    void foo() {\n" + "        do foo(); while (true);\n" + "    }\n" + "}", "class Foo {\n" + "    void foo() {\n" + "        do\n" + "            foo();\n" + "        while (true);\n" + "    }\n" + "}");
        doTextTest("class Foo {\n" + "    void foo() {\n" + "        while(true) foo();\n" + "    }\n" + "}", "class Foo {\n" + "    void foo() {\n" + "        while (true)\n" + "            foo();\n" + "    }\n" + "}");
        getSettings().KEEP_CONTROL_STATEMENT_IN_ONE_LINE = true;
        getSettings().WHILE_ON_NEW_LINE = false;
        doTextTest("class Foo {\n" + "    void foo() {\n" + "        if (a) foo();\n" + "        else bar();\n" + "    }\n" + "}", "class Foo {\n" + "    void foo() {\n" + "        if (a) foo();\n" + "        else bar();\n" + "    }\n" + "}");
        doTextTest("class Foo {\n" + "    void foo() {\n" + "        for (int i = 0; i < 10; i++) foo();\n" + "    }\n" + "}", "class Foo {\n" + "    void foo() {\n" + "        for (int i = 0; i < 10; i++) foo();\n" + "    }\n" + "}");
        doTextTest("class Foo {\n" + "    void foo() {\n" + "        for (int var : vars) foo();\n" + "    }\n" + "}", "class Foo {\n" + "    void foo() {\n" + "        for (int var : vars) foo();\n" + "    }\n" + "}");
        doTextTest("class Foo {\n" + "    void foo() {\n" + "        do foo(); while (true);\n" + "    }\n" + "}", "class Foo {\n" + "    void foo() {\n" + "        do foo(); while (true);\n" + "    }\n" + "}");
        doTextTest("class Foo {\n" + "    void foo() {\n" + "        while(true) foo();\n" + "    }\n" + "}", "class Foo {\n" + "    void foo() {\n" + "        while (true) foo();\n" + "    }\n" + "}");
        getSettings().RIGHT_MARGIN = 17;
        doTextTest("class Foo {\n" + "    void foo() {\n" + "        if (a) foo();\n" + "        else bar();\n" + "    }\n" + "}", "class Foo {\n" + "    void foo() {\n" + "        if (a)\n" + "            foo();\n" + "        else\n" + "            bar();\n" + "    }\n" + "}");
        getSettings().RIGHT_MARGIN = 30;
        doTextTest("class Foo {\n" + "    void foo() {\n" + "        for (int i = 0; i < 10; i++) foo();\n" + "    }\n" + "}", "class Foo {\n" + "    void foo() {\n" + "        for (int i = 0; i < 10; i++)\n" + "            foo();\n" + "    }\n" + "}");
        getSettings().RIGHT_MARGIN = 32;
        doTextTest("class Foo {\n" + "    void foo() {\n" + "        for (int var : vars) foo();\n" + "    }\n" + "}", "class Foo {\n" + "    void foo() {\n" + "        for (int var : vars)\n" + "            foo();\n" + "    }\n" + "}");
        getSettings().RIGHT_MARGIN = 12;
        doTextTest("class Foo {\n" + "    void foo() {\n" + "        do foo(); while (true);\n" + "    }\n" + "}", "class Foo {\n" + "    void foo() {\n" + "        do\n" + "            foo();\n" + "        while (true);\n" + "    }\n" + "}");
        getSettings().RIGHT_MARGIN = 23;
        doTextTest("class Foo {\n" + "    void foo() {\n" + "        while(true) foo();\n" + "    }\n" + "}", "class Foo {\n" + "    void foo() {\n" + "        while (true)\n" + "            foo();\n" + "    }\n" + "}");
    } finally {
        LanguageLevelProjectExtension.getInstance(facade.getProject()).setLanguageLevel(stored);
    }
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) LanguageLevel(com.intellij.pom.java.LanguageLevel)

Example 13 with LanguageLevel

use of com.intellij.pom.java.LanguageLevel in project intellij-community by JetBrains.

the class JavaFormatterTest method testSpaceAfterCommaInEnum.

public void testSpaceAfterCommaInEnum() throws IncorrectOperationException {
    getSettings().SPACE_AFTER_COMMA = true;
    final JavaPsiFacade facade = getJavaFacade();
    final LanguageLevel effectiveLanguageLevel = LanguageLevelProjectExtension.getInstance(facade.getProject()).getLanguageLevel();
    try {
        LanguageLevelProjectExtension.getInstance(facade.getProject()).setLanguageLevel(LanguageLevel.JDK_1_5);
        doTextTest("public enum StringExDirection {\n" + "\n" + "    RIGHT_TO_LEFT, LEFT_TO_RIGHT\n" + "\n" + "}", "public enum StringExDirection {\n" + "\n" + "    RIGHT_TO_LEFT, LEFT_TO_RIGHT\n" + "\n" + "}");
    } finally {
        LanguageLevelProjectExtension.getInstance(facade.getProject()).setLanguageLevel(effectiveLanguageLevel);
    }
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) LanguageLevel(com.intellij.pom.java.LanguageLevel)

Example 14 with LanguageLevel

use of com.intellij.pom.java.LanguageLevel in project intellij-community by JetBrains.

the class UncheckedWarningLocalInspectionBase method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
    LanguageLevel languageLevel = PsiUtil.getLanguageLevel(session.getFile());
    if (!languageLevel.isAtLeast(LanguageLevel.JDK_1_5))
        return super.buildVisitor(holder, isOnTheFly, session);
    return new UncheckedWarningsVisitor(isOnTheFly, languageLevel) {

        @Override
        protected void registerProblem(@NotNull String message, @Nullable PsiElement callExpression, @NotNull PsiElement psiElement, @NotNull LocalQuickFix[] quickFixes) {
            final String rawExpression = isMethodCalledOnRawType(callExpression);
            if (rawExpression != null) {
                final String referenceName = ((PsiMethodCallExpression) callExpression).getMethodExpression().getReferenceName();
                message += ". Reason: '" + rawExpression + "' has raw type, so result of " + referenceName + " is erased";
            }
            holder.registerProblem(psiElement, message, quickFixes);
        }
    };
}
Also used : LanguageLevel(com.intellij.pom.java.LanguageLevel) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with LanguageLevel

use of com.intellij.pom.java.LanguageLevel in project intellij-community by JetBrains.

the class JavaPsiImplementationHelperImpl method getEffectiveLanguageLevel.

@NotNull
@Override
public LanguageLevel getEffectiveLanguageLevel(@Nullable VirtualFile virtualFile) {
    if (virtualFile == null)
        return PsiUtil.getLanguageLevel(myProject);
    // prevent expensive look-up into VFS
    if (myProject.isDefault())
        return PsiUtil.getLanguageLevel(myProject);
    final VirtualFile folder = virtualFile.getParent();
    if (folder != null) {
        final LanguageLevel level = folder.getUserData(LanguageLevel.KEY);
        if (level != null)
            return level;
    }
    final ProjectFileIndex index = ProjectRootManager.getInstance(myProject).getFileIndex();
    Module module = index.getModuleForFile(virtualFile);
    if (module != null && index.isInSourceContent(virtualFile)) {
        return EffectiveLanguageLevelUtil.getEffectiveLanguageLevel(module);
    }
    LanguageLevel classesLanguageLevel = getClassesLanguageLevel(virtualFile);
    return classesLanguageLevel != null ? classesLanguageLevel : PsiUtil.getLanguageLevel(myProject);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LanguageLevel(com.intellij.pom.java.LanguageLevel) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

LanguageLevel (com.intellij.pom.java.LanguageLevel)98 LanguageLevelProjectExtension (com.intellij.openapi.roots.LanguageLevelProjectExtension)21 Nullable (org.jetbrains.annotations.Nullable)14 Module (com.intellij.openapi.module.Module)13 NotNull (org.jetbrains.annotations.NotNull)9 Sdk (com.intellij.openapi.projectRoots.Sdk)8 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)7 Project (com.intellij.openapi.project.Project)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 File (java.io.File)6 IOException (java.io.IOException)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 LocalInspectionToolWrapper (com.intellij.codeInspection.ex.LocalInspectionToolWrapper)3 Lexer (com.intellij.lexer.Lexer)3 LanguageLevelModuleExtension (com.intellij.openapi.roots.LanguageLevelModuleExtension)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 JavaProjectData (com.intellij.externalSystem.JavaProjectData)2 DisposeAwareProjectChange (com.intellij.openapi.externalSystem.util.DisposeAwareProjectChange)2 ConfigurationException (com.intellij.openapi.options.ConfigurationException)2