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;
}
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);
}
}
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);
}
}
}
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);
}
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");
}
Aggregations