Search in sources :

Example 6 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 7 with LanguageLevel

use of com.jetbrains.python.psi.LanguageLevel in project intellij-community by JetBrains.

the class PyActiveSdkConfigurable method rehighlightVersionSpecific.

private void rehighlightVersionSpecific(@Nullable final Sdk newSdk, @Nullable final Sdk prevSdk) {
    if (prevSdk != null && newSdk != null) {
        final PythonSdkFlavor flavor1 = PythonSdkFlavor.getFlavor(newSdk);
        final PythonSdkFlavor flavor2 = PythonSdkFlavor.getFlavor(prevSdk);
        if (flavor1 != null && flavor2 != null) {
            final LanguageLevel languageLevel1 = flavor1.getLanguageLevel(newSdk);
            final LanguageLevel languageLevel2 = flavor2.getLanguageLevel(prevSdk);
            if ((languageLevel1.isPy3K() && !languageLevel2.isPy3K()) || (!languageLevel1.isPy3K()) && languageLevel2.isPy3K()) {
                PyUtil.rehighlightOpenEditors(myProject);
            }
        }
    }
}
Also used : LanguageLevel(com.jetbrains.python.psi.LanguageLevel) PythonSdkFlavor(com.jetbrains.python.sdk.flavors.PythonSdkFlavor)

Example 8 with LanguageLevel

use of com.jetbrains.python.psi.LanguageLevel in project intellij-community by JetBrains.

the class PyChangeSignatureDialog method validateAndCommitData.

@Nullable
@Override
protected String validateAndCommitData() {
    final String functionName = myNameField.getText().trim();
    if (!functionName.equals(myMethod.getName())) {
        final boolean defined = IntroduceValidator.isDefinedInScope(functionName, myMethod.getMethod());
        if (defined) {
            return PyBundle.message("refactoring.change.signature.dialog.validation.name.defined");
        }
        if (!isNameValid(functionName, myProject)) {
            return PyBundle.message("refactoring.change.signature.dialog.validation.function.name");
        }
    }
    final List<PyParameterTableModelItem> parameters = myParametersTableModel.getItems();
    Set<String> parameterNames = new HashSet<>();
    boolean hadPositionalContainer = false;
    boolean hadKeywordContainer = false;
    boolean hadDefaultValue = false;
    boolean hadSingleStar = false;
    boolean hadParamsAfterSingleStar = false;
    LanguageLevel languageLevel = LanguageLevel.forElement(myMethod.getMethod());
    int parametersLength = parameters.size();
    for (int index = 0; index != parametersLength; ++index) {
        PyParameterTableModelItem info = parameters.get(index);
        final PyParameterInfo parameter = info.parameter;
        final String name = parameter.getName();
        final String nameWithoutStars = StringUtil.trimLeading(name, '*').trim();
        if (parameterNames.contains(nameWithoutStars)) {
            return PyBundle.message("ANN.duplicate.param.name");
        }
        parameterNames.add(nameWithoutStars);
        if (name.equals("*")) {
            hadSingleStar = true;
            if (index == parametersLength - 1) {
                return PyBundle.message("ANN.named.arguments.after.star");
            }
        } else if (name.startsWith("*") && !name.startsWith("**")) {
            if (hadKeywordContainer) {
                return PyBundle.message("ANN.starred.param.after.kwparam");
            }
            if (hadSingleStar || hadPositionalContainer) {
                return PyBundle.message("refactoring.change.signature.dialog.validation.multiple.star");
            }
            if (!isNameValid(name.substring(1), myProject)) {
                return PyBundle.message("refactoring.change.signature.dialog.validation.parameter.name");
            }
            hadPositionalContainer = true;
        } else if (name.startsWith("**")) {
            if (hadSingleStar && !hadParamsAfterSingleStar) {
                return PyBundle.message("ANN.named.arguments.after.star");
            }
            if (hadKeywordContainer) {
                return PyBundle.message("refactoring.change.signature.dialog.validation.multiple.double.star");
            }
            if (!isNameValid(name.substring(2), myProject)) {
                return PyBundle.message("refactoring.change.signature.dialog.validation.parameter.name");
            }
            hadKeywordContainer = true;
        } else {
            if (!isNameValid(name, myProject)) {
                return PyBundle.message("refactoring.change.signature.dialog.validation.parameter.name");
            }
            if (hadSingleStar) {
                hadParamsAfterSingleStar = true;
            }
            if (hadPositionalContainer && !languageLevel.isPy3K()) {
                return PyBundle.message("ANN.regular.param.after.vararg");
            } else if (hadKeywordContainer) {
                return PyBundle.message("ANN.regular.param.after.keyword");
            }
            final String defaultValue = info.getDefaultValue();
            if (defaultValue != null && !StringUtil.isEmptyOrSpaces(defaultValue) && parameter.getDefaultInSignature()) {
                hadDefaultValue = true;
            } else {
                if (hadDefaultValue && !hadSingleStar && (!languageLevel.isPy3K() || !hadPositionalContainer)) {
                    return PyBundle.message("ANN.non.default.param.after.default");
                }
            }
        }
        if (parameter.getOldIndex() < 0) {
            if (!parameter.getName().startsWith("*")) {
                if (StringUtil.isEmpty(info.defaultValueCodeFragment.getText())) {
                    return PyBundle.message("refactoring.change.signature.dialog.validation.default.missing");
                }
                if (StringUtil.isEmptyOrSpaces(parameter.getName())) {
                    return PyBundle.message("refactoring.change.signature.dialog.validation.parameter.missing");
                }
            }
        } else if (myMethod.getParameters().get(parameter.getOldIndex()).getDefaultInSignature() && StringUtil.isEmptyOrSpaces(parameter.getDefaultValue())) {
            return PyBundle.message("refactoring.change.signature.dialog.validation.default.missing");
        }
    }
    return null;
}
Also used : LanguageLevel(com.jetbrains.python.psi.LanguageLevel) HashSet(com.intellij.util.containers.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with LanguageLevel

use of com.jetbrains.python.psi.LanguageLevel in project intellij-community by JetBrains.

the class PyKeywordArgumentManipulator method handleContentChange.

@Override
public PyKeywordArgument handleContentChange(@NotNull PyKeywordArgument element, @NotNull TextRange range, String newContent) throws IncorrectOperationException {
    final ASTNode keywordNode = element.getKeywordNode();
    if (keywordNode != null && keywordNode.getTextRange().shiftRight(-element.getTextRange().getStartOffset()).equals(range)) {
        final LanguageLevel langLevel = LanguageLevel.forElement(element);
        final PyElementGenerator generator = PyElementGenerator.getInstance(element.getProject());
        final PyCallExpression callExpression = (PyCallExpression) generator.createExpressionFromText(langLevel, "foo(" + newContent + "=None)");
        final PyKeywordArgument kwArg = callExpression.getArgumentList().getKeywordArgument(newContent);
        element.getKeywordNode().getPsi().replace(kwArg.getKeywordNode().getPsi());
        return element;
    }
    throw new IncorrectOperationException("unsupported manipulation on keyword argument");
}
Also used : LanguageLevel(com.jetbrains.python.psi.LanguageLevel) ASTNode(com.intellij.lang.ASTNode) PyCallExpression(com.jetbrains.python.psi.PyCallExpression) PyKeywordArgument(com.jetbrains.python.psi.PyKeywordArgument) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator)

Example 10 with LanguageLevel

use of com.jetbrains.python.psi.LanguageLevel in project intellij-community by JetBrains.

the class PyPackageManagerImpl method createVirtualEnv.

@Override
@NotNull
public String createVirtualEnv(@NotNull String destinationDir, boolean useGlobalSite) throws ExecutionException {
    final List<String> args = new ArrayList<>();
    final Sdk sdk = getSdk();
    final LanguageLevel languageLevel = PythonSdkType.getLanguageLevelForSdk(sdk);
    final boolean usePyVenv = languageLevel.isAtLeast(LanguageLevel.PYTHON33);
    if (usePyVenv) {
        args.add("pyvenv");
        if (useGlobalSite) {
            args.add("--system-site-packages");
        }
        args.add(destinationDir);
        getHelperResult(PACKAGING_TOOL, args, false, true, null);
    } else {
        if (useGlobalSite) {
            args.add("--system-site-packages");
        }
        args.add(destinationDir);
        final boolean pre26 = languageLevel.isOlderThan(LanguageLevel.PYTHON26);
        final String name = "virtualenv-" + (pre26 ? VIRTUALENV_PRE_26_VERSION : VIRTUALENV_VERSION);
        final String dirName = extractHelper(name + ".tar.gz");
        try {
            final String fileName = dirName + name + File.separatorChar + "virtualenv.py";
            getPythonProcessResult(fileName, args, false, true, dirName + name);
        } finally {
            FileUtil.delete(new File(dirName));
        }
    }
    final String binary = PythonSdkType.getPythonExecutable(destinationDir);
    final String binaryFallback = destinationDir + File.separator + "bin" + File.separator + "python";
    final String path = (binary != null) ? binary : binaryFallback;
    if (usePyVenv) {
        // Still no 'packaging' and 'pysetup3' for Python 3.3rc1, see PEP 405
        final VirtualFile binaryFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(path);
        if (binaryFile != null) {
            final ProjectJdkImpl tmpSdk = new ProjectJdkImpl("", PythonSdkType.getInstance());
            tmpSdk.setHomePath(path);
            final PyPackageManager manager = PyPackageManager.getInstance(tmpSdk);
            manager.installManagement();
        }
    }
    return path;
}
Also used : LanguageLevel(com.jetbrains.python.psi.LanguageLevel) ProjectJdkImpl(com.intellij.openapi.projectRoots.impl.ProjectJdkImpl) Sdk(com.intellij.openapi.projectRoots.Sdk) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

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