use of com.jetbrains.python.psi.LanguageLevel in project intellij-community by JetBrains.
the class PyCCChangeCourseInfo method setupLanguageLevels.
protected void setupLanguageLevels(Course course, CCNewProjectPanel panel) {
JLabel languageLevelLabel = panel.getLanguageLevelLabel();
languageLevelLabel.setText("Python:");
languageLevelLabel.setVisible(true);
ComboBox<String> languageLevelCombobox = panel.getLanguageLevelCombobox();
languageLevelCombobox.addItem(ALL_VERSIONS);
languageLevelCombobox.addItem(PYTHON_3);
languageLevelCombobox.addItem(PYTHON_2);
for (LanguageLevel level : LanguageLevel.values()) {
languageLevelCombobox.addItem(level.toString());
}
languageLevelCombobox.setVisible(true);
final String version = course.getLanguageVersion();
if (version != null) {
languageLevelCombobox.setSelectedItem(version);
} else {
languageLevelCombobox.setSelectedItem(ALL_VERSIONS);
}
}
use of com.jetbrains.python.psi.LanguageLevel in project intellij-community by JetBrains.
the class PythonLanguageLevelPusher method updateSdkLanguageLevel.
private void updateSdkLanguageLevel(@NotNull final Project project, final Sdk sdk) {
final LanguageLevel languageLevel = PythonSdkType.getLanguageLevelForSdk(sdk);
final VirtualFile[] files = sdk.getRootProvider().getFiles(OrderRootType.CLASSES);
final Application application = ApplicationManager.getApplication();
PyUtil.invalidateLanguageLevelCache(project);
final Runnable markFiles = () -> application.runReadAction(() -> {
if (project.isDisposed()) {
return;
}
for (VirtualFile file : files) {
if (file.isValid()) {
VirtualFile parent = file.getParent();
boolean suppressSizeLimit = false;
if (parent != null && parent.getName().equals(PythonSdkType.SKELETON_DIR_NAME)) {
suppressSizeLimit = true;
}
markRecursively(project, file, languageLevel, suppressSizeLimit);
}
}
});
if (application.isUnitTestMode()) {
markFiles.run();
} else {
application.executeOnPooledThread(markFiles);
}
}
use of com.jetbrains.python.psi.LanguageLevel in project intellij-community by JetBrains.
the class PythonConsoleParsingTest method doTest.
public void doTest(LanguageLevel languageLevel) {
LanguageLevel prev = myLanguageLevel;
myLanguageLevel = languageLevel;
try {
doTest(true);
} finally {
myLanguageLevel = prev;
}
}
use of com.jetbrains.python.psi.LanguageLevel in project intellij-community by JetBrains.
the class PyCompatibilityInspectionAdvertiser method annotate.
@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
if (element instanceof PyFile) {
final PyFile pyFile = (PyFile) element;
final Project project = element.getProject();
final VirtualFile vFile = pyFile.getVirtualFile();
if (vFile != null && FileIndexFacade.getInstance(project).isInLibraryClasses(vFile)) {
return;
}
final Boolean showingFlag = project.getUserData(DONT_SHOW_BALLOON);
if (showingFlag != null && showingFlag.booleanValue()) {
return;
}
if (!moduleUsesPythonSdk(pyFile)) {
return;
}
final int inspectionVersion = getSettings(project).version;
if (inspectionVersion < PyCompatibilityInspection.LATEST_INSPECTION_VERSION) {
if (isCompatibilityInspectionEnabled(element)) {
final LanguageLevel pyVersion = getLatestConfiguredCompatiblePython3Version(element);
if (pyVersion != null && pyVersion.isOlderThan(LanguageLevel.getLatest())) {
showStalePython3VersionWarning(pyFile, project, pyVersion);
}
} else if (containsFutureImports(pyFile)) {
showInspectionAdvertisement(project, USING_FUTURE_IMPORTS);
} else if (containsSixImport(pyFile)) {
showInspectionAdvertisement(project, USING_SIX);
}
}
}
}
use of com.jetbrains.python.psi.LanguageLevel in project intellij-community by JetBrains.
the class AddEncodingQuickFix method applyFix.
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
final PsiFile file = element.getContainingFile();
if (file == null)
return;
PsiElement firstLine = file.getFirstChild();
if (firstLine instanceof PsiComment && firstLine.getText().startsWith("#!")) {
firstLine = firstLine.getNextSibling();
}
final LanguageLevel languageLevel = LanguageLevel.forElement(file);
final String commentText = String.format(PyEncodingUtil.ENCODING_FORMAT_PATTERN[myEncodingFormatIndex], myDefaultEncoding);
final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
PsiComment encodingComment = elementGenerator.createFromText(languageLevel, PsiComment.class, commentText);
encodingComment = (PsiComment) file.addBefore(encodingComment, firstLine);
final FileEditor fileEditor = FileEditorManager.getInstance(project).getSelectedEditor(element.getContainingFile().getVirtualFile());
if (fileEditor instanceof TextEditor) {
if (encodingComment.getNextSibling() == null || !encodingComment.getNextSibling().textContains('\n')) {
file.addAfter(elementGenerator.createFromText(languageLevel, PsiWhiteSpace.class, "\n"), encodingComment);
}
final Editor editor = ((TextEditor) fileEditor).getEditor();
final Document document = editor.getDocument();
final int insertedLineNumber = document.getLineNumber(encodingComment.getTextOffset());
editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(insertedLineNumber + 1, 0));
}
}
Aggregations