PY-11418 Allow to highlight names in call expressions

This commit is contained in:
Mikhail Golubev
2017-10-09 14:56:50 +03:00
parent 650cd506d4
commit e9a5faf2fb
6 changed files with 51 additions and 20 deletions
@@ -109,6 +109,8 @@ public class PyHighlighter extends SyntaxHighlighterBase {
public static final TextAttributesKey PY_KEYWORD_ARGUMENT = TextAttributesKey.createTextAttributesKey("PY.KEYWORD_ARGUMENT", PARAMETER);
public static final TextAttributesKey PY_FUNCTION_CALL = TextAttributesKey.createTextAttributesKey("PY.FUNCTION_CALL", FUNCTION_CALL);
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);
@@ -60,6 +60,7 @@ public class PythonColorsPage implements ColorSettingsPage, InspectionColorSetti
new AttributesDescriptor("Parameter", PyHighlighter.PY_PARAMETER),
new AttributesDescriptor("'self' parameter", PyHighlighter.PY_SELF_PARAMETER),
new AttributesDescriptor("Keyword argument", PyHighlighter.PY_KEYWORD_ARGUMENT),
new AttributesDescriptor("Function call", PyHighlighter.PY_FUNCTION_CALL),
new AttributesDescriptor("Valid escape sequence", PyHighlighter.PY_VALID_STRING_ESCAPE),
new AttributesDescriptor("Invalid escape sequence", PyHighlighter.PY_INVALID_STRING_ESCAPE),
};
@@ -76,6 +77,7 @@ public class PythonColorsPage implements ColorSettingsPage, InspectionColorSetti
.put("self", PyHighlighter.PY_SELF_PARAMETER)
.put("param", PyHighlighter.PY_PARAMETER)
.put("kwarg", PyHighlighter.PY_KEYWORD_ARGUMENT)
.put("call", PyHighlighter.PY_FUNCTION_CALL)
.build();
@NotNull
@@ -112,18 +114,18 @@ public class PythonColorsPage implements ColorSettingsPage, InspectionColorSetti
" <docComment>\"\"\" Syntax Highlighting Demo\n" +
" <docCommentTag>@param</docCommentTag> x Parameter\"\"\"</docComment>\n" +
" s = (\"Test\", 2+3, {'a': 'b'}, <param>x</param>) # Comment\n" +
" print s[0].lower()\n"+
" print s[0].<call>lower()</call>\n"+
"\n"+
"class <classDef>Foo</classDef>:\n"+
" def <predefined>__init__</predefined>(<self>self</self>):\n" +
" byte_string = 'newline:\\n also newline:\\x0a'\n" +
" text_string = u\"Cyrillic \u042f is \\u042f. Oops: \\u042g\"\n"+
" <self>self</self>.makeSense(<kwarg>whatever</kwarg>=1)\n" +
" <self>self</self>.<call>makeSense</call>(<kwarg>whatever</kwarg>=1)\n" +
" \n" +
" def <funcDef>makeSense</funcDef>(<self>self</self>, <param>whatever</param>):\n"+
" <self>self</self>.sense = <param>whatever</param>\n"+
"\n"+
"x = <builtin>len</builtin>('abc')\n"+
"x = <builtin><call>len</call></builtin>('abc')\n"+
"print(f.<predefinedUsage>__doc__</predefinedUsage>)"
;
}
@@ -22,6 +22,8 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.python.highlighting.PyHighlighter;
import com.jetbrains.python.psi.*;
import static com.jetbrains.python.psi.PyUtil.as;
/**
* @author yole
*/
@@ -59,4 +61,16 @@ public class HighlightingAnnotator extends PyAnnotator {
annotation.setTextAttributes(PyHighlighter.PY_KEYWORD_ARGUMENT);
}
}
@Override
public void visitPyCallExpression(PyCallExpression node) {
final PyReferenceExpression callee = as(node.getCallee(), PyReferenceExpression.class);
if (callee != null) {
final ASTNode functionName = callee.getNameElement();
if (functionName != null) {
final Annotation annotation = getHolder().createInfoAnnotation(functionName, null);
annotation.setTextAttributes(PyHighlighter.PY_FUNCTION_CALL);
}
}
}
}
+1 -1
View File
@@ -5,7 +5,7 @@
# predefined decl: green bold
# predefined usage: yellow bold
<info descr="null" type="INFORMATION" foreground="0x00ff00" background="0x000000" effectcolor="0xffffff" effecttype="BOXED" fonttype="1">len</info>("")
<info descr="null" type="INFORMATION" foreground="0x00ff00" background="0x000000" effectcolor="0xffffff" effecttype="BOXED" fonttype="1"><info>len</info></info>("")
len = [] # redefine
len # no highlight
@@ -0,0 +1,5 @@
<info descr="null">func</info>()
mod.Class.<info descr="null">method</info>()
<info descr="null">chained</info>().<info descr="null">calls</info>()
(foo or bar)()
<info descr="null"><info descr="null">len</info></info>(xs)
@@ -25,6 +25,7 @@ import com.jetbrains.python.documentation.docstrings.DocStringFormat;
import com.jetbrains.python.fixtures.PyTestCase;
import com.jetbrains.python.psi.LanguageLevel;
import com.jetbrains.python.psi.impl.PythonLanguageLevelPusher;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
@@ -37,10 +38,7 @@ public class PythonHighlightingTest extends PyTestCase {
private static final String TEST_PATH = "/highlighting/";
public void testBuiltins() {
EditorColorsManager manager = EditorColorsManager.getInstance();
EditorColorsScheme scheme = (EditorColorsScheme)manager.getGlobalScheme().clone();
manager.addColorsScheme(scheme);
EditorColorsManager.getInstance().setGlobalScheme(scheme);
EditorColorsScheme scheme = createTemporaryColorScheme();
TextAttributesKey xKey;
TextAttributes xAttributes;
@@ -57,10 +55,7 @@ public class PythonHighlightingTest extends PyTestCase {
}
public void testDeclarations() {
EditorColorsManager manager = EditorColorsManager.getInstance();
EditorColorsScheme scheme = (EditorColorsScheme)manager.getGlobalScheme().clone();
manager.addColorsScheme(scheme);
EditorColorsManager.getInstance().setGlobalScheme(scheme);
EditorColorsScheme scheme = createTemporaryColorScheme();
TextAttributesKey xKey = TextAttributesKey.find("PY.CLASS_DEFINITION");
TextAttributes xAttributes = new TextAttributes(Color.blue, Color.black, Color.white, EffectType.BOXED, Font.BOLD);
@@ -217,10 +212,7 @@ public class PythonHighlightingTest extends PyTestCase {
public void testYieldInNestedFunction() {
// highlight func declaration first, lest we get an "Extra fragment highlighted" error.
EditorColorsManager manager = EditorColorsManager.getInstance();
EditorColorsScheme scheme = (EditorColorsScheme)manager.getGlobalScheme().clone();
manager.addColorsScheme(scheme);
EditorColorsManager.getInstance().setGlobalScheme(scheme);
EditorColorsScheme scheme = createTemporaryColorScheme();
TextAttributesKey xKey = TextAttributesKey.find("PY.FUNC_DEFINITION");
TextAttributes xAttributes = new TextAttributes(Color.red, Color.black, Color.white, EffectType.BOXED, Font.BOLD);
@@ -288,10 +280,7 @@ public class PythonHighlightingTest extends PyTestCase {
// PY-19927
public void testMagicMethods() {
EditorColorsManager manager = EditorColorsManager.getInstance();
EditorColorsScheme scheme = (EditorColorsScheme)manager.getGlobalScheme().clone();
manager.addColorsScheme(scheme);
EditorColorsManager.getInstance().setGlobalScheme(scheme);
EditorColorsScheme scheme = createTemporaryColorScheme();
TextAttributesKey xKey = TextAttributesKey.find("PY.PREDEFINED_DEFINITION");
TextAttributes xAttributes = new TextAttributes(Color.green, Color.black, Color.white, EffectType.BOXED, Font.BOLD);
@@ -375,6 +364,25 @@ public class PythonHighlightingTest extends PyTestCase {
doTest(true, true);
}
// PY-11418
public void testFunctionCalls() {
final EditorColorsScheme scheme = createTemporaryColorScheme();
final TextAttributesKey callKey = TextAttributesKey.find("PY.FUNCTION_CALL");
scheme.setAttributes(callKey, new TextAttributes(Color.green, Color.black, Color.white, EffectType.BOXED, Font.BOLD));
final TextAttributesKey builtinKey = TextAttributesKey.find("PY.BUILTIN_NAME");
scheme.setAttributes(builtinKey, new TextAttributes(Color.blue, Color.black, Color.white, EffectType.BOXED, Font.BOLD));
doTest();
}
@NotNull
private static EditorColorsScheme createTemporaryColorScheme() {
EditorColorsManager manager = EditorColorsManager.getInstance();
EditorColorsScheme scheme = (EditorColorsScheme)manager.getGlobalScheme().clone();
manager.addColorsScheme(scheme);
EditorColorsManager.getInstance().setGlobalScheme(scheme);
return scheme;
}
// ---
private void doTest(final LanguageLevel languageLevel, final boolean checkWarnings, final boolean checkInfos) {
PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), languageLevel);