PY-34617 Move getClassAttributes(), getOwnSlots() from PyAstClass

GitOrigin-RevId: 39ee4f79543a35fc696f9c754f5c5cf89b711f63
This commit is contained in:
Petr
2024-07-26 17:38:43 +02:00
committed by intellij-monorepo-bot
parent 5147d37e25
commit cfa28c0d2a
3 changed files with 39 additions and 65 deletions

View File

@@ -20,21 +20,14 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ArrayFactory;
import com.intellij.util.containers.ContainerUtil;
import com.jetbrains.python.PyElementTypes;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.ast.impl.PyUtilCore;
import com.jetbrains.python.ast.controlFlow.AstScopeOwner;
import com.jetbrains.python.ast.docstring.DocStringUtilCore;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Represents a class declaration in source.
*/
@@ -93,56 +86,6 @@ public interface PyAstClass extends PsiNameIdentifierOwner, PyAstCompoundStateme
*/
PyAstExpression @NotNull [] getSuperClassExpressions();
/**
* Effectively collects assignments inside the class body.
* <p/>
* This method does not access AST if underlying PSI is stub based.
* Note that only <strong>own</strong> attrs are fetched, not parent attrs.
* If you need parent attributes, consider using {@link #getClassAttributesInherited(TypeEvalContext)}
*
* @see #getClassAttributesInherited(TypeEvalContext)
*/
default List<? extends PyAstTargetExpression> getClassAttributes() {
List<PyAstTargetExpression> result = new ArrayList<>();
for (PsiElement psiElement : getStatementList().getChildren()) {
if (psiElement instanceof PyAstAssignmentStatement assignmentStatement) {
final PyAstExpression[] targets = assignmentStatement.getTargets();
for (PyAstExpression target : targets) {
if (target instanceof PyAstTargetExpression) {
result.add((PyAstTargetExpression)target);
}
}
}
else if (psiElement instanceof PyAstTypeDeclarationStatement) {
final PyAstExpression target = ((PyAstTypeDeclarationStatement)psiElement).getTarget();
if (target instanceof PyAstTargetExpression) {
result.add((PyAstTargetExpression)target);
}
}
}
return result;
}
/**
* Returns the list of names in the class' __slots__ attribute, or null if the class
* does not define such an attribute.
*
* @return the list of names or null.
*/
@Nullable
default List<String> getOwnSlots() {
final PyAstTargetExpression slots = ContainerUtil.find(getClassAttributes(), target -> PyNames.SLOTS.equals(target.getName()));
if (slots != null) {
final PyAstExpression value = slots.findAssignedValue();
return value instanceof PyAstStringLiteralExpression
? Collections.singletonList(((PyAstStringLiteralExpression)value).getStringValue())
: PyUtilCore.strListValue(value);
}
return null;
}
@Override
@Nullable
default String getDocStringValue() {