PY-31442 Move PyStringLiteralExpressionImpl#getPrefixLength to PyStringLiteralUtil

This commit is contained in:
Mikhail Golubev
2018-10-01 12:46:41 +03:00
parent d74cfa71f5
commit 79189b4f5e
13 changed files with 25 additions and 34 deletions
@@ -19,7 +19,6 @@ import com.jetbrains.python.inspections.PyStringFormatParser;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.PyBuiltinCache;
import com.jetbrains.python.psi.impl.PyPsiUtils;
import com.jetbrains.python.psi.impl.PyStringLiteralExpressionImpl;
import com.jetbrains.python.psi.types.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -93,7 +92,7 @@ public class ConvertFormatOperatorToMethodIntention extends PyBaseIntentionActio
CharSequence text = stringNode.getChars();
int openPos = 0;
boolean hasPrefix = false;
final int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(String.valueOf(text));
final int prefixLength = PyStringLiteralUtil.getPrefixLength(String.valueOf(text));
if (prefixLength != 0) hasPrefix = true;
openPos += prefixLength;
@@ -7,11 +7,7 @@ import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.psi.PyDocStringOwner;
import com.jetbrains.python.psi.PyElementGenerator;
import com.jetbrains.python.psi.PyFile;
import com.jetbrains.python.psi.PyStringLiteralExpression;
import com.jetbrains.python.psi.impl.PyStringLiteralExpressionImpl;
import com.jetbrains.python.psi.*;
import org.jetbrains.annotations.NotNull;
/**
@@ -39,7 +35,7 @@ public class PyQuotedStringIntention extends PyBaseIntentionAction {
if (docStringOwner.getDocStringExpression() == string) return false;
}
String stringText = string.getText();
int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(stringText);
int prefixLength = PyStringLiteralUtil.getPrefixLength(stringText);
stringText = stringText.substring(prefixLength);
if (stringText.length() >= 6) {
@@ -66,7 +62,7 @@ public class PyQuotedStringIntention extends PyBaseIntentionAction {
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
if (string != null) {
final String stringText = string.getText();
int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(stringText);
int prefixLength = PyStringLiteralUtil.getPrefixLength(stringText);
final String text = stringText.substring(prefixLength);
if (text.startsWith("'") && text.endsWith("'")) {
@@ -17,7 +17,6 @@ package com.jetbrains.python.console
import com.intellij.ide.DataManager
import com.intellij.openapi.actionSystem.IdeActions
import com.intellij.openapi.application.Result
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.actionSystem.EditorActionManager
@@ -33,9 +32,9 @@ import com.intellij.util.DocumentUtil
import com.jetbrains.python.PyTokenTypes
import com.jetbrains.python.psi.PyStatementListContainer
import com.jetbrains.python.psi.PyStringLiteralExpression
import com.jetbrains.python.psi.PyStringLiteralUtil
import com.jetbrains.python.psi.PyTryPart
import com.jetbrains.python.psi.impl.PyPsiUtils
import com.jetbrains.python.psi.impl.PyStringLiteralExpressionImpl
class PyConsoleEnterHandler {
@@ -126,12 +125,12 @@ class PyConsoleEnterHandler {
}
private fun isMultilineString(str: String): Boolean {
val text = str.substring(PyStringLiteralExpressionImpl.getPrefixLength(str))
val text = str.substring(PyStringLiteralUtil.getPrefixLength(str))
return text.startsWith("\"\"\"") || text.startsWith("'''")
}
private fun isCompleteDocString(str: String): Boolean {
val prefixLen = PyStringLiteralExpressionImpl.getPrefixLength(str)
val prefixLen = PyStringLiteralUtil.getPrefixLength(str)
val text = str.substring(prefixLen)
for (token in arrayOf("\"\"\"", "'''")) {
if (text.length >= 2 * token.length && text.startsWith(token) && text.endsWith(token)) {
@@ -38,7 +38,6 @@ import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.codeInsight.PyCodeInsightSettings;
import com.jetbrains.python.documentation.docstrings.*;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.PyStringLiteralExpressionImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -140,7 +139,7 @@ public class PythonEnterHandler extends EnterHandlerDelegateAdapter {
if (string != null && prevElement != null && PyTokenTypes.STRING_NODES.contains(prevElement.getNode().getElementType())
&& string.getTextOffset() < offset && !(element.getNode() instanceof PsiWhiteSpace)) {
final String stringText = element.getText();
final int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(stringText);
final int prefixLength = PyStringLiteralUtil.getPrefixLength(stringText);
if (string.getTextOffset() + prefixLength >= offset) {
return Result.Continue;
}
@@ -412,7 +411,7 @@ public class PythonEnterHandler extends EnterHandlerDelegateAdapter {
if (pyString != null) {
String nodeText = element.getText();
final int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(nodeText);
final int prefixLength = PyStringLiteralUtil.getPrefixLength(nodeText);
nodeText = nodeText.substring(prefixLength);
final String literalText = pyString.getText();
@@ -24,7 +24,7 @@ import com.jetbrains.python.PyBundle;
import com.jetbrains.python.inspections.quickfix.ConvertDocstringQuickFix;
import com.jetbrains.python.psi.PyDocStringOwner;
import com.jetbrains.python.psi.PyStringLiteralExpression;
import com.jetbrains.python.psi.impl.PyStringLiteralExpressionImpl;
import com.jetbrains.python.psi.PyStringLiteralUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -59,7 +59,7 @@ public class PySingleQuotedDocstringInspection extends PyInspection {
@Override
public void visitPyStringLiteralExpression(final PyStringLiteralExpression string) {
String stringText = string.getText();
int length = PyStringLiteralExpressionImpl.getPrefixLength(stringText);
int length = PyStringLiteralUtil.getPrefixLength(stringText);
stringText = stringText.substring(length);
final PyDocStringOwner docStringOwner = PsiTreeUtil.getParentOfType(string, PyDocStringOwner.class);
if (docStringOwner != null) {
@@ -11,7 +11,7 @@ import com.jetbrains.python.PyBundle;
import com.jetbrains.python.psi.PyElementGenerator;
import com.jetbrains.python.psi.PyExpression;
import com.jetbrains.python.psi.PyStringLiteralExpression;
import com.jetbrains.python.psi.impl.PyStringLiteralExpressionImpl;
import com.jetbrains.python.psi.PyStringLiteralUtil;
import org.jetbrains.annotations.NotNull;
/**
@@ -34,8 +34,7 @@ public class ConvertDocstringQuickFix implements LocalQuickFix {
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
String stringText = expression.getText();
int prefixLength = PyStringLiteralExpressionImpl
.getPrefixLength(stringText);
int prefixLength = PyStringLiteralUtil.getPrefixLength(stringText);
String prefix = stringText.substring(0, prefixLength);
String content = stringText.substring(prefixLength);
if (content.startsWith("'''") ) {
@@ -22,7 +22,7 @@ import com.intellij.openapi.project.Project;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.psi.PyElementGenerator;
import com.jetbrains.python.psi.PyStringLiteralExpression;
import com.jetbrains.python.psi.impl.PyStringLiteralExpressionImpl;
import com.jetbrains.python.psi.PyStringLiteralUtil;
import org.jetbrains.annotations.NotNull;
import static com.jetbrains.python.psi.PyUtil.as;
@@ -57,7 +57,7 @@ public class RemovePrefixQuickFix implements LocalQuickFix {
final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
for (ASTNode node : pyString.getStringNodes()) {
final String nodeText = node.getText();
final int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(nodeText);
final int prefixLength = PyStringLiteralUtil.getPrefixLength(nodeText);
if (nodeText.substring(0, prefixLength).equalsIgnoreCase(myPrefix)) {
final PyStringLiteralExpression replacement = elementGenerator.createStringLiteralAlreadyEscaped(nodeText.substring(prefixLength));
node.getPsi().replace(replacement.getFirstChild());
@@ -126,6 +126,10 @@ public class PyStringLiteralUtil {
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);
@@ -1919,7 +1919,7 @@ public class PyUtil {
}
myNode = node;
final String nodeText = node.getText();
final int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(nodeText);
final int prefixLength = PyStringLiteralUtil.getPrefixLength(nodeText);
myPrefix = nodeText.substring(0, prefixLength);
myContentRange = PyStringLiteralExpressionImpl.getNodeTextRange(nodeText);
myQuote = nodeText.substring(prefixLength, myContentRange.getStartOffset());
@@ -29,7 +29,7 @@ public class PyLiteralStringNodeImpl extends LeafPsiElement implements PyLiteral
@Override
public int getPrefixLength() {
return PyStringLiteralUtil.getPrefixEndOffset(getText(), 0);
return PyStringLiteralUtil.getPrefixLength(getText());
}
@NotNull
@@ -96,7 +96,7 @@ public class PyStringLiteralExpressionImpl extends PyElementImpl implements PySt
// TODO replace all usages with PyStringLiteralUtil.getStringValue(String)
public static TextRange getNodeTextRange(final String text) {
LOG.assertTrue(PyStringLiteralUtil.isStringLiteralToken(text), "Text of a single string literal node expected: " + text);
int startOffset = getPrefixLength(text);
int startOffset = PyStringLiteralUtil.getPrefixLength(text);
int delimiterLength = 1;
final String afterPrefix = text.substring(startOffset);
if (afterPrefix.startsWith("\"\"\"") || afterPrefix.startsWith("'''")) {
@@ -111,10 +111,6 @@ public class PyStringLiteralExpressionImpl extends PyElementImpl implements PySt
return new TextRange(startOffset, endOffset);
}
public static int getPrefixLength(String text) {
return PyStringLiteralUtil.getPrefixEndOffset(text, 0);
}
@Override
@NotNull
public List<Pair<TextRange, String>> getDecodedFragments() {
@@ -21,7 +21,6 @@ import com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil;
import com.jetbrains.python.inspections.quickfix.*;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.PyPsiUtils;
import com.jetbrains.python.psi.impl.PyStringLiteralExpressionImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -219,7 +218,7 @@ public abstract class CompatibilityVisitor extends PyAnnotator {
for (ASTNode stringNode : node.getStringNodes()) {
final String text = stringNode.getText();
final int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(text);
final int prefixLength = PyStringLiteralUtil.getPrefixLength(text);
final String prefix = text.substring(0, prefixLength).toUpperCase();
if (prefix.isEmpty()) continue;
@@ -6,8 +6,8 @@ import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.psi.PyStringLiteralExpression;
import com.jetbrains.python.psi.PyStringLiteralUtil;
import com.jetbrains.python.psi.impl.PyPsiUtils;
import com.jetbrains.python.psi.impl.PyStringLiteralExpressionImpl;
import org.jetbrains.annotations.NotNull;
import java.util.List;
@@ -26,7 +26,7 @@ public class StringLiteralQuotesAnnotator extends PyAnnotator {
final List<ASTNode> stringNodes = node.getStringNodes();
for (ASTNode stringNode : stringNodes) {
final String nodeText = PyPsiUtils.getElementTextWithoutHostEscaping(stringNode.getPsi());
final int index = PyStringLiteralExpressionImpl.getPrefixLength(nodeText);
final int index = PyStringLiteralUtil.getPrefixLength(nodeText);
final String unprefixed = nodeText.substring(index);
final boolean foundError;
if (StringUtil.startsWith(unprefixed, TRIPLE_QUOTES)) {