public DeclarationLocation getSuperImplementations(PythonParserResult info, int lexOffset) { // Figure out if we're on a method, and if so, locate the nearest // method it is overriding. // Otherwise, if we're on a class (anywhere, not just definition), // go to the super class. PythonTree root = PythonAstUtils.getRoot(info); if (root != null) { // Determine function or call under caret int astOffset = PythonAstUtils.getAstOffset(info, lexOffset); if (astOffset != -1) { AstPath path = AstPath.get(root, astOffset); PythonTree leaf = path.leaf(); String name = null; boolean findClass = false; // false=function, true=class if (leaf instanceof FunctionDef) { name = ((FunctionDef) leaf).getInternalName(); } else if (leaf instanceof Name) { name = ((Name) leaf).getInternalId(); if (path.leafParent() instanceof ClassDef) { findClass = true; } } else if (leaf instanceof ClassDef) { name = ((ClassDef) leaf).getInternalName(); findClass = true; } Set<IndexedElement> elements = null; PythonIndex index = PythonIndex.get(info.getSnapshot().getSource().getFileObject()); if (findClass) { elements = index.getSuperClasses(name); } else { ClassDef clz = PythonAstUtils.getClassDef(path); if (clz != null) { elements = index.getOverridingMethods(clz.getInternalName(), name); } } if (elements != null && elements.size() > 0) { // Pick the closest element as the default candidate IndexedElement candidate = null; int depth = Integer.MAX_VALUE; for (IndexedElement element : elements) { if (element.getOrder() < depth) { candidate = element; depth = element.getOrder(); } } return getDeclarationLocation(info, candidate, elements); } } } return DeclarationLocation.NONE; }
PythonAltLocation(IndexedElement element, boolean isPreferred) { this.element = element; this.isPreferred = isPreferred; order = element.getOrder(); }