PY-20401 Add highlighting for function and variable annotations

This commit is contained in:
Mikhail Golubev
2017-09-06 14:27:25 +03:00
parent 4ccdd97276
commit e6fde8b92f
5 changed files with 28 additions and 2 deletions

View File

@@ -112,6 +112,8 @@ 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_VALID_STRING_ESCAPE = TextAttributesKey.createTextAttributesKey("PY.VALID_STRING_ESCAPE", VALID_STRING_ESCAPE);
public static final TextAttributesKey PY_INVALID_STRING_ESCAPE = TextAttributesKey.createTextAttributesKey("PY.INVALID_STRING_ESCAPE", INVALID_STRING_ESCAPE);

View File

@@ -62,6 +62,7 @@ public class PythonColorsPage implements ColorSettingsPage, InspectionColorSetti
new AttributesDescriptor("Keyword argument", PyHighlighter.PY_KEYWORD_ARGUMENT),
new AttributesDescriptor("Function call", PyHighlighter.PY_FUNCTION_CALL),
new AttributesDescriptor("Method call", PyHighlighter.PY_METHOD_CALL),
new AttributesDescriptor("Function and variable annotations", PyHighlighter.PY_ANNOTATION),
new AttributesDescriptor("Valid escape sequence", PyHighlighter.PY_VALID_STRING_ESCAPE),
new AttributesDescriptor("Invalid escape sequence", PyHighlighter.PY_INVALID_STRING_ESCAPE),
};
@@ -80,6 +81,7 @@ public class PythonColorsPage implements ColorSettingsPage, InspectionColorSetti
.put("kwarg", PyHighlighter.PY_KEYWORD_ARGUMENT)
.put("call", PyHighlighter.PY_FUNCTION_CALL)
.put("mcall", PyHighlighter.PY_METHOD_CALL)
.put("annotation", PyHighlighter.PY_ANNOTATION)
.build();
@NotNull
@@ -119,8 +121,9 @@ public class PythonColorsPage implements ColorSettingsPage, InspectionColorSetti
" print s[0].<mcall>lower()</mcall>\n"+
"\n"+
"class <classDef>Foo</classDef>:\n"+
" def <predefined>__init__</predefined>(<self>self</self>):\n" +
" byte_string = 'newline:\\n also newline:\\x0a'\n" +
" tags: <annotation>List[<builtin>str</builtin>]</annotation>\n" +
" def <predefined>__init__</predefined>(<self>self</self>: <annotation>Foo</annotation>):\n" +
" byte_string: <annotation><builtin>str</builtin></annotation> = 'newline:\\n also newline:\\x0a'\n" +
" text_string = u\"Cyrillic \u042f is \\u042f. Oops: \\u042g\"\n"+
" <self>self</self>.<mcall>makeSense</mcall>(<kwarg>whatever</kwarg>=1)\n" +
" \n" +

View File

@@ -77,4 +77,14 @@ public class HighlightingAnnotator extends PyAnnotator {
}
}
}
@Override
public void visitPyAnnotation(PyAnnotation node) {
final PyExpression value = node.getValue();
if (value != null) {
final String message = ApplicationManager.getApplication().isUnitTestMode() ? PyHighlighter.PY_ANNOTATION.getExternalName() : null;
final Annotation annotation = getHolder().createInfoAnnotation(value, message);
annotation.setTextAttributes(PyHighlighter.PY_ANNOTATION);
}
}
}

View File

@@ -0,0 +1,6 @@
TOP_LEVEL: <info descr="null"><info descr="PY.ANNOTATION">str</info></info> = 'foo'
class <info descr="null">C</info>:
attr: <info descr="PY.ANNOTATION">Optional[Any]</info> = None
def <info descr="null">method</info>(<info descr="null">self</info>, <info descr="null">xs: <info descr="PY.ANNOTATION">List[<info descr="null">int</info>]</info></info>) -> <info descr="PY.ANNOTATION">None</info>:
pass

View File

@@ -376,6 +376,11 @@ public class PythonHighlightingTest extends PyTestCase {
doTest();
}
// PY-20401
public void testAnnotations() {
runWithLanguageLevel(LanguageLevel.PYTHON36, this::doTest);
}
@NotNull
private static EditorColorsScheme createTemporaryColorScheme() {
EditorColorsManager manager = EditorColorsManager.getInstance();