From a7bc6362df5744e5aaa6bae700a3ae8bf51b5cd6 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Thu, 22 Sep 2022 13:08:18 +0200 Subject: [PATCH] Python: Extract template language commons GitOrigin-RevId: 1919f022b88d2e8ff3a5966a4c836a38d0e3801c --- .idea/modules.xml | 1 + .../python/PythonCommunityPluginModules.kt | 1 + .../python/intellij.fullLine.python.iml | 1 + .../python/ParameterListFormatter.kt | 4 +- python/intellij.python.community.impl.iml | 1 + .../intellij.python.community.core.impl.iml | 14 +++ .../com/jetbrains/python/BaseReference.java | 0 .../PyUserInitiatedResolvableReference.java | 0 .../psi/FileReferenceWithOneContext.java | 0 .../python/psi/PyStringLiteralCoreUtil.java | 103 ++++++++++++++++++ .../python/psi/WeakFileReference.java | 0 .../intellij.python.psi.impl.iml | 1 + .../PyFStringLikeCompletionContributor.java | 2 +- ...yStringConcatenationToFormatIntention.java | 2 +- .../DocStringParameterReference.java | 2 +- .../doctest/PyDocstringLanguageInjector.java | 4 +- .../python/lexer/PyStringLiteralLexer.java | 3 +- .../python/lexer/PythonHighlightingLexer.java | 3 +- .../python/psi/PyStringLiteralUtil.java | 91 +--------------- .../python/psi/impl/ParamHelper.java | 4 +- .../psi/impl/PyElementGeneratorImpl.java | 2 +- .../impl/PyFormattedStringElementImpl.java | 7 +- .../psi/impl/PyPlainStringElementImpl.java | 3 +- .../PyStringLiteralExpressionManipulator.java | 3 +- .../refactoring/PyReplaceExpressionUtil.java | 2 +- .../introduce/IntroduceHandler.java | 2 +- .../python/PythonFoldingBuilder.java | 2 +- .../jetbrains/python/PythonStringUtil.java | 5 +- .../actions/PyFillParagraphHandler.java | 4 +- .../moveUpDown/PyStatementMover.java | 2 +- .../python/editor/BaseQuoteHandler.java | 6 +- 31 files changed, 155 insertions(+), 120 deletions(-) create mode 100644 python/python-core-impl/intellij.python.community.core.impl.iml rename python/{ => python-core-impl}/src/com/jetbrains/python/BaseReference.java (100%) rename python/{python-psi-impl => python-core-impl}/src/com/jetbrains/python/PyUserInitiatedResolvableReference.java (100%) rename python/{ => python-core-impl}/src/com/jetbrains/python/psi/FileReferenceWithOneContext.java (100%) create mode 100644 python/python-core-impl/src/com/jetbrains/python/psi/PyStringLiteralCoreUtil.java rename python/{ => python-core-impl}/src/com/jetbrains/python/psi/WeakFileReference.java (100%) diff --git a/.idea/modules.xml b/.idea/modules.xml index a0c84bfe3fb2..d90e2b7807a3 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -962,6 +962,7 @@ + diff --git a/platform/build-scripts/src/org/jetbrains/intellij/build/python/PythonCommunityPluginModules.kt b/platform/build-scripts/src/org/jetbrains/intellij/build/python/PythonCommunityPluginModules.kt index fa881db09f1b..bc97b384245b 100644 --- a/platform/build-scripts/src/org/jetbrains/intellij/build/python/PythonCommunityPluginModules.kt +++ b/platform/build-scripts/src/org/jetbrains/intellij/build/python/PythonCommunityPluginModules.kt @@ -15,6 +15,7 @@ object PythonCommunityPluginModules { "intellij.python.community.plugin.java", "intellij.python.psi", "intellij.python.psi.impl", + "intellij.python.community.core.impl", "intellij.python.pydev", "intellij.python.community.impl", "intellij.python.langInjection", diff --git a/plugins/full-line/python/intellij.fullLine.python.iml b/plugins/full-line/python/intellij.fullLine.python.iml index a39745e24a03..cc69072bb583 100644 --- a/plugins/full-line/python/intellij.fullLine.python.iml +++ b/plugins/full-line/python/intellij.fullLine.python.iml @@ -21,5 +21,6 @@ + \ No newline at end of file diff --git a/plugins/full-line/python/src/org/jetbrains/completion/full/line/language/formatters/python/ParameterListFormatter.kt b/plugins/full-line/python/src/org/jetbrains/completion/full/line/language/formatters/python/ParameterListFormatter.kt index 1cfd05fc1dc3..bcef52989836 100644 --- a/plugins/full-line/python/src/org/jetbrains/completion/full/line/language/formatters/python/ParameterListFormatter.kt +++ b/plugins/full-line/python/src/org/jetbrains/completion/full/line/language/formatters/python/ParameterListFormatter.kt @@ -5,7 +5,7 @@ import com.intellij.psi.PsiElement import com.intellij.util.containers.ContainerUtil import com.jetbrains.python.psi.PyNamedParameter import com.jetbrains.python.psi.PyParameterList -import com.jetbrains.python.psi.PyStringLiteralUtil +import com.jetbrains.python.psi.PyStringLiteralCoreUtil import com.jetbrains.python.psi.types.PyCallableParameter import com.jetbrains.python.psi.types.PyCallableParameterImpl import org.jetbrains.completion.full.line.language.ElementFormatter @@ -46,7 +46,7 @@ class ParameterListFormatter : ElementFormatter { private fun includeDefaultValue(defaultValue: String): String { val sb = StringBuilder() - val quotes = PyStringLiteralUtil.getQuotes(defaultValue) + val quotes = PyStringLiteralCoreUtil.getQuotes(defaultValue) sb.append("=") if (quotes != null) { diff --git a/python/intellij.python.community.impl.iml b/python/intellij.python.community.impl.iml index 76fe00fb034b..58aab2a53ac6 100644 --- a/python/intellij.python.community.impl.iml +++ b/python/intellij.python.community.impl.iml @@ -115,5 +115,6 @@ + \ No newline at end of file diff --git a/python/python-core-impl/intellij.python.community.core.impl.iml b/python/python-core-impl/intellij.python.community.core.impl.iml new file mode 100644 index 000000000000..e7f25b0652d7 --- /dev/null +++ b/python/python-core-impl/intellij.python.community.core.impl.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/python/src/com/jetbrains/python/BaseReference.java b/python/python-core-impl/src/com/jetbrains/python/BaseReference.java similarity index 100% rename from python/src/com/jetbrains/python/BaseReference.java rename to python/python-core-impl/src/com/jetbrains/python/BaseReference.java diff --git a/python/python-psi-impl/src/com/jetbrains/python/PyUserInitiatedResolvableReference.java b/python/python-core-impl/src/com/jetbrains/python/PyUserInitiatedResolvableReference.java similarity index 100% rename from python/python-psi-impl/src/com/jetbrains/python/PyUserInitiatedResolvableReference.java rename to python/python-core-impl/src/com/jetbrains/python/PyUserInitiatedResolvableReference.java diff --git a/python/src/com/jetbrains/python/psi/FileReferenceWithOneContext.java b/python/python-core-impl/src/com/jetbrains/python/psi/FileReferenceWithOneContext.java similarity index 100% rename from python/src/com/jetbrains/python/psi/FileReferenceWithOneContext.java rename to python/python-core-impl/src/com/jetbrains/python/psi/FileReferenceWithOneContext.java diff --git a/python/python-core-impl/src/com/jetbrains/python/psi/PyStringLiteralCoreUtil.java b/python/python-core-impl/src/com/jetbrains/python/psi/PyStringLiteralCoreUtil.java new file mode 100644 index 000000000000..ffe6027a8a9b --- /dev/null +++ b/python/python-core-impl/src/com/jetbrains/python/psi/PyStringLiteralCoreUtil.java @@ -0,0 +1,103 @@ +// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.jetbrains.python.psi; + +import com.google.common.collect.ImmutableList; +import com.intellij.openapi.util.Pair; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class PyStringLiteralCoreUtil { + /** + * Valid string prefix characters (lowercased) as defined in Python lexer. + */ + public static final String PREFIX_CHARACTERS = "ubcrf"; + + /** + * Maximum length of a string prefix as defined in Python lexer. + */ + public static final int MAX_PREFIX_LENGTH = 3; + + private static final ImmutableList QUOTES = ImmutableList.of("'''", "\"\"\"", "'", "\""); + + protected PyStringLiteralCoreUtil() { + } + + /** + * Returns a pair where the first element is the prefix combined with the opening quote and the second is the closing quote. + *

+ * If the given string literal is not properly quoted, e.g. the closing quote has fewer quotes as opposed to the + * opening one, or it's missing altogether this method returns null. + *

+ * Examples: + *

+   *   ur"foo" -> ("ur, ")
+   *   ur'bar -> null
+   *   """baz""" -> (""", """)
+   *   '''quux' -> null
+   * 
+ */ + @Nullable + public static Pair getQuotes(@NotNull String text) { + final String prefix = getPrefix(text); + final String mainText = text.substring(prefix.length()); + for (String quote : QUOTES) { + final Pair quotes = getQuotes(mainText, prefix, quote); + if (quotes != null) { + return quotes; + } + } + return null; + } + + /** + * Finds the end offset of the string prefix starting from {@code startOffset} in the given char sequence. + * String prefix may contain only up to {@link #MAX_PREFIX_LENGTH} characters from {@link #PREFIX_CHARACTERS} + * (case insensitively). + * + * @return end offset of found string prefix + */ + public static int getPrefixEndOffset(@NotNull CharSequence text, int startOffset) { + int offset; + for (offset = startOffset; offset < Math.min(startOffset + MAX_PREFIX_LENGTH, text.length()); offset++) { + if (PREFIX_CHARACTERS.indexOf(Character.toLowerCase(text.charAt(offset))) < 0) { + break; + } + } + return offset; + } + + @NotNull + public static String getPrefix(@NotNull CharSequence text) { + return getPrefix(text, 0); + } + + /** + * Extracts string prefix from the given char sequence using {@link #getPrefixEndOffset(CharSequence, int)}. + * + * @return extracted string prefix + * @see #getPrefixEndOffset(CharSequence, int) + */ + @NotNull + public static String getPrefix(@NotNull CharSequence text, int startOffset) { + return text.subSequence(startOffset, getPrefixEndOffset(text, startOffset)).toString(); + } + + @Nullable + private static Pair getQuotes(@NotNull String text, @NotNull String prefix, @NotNull String quote) { + final int length = text.length(); + final int n = quote.length(); + if (length >= 2 * n && text.startsWith(quote) && text.endsWith(quote)) { + return Pair.create(prefix + text.substring(0, n), text.substring(length - n)); + } + return null; + } + + public static String stripQuotesAroundValue(String text) { + Pair quotes = getQuotes(text); + if (quotes == null) { + return text; + } + + return text.substring(quotes.first.length(), text.length() - quotes.second.length()); + } +} diff --git a/python/src/com/jetbrains/python/psi/WeakFileReference.java b/python/python-core-impl/src/com/jetbrains/python/psi/WeakFileReference.java similarity index 100% rename from python/src/com/jetbrains/python/psi/WeakFileReference.java rename to python/python-core-impl/src/com/jetbrains/python/psi/WeakFileReference.java diff --git a/python/python-psi-impl/intellij.python.psi.impl.iml b/python/python-psi-impl/intellij.python.psi.impl.iml index 1f04ddd411c8..c6f5beeec1cb 100644 --- a/python/python-psi-impl/intellij.python.psi.impl.iml +++ b/python/python-psi-impl/intellij.python.psi.impl.iml @@ -33,5 +33,6 @@ +
\ No newline at end of file diff --git a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/completion/PyFStringLikeCompletionContributor.java b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/completion/PyFStringLikeCompletionContributor.java index dd1cbb836667..a0415bc1883d 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/completion/PyFStringLikeCompletionContributor.java +++ b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/completion/PyFStringLikeCompletionContributor.java @@ -101,7 +101,7 @@ public class PyFStringLikeCompletionContributor extends CompletionContributor im document.insertString(tailOffset, "}"); } // It can happen when completion is invoked on multiple carets inside the same string - String stringElemPrefix = PyStringLiteralUtil.getPrefix(docChars, stringElemStart); + String stringElemPrefix = PyStringLiteralCoreUtil.getPrefix(docChars, stringElemStart); if (!PyStringLiteralUtil.isFormattedPrefix(stringElemPrefix)) { document.insertString(stringElemStart, "f"); } diff --git a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java index d1d7a2ccdba1..7665d6c3d6ac 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java +++ b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java @@ -136,7 +136,7 @@ public class PyStringConcatenationToFormatIntention extends PyBaseIntentionActio isUnicode = true; } if (!quotesDetected) { - quotes = PyStringLiteralUtil.getQuotes(expression.getText()); + quotes = PyStringLiteralCoreUtil.getQuotes(expression.getText()); quotesDetected = true; } String value = ((PyStringLiteralExpression)expression).getStringValue(); diff --git a/python/python-psi-impl/src/com/jetbrains/python/documentation/docstrings/DocStringParameterReference.java b/python/python-psi-impl/src/com/jetbrains/python/documentation/docstrings/DocStringParameterReference.java index a8dbbea81fe9..ff2fdfac31d5 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/documentation/docstrings/DocStringParameterReference.java +++ b/python/python-psi-impl/src/com/jetbrains/python/documentation/docstrings/DocStringParameterReference.java @@ -157,7 +157,7 @@ public class DocStringParameterReference extends PsiReferenceBase quotes = PyStringLiteralUtil.getQuotes(range.substring(myElement.getText())); + Pair quotes = PyStringLiteralCoreUtil.getQuotes(range.substring(myElement.getText())); if (quotes != null) { range = TextRange.create(range.getStartOffset() + quotes.first.length(), range.getEndOffset() - quotes.second.length()); diff --git a/python/python-psi-impl/src/com/jetbrains/python/documentation/doctest/PyDocstringLanguageInjector.java b/python/python-psi-impl/src/com/jetbrains/python/documentation/doctest/PyDocstringLanguageInjector.java index 76f0a3fb4365..d8ddff66c934 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/documentation/doctest/PyDocstringLanguageInjector.java +++ b/python/python-psi-impl/src/com/jetbrains/python/documentation/doctest/PyDocstringLanguageInjector.java @@ -25,8 +25,8 @@ import com.intellij.psi.LanguageInjector; import com.intellij.psi.PsiLanguageInjectionHost; import com.jetbrains.python.documentation.PyDocumentationSettings; import com.jetbrains.python.documentation.docstrings.DocStringUtil; +import com.jetbrains.python.psi.PyStringLiteralCoreUtil; import com.jetbrains.python.psi.PyStringLiteralExpression; -import com.jetbrains.python.psi.PyStringLiteralUtil; import org.jetbrains.annotations.NotNull; import java.util.List; @@ -45,7 +45,7 @@ public class PyDocstringLanguageInjector implements LanguageInjector { int end = host.getTextLength() - 1; final String text = host.getText(); - final Pair quotes = PyStringLiteralUtil.getQuotes(text); + final Pair quotes = PyStringLiteralCoreUtil.getQuotes(text); final List strings = StringUtil.split(text, "\n", false); boolean gotExample = false; diff --git a/python/python-psi-impl/src/com/jetbrains/python/lexer/PyStringLiteralLexer.java b/python/python-psi-impl/src/com/jetbrains/python/lexer/PyStringLiteralLexer.java index 0699e6d48463..22adab68172b 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/lexer/PyStringLiteralLexer.java +++ b/python/python-psi-impl/src/com/jetbrains/python/lexer/PyStringLiteralLexer.java @@ -3,6 +3,7 @@ package com.jetbrains.python.lexer; import com.intellij.psi.tree.IElementType; import com.jetbrains.python.PyTokenTypes; +import com.jetbrains.python.psi.PyStringLiteralCoreUtil; import com.jetbrains.python.psi.PyStringLiteralUtil; import org.jetbrains.annotations.NotNull; @@ -38,7 +39,7 @@ public class PyStringLiteralLexer extends PyStringLiteralLexerBase { myLastState = initialState; // the following could be parsing steps if we wanted this info as tokens - final String prefix = PyStringLiteralUtil.getPrefix(buffer, myStart); + final String prefix = PyStringLiteralCoreUtil.getPrefix(buffer, myStart); myIsRaw = PyStringLiteralUtil.isRawPrefix(prefix); diff --git a/python/python-psi-impl/src/com/jetbrains/python/lexer/PythonHighlightingLexer.java b/python/python-psi-impl/src/com/jetbrains/python/lexer/PythonHighlightingLexer.java index c167f706d0fe..c89603e3b939 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/lexer/PythonHighlightingLexer.java +++ b/python/python-psi-impl/src/com/jetbrains/python/lexer/PythonHighlightingLexer.java @@ -20,6 +20,7 @@ import com.jetbrains.python.PyNames; import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.psi.FutureFeature; import com.jetbrains.python.psi.LanguageLevel; +import com.jetbrains.python.psi.PyStringLiteralCoreUtil; import com.jetbrains.python.psi.PyStringLiteralUtil; import org.jetbrains.annotations.NotNull; @@ -43,7 +44,7 @@ public class PythonHighlightingLexer extends PythonLexer { @NotNull String tokenText, @NotNull LanguageLevel languageLevel, boolean unicodeImport) { - final String prefix = PyStringLiteralUtil.getPrefix(tokenText); + final String prefix = PyStringLiteralCoreUtil.getPrefix(tokenText); if (tokenType == PyTokenTypes.SINGLE_QUOTED_STRING) { if (languageLevel.isPy3K()) { diff --git a/python/python-psi-impl/src/com/jetbrains/python/psi/PyStringLiteralUtil.java b/python/python-psi-impl/src/com/jetbrains/python/psi/PyStringLiteralUtil.java index f05ee552a4a7..4b3896b27bff 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/psi/PyStringLiteralUtil.java +++ b/python/python-psi-impl/src/com/jetbrains/python/psi/PyStringLiteralUtil.java @@ -15,7 +15,6 @@ */ package com.jetbrains.python.psi; -import com.google.common.collect.ImmutableList; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.TextRange; @@ -31,16 +30,7 @@ import java.util.List; /** * @author Mikhail Golubev */ -public final class PyStringLiteralUtil { - /** - * Valid string prefix characters (lowercased) as defined in Python lexer. - */ - public static final String PREFIX_CHARACTERS = "ubcrf"; - /** - * Maximum length of a string prefix as defined in Python lexer. - */ - public static final int MAX_PREFIX_LENGTH = 3; - private static final ImmutableList QUOTES = ImmutableList.of("'''", "\"\"\"", "'", "\""); +public final class PyStringLiteralUtil extends PyStringLiteralCoreUtil { private static final Logger LOG = Logger.getInstance(PyStringLiteralUtil.class); @@ -91,33 +81,6 @@ public final class PyStringLiteralUtil { return text != null && getQuotes(text) != null; } - /** - * Returns a pair where the first element is the prefix combined with the opening quote and the second is the closing quote. - *

- * If the given string literal is not properly quoted, e.g. the closing quote has fewer quotes as opposed to the - * opening one, or it's missing altogether this method returns null. - *

- * Examples: - *

-   *   ur"foo" -> ("ur, ")
-   *   ur'bar -> null
-   *   """baz""" -> (""", """)
-   *   '''quux' -> null
-   * 
- */ - @Nullable - public static Pair getQuotes(@NotNull String text) { - final String prefix = getPrefix(text); - final String mainText = text.substring(prefix.length()); - for (String quote : QUOTES) { - final Pair quotes = getQuotes(mainText, prefix, quote); - if (quotes != null) { - return quotes; - } - } - return null; - } - /** * Returns the range of the string literal text between the opening quote and the closing one. * If the closing quote is either missing or mismatched, this range spans until the end of the literal. @@ -140,43 +103,10 @@ public final class PyStringLiteralUtil { return new TextRange(startOffset, endOffset); } - /** - * Finds the end offset of the string prefix starting from {@code startOffset} in the given char sequence. - * String prefix may contain only up to {@link #MAX_PREFIX_LENGTH} characters from {@link #PREFIX_CHARACTERS} - * (case insensitively). - * - * @return end offset of found string prefix - */ - public static int getPrefixEndOffset(@NotNull CharSequence text, int startOffset) { - int offset; - for (offset = startOffset; offset < Math.min(startOffset + MAX_PREFIX_LENGTH, text.length()); offset++) { - if (PREFIX_CHARACTERS.indexOf(Character.toLowerCase(text.charAt(offset))) < 0) { - break; - } - } - return offset; - } - public static int getPrefixLength(@NotNull String text) { return getPrefixEndOffset(text, 0); } - @NotNull - public static String getPrefix(@NotNull CharSequence text) { - return getPrefix(text, 0); - } - - /** - * Extracts string prefix from the given char sequence using {@link #getPrefixEndOffset(CharSequence, int)}. - * - * @return extracted string prefix - * @see #getPrefixEndOffset(CharSequence, int) - */ - @NotNull - public static String getPrefix(@NotNull CharSequence text, int startOffset) { - return text.subSequence(startOffset, getPrefixEndOffset(text, startOffset)).toString(); - } - /** * @return whether the given prefix contains either 'u' or 'U' character */ @@ -212,16 +142,6 @@ public final class PyStringLiteralUtil { return quote == '"' ? '\'' : '"'; } - @Nullable - private static Pair getQuotes(@NotNull String text, @NotNull String prefix, @NotNull String quote) { - final int length = text.length(); - final int n = quote.length(); - if (length >= 2 * n && text.startsWith(quote) && text.endsWith(quote)) { - return Pair.create(prefix + text.substring(0, n), text.substring(length - n)); - } - return null; - } - public static TextRange getTextRange(PsiElement element) { if (element instanceof PyStringLiteralExpression) { final List ranges = ((PyStringLiteralExpression)element).getStringValueTextRanges(); @@ -255,13 +175,4 @@ public final class PyStringLiteralUtil { return o.getText(); } } - - public static String stripQuotesAroundValue(String text) { - Pair quotes = getQuotes(text); - if (quotes == null) { - return text; - } - - return text.substring(quotes.first.length(), text.length() - quotes.second.length()); - } } diff --git a/python/python-psi-impl/src/com/jetbrains/python/psi/impl/ParamHelper.java b/python/python-psi-impl/src/com/jetbrains/python/psi/impl/ParamHelper.java index f2a40454dbb2..9b3ad2156010 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/psi/impl/ParamHelper.java +++ b/python/python-psi-impl/src/com/jetbrains/python/psi/impl/ParamHelper.java @@ -176,7 +176,7 @@ public final class ParamHelper { // According to PEP 8 equal sign should be surrounded by spaces if annotation is present sb.append(parameterRenderedAsTyped ? " = " : "="); - final Pair quotes = PyStringLiteralUtil.getQuotes(defaultValue); + final Pair quotes = PyStringLiteralCoreUtil.getQuotes(defaultValue); if (quotes != null) { final String value = defaultValue.substring(quotes.getFirst().length(), defaultValue.length() - quotes.getSecond().length()); sb.append(quotes.getFirst()); @@ -197,7 +197,7 @@ public final class ParamHelper { @Nullable public static String getDefaultValueText(@Nullable PyExpression defaultValue) { if (defaultValue instanceof PyStringLiteralExpression) { - final Pair quotes = PyStringLiteralUtil.getQuotes(defaultValue.getText()); + final Pair quotes = PyStringLiteralCoreUtil.getQuotes(defaultValue.getText()); if (quotes != null) { return quotes.getFirst() + ((PyStringLiteralExpression)defaultValue).getStringValue() + quotes.getSecond(); } diff --git a/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyElementGeneratorImpl.java b/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyElementGeneratorImpl.java index 0d8533bd371a..9c496a61ec6e 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyElementGeneratorImpl.java +++ b/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyElementGeneratorImpl.java @@ -96,7 +96,7 @@ public class PyElementGeneratorImpl extends PyElementGenerator { @Override public PyStringLiteralExpression createStringLiteral(@NotNull StringLiteralExpression oldElement, @NotNull String unescaped) { - Pair quotes = PyStringLiteralUtil.getQuotes(oldElement.getText()); + Pair quotes = PyStringLiteralCoreUtil.getQuotes(oldElement.getText()); if (quotes != null) { return createStringLiteralAlreadyEscaped(quotes.first + unescaped + quotes.second); } diff --git a/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyFormattedStringElementImpl.java b/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyFormattedStringElementImpl.java index 0770a0cba7aa..8d94bc52b6ef 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyFormattedStringElementImpl.java +++ b/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyFormattedStringElementImpl.java @@ -10,10 +10,7 @@ import com.intellij.psi.tree.IElementType; import com.intellij.util.containers.ContainerUtil; import com.jetbrains.python.PyElementTypes; import com.jetbrains.python.PyTokenTypes; -import com.jetbrains.python.psi.PyElementVisitor; -import com.jetbrains.python.psi.PyFStringFragment; -import com.jetbrains.python.psi.PyFormattedStringElement; -import com.jetbrains.python.psi.PyStringLiteralUtil; +import com.jetbrains.python.psi.*; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -46,7 +43,7 @@ public class PyFormattedStringElementImpl extends PyElementImpl implements PyFor @NotNull @Override public String getPrefix() { - return PyStringLiteralUtil.getPrefix(getText()); + return PyStringLiteralCoreUtil.getPrefix(getText()); } @Override diff --git a/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyPlainStringElementImpl.java b/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyPlainStringElementImpl.java index 4cb40adabbe7..22ad6659dc09 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyPlainStringElementImpl.java +++ b/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyPlainStringElementImpl.java @@ -6,6 +6,7 @@ import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.impl.source.tree.LeafPsiElement; import com.intellij.psi.tree.IElementType; import com.jetbrains.python.psi.PyPlainStringElement; +import com.jetbrains.python.psi.PyStringLiteralCoreUtil; import com.jetbrains.python.psi.PyStringLiteralUtil; import org.jetbrains.annotations.NotNull; @@ -22,7 +23,7 @@ public class PyPlainStringElementImpl extends LeafPsiElement implements PyPlainS @NotNull @Override public String getPrefix() { - return PyStringLiteralUtil.getPrefix(getText()); + return PyStringLiteralCoreUtil.getPrefix(getText()); } @Override diff --git a/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionManipulator.java b/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionManipulator.java index 19e3b081b116..64df389a1320 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionManipulator.java +++ b/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyStringLiteralExpressionManipulator.java @@ -20,6 +20,7 @@ import com.intellij.openapi.util.TextRange; import com.intellij.psi.AbstractElementManipulator; import com.intellij.util.IncorrectOperationException; import com.jetbrains.python.psi.PyElementGenerator; +import com.jetbrains.python.psi.PyStringLiteralCoreUtil; import com.jetbrains.python.psi.PyStringLiteralExpression; import com.jetbrains.python.psi.PyStringLiteralUtil; import org.jetbrains.annotations.NotNull; @@ -66,7 +67,7 @@ public class PyStringLiteralExpressionManipulator extends AbstractElementManipul @NotNull private static Pair calculateQuotes(@NotNull String text) { - final Pair quotes = PyStringLiteralUtil.getQuotes(text); + final Pair quotes = PyStringLiteralCoreUtil.getQuotes(text); if (quotes == null || quotes.first == null && quotes.second == null) return Pair.createNonNull("\"", "\""); diff --git a/python/python-psi-impl/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java b/python/python-psi-impl/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java index 7b7360f2edd9..5d4241aebc78 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java +++ b/python/python-psi-impl/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java @@ -124,7 +124,7 @@ public final class PyReplaceExpressionUtil implements PyElementTypes { @NotNull PsiElement newExpression, @NotNull TextRange textRange) { final String fullText = oldExpression.getText(); - final Pair detectedQuotes = PyStringLiteralUtil.getQuotes(fullText); + final Pair detectedQuotes = PyStringLiteralCoreUtil.getQuotes(fullText); final Pair quotes = detectedQuotes != null ? detectedQuotes : Pair.create("'", "'"); final String prefix = fullText.substring(0, textRange.getStartOffset()); final String suffix = fullText.substring(textRange.getEndOffset(), oldExpression.getTextLength()); diff --git a/python/python-psi-impl/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java b/python/python-psi-impl/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java index cefc9fd7d9a1..a4cf3fb56bc1 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java +++ b/python/python-psi-impl/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java @@ -563,7 +563,7 @@ abstract public class IntroduceHandler implements RefactoringActionHandler { if (data != null) { final PsiElement parent = data.getFirst(); final String text = parent.getText(); - final Pair detectedQuotes = PyStringLiteralUtil.getQuotes(text); + final Pair detectedQuotes = PyStringLiteralCoreUtil.getQuotes(text); final Pair quotes = detectedQuotes != null ? detectedQuotes : Pair.create("'", "'"); final TextRange range = data.getSecond(); final String substring = range.substring(text); diff --git a/python/src/com/jetbrains/python/PythonFoldingBuilder.java b/python/src/com/jetbrains/python/PythonFoldingBuilder.java index 0450e58585a2..ddd2c0ea1bac 100644 --- a/python/src/com/jetbrains/python/PythonFoldingBuilder.java +++ b/python/src/com/jetbrains/python/PythonFoldingBuilder.java @@ -208,7 +208,7 @@ public class PythonFoldingBuilder extends CustomFoldingBuilder implements DumbAw private static String getLanguagePlaceholderForString(PyStringLiteralExpression stringLiteralExpression) { String stringText = stringLiteralExpression.getText(); - Pair quotes = PyStringLiteralUtil.getQuotes(stringText); + Pair quotes = PyStringLiteralCoreUtil.getQuotes(stringText); if (quotes != null) { return quotes.second + "..." + quotes.second; } diff --git a/python/src/com/jetbrains/python/PythonStringUtil.java b/python/src/com/jetbrains/python/PythonStringUtil.java index 2894a2238917..9245f9bb0662 100644 --- a/python/src/com/jetbrains/python/PythonStringUtil.java +++ b/python/src/com/jetbrains/python/PythonStringUtil.java @@ -8,6 +8,7 @@ import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.util.QualifiedName; import com.intellij.util.PathUtil; +import com.jetbrains.python.psi.PyStringLiteralCoreUtil; import com.jetbrains.python.psi.PyStringLiteralUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -106,8 +107,8 @@ public final class PythonStringUtil { Pair quotes = null; if (PyStringLiteralUtil.isQuoted(s)) { - quotes = PyStringLiteralUtil.getQuotes(s); - s = PyStringLiteralUtil.stripQuotesAroundValue(s); + quotes = PyStringLiteralCoreUtil.getQuotes(s); + s = PyStringLiteralCoreUtil.stripQuotesAroundValue(s); } s = removeLastSuffix(s, separator); diff --git a/python/src/com/jetbrains/python/actions/PyFillParagraphHandler.java b/python/src/com/jetbrains/python/actions/PyFillParagraphHandler.java index e842a62be704..c0d70a92fe84 100644 --- a/python/src/com/jetbrains/python/actions/PyFillParagraphHandler.java +++ b/python/src/com/jetbrains/python/actions/PyFillParagraphHandler.java @@ -29,7 +29,7 @@ public class PyFillParagraphHandler extends ParagraphFillHandler { if (stringLiteralExpression != null) { final String text = stringLiteralExpression.getText(); final Pair quotes = - PyStringLiteralUtil.getQuotes(text); + PyStringLiteralCoreUtil.getQuotes(text); final PyDocStringOwner docStringOwner = PsiTreeUtil.getParentOfType(stringLiteralExpression, PyDocStringOwner.class); if (docStringOwner != null && stringLiteralExpression.equals(docStringOwner.getDocStringExpression())) { String indent = getIndent(stringLiteralExpression); @@ -73,7 +73,7 @@ public class PyFillParagraphHandler extends ParagraphFillHandler { if (stringLiteralExpression != null) { final String text = stringLiteralExpression.getText(); final Pair quotes = - PyStringLiteralUtil.getQuotes(text); + PyStringLiteralCoreUtil.getQuotes(text); final PyDocStringOwner docStringOwner = PsiTreeUtil.getParentOfType(stringLiteralExpression, PyDocStringOwner.class); if (docStringOwner != null && stringLiteralExpression.equals(docStringOwner.getDocStringExpression())) { String indent = getIndent(stringLiteralExpression); diff --git a/python/src/com/jetbrains/python/codeInsight/editorActions/moveUpDown/PyStatementMover.java b/python/src/com/jetbrains/python/codeInsight/editorActions/moveUpDown/PyStatementMover.java index e8854106528a..f8c780331433 100644 --- a/python/src/com/jetbrains/python/codeInsight/editorActions/moveUpDown/PyStatementMover.java +++ b/python/src/com/jetbrains/python/codeInsight/editorActions/moveUpDown/PyStatementMover.java @@ -91,7 +91,7 @@ public class PyStatementMover extends LineMover { if (nearLine >= document.getLineCount() || nearLine <= 0) return false; final PyStringLiteralExpression stringLiteralExpression = PsiTreeUtil.getParentOfType(elementToMove1, PyStringLiteralExpression.class); if (stringLiteralExpression != null) { - final Pair quotes = PyStringLiteralUtil.getQuotes(stringLiteralExpression.getText()); + final Pair quotes = PyStringLiteralCoreUtil.getQuotes(stringLiteralExpression.getText()); if (quotes != null && (quotes.first.equals("'''") || quotes.first.equals("\"\"\""))) { final String text1 = document.getText(TextRange.create(start, end)).trim(); final String text2 = document.getText(TextRange.create(document.getLineStartOffset(nearLine), document.getLineEndOffset(nearLine))).trim(); diff --git a/python/src/com/jetbrains/python/editor/BaseQuoteHandler.java b/python/src/com/jetbrains/python/editor/BaseQuoteHandler.java index b7fbc9cc058f..e98ffe475b30 100644 --- a/python/src/com/jetbrains/python/editor/BaseQuoteHandler.java +++ b/python/src/com/jetbrains/python/editor/BaseQuoteHandler.java @@ -8,7 +8,7 @@ import com.intellij.openapi.editor.highlighter.HighlighterIterator; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.tree.IElementType; import com.intellij.psi.tree.TokenSet; -import com.jetbrains.python.psi.PyStringLiteralUtil; +import com.jetbrains.python.psi.PyStringLiteralCoreUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -46,7 +46,7 @@ public class BaseQuoteHandler implements MultiCharQuoteHandler { } if (getOpeningQuotesTokens().contains(iterator.getTokenType())) { int start = iterator.getStart(); - if (offset - start <= PyStringLiteralUtil.MAX_PREFIX_LENGTH) { + if (offset - start <= PyStringLiteralCoreUtil.MAX_PREFIX_LENGTH) { if (getLiteralStartOffset(text, start) == offset) return true; } } @@ -74,7 +74,7 @@ public class BaseQuoteHandler implements MultiCharQuoteHandler { } private static int getLiteralStartOffset(CharSequence text, int start) { - return PyStringLiteralUtil.getPrefixEndOffset(text, start); + return PyStringLiteralCoreUtil.getPrefixEndOffset(text, start); } @Override