Search in sources :

Example 1 with LanguageLevel

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);
    }
}
Also used : LanguageLevel(com.jetbrains.python.psi.LanguageLevel)

Example 2 with LanguageLevel

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);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LanguageLevel(com.jetbrains.python.psi.LanguageLevel) Application(com.intellij.openapi.application.Application)

Example 3 with LanguageLevel

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;
    }
}
Also used : LanguageLevel(com.jetbrains.python.psi.LanguageLevel)

Example 4 with LanguageLevel

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);
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) LanguageLevel(com.jetbrains.python.psi.LanguageLevel) PyFile(com.jetbrains.python.psi.PyFile)

Example 5 with LanguageLevel

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));
    }
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) FileEditor(com.intellij.openapi.fileEditor.FileEditor) Document(com.intellij.openapi.editor.Document) PsiComment(com.intellij.psi.PsiComment) TextEditor(com.intellij.openapi.fileEditor.TextEditor) LanguageLevel(com.jetbrains.python.psi.LanguageLevel) PsiFile(com.intellij.psi.PsiFile) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

LanguageLevel (com.jetbrains.python.psi.LanguageLevel)14 Sdk (com.intellij.openapi.projectRoots.Sdk)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 ProjectJdkImpl (com.intellij.openapi.projectRoots.impl.ProjectJdkImpl)2 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)2 PythonSdkFlavor (com.jetbrains.python.sdk.flavors.PythonSdkFlavor)2 File (java.io.File)2 NotNull (org.jetbrains.annotations.NotNull)2 ASTNode (com.intellij.lang.ASTNode)1 Application (com.intellij.openapi.application.Application)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)1 FileEditor (com.intellij.openapi.fileEditor.FileEditor)1 TextEditor (com.intellij.openapi.fileEditor.TextEditor)1 Project (com.intellij.openapi.project.Project)1 SdkModificator (com.intellij.openapi.projectRoots.SdkModificator)1 SdkType (com.intellij.openapi.projectRoots.SdkType)1 PsiComment (com.intellij.psi.PsiComment)1 PsiElement (com.intellij.psi.PsiElement)1