highlighting for Python keyword arguments (PY-5418)

This commit is contained in:
Dmitry Jemerov
2012-12-13 16:13:31 +01:00
parent 8acf08e43a
commit 4ebec8fb6e
5 changed files with 26 additions and 5 deletions
@@ -253,4 +253,8 @@ public class PyElementVisitor extends PsiElementVisitor {
public void visitPyConditionalExpression(PyConditionalExpression node) {
visitPyElement(node);
}
public void visitPyKeywordArgument(PyKeywordArgument node) {
visitPyElement(node);
}
}
@@ -116,6 +116,9 @@ public class PyHighlighter extends SyntaxHighlighterBase {
public static final TextAttributesKey PY_SELF_PARAMETER = TextAttributesKey.createTextAttributesKey(
"PY.SELF_PARAMETER", new TextAttributes(new Color(148, 85, 141), null, null, null, Font.PLAIN));
public static final TextAttributesKey PY_KEYWORD_ARGUMENT = TextAttributesKey.createTextAttributesKey(
"PY.KEYWORD_ARGUMENT", new TextAttributes(new Color(102, 0, 153), null, null, null, Font.PLAIN));
public static final TextAttributesKey PY_VALID_STRING_ESCAPE = _copy("PY.VALID_STRING_ESCAPE", VALID_STRING_ESCAPE);
public static final TextAttributesKey PY_INVALID_STRING_ESCAPE = _copy("PY.INVALID_STRING_ESCAPE", INVALID_STRING_ESCAPE);
@@ -44,6 +44,7 @@ public class PythonColorsPage implements ColorSettingsPage, InspectionColorSetti
new AttributesDescriptor("Predefined name", PyHighlighter.PY_PREDEFINED_USAGE),
new AttributesDescriptor("Parameter", PyHighlighter.PY_PARAMETER),
new AttributesDescriptor("'self' parameter", PyHighlighter.PY_SELF_PARAMETER),
new AttributesDescriptor("Keyword argument", PyHighlighter.PY_KEYWORD_ARGUMENT),
new AttributesDescriptor("Valid escape sequence", PyHighlighter.PY_VALID_STRING_ESCAPE),
new AttributesDescriptor("Invalid escape sequence", PyHighlighter.PY_INVALID_STRING_ESCAPE),
};
@@ -59,6 +60,7 @@ public class PythonColorsPage implements ColorSettingsPage, InspectionColorSetti
.put("builtin", PyHighlighter.PY_BUILTIN_NAME)
.put("self", PyHighlighter.PY_SELF_PARAMETER)
.put("param", PyHighlighter.PY_PARAMETER)
.put("kwarg", PyHighlighter.PY_KEYWORD_ARGUMENT)
.build();
@NotNull
@@ -99,9 +101,9 @@ public class PythonColorsPage implements ColorSettingsPage, InspectionColorSetti
"\n"+
"class <classDef>Foo</classDef>:\n"+
" def <predefined>__init__</predefined>(<self>self</self>):\n" +
" <self>self</self>.sense = None\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" +
" \n" +
" def <funcDef>makeSense</funcDef>(<self>self</self>, <param>whatever</param>):\n"+
" <self>self</self>.sense = <param>whatever</param>\n"+
@@ -6,10 +6,7 @@ import com.intellij.psi.PsiReference;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.psi.LanguageLevel;
import com.jetbrains.python.psi.PyElementGenerator;
import com.jetbrains.python.psi.PyExpression;
import com.jetbrains.python.psi.PyKeywordArgument;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.types.PyType;
import com.jetbrains.python.psi.types.TypeEvalContext;
import org.jetbrains.annotations.NonNls;
@@ -73,4 +70,9 @@ public class PyKeywordArgumentImpl extends PyElementImpl implements PyKeywordArg
getNode().replaceChild(getKeywordNode(), keywordArgument.getKeywordNode());
return this;
}
@Override
protected void acceptPyVisitor(PyElementVisitor pyVisitor) {
pyVisitor.visitPyKeywordArgument(this);
}
}
@@ -1,5 +1,6 @@
package com.jetbrains.python.validation;
import com.intellij.lang.ASTNode;
import com.intellij.lang.annotation.Annotation;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ArrayUtil;
@@ -36,6 +37,15 @@ public class HighlightingAnnotator extends PyAnnotator {
}
}
@Override
public void visitPyKeywordArgument(PyKeywordArgument node) {
ASTNode keywordNode = node.getKeywordNode();
if (keywordNode != null) {
Annotation annotation = getHolder().createInfoAnnotation(keywordNode, null);
annotation.setTextAttributes(PyHighlighter.PY_KEYWORD_ARGUMENT);
}
}
private static boolean isSelf(PyParameter node, PyFunction function) {
final PyNamedParameter named = node.getAsNamed();
if (named != null) {