All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jetbrains.python.codeInsight.PyLineMarkerProvider Maven / Gradle / Ivy

Go to download

A packaging of the IntelliJ Community Edition python-community library. This is release number 1 of trunk branch 142.

The newest version!
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jetbrains.python.codeInsight;

import com.intellij.codeHighlighting.Pass;
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzerSettings;
import com.intellij.codeInsight.daemon.LineMarkerInfo;
import com.intellij.codeInsight.daemon.LineMarkerProvider;
import com.intellij.icons.AllIcons;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.CollectionQuery;
import com.intellij.util.Function;
import com.intellij.util.Processor;
import com.intellij.util.Query;
import com.intellij.util.containers.HashSet;
import com.intellij.util.containers.MultiMap;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.psi.PyClass;
import com.jetbrains.python.psi.PyFunction;
import com.jetbrains.python.psi.PyTargetExpression;
import com.jetbrains.python.psi.PyUtil;
import com.jetbrains.python.psi.search.PyClassInheritorsSearch;
import com.jetbrains.python.psi.search.PyOverridingMethodsSearch;
import com.jetbrains.python.psi.search.PySuperMethodsSearch;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author yole
 */
public class PyLineMarkerProvider implements LineMarkerProvider, PyLineSeparatorUtil.Provider {

  private static class TooltipProvider implements Function {
    private final String myText;

    private TooltipProvider(String text) {
      myText = text;
    }

    public String fun(PsiElement psiElement) {
      return myText;
    }
  }

  private static final Function ourSubclassTooltipProvider = new Function() {
    public String fun(PyClass pyClass) {
      final StringBuilder builder = new StringBuilder("Is subclassed by:");
      final AtomicInteger count = new AtomicInteger();
      PyClassInheritorsSearch.search(pyClass, true).forEach(new Processor() {
        public boolean process(PyClass pyClass) {
          if (count.incrementAndGet() >= 10) {
            builder.setLength(0);
            builder.append("Has subclasses");
            return false;
          }
          builder.append("
  ").append(pyClass.getName()); return true; } }); return builder.toString(); } }; private static final Function ourOverridingMethodTooltipProvider = new Function() { public String fun(final PyFunction pyFunction) { final StringBuilder builder = new StringBuilder("Is overridden in:"); final AtomicInteger count = new AtomicInteger(); PyClassInheritorsSearch.search(pyFunction.getContainingClass(), true).forEach(new Processor() { public boolean process(PyClass pyClass) { if (count.incrementAndGet() >= 10) { builder.setLength(0); builder.append("Has overridden methods"); return false; } if (pyClass.findMethodByName(pyFunction.getName(), false) != null) { builder.append("
  ").append(pyClass.getName()); } return true; } }); return builder.toString(); } }; private static final PyLineMarkerNavigator ourSuperMethodNavigator = new PyLineMarkerNavigator() { protected String getTitle(final PsiElement elt) { return "Choose Super Method of " + ((PyFunction)elt.getParent()).getName(); } @Nullable protected Query search(final PsiElement elt) { if (!(elt.getParent() instanceof PyFunction)) return null; return PySuperMethodsSearch.search((PyFunction)elt.getParent()); } }; private static final PyLineMarkerNavigator ourSuperAttributeNavigator = new PyLineMarkerNavigator() { protected String getTitle(final PsiElement elt) { return "Choose Super Attribute of " + ((PyTargetExpression)elt).getName(); } @Nullable protected Query search(final PsiElement elt) { List result = new ArrayList(); PyClass containingClass = PsiTreeUtil.getParentOfType(elt, PyClass.class); if (containingClass != null && elt instanceof PyTargetExpression) { for (PyClass ancestor : containingClass.getAncestorClasses()) { final PyTargetExpression attribute = ancestor.findClassAttribute(((PyTargetExpression)elt).getReferencedName(), false); if (attribute != null) { result.add(attribute); } } } return new CollectionQuery(result); } }; private static final PyLineMarkerNavigator ourSubclassNavigator = new PyLineMarkerNavigator() { protected String getTitle(final PyClass elt) { return "Choose Subclass of "+ elt.getName(); } protected Query search(final PyClass elt) { return PyClassInheritorsSearch.search(elt, true); } }; private static final PyLineMarkerNavigator ourOverridingMethodNavigator = new PyLineMarkerNavigator() { protected String getTitle(final PyFunction elt) { return "Choose Overriding Method of " + elt.getName(); } protected Query search(final PyFunction elt) { return PyOverridingMethodsSearch.search(elt, true); } }; public LineMarkerInfo getLineMarkerInfo(@NotNull final PsiElement element) { final ASTNode node = element.getNode(); if (node != null && node.getElementType() == PyTokenTypes.IDENTIFIER && element.getParent() instanceof PyFunction) { final PyFunction function = (PyFunction)element.getParent(); return getMethodMarker(element, function); } if (element instanceof PyTargetExpression && PyUtil.isClassAttribute(element)) { return getAttributeMarker((PyTargetExpression) element); } if (DaemonCodeAnalyzerSettings.getInstance().SHOW_METHOD_SEPARATORS && isSeparatorAllowed(element)) { return PyLineSeparatorUtil.addLineSeparatorIfNeeded(this, element); } return null; } public boolean isSeparatorAllowed(PsiElement element) { return element instanceof PyFunction || element instanceof PyClass; } @Nullable private static LineMarkerInfo getMethodMarker(final PsiElement element, final PyFunction function) { if (PyNames.INIT.equals(function.getName())) { return null; } final PsiElement superMethod = PySuperMethodsSearch.search(function).findFirst(); if (superMethod != null) { PyClass superClass = null; if (superMethod instanceof PyFunction) { superClass = ((PyFunction)superMethod).getContainingClass(); } // TODO: show "implementing" instead of "overriding" icon for Python implementations of Java interface methods return new LineMarkerInfo(element, element.getTextRange().getStartOffset(), AllIcons.Gutter.OverridingMethod, Pass.UPDATE_ALL, superClass == null ? null : new TooltipProvider("Overrides method in " + superClass.getName()), ourSuperMethodNavigator); } return null; } @Nullable private static LineMarkerInfo getAttributeMarker(PyTargetExpression element) { final String name = element.getReferencedName(); if (name == null) { return null; } PyClass containingClass = PsiTreeUtil.getParentOfType(element, PyClass.class); if (containingClass == null) return null; for (PyClass ancestor : containingClass.getAncestorClasses()) { final PyTargetExpression ancestorAttr = ancestor.findClassAttribute(name, false); if (ancestorAttr != null) { return new LineMarkerInfo(element, element.getTextRange().getStartOffset(), AllIcons.Gutter.OverridingMethod, Pass.UPDATE_ALL, new TooltipProvider("Overrides attribute in " + ancestor.getName()), ourSuperAttributeNavigator); } } return null; } public void collectSlowLineMarkers(@NotNull final List elements, @NotNull final Collection result) { Set functions = new HashSet(); for(PsiElement element: elements) { if (element instanceof PyClass) { collectInheritingClasses((PyClass) element, result); } else if (element instanceof PyFunction) { functions.add((PyFunction)element); } } collectOverridingMethods(functions, result); } private static void collectInheritingClasses(final PyClass element, final Collection result) { if (PyClassInheritorsSearch.search(element, false).findFirst() != null) { result.add(new LineMarkerInfo(element, element.getTextOffset(), AllIcons.Gutter.OverridenMethod, Pass.UPDATE_OVERRIDEN_MARKERS, ourSubclassTooltipProvider, ourSubclassNavigator)); } } private static void collectOverridingMethods(final Set functions, final Collection result) { Set classes = new HashSet(); final MultiMap candidates = new MultiMap(); for(PyFunction function: functions) { PyClass pyClass = function.getContainingClass(); if (pyClass != null && function.getName() != null) { classes.add(pyClass); candidates.putValue(pyClass, function); } } final Set overridden = new HashSet(); for(final PyClass pyClass: classes) { PyClassInheritorsSearch.search(pyClass, true).forEach(new Processor() { public boolean process(final PyClass inheritor) { for (Iterator it = candidates.get(pyClass).iterator(); it.hasNext();) { PyFunction func = it.next(); if (inheritor.findMethodByName(func.getName(), false) != null) { overridden.add(func); it.remove(); } } return !candidates.isEmpty(); } }); if (candidates.isEmpty()) break; } for(PyFunction func: overridden) { result.add(new LineMarkerInfo(func, func.getTextOffset(), AllIcons.Gutter.OverridenMethod, Pass.UPDATE_OVERRIDEN_MARKERS, ourOverridingMethodTooltipProvider, ourOverridingMethodNavigator)); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy