diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightNamesUtil.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightNamesUtil.java index 0baa17786e12..4e89292e1ac4 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightNamesUtil.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightNamesUtil.java @@ -6,6 +6,7 @@ import com.intellij.codeHighlighting.RainbowHighlighter; import com.intellij.codeInsight.daemon.impl.HighlightInfo; import com.intellij.codeInsight.daemon.impl.HighlightInfoType; import com.intellij.codeInsight.daemon.impl.JavaHighlightInfoTypes; +import com.intellij.ide.highlighter.JavaHighlightingColors; import com.intellij.lang.ASTNode; import com.intellij.lang.java.JavaLanguage; import com.intellij.openapi.diagnostic.Logger; @@ -58,6 +59,9 @@ public final class HighlightNamesUtil { : JavaHighlightInfoTypes.CONSTRUCTOR_CALL; if (type != null) { TextAttributes attributes = mergeWithScopeAttributes(methodOrClass, type, colorsScheme); + if (!isDeclaration) { + attributes = mergeWithVisibilityAttributes(methodOrClass, attributes, colorsScheme); + } HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(type).range(elementToHighlight.getTextRange()); if (attributes != null) { builder.textAttributes(attributes); @@ -93,6 +97,27 @@ public final class HighlightNamesUtil { TextAttributes scopeAttributes = getScopeAttributes(element, colorsScheme); return TextAttributes.merge(scopeAttributes, regularAttributes); } + + private static TextAttributes mergeWithVisibilityAttributes(PsiModifierListOwner listOwner, TextAttributes basedAttributes, @NotNull TextAttributesScheme colorsScheme) { + TextAttributesKey attributesKey = null; + if (listOwner.hasModifierProperty(PsiModifier.PUBLIC)) { + attributesKey = JavaHighlightingColors.PUBLIC_REFERENCE_ATTRIBUTES; + } + else if (listOwner.hasModifierProperty(PsiModifier.PROTECTED)) { + attributesKey = JavaHighlightingColors.PROTECTED_REFERENCE_ATTRIBUTES; + } + else if (listOwner.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) { + attributesKey = JavaHighlightingColors.PACKAGE_PRIVATE_REFERENCE_ATTRIBUTES; + } + else if (listOwner.hasModifierProperty(PsiModifier.PRIVATE)) { + attributesKey = JavaHighlightingColors.PRIVATE_REFERENCE_ATTRIBUTES; + } + if (attributesKey != null) { + TextAttributes visibilityAttributes = colorsScheme.getAttributes(attributesKey); + if (visibilityAttributes != null && !visibilityAttributes.isEmpty()) return TextAttributes.merge(basedAttributes, visibilityAttributes); + } + return basedAttributes; + } @NotNull static HighlightInfo highlightClassName(@Nullable PsiClass aClass, @NotNull PsiElement elementToHighlight, @NotNull TextAttributesScheme colorsScheme) { @@ -114,6 +139,9 @@ public final class HighlightNamesUtil { HighlightInfoType type = getClassNameHighlightType(aClass, elementToHighlight); TextAttributes attributes = mergeWithScopeAttributes(aClass, type, colorsScheme); + if (aClass != null && elementToHighlight instanceof PsiJavaCodeReferenceElement) { + attributes = mergeWithVisibilityAttributes(aClass, attributes, colorsScheme); + } HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(type).range(range); if (attributes != null) { builder.textAttributes(attributes); @@ -131,6 +159,9 @@ public final class HighlightNamesUtil { } if (variable instanceof PsiField) { TextAttributes attributes = mergeWithScopeAttributes(variable, varType, colorsScheme); + if (elementToHighlight.getParent() instanceof PsiReferenceExpression) { + attributes = mergeWithVisibilityAttributes(variable, attributes, colorsScheme); + } HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(varType).range(elementToHighlight); if (attributes != null) { builder.textAttributes(attributes); diff --git a/java/java-analysis-impl/src/com/intellij/ide/highlighter/JavaHighlightingColors.java b/java/java-analysis-impl/src/com/intellij/ide/highlighter/JavaHighlightingColors.java index 089eddb844d1..7f5eee3ff868 100644 --- a/java/java-analysis-impl/src/com/intellij/ide/highlighter/JavaHighlightingColors.java +++ b/java/java-analysis-impl/src/com/intellij/ide/highlighter/JavaHighlightingColors.java @@ -105,4 +105,10 @@ public final class JavaHighlightingColors { = TextAttributesKey.createTextAttributesKey("ANNOTATION_ATTRIBUTE_NAME_ATTRIBUTES", DefaultLanguageHighlighterColors.METADATA); public static final TextAttributesKey ANNOTATION_ATTRIBUTE_VALUE_ATTRIBUTES = TextAttributesKey.createTextAttributesKey("ANNOTATION_ATTRIBUTE_VALUE_ATTRIBUTES", DefaultLanguageHighlighterColors.METADATA); + + //visibility + public static final TextAttributesKey PUBLIC_REFERENCE_ATTRIBUTES = TextAttributesKey.createTextAttributesKey("PUBLIC_REFERENCE", (TextAttributesKey)null); + public static final TextAttributesKey PROTECTED_REFERENCE_ATTRIBUTES = TextAttributesKey.createTextAttributesKey("PROTECTED_REFERENCE", (TextAttributesKey)null); + public static final TextAttributesKey PACKAGE_PRIVATE_REFERENCE_ATTRIBUTES = TextAttributesKey.createTextAttributesKey("PACKAGE_PRIVATE_REFERENCE", (TextAttributesKey)null); + public static final TextAttributesKey PRIVATE_REFERENCE_ATTRIBUTES = TextAttributesKey.createTextAttributesKey("PRIVATE_REFERENCE", (TextAttributesKey)null); } diff --git a/java/java-impl/src/com/intellij/openapi/options/colors/pages/JavaColorSettingsPage.java b/java/java-impl/src/com/intellij/openapi/options/colors/pages/JavaColorSettingsPage.java index 57a84aead7c7..39dfc099c0ef 100644 --- a/java/java-impl/src/com/intellij/openapi/options/colors/pages/JavaColorSettingsPage.java +++ b/java/java-impl/src/com/intellij/openapi/options/colors/pages/JavaColorSettingsPage.java @@ -90,6 +90,10 @@ public class JavaColorSettingsPage implements RainbowColorSettingsPage, Inspecti new AttributesDescriptor(JavaBundle.message("options.java.attribute.descriptor.static.method"), JavaHighlightingColors.STATIC_METHOD_ATTRIBUTES), new AttributesDescriptor(JavaBundle.message("options.java.attribute.descriptor.abstract.method"), JavaHighlightingColors.ABSTRACT_METHOD_ATTRIBUTES), new AttributesDescriptor(JavaBundle.message("options.java.attribute.descriptor.inherited.method"), JavaHighlightingColors.INHERITED_METHOD_ATTRIBUTES), + new AttributesDescriptor(JavaBundle.message("options.java.attribute.descriptor.public"), JavaHighlightingColors.PUBLIC_REFERENCE_ATTRIBUTES), + new AttributesDescriptor(JavaBundle.message("options.java.attribute.descriptor.protected"), JavaHighlightingColors.PROTECTED_REFERENCE_ATTRIBUTES), + new AttributesDescriptor(JavaBundle.message("options.java.attribute.descriptor.package.private"), JavaHighlightingColors.PACKAGE_PRIVATE_REFERENCE_ATTRIBUTES), + new AttributesDescriptor(JavaBundle.message("options.java.attribute.descriptor.private"), JavaHighlightingColors.PRIVATE_REFERENCE_ATTRIBUTES), new AttributesDescriptor(JavaBundle.message("options.java.attribute.descriptor.annotation.name"), JavaHighlightingColors.ANNOTATION_NAME_ATTRIBUTES), new AttributesDescriptor(JavaBundle.message("options.java.attribute.descriptor.annotation.attribute.name"), JavaHighlightingColors.ANNOTATION_ATTRIBUTE_NAME_ATTRIBUTES) @@ -135,6 +139,10 @@ public class JavaColorSettingsPage implements RainbowColorSettingsPage, Inspecti ourTags.put("staticallyConstImported", JavaHighlightingColors.STATIC_FINAL_FIELD_IMPORTED_ATTRIBUTES); ourTags.put("staticallyImported", JavaHighlightingColors.STATIC_FIELD_IMPORTED_ATTRIBUTES); ourTags.put("static_imported_method", JavaHighlightingColors.STATIC_METHOD_CALL_IMPORTED_ATTRIBUTES); + ourTags.put("public", JavaHighlightingColors.PUBLIC_REFERENCE_ATTRIBUTES); + ourTags.put("protected", JavaHighlightingColors.PROTECTED_REFERENCE_ATTRIBUTES); + ourTags.put("package_private", JavaHighlightingColors.PACKAGE_PRIVATE_REFERENCE_ATTRIBUTES); + ourTags.put("private", JavaHighlightingColors.PRIVATE_REFERENCE_ATTRIBUTES); } @Override @@ -185,6 +193,8 @@ public class JavaColorSettingsPage implements RainbowColorSettingsPage, Inspecti " private UnknownType anotherString = \"Another\\nStrin\\g\";\n" + " public static int staticField = 0;\n" + " public final int instanceFinalField = 0;\n" + + " protected final int protectedField = 0;\n" + + " final int packagePrivateField = 0;\n" + "\n" + " /**" + RainbowHighlighter.generatePaletteExample("\n * ") + "\n" + @@ -199,7 +209,8 @@ public class JavaColorSettingsPage implements RainbowColorSettingsPage, Inspecti " int reassignedValue = this.staticField + param2 + param3;\n" + " long localVar1, localVar2, localVar3, localVar4;\n" + " int localVar = \"IntelliJ\"; // Error, incompatible types\n" + - " System.out.println(anotherString + toString() + localVar);\n" + + " System.out.println(anotherString + toString() + localVar);\n" + + " int sum = protectedField + packagePrivateField + staticField;\n" + " long time = parse(\"1.2.3\"); // Method is deprecated\n" + " new Thread().countStackFrames(); // Method is deprecated and marked for removal\n" + " reassignedValue ++; \n" + diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/visibility/Simple.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/visibility/Simple.java new file mode 100644 index 000000000000..0945271ee6d5 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/visibility/Simple.java @@ -0,0 +1,12 @@ +public class Simple { +private final int bar = 0; +private class Inner { } +private int foo() { return 0;} + + void bazz() { + int x = +new Inner().hashCode() + +bar + +foo(); + } + } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/AdvHighlightingTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/AdvHighlightingTest.java index 1e4e21085f01..2a7e3d1ec3a6 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/AdvHighlightingTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/AdvHighlightingTest.java @@ -6,6 +6,7 @@ import com.intellij.application.options.colors.ScopeAttributesUtil; import com.intellij.codeInsight.daemon.DaemonAnalyzerTestCase; import com.intellij.codeInsight.daemon.impl.HighlightInfo; import com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase; +import com.intellij.ide.highlighter.JavaHighlightingColors; import com.intellij.openapi.application.ex.PathManagerEx; import com.intellij.openapi.editor.colors.EditorColorsManager; import com.intellij.openapi.editor.colors.EditorColorsScheme; @@ -16,6 +17,7 @@ import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleManager; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.roots.ModuleRootModificationUtil; +import com.intellij.openapi.util.Disposer; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.packageDependencies.DependencyValidationManager; @@ -52,6 +54,16 @@ public class AdvHighlightingTest extends DaemonAnalyzerTestCase { doTest(BASE_PATH + "/packageClassClash1/pkg/sub.java", BASE_PATH + "/packageClassClash1", false, false); } + private EditorColorsScheme cloneColorSchema() { + EditorColorsManager manager = EditorColorsManager.getInstance(); + EditorColorsScheme globalScheme = manager.getGlobalScheme(); + EditorColorsScheme scheme = (EditorColorsScheme)globalScheme.clone(); + manager.addColorsScheme(scheme); + manager.setGlobalScheme(scheme); + Disposer.register(getTestRootDisposable(), () -> manager.setGlobalScheme(globalScheme)); + return scheme; + } + public void testScopeBased() { NamedScope xScope = new NamedScope("xxx", new PatternPackageSet("x..*", PatternPackageSet.SCOPE_SOURCE, null)); NamedScope utilScope = new NamedScope("util", new PatternPackageSet("java.util.*", PatternPackageSet.SCOPE_LIBRARY, null)); @@ -59,10 +71,7 @@ public class AdvHighlightingTest extends DaemonAnalyzerTestCase { scopeManager.addScope(xScope); scopeManager.addScope(utilScope); - EditorColorsManager manager = EditorColorsManager.getInstance(); - EditorColorsScheme scheme = (EditorColorsScheme)manager.getGlobalScheme().clone(); - manager.addColorsScheme(scheme); - EditorColorsManager.getInstance().setGlobalScheme(scheme); + EditorColorsScheme scheme = cloneColorSchema(); TextAttributesKey xKey = ScopeAttributesUtil.getScopeTextAttributeKey(xScope.getScopeId()); TextAttributes xAttributes = new TextAttributes(Color.cyan, Color.darkGray, Color.blue, EffectType.BOXED, Font.ITALIC); scheme.setAttributes(xKey, xAttributes); @@ -86,10 +95,7 @@ public class AdvHighlightingTest extends DaemonAnalyzerTestCase { scopeManager.addScope(xScope); scopeManager.addScope(utilScope); - EditorColorsManager manager = EditorColorsManager.getInstance(); - EditorColorsScheme scheme = (EditorColorsScheme)manager.getGlobalScheme().clone(); - manager.addColorsScheme(scheme); - EditorColorsManager.getInstance().setGlobalScheme(scheme); + EditorColorsScheme scheme = cloneColorSchema(); TextAttributesKey xKey = ScopeAttributesUtil.getScopeTextAttributeKey(xScope.getScopeId()); TextAttributes xAttributes = new TextAttributes(Color.cyan, Color.darkGray, Color.blue, null, Font.ITALIC); scheme.setAttributes(xKey, xAttributes); @@ -111,6 +117,14 @@ public class AdvHighlightingTest extends DaemonAnalyzerTestCase { } } + public void testVisibilityBased() { + EditorColorsScheme scheme = cloneColorSchema(); + TextAttributesKey xKey = JavaHighlightingColors.PRIVATE_REFERENCE_ATTRIBUTES; + TextAttributes xAttributes = new TextAttributes(null, null, Color.orange, EffectType.BOXED, Font.PLAIN); + scheme.setAttributes(xKey, xAttributes); + testFile(BASE_PATH + "/visibility/Simple.java").projectRoot(BASE_PATH + "/visibility").checkSymbolNames().test(); + } + public void testMultiJDKConflict() { String path = PathManagerEx.getTestDataPath() + BASE_PATH + "/" + getTestName(true); VirtualFile root = LocalFileSystem.getInstance().findFileByIoFile(new File(path)); diff --git a/java/java-tests/testSrc/com/intellij/java/editor/JavaEditorTextAttributesTest.java b/java/java-tests/testSrc/com/intellij/java/editor/JavaEditorTextAttributesTest.java index d32c7d446fc0..64ffec9ee734 100644 --- a/java/java-tests/testSrc/com/intellij/java/editor/JavaEditorTextAttributesTest.java +++ b/java/java-tests/testSrc/com/intellij/java/editor/JavaEditorTextAttributesTest.java @@ -150,7 +150,11 @@ public class JavaEditorTextAttributesTest extends LightPlatformTestCase { "LOCAL_VARIABLE_ATTRIBUTES { color: #000000; font-style: normal; }\n" + "METHOD_CALL_ATTRIBUTES { color: #000000; font-style: normal; }\n" + "METHOD_DECLARATION_ATTRIBUTES { color: #000000; font-style: normal; }\n" + + "PACKAGE_PRIVATE_REFERENCE { color: #000000; font-style: normal; }\n" + "PARAMETER_ATTRIBUTES { color: #000000; font-style: normal; }\n" + + "PRIVATE_REFERENCE { color: #000000; font-style: normal; }\n" + + "PROTECTED_REFERENCE { color: #000000; font-style: normal; }\n" + + "PUBLIC_REFERENCE { color: #000000; font-style: normal; }\n" + "REASSIGNED_LOCAL_VARIABLE_ATTRIBUTES { color: #000000; font-style: normal; text-decoration: underline #909090; }\n" + "REASSIGNED_PARAMETER_ATTRIBUTES { color: #000000; font-style: normal; text-decoration: underline #909090; }\n" + "STATIC_FIELD_ATTRIBUTES { color: #660e7a; font-style: italic; }\n" + @@ -202,7 +206,11 @@ public class JavaEditorTextAttributesTest extends LightPlatformTestCase { "LOCAL_VARIABLE_ATTRIBUTES { color: #a9b7c6; font-style: normal; }\n" + "METHOD_CALL_ATTRIBUTES { color: #a9b7c6; font-style: normal; }\n" + "METHOD_DECLARATION_ATTRIBUTES { color: #ffc66d; font-style: normal; }\n" + + "PACKAGE_PRIVATE_REFERENCE { color: #a9b7c6; font-style: normal; }\n" + "PARAMETER_ATTRIBUTES { color: #a9b7c6; font-style: normal; }\n" + + "PRIVATE_REFERENCE { color: #a9b7c6; font-style: normal; }\n" + + "PROTECTED_REFERENCE { color: #a9b7c6; font-style: normal; }\n" + + "PUBLIC_REFERENCE { color: #a9b7c6; font-style: normal; }\n" + "REASSIGNED_LOCAL_VARIABLE_ATTRIBUTES { color: #a9b7c6; font-style: normal; text-decoration: underline #707d95; }\n" + "REASSIGNED_PARAMETER_ATTRIBUTES { color: #a9b7c6; font-style: normal; text-decoration: underline #707d95; }\n" + "STATIC_FIELD_ATTRIBUTES { color: #9876aa; font-style: italic; }\n" + diff --git a/java/openapi/resources/messages/JavaBundle.properties b/java/openapi/resources/messages/JavaBundle.properties index d0ea47bcf13e..8a78c3cd0866 100644 --- a/java/openapi/resources/messages/JavaBundle.properties +++ b/java/openapi/resources/messages/JavaBundle.properties @@ -1033,6 +1033,10 @@ options.java.attribute.descriptor.static.final.imported.field=Class Fields//Cons options.java.attribute.descriptor.static.imported.field=Class Fields//Static imported field options.java.attribute.descriptor.static.method=Methods//Static method options.java.attribute.descriptor.type.parameter=Parameters//Type parameter +options.java.attribute.descriptor.public=Visibility//Public +options.java.attribute.descriptor.protected=Visibility//Protected +options.java.attribute.descriptor.package.private=Visibility//Package-private +options.java.attribute.descriptor.private=Visibility//Private options.java.display.name=Java override.implement.broken.file.template.message=Please Correct "Overridden/Implemented Method Body" Template override.implement.broken.file.template.title=File Template Error