Search in sources :

Example 16 with PyElementGenerator

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

the class PyConvertLiteralToTupleIntention method prepareOriginalElementCopy.

@NotNull
@Override
protected PsiElement prepareOriginalElementCopy(@NotNull PsiElement copy) {
    final PySequenceExpression sequenceExpression = unwrapCollection(copy);
    final PyExpression[] elements = sequenceExpression.getElements();
    if (elements.length == 1) {
        final PyExpression onlyElement = elements[0];
        final PsiElement next = PyPsiUtils.getNextNonCommentSibling(onlyElement, true);
        if (next != null && next.getNode().getElementType() != PyTokenTypes.COMMA) {
            final PyElementGenerator generator = PyElementGenerator.getInstance(copy.getProject());
            final ASTNode anchor = onlyElement.getNode().getTreeNext();
            sequenceExpression.getNode().addChild(generator.createComma(), anchor);
        }
    }
    return copy;
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression) ASTNode(com.intellij.lang.ASTNode) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PySequenceExpression(com.jetbrains.python.psi.PySequenceExpression) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with PyElementGenerator

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

the class PyFlipComparisonIntention method doInvoke.

public void doInvoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
    PyBinaryExpression binaryExpression = PsiTreeUtil.getParentOfType(element, PyBinaryExpression.class, false);
    while (binaryExpression != null) {
        if (FLIPPED_OPERATORS.containsKey(binaryExpression.getOperator())) {
            PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
            binaryExpression.replace(elementGenerator.createBinaryExpression(FLIPPED_OPERATORS.get(binaryExpression.getOperator()), binaryExpression.getRightExpression(), binaryExpression.getLeftExpression()));
            return;
        }
        binaryExpression = PsiTreeUtil.getParentOfType(binaryExpression, PyBinaryExpression.class);
    }
}
Also used : PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PyBinaryExpression(com.jetbrains.python.psi.PyBinaryExpression) PsiElement(com.intellij.psi.PsiElement)

Example 18 with PyElementGenerator

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

the class PyQuotedStringIntention method doInvoke.

public void doInvoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    PyStringLiteralExpression string = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyStringLiteralExpression.class);
    PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    if (string != null) {
        final String stringText = string.getText();
        int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(stringText);
        final String text = stringText.substring(prefixLength);
        if (text.startsWith("'") && text.endsWith("'")) {
            String result = convertSingleToDoubleQuoted(stringText);
            PyStringLiteralExpression st = elementGenerator.createStringLiteralAlreadyEscaped(result);
            string.replace(st);
        }
        if (text.startsWith("\"") && text.endsWith("\"")) {
            String result = convertDoubleToSingleQuoted(stringText);
            PyStringLiteralExpression st = elementGenerator.createStringLiteralAlreadyEscaped(result);
            string.replace(st);
        }
    }
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator)

Example 19 with PyElementGenerator

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

the class PyStringLiteralExpressionManipulator method handleContentChange.

@Override
public PyStringLiteralExpressionImpl handleContentChange(@NotNull PyStringLiteralExpressionImpl element, @NotNull TextRange range, String newContent) {
    final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(element.getProject());
    final String escapedText = calculateEscapedText(element.getText(), range, newContent);
    final PyStringLiteralExpression escaped = elementGenerator.createStringLiteralAlreadyEscaped(escapedText);
    return (PyStringLiteralExpressionImpl) element.replace(escaped);
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator)

Example 20 with PyElementGenerator

use of com.jetbrains.python.psi.PyElementGenerator 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)

Aggregations

PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)26 PsiElement (com.intellij.psi.PsiElement)18 PyExpression (com.jetbrains.python.psi.PyExpression)9 PyBinaryExpression (com.jetbrains.python.psi.PyBinaryExpression)5 ASTNode (com.intellij.lang.ASTNode)4 PyStringLiteralExpression (com.jetbrains.python.psi.PyStringLiteralExpression)4 PyNumericLiteralExpression (com.jetbrains.python.psi.PyNumericLiteralExpression)3 Editor (com.intellij.openapi.editor.Editor)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 LanguageLevel (com.jetbrains.python.psi.LanguageLevel)2 PyFunction (com.jetbrains.python.psi.PyFunction)2 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Document (com.intellij.openapi.editor.Document)1 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)1 FileEditor (com.intellij.openapi.fileEditor.FileEditor)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 TextEditor (com.intellij.openapi.fileEditor.TextEditor)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiComment (com.intellij.psi.PsiComment)1