PY-34617 Move getTopLevelAttributes(), findTopLevelAttribute() from PyAstFile

GitOrigin-RevId: 226a7e968851ab0c8730bb79ca90d0ed5dcec364
This commit is contained in:
Petr
2024-07-26 19:09:29 +02:00
committed by intellij-monorepo-bot
parent cfa28c0d2a
commit 16a7fb4b3e
4 changed files with 47 additions and 24 deletions

View File

@@ -17,7 +17,6 @@ package com.jetbrains.python.ast;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiNamedElement;
import com.jetbrains.python.ast.controlFlow.AstScopeOwner;
import com.jetbrains.python.ast.docstring.DocStringUtilCore;
import com.jetbrains.python.psi.FutureFeature;
@@ -41,13 +40,6 @@ public interface PyAstFile extends PyAstElement, PsiFile, PyAstDocStringOwner, A
return stmts;
}
List<? extends PyAstTargetExpression> getTopLevelAttributes();
@Nullable
default PyAstTargetExpression findTopLevelAttribute(@NotNull String name) {
return findByName(name, getTopLevelAttributes());
}
LanguageLevel getLanguageLevel();
/**
@@ -55,15 +47,6 @@ public interface PyAstFile extends PyAstElement, PsiFile, PyAstDocStringOwner, A
*/
boolean hasImportFromFuture(FutureFeature feature);
private static <T extends PsiNamedElement> T findByName(@NotNull String name, @NotNull List<T> namedElements) {
for (T namedElement : namedElements) {
if (name.equals(namedElement.getName())) {
return namedElement;
}
}
return null;
}
@ApiStatus.Internal
default boolean isAcceptedFor(@NotNull Class<?> visitorClass) {
return true;

View File

@@ -39,7 +39,6 @@ public interface PyFile extends PyAstFile, PyElement, PsiFile, PyDocStringOwner,
@NotNull
List<PyFunction> getTopLevelFunctions();
@Override
List<PyTargetExpression> getTopLevelAttributes();
@Nullable
@@ -48,11 +47,8 @@ public interface PyFile extends PyAstFile, PyElement, PsiFile, PyDocStringOwner,
@Nullable
PyClass findTopLevelClass(@NonNls @NotNull String name);
@Override
@Nullable
default PyTargetExpression findTopLevelAttribute(@NotNull String name) {
return (PyTargetExpression)PyAstFile.super.findTopLevelAttribute(name);
}
PyTargetExpression findTopLevelAttribute(@NotNull String name);
@NotNull
List<PyTypeAliasStatement> getTypeAliasStatements();

View File

@@ -221,6 +221,11 @@ public class PyFileImpl extends PsiFileBase implements PyFile, PyExpression {
return findByName(name, getTopLevelClasses());
}
@Override
public @Nullable PyTargetExpression findTopLevelAttribute(@NotNull String name) {
return findByName(name, getTopLevelAttributes());
}
@Override
public @NotNull List<PyTypeAliasStatement> getTypeAliasStatements() {
return PyPsiUtils.collectStubChildren(this, getGreenStub(), PyTypeAliasStatement.class);

View File

@@ -6,10 +6,15 @@ import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.source.PsiFileImpl;
import com.intellij.psi.stubs.StubElement;
import com.intellij.util.xmlb.annotations.OptionTag;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.ast.PyAstClass;
import com.jetbrains.python.ast.PyAstFile;
import com.jetbrains.python.ast.PyAstFunction;
import com.jetbrains.python.ast.PyAstTargetExpression;
import com.jetbrains.python.ast.impl.PyPsiUtilsCore;
import com.jetbrains.python.defaultProjectAwareService.PyDefaultProjectAwareService;
@@ -65,12 +70,12 @@ public abstract class PyDocumentationSettings
@Nullable
public static DocStringFormat getFormatFromDocformatAttribute(@NotNull PsiFile file) {
if (file instanceof PyAstFile) {
final PyAstTargetExpression expr = ((PyAstFile)file).findTopLevelAttribute(PyNames.DOCFORMAT);
final PyAstTargetExpression expr = getDocFormatAttribute(file);
if (expr != null) {
final String docformat = PyPsiUtilsCore.strValue(expr.findAssignedValue());
if (docformat != null) {
final List<String> words = StringUtil.split(docformat, " ");
if (words.size() > 0) {
if (!words.isEmpty()) {
final DocStringFormat fileFormat = DocStringFormat.fromName(words.get(0));
if (fileFormat != null) {
return fileFormat;
@@ -82,6 +87,40 @@ public abstract class PyDocumentationSettings
return null;
}
@Nullable
private static PyAstTargetExpression getDocFormatAttribute(@NotNull PsiFile file) {
StubElement<?> stub = ((PsiFileImpl)file).getGreenStub();
if (stub != null) {
return getDocFormatAttribute(stub.getChildrenStubs());
}
else {
return getDocFormatAttribute(file.getChildren());
}
}
@Nullable
private static PyAstTargetExpression getDocFormatAttribute(@NotNull List<StubElement<?>> stubs) {
for (StubElement<?> stub : stubs) {
if (stub.getPsi() instanceof PyAstTargetExpression targetExpression && PyNames.DOCFORMAT.equals(targetExpression.getName())) {
return targetExpression;
}
}
return null;
}
@Nullable
private static PyAstTargetExpression getDocFormatAttribute(@NotNull PsiElement @NotNull [] elements) {
for (PsiElement element : elements) {
if (element instanceof PyAstClass || element instanceof PyAstFunction) continue;
if (element instanceof PyAstTargetExpression targetExpression && PyNames.DOCFORMAT.equals(targetExpression.getName())) {
return targetExpression;
}
PyAstTargetExpression targetExpression = getDocFormatAttribute(element.getChildren());
if (targetExpression != null) return targetExpression;
}
return null;
}
@NotNull
public final DocStringFormat getFormat() {
return getState().getFormat();