PY-20401 Use a lower highlighting level for annotations

So that other annotations, such as the one for builtin symbols, always
have priority over it, but it still was possible to, say, add
a background color or a font style for type annotations using this new
settings.
This commit is contained in:
Mikhail Golubev
2017-10-09 14:56:50 +03:00
parent 0dd7aef0c1
commit d5fb34404e
6 changed files with 23 additions and 28 deletions
@@ -112,7 +112,7 @@ public class PyHighlighter extends SyntaxHighlighterBase {
public static final TextAttributesKey PY_FUNCTION_CALL = TextAttributesKey.createTextAttributesKey("PY.FUNCTION_CALL", FUNCTION_CALL);
public static final TextAttributesKey PY_METHOD_CALL = TextAttributesKey.createTextAttributesKey("PY.METHOD_CALL", PY_FUNCTION_CALL);
public static final TextAttributesKey PY_ANNOTATION = TextAttributesKey.createTextAttributesKey("PY.ANNOTATION");
public static final TextAttributesKey PY_ANNOTATION = TextAttributesKey.createTextAttributesKey("PY.ANNOTATION", IDENTIFIER);
public static final TextAttributesKey PY_VALID_STRING_ESCAPE = TextAttributesKey.createTextAttributesKey("PY.VALID_STRING_ESCAPE", VALID_STRING_ESCAPE);
@@ -15,6 +15,7 @@
*/
package com.jetbrains.python.validation;
import com.intellij.codeInsight.daemon.impl.HighlightInfoType;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.psi.PsiElement;
@@ -81,7 +82,7 @@ public class HighlightingAnnotator extends PyAnnotator {
public void visitPyAnnotation(PyAnnotation node) {
final PyExpression value = node.getValue();
if (value != null) {
addHighlightingAnnotation(value, PyHighlighter.PY_ANNOTATION);
addHighlightingAnnotation(value, PyHighlighter.PY_ANNOTATION, HighlightInfoType.SYMBOL_TYPE_SEVERITY);
}
}
@@ -18,13 +18,10 @@ package com.jetbrains.python.validation;
import com.intellij.lang.ASTNode;
import com.intellij.lang.annotation.Annotation;
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.python.highlighting.PyHighlighter;
import com.jetbrains.python.psi.PyAnnotation;
import com.jetbrains.python.psi.PyElementVisitor;
import org.jetbrains.annotations.NotNull;
@@ -58,24 +55,21 @@ public abstract class PyAnnotator extends PyElementVisitor {
}
protected void addHighlightingAnnotation(@NotNull PsiElement target, @NotNull TextAttributesKey key) {
if (annotationHighlightingEnabled() && insideAnnotationValue(target) && key != PyHighlighter.PY_ANNOTATION) {
return;
}
addHighlightingAnnotation(target, key, HighlightSeverity.INFORMATION);
}
protected void addHighlightingAnnotation(@NotNull PsiElement target,
@NotNull TextAttributesKey key,
@NotNull HighlightSeverity severity) {
final String message = myTestMode ? key.getExternalName() : null;
final Annotation annotation = getHolder().createInfoAnnotation(target, message);
// CodeInsightTestFixture#testHighlighting doesn't consider annotations with severity level < INFO
final HighlightSeverity actualSeverity =
myTestMode && severity.myVal < HighlightSeverity.INFORMATION.myVal ? HighlightSeverity.INFORMATION : severity;
final Annotation annotation = getHolder().createAnnotation(actualSeverity, target.getTextRange(), message);
annotation.setTextAttributes(key);
}
protected void addHighlightingAnnotation(@NotNull ASTNode target, @NotNull TextAttributesKey key) {
addHighlightingAnnotation(target.getPsi(), key);
}
private static boolean insideAnnotationValue(@NotNull PsiElement target) {
final PyAnnotation annotation = PsiTreeUtil.getParentOfType(target, PyAnnotation.class);
return annotation != null && PsiTreeUtil.isAncestor(annotation.getValue(), target, false);
}
private static boolean annotationHighlightingEnabled() {
return !EditorColorsManager.getInstance().getGlobalScheme().getAttributes(PyHighlighter.PY_ANNOTATION).isEmpty();
}
}
+2 -2
View File
@@ -1,6 +1,6 @@
TOP_LEVEL: <info descr="PY.ANNOTATION">str</info> = 'foo'
TOP_LEVEL: <info descr="PY.ANNOTATION"><info descr="PY.BUILTIN_NAME">str</info></info> = 'foo'
class <info descr="PY.CLASS_DEFINITION">C</info>:
attr: <info descr="PY.ANNOTATION">Optional[Any]</info> = None
def <info descr="PY.FUNC_DEFINITION">method</info>(<info descr="PY.SELF_PARAMETER">self</info>, <info descr="PY.PARAMETER">xs</info>: <info descr="PY.ANNOTATION">List[int]</info>) -> <info descr="PY.ANNOTATION">None</info>:
def <info descr="PY.FUNC_DEFINITION">method</info>(<info descr="PY.SELF_PARAMETER">self</info>, <info descr="PY.PARAMETER">xs</info>: <info descr="PY.ANNOTATION">List[<info descr="PY.BUILTIN_NAME">int</info>]</info>) -> <info descr="PY.ANNOTATION"><info descr="PY.KEYWORD">None</info></info>:
pass
@@ -1,2 +1,2 @@
def <info descr="PY.FUNC_DEFINITION">f</info>(<info descr="PY.PARAMETER">p1</info>: <info descr="PY.ANNOTATION">int</info>, <info descr="PY.PARAMETER">p2</info>: <info descr="PY.ANNOTATION">int</info> = 42):
def <info descr="PY.FUNC_DEFINITION">f</info>(<info descr="PY.PARAMETER">p1</info>: <info descr="PY.ANNOTATION"><info descr="PY.BUILTIN_NAME">int</info></info>, <info descr="PY.PARAMETER">p2</info>: <info descr="PY.ANNOTATION"><info descr="PY.BUILTIN_NAME">int</info></info> = 42):
<info descr="PY.BUILTIN_NAME">print</info>(<info descr="PY.PARAMETER">p1</info>, <info descr="PY.PARAMETER">p2</info>)
@@ -23,7 +23,6 @@ import com.intellij.openapi.editor.markup.TextAttributes;
import com.jetbrains.python.documentation.PyDocumentationSettings;
import com.jetbrains.python.documentation.docstrings.DocStringFormat;
import com.jetbrains.python.fixtures.PyTestCase;
import com.jetbrains.python.highlighting.PyHighlighter;
import com.jetbrains.python.psi.LanguageLevel;
import com.jetbrains.python.psi.impl.PythonLanguageLevelPusher;
import org.jetbrains.annotations.NotNull;
@@ -372,13 +371,11 @@ public class PythonHighlightingTest extends PyTestCase {
// PY-20401
public void testAnnotations() {
createTemporaryColorScheme().setAttributes(PyHighlighter.PY_ANNOTATION, new TextAttributes(Color.RED, null, null, null, Font.PLAIN));
runWithLanguageLevel(LanguageLevel.PYTHON36, this::doTest);
}
// PY-22729
public void testParametersWithAnnotationsAndDefaults() {
createTemporaryColorScheme().setAttributes(PyHighlighter.PY_ANNOTATION, new TextAttributes(Color.RED, null, null, null, Font.PLAIN));
runWithLanguageLevel(LanguageLevel.PYTHON30, this::doTest);
}
@@ -403,12 +400,15 @@ public class PythonHighlightingTest extends PyTestCase {
}
private void doTest() {
final String TEST_PATH = "/highlighting/";
myFixture.testHighlighting(true, true, false, TEST_PATH + getTestName(true) + PyNames.DOT_PY);
doTest(true, true);
}
private void doTest(boolean checkWarnings, boolean checkInfos) {
myFixture.testHighlighting(checkWarnings, checkInfos, false, TEST_PATH + getTestName(true) + PyNames.DOT_PY);
myFixture.testHighlighting(checkWarnings, checkInfos, false, getTestName(true) + PyNames.DOT_PY);
}
@Override
protected String getTestDataPath() {
return super.getTestDataPath() + "/highlighting/";
}
}