From dc1a08662b3e6dd5301d2f4a73c18081150feae9 Mon Sep 17 00:00:00 2001 From: Dmitry Trofimov Date: Fri, 29 Mar 2013 16:45:19 +0100 Subject: [PATCH] Fixed renaming prefix for static roots (PY-9307). --- .../python/psi/PyElementGenerator.java | 2 ++ .../psi/impl/PyElementGeneratorImpl.java | 22 +++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/python/psi-api/src/com/jetbrains/python/psi/PyElementGenerator.java b/python/psi-api/src/com/jetbrains/python/psi/PyElementGenerator.java index b2647c45c3e3..6f4ebd6ea5c6 100644 --- a/python/psi-api/src/com/jetbrains/python/psi/PyElementGenerator.java +++ b/python/psi-api/src/com/jetbrains/python/psi/PyElementGenerator.java @@ -33,6 +33,8 @@ public abstract class PyElementGenerator { public abstract PyStringLiteralExpression createStringLiteralFromString(@Nullable PsiFile destination, String unescaped); public abstract PyStringLiteralExpression createStringLiteralFromString(@NotNull String unescaped); + public abstract PyStringLiteralExpression createStringLiteral(@NotNull PyStringLiteralExpression oldElement, @NotNull String unescaped); + public abstract PyListLiteralExpression createListLiteral(); public abstract ASTNode createComma(); diff --git a/python/src/com/jetbrains/python/psi/impl/PyElementGeneratorImpl.java b/python/src/com/jetbrains/python/psi/impl/PyElementGeneratorImpl.java index 29c121fe571a..1a38ffb26abd 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyElementGeneratorImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyElementGeneratorImpl.java @@ -2,6 +2,7 @@ package com.jetbrains.python.psi.impl; import com.intellij.lang.ASTNode; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Pair; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; @@ -13,6 +14,7 @@ import com.intellij.util.IncorrectOperationException; import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.PythonFileType; import com.jetbrains.python.PythonLanguage; +import com.jetbrains.python.PythonStringUtil; import com.jetbrains.python.psi.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -66,6 +68,16 @@ public class PyElementGeneratorImpl extends PyElementGenerator { return createStringLiteralFromString(null, unescaped); } + public PyStringLiteralExpression createStringLiteral(@NotNull PyStringLiteralExpression oldElement, @NotNull String unescaped) { + Pair quotes = PythonStringUtil.getQuotes(oldElement.getText()); + if (quotes != null) { + return createStringLiteralAlreadyEscaped(quotes.first + unescaped + quotes.second); + } + else { + return createStringLiteralFromString(unescaped); + } + } + public PyStringLiteralExpression createStringLiteralFromString(@Nullable PsiFile destination, @NotNull String unescaped) { boolean useDouble = !unescaped.contains("\""); boolean useMulti = unescaped.matches(".*(\r|\n).*"); @@ -253,18 +265,20 @@ public class PyElementGeneratorImpl extends PyElementGenerator { public PyNamedParameter createParameter(@NotNull String name, @Nullable String defaultValue, @Nullable String annotation, @NotNull LanguageLevel languageLevel) { String parameterText = name; - if (annotation != null) + if (annotation != null) { parameterText += ": " + annotation; - if (defaultValue != null) + } + if (defaultValue != null) { parameterText += " = " + defaultValue; + } return createFromText(languageLevel, PyNamedParameter.class, "def f(" + parameterText + "): pass", PATH_PARAMETER); } @Override public PyKeywordArgument createKeywordArgument(LanguageLevel languageLevel, String keyword, String value) { - PyCallExpression callExpression = (PyCallExpression) createExpressionFromText(languageLevel, "foo(" + keyword + "=" + value + ")"); - return (PyKeywordArgument) callExpression.getArguments()[0]; + PyCallExpression callExpression = (PyCallExpression)createExpressionFromText(languageLevel, "foo(" + keyword + "=" + value + ")"); + return (PyKeywordArgument)callExpression.getArguments()[0]; } @NotNull