Search in sources :

Example 1 with PyKeywordArgument

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

ASTNode (com.intellij.lang.ASTNode)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 LanguageLevel (com.jetbrains.python.psi.LanguageLevel)1 PyCallExpression (com.jetbrains.python.psi.PyCallExpression)1 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)1 PyKeywordArgument (com.jetbrains.python.psi.PyKeywordArgument)1