2 * Copyright 2000-2017 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.jetbrains.python.validation;
18 import com.intellij.lang.ASTNode;
19 import com.intellij.openapi.project.IndexNotReadyException;
20 import com.intellij.psi.PsiElement;
21 import com.jetbrains.python.PyNames;
22 import com.jetbrains.python.highlighting.PyHighlighter;
23 import com.jetbrains.python.psi.*;
24 import org.jetbrains.annotations.NotNull;
26 import java.util.Objects;
29 * Highlights class definitions, functrion definitions, and decorators.
32 public class PyDefinitionsAnnotator extends PyAnnotator {
35 public void visitPyClass(PyClass node) {
36 final ASTNode name_node = node.getNameNode();
37 if (name_node != null) {
38 addHighlightingAnnotation(name_node, PyHighlighter.PY_CLASS_DEFINITION);
43 public void visitPyFunction(PyFunction node) {
44 ASTNode name_node = node.getNameNode();
45 if (name_node != null) {
46 final String name = node.getName();
47 LanguageLevel languageLevel = LanguageLevel.forElement(node);
48 if (PyNames.UNDERSCORED_ATTRIBUTES.contains(name) || PyNames.getBuiltinMethods(languageLevel).containsKey(name)) {
49 PyClass cls = node.getContainingClass();
50 if (PyUtil.isNewMethod(node)) {
51 boolean new_style_class = false;
53 if (cls != null) new_style_class = cls.isNewStyleClass(null);
55 catch (IndexNotReadyException ignored) {
57 if (new_style_class) {
58 addHighlightingAnnotation(name_node, PyHighlighter.PY_PREDEFINED_DEFINITION);
62 addHighlightingAnnotation(name_node, PyHighlighter.PY_PREDEFINED_DEFINITION);
66 addHighlightingAnnotation(name_node, PyHighlighter.PY_FUNC_DEFINITION);
72 public void visitPyDecoratorList(PyDecoratorList node) {
73 PyDecorator[] decos = node.getDecorators();
74 for (PyDecorator deco : decos) {
75 highlightDecorator(deco);
79 private void highlightDecorator(@NotNull PyDecorator node) {
80 final PsiElement atSign = node.getFirstChild();
82 addHighlightingAnnotation(atSign, PyHighlighter.PY_DECORATOR);
83 if (node.getQualifiedName() != null) {
84 addHighlightingAnnotation(Objects.requireNonNull(node.getCallee()), PyHighlighter.PY_DECORATOR);