visibility based highlighting (IDEA-80384; IDEA-228556)

GitOrigin-RevId: 1c8c903d4bdc7be5459987cc5c081f302e4e2d16
This commit is contained in:
Anna Kozlova
2021-05-10 11:02:33 +00:00
committed by intellij-monorepo-bot
parent d4a7e73545
commit 66873cdedb
7 changed files with 95 additions and 9 deletions
@@ -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);
@@ -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);
}
@@ -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>UnknownType</unknownType> <field>anotherString</field> = \"Another\\nStrin\\g\";\n" +
" public static int <static>staticField</static> = 0;\n" +
" public final int <instanceFinalField>instanceFinalField</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 <reassignedLocalVar>reassignedValue</reassignedLocalVar> = this.<warning>staticField</warning> + <param>param2</param> + <param>param3</param>;\n" +
" long <localVar>localVar1</localVar>, <localVar>localVar2</localVar>, <localVar>localVar3</localVar>, <localVar>localVar4</localVar>;\n" +
" <error>int <localVar>localVar</localVar> = \"IntelliJ\"</error>; // Error, incompatible types\n" +
" <class>System</class>.<static>out</static>.<methodCall>println</methodCall>(<field>anotherString</field> + <inherited_method>toString</inherited_method>() + <localVar>localVar</localVar>);\n" +
" <class>System</class>.<static>out</static>.<methodCall>println</methodCall>(<private><field>anotherString</field></private> + <inherited_method>toString</inherited_method>() + <localVar>localVar</localVar>);\n" +
" int <localVar>sum</localVar> = <protected><field>protectedField</field></protected> + <package_private><field>packagePrivateField</field></package_private> + <public><static>staticField</static></public>;\n" +
" long <localVar>time</localVar> = <static_imported_method><deprecated>parse</deprecated></static_imported_method>(\"1.2.3\"); // Method is deprecated\n" +
" new <class>Thread</class>().<for_removal>countStackFrames</for_removal>(); // Method is deprecated and marked for removal\n" +
" <reassignedLocalVar>reassignedValue</reassignedLocalVar> ++; \n" +
@@ -0,0 +1,12 @@
public class <symbolName descr="null">Simple</symbolName> {
private final int <symbolName descr="null">bar</symbolName> = 0;
private class <symbolName descr="null">Inner</symbolName> { }
private int <symbolName descr="null">foo</symbolName>() { return 0;}
void <symbolName descr="null">bazz</symbolName>() {
int <symbolName descr="null">x</symbolName> =
new <symbolName descr="null" type="CONSTRUCTOR_CALL" foreground="0x000000" effectcolor="0xffc800" effecttype="BOXED" fonttype="0">Inner</symbolName>().<symbolName descr="null">hashCode</symbolName>() +
<symbolName descr="null" type="INSTANCE_FINAL_FIELD" foreground="0x660e7a" effectcolor="0xffc800" effecttype="BOXED" fonttype="1">bar</symbolName> +
<symbolName descr="null" type="METHOD_CALL" foreground="0x000000" effectcolor="0xffc800" effecttype="BOXED" fonttype="0">foo</symbolName>();
}
}
@@ -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));
@@ -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" +
@@ -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