mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Guess Python language level for scratch files (PY-15898)
The language level pusher was unable to find the correct language level because there is no SDK associated with scratch files. Now we use the same guessing mechanism we've been using for PyUtil.getLanguageLevelForFile().
This commit is contained in:
@@ -56,6 +56,7 @@ public enum LanguageLevel {
|
||||
|
||||
public static LanguageLevel FORCE_LANGUAGE_LEVEL = null;
|
||||
|
||||
@NotNull
|
||||
public static LanguageLevel getDefault() {
|
||||
return DEFAULT2;
|
||||
}
|
||||
|
||||
@@ -828,7 +828,8 @@ public class PyUtil {
|
||||
return guessLanguageLevel(project);
|
||||
}
|
||||
|
||||
private static LanguageLevel guessLanguageLevel(@NotNull Project project) {
|
||||
@NotNull
|
||||
public static LanguageLevel guessLanguageLevel(@NotNull Project project) {
|
||||
final ModuleManager moduleManager = ModuleManager.getInstance(project);
|
||||
if (moduleManager != null) {
|
||||
for (Module projectModule : moduleManager.getModules()) {
|
||||
|
||||
@@ -49,6 +49,7 @@ import com.jetbrains.python.PythonFileType;
|
||||
import com.jetbrains.python.PythonModuleTypeBase;
|
||||
import com.jetbrains.python.facet.PythonFacetSettings;
|
||||
import com.jetbrains.python.psi.LanguageLevel;
|
||||
import com.jetbrains.python.psi.PyUtil;
|
||||
import com.jetbrains.python.psi.resolve.PythonSdkPathCache;
|
||||
import com.jetbrains.python.sdk.PythonSdkType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -108,7 +109,10 @@ public class PythonLanguageLevelPusher implements FilePropertyPusher<LanguageLev
|
||||
}
|
||||
if (file == null) return null;
|
||||
final Sdk sdk = getFileSdk(project, file);
|
||||
return PythonSdkType.getLanguageLevelForSdk(sdk);
|
||||
if (sdk != null) {
|
||||
return PythonSdkType.getLanguageLevelForSdk(sdk);
|
||||
}
|
||||
return PyUtil.guessLanguageLevel(project);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
@@ -845,6 +845,7 @@ public class PythonSdkType extends SdkType {
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static LanguageLevel getLanguageLevelForSdk(@Nullable Sdk sdk) {
|
||||
if (sdk != null && sdk.getSdkType() instanceof PythonSdkType) {
|
||||
final PythonSdkFlavor flavor = PythonSdkFlavor.getFlavor(sdk);
|
||||
|
||||
@@ -77,8 +77,9 @@ public class PyPySdkFlavor extends PythonSdkFlavor {
|
||||
return "PyPy";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public LanguageLevel getLanguageLevel(Sdk sdk) {
|
||||
public LanguageLevel getLanguageLevel(@NotNull Sdk sdk) {
|
||||
final String versionString = sdk.getVersionString();
|
||||
if (versionString != null) {
|
||||
final Matcher matcher = VERSION_STRING_RE.matcher(versionString);
|
||||
|
||||
@@ -218,7 +218,8 @@ public abstract class PythonSdkFlavor {
|
||||
@NotNull
|
||||
public abstract String getName();
|
||||
|
||||
public LanguageLevel getLanguageLevel(Sdk sdk) {
|
||||
@NotNull
|
||||
public LanguageLevel getLanguageLevel(@NotNull Sdk sdk) {
|
||||
final String version = sdk.getVersionString();
|
||||
final String prefix = getName() + " ";
|
||||
if (version != null && version.startsWith(prefix)) {
|
||||
|
||||
Reference in New Issue
Block a user