use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyDeclarationRangeHandler method getDeclarationRange.
@NotNull
@Override
public TextRange getDeclarationRange(@NotNull PsiElement container) {
int start = container.getTextRange().getStartOffset();
if (container instanceof PyFunction) {
PyParameterList parameterList = ((PyFunction) container).getParameterList();
return new TextRange(start, parameterList.getTextRange().getEndOffset());
}
if (container instanceof PyClass) {
PyArgumentList argumentList = ((PyClass) container).getSuperClassExpressionList();
if (argumentList != null) {
return new TextRange(start, argumentList.getTextRange().getEndOffset());
}
ASTNode nameNode = ((PyClass) container).getNameNode();
if (nameNode != null) {
return new TextRange(start, nameNode.getStartOffset() + nameNode.getTextLength());
}
}
return container.getTextRange();
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyLineMarkerProvider method collectSlowLineMarkers.
public void collectSlowLineMarkers(@NotNull final List<PsiElement> elements, @NotNull final Collection<LineMarkerInfo> result) {
Set<PyFunction> functions = new HashSet<PyFunction>();
for (PsiElement element : elements) {
if (element instanceof PyClass) {
collectInheritingClasses((PyClass) element, result);
} else if (element instanceof PyFunction) {
functions.add((PyFunction) element);
}
}
collectOverridingMethods(functions, result);
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyLineMarkerProvider method collectOverridingMethods.
private static void collectOverridingMethods(final Set<PyFunction> functions, final Collection<LineMarkerInfo> result) {
Set<PyClass> classes = new HashSet<PyClass>();
final MultiMap<PyClass, PyFunction> candidates = new MultiMap<PyClass, PyFunction>();
for (PyFunction function : functions) {
PyClass pyClass = function.getContainingClass();
if (pyClass != null && function.getName() != null) {
classes.add(pyClass);
candidates.putValue(pyClass, function);
}
}
final Set<PyFunction> overridden = new HashSet<PyFunction>();
for (final PyClass pyClass : classes) {
PyClassInheritorsSearch.search(pyClass, true).forEach(inheritor -> {
for (Iterator<PyFunction> it = candidates.get(pyClass).iterator(); it.hasNext(); ) {
PyFunction func = it.next();
if (inheritor.findMethodByName(func.getName(), false, null) != null) {
overridden.add(func);
it.remove();
}
}
return !candidates.isEmpty();
});
if (candidates.isEmpty())
break;
}
for (PyFunction func : overridden) {
result.add(new LineMarkerInfo<PyFunction>(func, func.getTextOffset(), AllIcons.Gutter.OverridenMethod, Pass.LINE_MARKERS, ourOverridingMethodTooltipProvider, ourOverridingMethodNavigator));
}
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyMethodNameTypedHandler method beforeCharTyped.
@Override
public Result beforeCharTyped(char character, Project project, Editor editor, PsiFile file, FileType fileType) {
// else we'd mess up with other file types!
if (DumbService.isDumb(project) || !(fileType instanceof PythonFileType))
return Result.CONTINUE;
if (character == '(') {
if (!PyCodeInsightSettings.getInstance().INSERT_SELF_FOR_METHODS) {
return Result.CONTINUE;
}
final Document document = editor.getDocument();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
final int offset = editor.getCaretModel().getOffset();
PsiElement token = file.findElementAt(offset - 1);
// sanity check: beyond EOL
if (token == null)
return Result.CONTINUE;
final ASTNode token_node = token.getNode();
if (token_node != null && token_node.getElementType() == PyTokenTypes.IDENTIFIER) {
PsiElement maybe_def = PyPsiUtils.getPrevNonCommentSibling(token.getPrevSibling(), false);
if (maybe_def != null) {
ASTNode def_node = maybe_def.getNode();
if (def_node != null && def_node.getElementType() == PyTokenTypes.DEF_KEYWORD) {
PsiElement maybe_func = token.getParent();
if (maybe_func instanceof PyFunction) {
PyFunction func = (PyFunction) maybe_func;
PyUtil.MethodFlags flags = PyUtil.MethodFlags.of(func);
if (flags != null) {
// we're in a method
// TODO: all string constants go to Settings
String pname = flags.isClassMethod() || flags.isMetaclassMethod() ? "cls" : "self";
final boolean is_new = PyNames.NEW.equals(func.getName());
if (flags.isMetaclassMethod() && is_new) {
pname = "typ";
} else if (flags.isClassMethod() || is_new) {
pname = "cls";
} else if (flags.isStaticMethod())
pname = "";
documentManager.commitDocument(document);
// TODO: only print the ")" if Settings require it
int caretOffset = editor.getCaretModel().getOffset();
String textToType = "(" + pname + ")";
CharSequence chars = editor.getDocument().getCharsSequence();
if (caretOffset == chars.length() || chars.charAt(caretOffset) != ':') {
textToType += ':';
}
// right after param name
EditorModificationUtil.insertStringAtCaret(editor, textToType, true, 1 + pname.length());
return Result.STOP;
}
}
}
}
}
}
// the default
return Result.CONTINUE;
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PythonPsiManager method isOutOfCodeBlock.
protected boolean isOutOfCodeBlock(@NotNull PsiElement element) {
while (true) {
if (element instanceof PyFile) {
return true;
}
if (element instanceof PsiFile || element instanceof PsiDirectory || element == null) {
return false;
}
PsiElement pparent = element.getParent();
if (pparent instanceof PyFunction) {
PyFunction pyFunction = (PyFunction) pparent;
return element == pyFunction.getParameterList() || element == pyFunction.getNameIdentifier();
}
element = pparent;
}
}
Aggregations