mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Guess Python language level from SDKs of opened projects if it's not available for PSI elements
This commit is contained in:
@@ -129,7 +129,7 @@ public class PyUserSkeletonsUtil {
|
||||
final PsiDirectory psiDirectory = PsiManager.getInstance(project).findDirectory(directory);
|
||||
PsiElement fileSkeleton = new QualifiedNameResolverImpl(qName).resolveModuleAt(psiDirectory);
|
||||
if (fileSkeleton instanceof PsiDirectory) {
|
||||
fileSkeleton = PyUtil.getPackageElement((PsiDirectory)fileSkeleton);
|
||||
fileSkeleton = PyUtil.getPackageElement((PsiDirectory)fileSkeleton, foothold);
|
||||
}
|
||||
if (fileSkeleton instanceof PyFile) {
|
||||
cache.put(cacheQName, Collections.singletonList(fileSkeleton));
|
||||
|
||||
@@ -21,11 +21,11 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.util.containers.FactoryMap;
|
||||
import com.jetbrains.python.console.parsing.PyConsoleHighlightingLexer;
|
||||
import com.jetbrains.python.console.PydevConsoleRunner;
|
||||
import com.jetbrains.python.lexer.PythonHighlightingLexer;
|
||||
import com.jetbrains.python.psi.LanguageLevel;
|
||||
import com.jetbrains.python.psi.PyUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
@@ -52,11 +52,10 @@ public class PySyntaxHighlighterFactory extends SyntaxHighlighterFactory {
|
||||
};
|
||||
|
||||
@NotNull
|
||||
public SyntaxHighlighter getSyntaxHighlighter(final Project project, final VirtualFile virtualFile) {
|
||||
LanguageLevel languageLevel = virtualFile != null ? PyUtil.getLanguageLevelForVirtualFile(virtualFile) : LanguageLevel.getDefault();
|
||||
if (virtualFile != null && PydevConsoleRunner.isInPydevConsole(virtualFile)) {
|
||||
return myConsoleMap.get(languageLevel);
|
||||
}
|
||||
return myMap.get(languageLevel);
|
||||
public SyntaxHighlighter getSyntaxHighlighter(@Nullable final Project project, @Nullable final VirtualFile virtualFile) {
|
||||
final LanguageLevel level = project != null && virtualFile != null ?
|
||||
PyUtil.getLanguageLevelForVirtualFile(project, virtualFile) :
|
||||
LanguageLevel.getDefault();
|
||||
return myMap.get(level);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,8 +30,10 @@ import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.extensions.Extensions;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.module.ModuleManager;
|
||||
import com.intellij.openapi.module.ModuleUtilCore;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import com.intellij.openapi.roots.ModuleRootManager;
|
||||
import com.intellij.openapi.ui.MessageType;
|
||||
import com.intellij.openapi.ui.popup.Balloon;
|
||||
@@ -68,6 +70,7 @@ import com.jetbrains.python.psi.types.*;
|
||||
import com.jetbrains.python.refactoring.classes.PyDependenciesComparator;
|
||||
import com.jetbrains.python.refactoring.classes.extractSuperclass.PyExtractSuperclassHelper;
|
||||
import com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo;
|
||||
import com.jetbrains.python.sdk.PythonSdkType;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -741,7 +744,8 @@ public class PyUtil {
|
||||
* @see {@link LanguageLevel#forElement}
|
||||
*/
|
||||
@NotNull
|
||||
public static LanguageLevel getLanguageLevelForVirtualFile(@NotNull VirtualFile virtualFile) {
|
||||
public static LanguageLevel getLanguageLevelForVirtualFile(@NotNull Project project,
|
||||
@NotNull VirtualFile virtualFile) {
|
||||
if (virtualFile instanceof VirtualFileWindow)
|
||||
virtualFile = ((VirtualFileWindow)virtualFile).getDelegate();
|
||||
|
||||
@@ -764,7 +768,19 @@ public class PyUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
return guessLanguageLevel(project);
|
||||
}
|
||||
|
||||
private static LanguageLevel guessLanguageLevel(@NotNull Project project) {
|
||||
final ModuleManager moduleManager = ModuleManager.getInstance(project);
|
||||
if (moduleManager != null) {
|
||||
for (Module projectModule : moduleManager.getModules()) {
|
||||
final Sdk sdk = PythonSdkType.findPythonSdk(projectModule);
|
||||
if (sdk != null) {
|
||||
return PythonSdkType.getLanguageLevelForSdk(sdk);
|
||||
}
|
||||
}
|
||||
}
|
||||
return LanguageLevel.getDefault();
|
||||
}
|
||||
|
||||
@@ -828,11 +844,14 @@ public class PyUtil {
|
||||
return target;
|
||||
}
|
||||
|
||||
public static boolean isPackage(@NotNull PsiDirectory directory) {
|
||||
public static boolean isPackage(@NotNull PsiDirectory directory, @Nullable PsiElement anchor) {
|
||||
if (turnDirIntoInit(directory) != null) {
|
||||
return true;
|
||||
}
|
||||
if (getLanguageLevelForVirtualFile(directory.getVirtualFile()).isAtLeast(LanguageLevel.PYTHON33)) {
|
||||
final LanguageLevel level = anchor != null ?
|
||||
LanguageLevel.forElement(anchor) :
|
||||
getLanguageLevelForVirtualFile(directory.getProject(), directory.getVirtualFile());
|
||||
if (level.isAtLeast(LanguageLevel.PYTHON33)) {
|
||||
return true;
|
||||
}
|
||||
return hasNamespacePackageFile(directory);
|
||||
@@ -843,8 +862,8 @@ public class PyUtil {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PsiElement getPackageElement(@NotNull PsiDirectory directory) {
|
||||
if (isPackage(directory)) {
|
||||
public static PsiElement getPackageElement(@NotNull PsiDirectory directory, @Nullable PsiElement anchor) {
|
||||
if (isPackage(directory, anchor)) {
|
||||
final PsiElement init = turnDirIntoInit(directory);
|
||||
if (init != null) {
|
||||
return init;
|
||||
|
||||
@@ -294,7 +294,7 @@ public class PyFileImpl extends PsiFileBase implements PyFile, PyExpression {
|
||||
if (virtualFile == null) {
|
||||
virtualFile = getViewProvider().getVirtualFile();
|
||||
}
|
||||
return PyUtil.getLanguageLevelForVirtualFile(virtualFile);
|
||||
return PyUtil.getLanguageLevelForVirtualFile(getProject(), virtualFile);
|
||||
}
|
||||
|
||||
public Icon getIcon(int flags) {
|
||||
|
||||
@@ -143,7 +143,7 @@ public class PyImportedModule extends LightElement implements NameDefiner {
|
||||
element = ResolveImportUtil.resolveModuleInRoots(getImportedPrefix(), getContainingFile());
|
||||
}
|
||||
if (element instanceof PsiDirectory) {
|
||||
return PyUtil.getPackageElement((PsiDirectory)element);
|
||||
return PyUtil.getPackageElement((PsiDirectory)element, this);
|
||||
}
|
||||
return element;
|
||||
}
|
||||
|
||||
@@ -427,7 +427,7 @@ public class PyReferenceImpl implements PsiReferenceEx, PsiPolyVariantReference
|
||||
}
|
||||
else if (resolveResult instanceof PsiDirectory) {
|
||||
final PsiDirectory directory = (PsiDirectory)resolveResult;
|
||||
if (PyUtil.isPackage(directory) && directory == element) {
|
||||
if (PyUtil.isPackage(directory, null) && directory == element) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@ public class ResolveImportUtil {
|
||||
throw new PsiInvalidElementAccessException(candidate, "Got an invalid candidate from resolveImportSourceCandidates(): " + candidate.getClass());
|
||||
}
|
||||
if (candidate instanceof PsiDirectory) {
|
||||
candidate = PyUtil.getPackageElement((PsiDirectory)candidate);
|
||||
candidate = PyUtil.getPackageElement((PsiDirectory)candidate, importStatement);
|
||||
}
|
||||
PsiElement result = resolveChild(candidate, name, file, false, true);
|
||||
if (result != null) {
|
||||
@@ -326,7 +326,7 @@ public class ResolveImportUtil {
|
||||
if (referencedName == null) return null;
|
||||
|
||||
final PsiDirectory subdir = dir.findSubdirectory(referencedName);
|
||||
if (subdir != null && (!checkForPackage || PyUtil.isPackage(subdir))) {
|
||||
if (subdir != null && (!checkForPackage || PyUtil.isPackage(subdir, containingFile))) {
|
||||
return subdir;
|
||||
}
|
||||
|
||||
@@ -358,7 +358,7 @@ public class ResolveImportUtil {
|
||||
ResolveResultList ret = new ResolveResultList();
|
||||
for (PsiElement target : targets) {
|
||||
if (target instanceof PsiDirectory) {
|
||||
target = PyUtil.getPackageElement((PsiDirectory)target);
|
||||
target = PyUtil.getPackageElement((PsiDirectory)target, null);
|
||||
}
|
||||
if (target != null) { // Ignore non-package dirs, worthless
|
||||
int rate = RatedResolveResult.RATE_HIGH;
|
||||
|
||||
@@ -450,7 +450,7 @@ public class PyTypeParser {
|
||||
break;
|
||||
}
|
||||
if (module instanceof PsiDirectory) {
|
||||
module = PyUtil.getPackageElement((PsiDirectory)module);
|
||||
module = PyUtil.getPackageElement((PsiDirectory)module, myAnchor);
|
||||
}
|
||||
if (module instanceof PyTypedElement) {
|
||||
final PyType moduleType = context.getType((PyTypedElement)module);
|
||||
|
||||
Reference in New Issue
Block a user