PY-20401 Don't highlight anything else inside highlighted annotations

This commit is contained in:
Mikhail Golubev
2017-10-09 14:56:50 +03:00
parent 47144c3dfe
commit 0dd7aef0c1
5 changed files with 26 additions and 7 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", IDENTIFIER);
public static final TextAttributesKey PY_ANNOTATION = TextAttributesKey.createTextAttributesKey("PY.ANNOTATION");
public static final TextAttributesKey PY_VALID_STRING_ESCAPE = TextAttributesKey.createTextAttributesKey("PY.VALID_STRING_ESCAPE", VALID_STRING_ESCAPE);
@@ -17,11 +17,15 @@ package com.jetbrains.python.validation;
import com.intellij.lang.ASTNode;
import com.intellij.lang.annotation.Annotation;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.jetbrains.python.psi.PyElementVisitor;
import com.intellij.lang.annotation.AnnotationHolder;
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;
/**
@@ -54,6 +58,9 @@ public abstract class PyAnnotator extends PyElementVisitor {
}
protected void addHighlightingAnnotation(@NotNull PsiElement target, @NotNull TextAttributesKey key) {
if (annotationHighlightingEnabled() && insideAnnotationValue(target) && key != PyHighlighter.PY_ANNOTATION) {
return;
}
final String message = myTestMode ? key.getExternalName() : null;
final Annotation annotation = getHolder().createInfoAnnotation(target, message);
annotation.setTextAttributes(key);
@@ -62,4 +69,13 @@ public abstract class PyAnnotator extends PyElementVisitor {
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"><info descr="PY.BUILTIN_NAME">str</info></info> = 'foo'
TOP_LEVEL: <info descr="PY.ANNOTATION">str</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[<info descr="PY.BUILTIN_NAME">int</info>]</info>) -> <info descr="PY.ANNOTATION"><info descr="PY.KEYWORD">None</info></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[int]</info>) -> <info descr="PY.ANNOTATION">None</info>:
pass
@@ -1,2 +1,2 @@
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):
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):
<info descr="PY.BUILTIN_NAME">print</info>(<info descr="PY.PARAMETER">p1</info>, <info descr="PY.PARAMETER">p2</info>)
@@ -23,6 +23,7 @@ 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;
@@ -371,11 +372,13 @@ 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);
}