KtDeclaration (@Nullable PsiElement element) { if (element == null) return null; if (getParentFileAnnotationEntry(element) != null) return null; KtDeclaration declaration = null; if (element.getNode().getElementType() == KtTokens.LBRACE) { KtLambdaExpression lambda = PsiTreeUtil.getParentOfType(element, KtLambdaExpression.class, true); KtFunction function = PsiTreeUtil.getParentOfType(lambda, KtFunction.class, true); if (function != null && function.getBodyExpression() == lambda) { declaration = function; } } if (declaration == null) { declaration = PsiTreeUtil.getParentOfType(element, KtDeclaration.class, false); } if (declaration instanceof KtParameter) return null; if (declaration instanceof KtTypeParameter) { return getMovableDeclaration(declaration.getParent()); } return PsiTreeUtil.instanceOf(PsiTreeUtil.getParentOfType(declaration, DECLARATION_CONTAINER_CLASSES), CLASSBODYLIKE_DECLARATION_CONTAINER_CLASSES) ? declaration : null; }