This commit is contained in:
Valentina Kiryushkina
2015-12-09 20:22:32 +03:00
parent 42dd307360
commit 59baaaf1e0
3 changed files with 39 additions and 42 deletions
@@ -32,14 +32,14 @@ public class PySubstitutionChunkReference extends PsiReferenceBase<PyStringLiter
private final int myPosition;
private final PyStringFormatParser.SubstitutionChunk myChunk;
public PySubstitutionChunkReference(PyStringLiteralExpression element, @NotNull final PyStringFormatParser.SubstitutionChunk chunk, final int position) {
public PySubstitutionChunkReference(@NotNull final PyStringLiteralExpression element, @NotNull final PyStringFormatParser.SubstitutionChunk chunk, final int position) {
super(element, getKeyWordRange(element, chunk));
myChunk = chunk;
myPosition = position;
}
@Nullable
@Override
public HighlightSeverity getUnresolvedHighlightSeverity(TypeEvalContext context) {
public HighlightSeverity getUnresolvedHighlightSeverity(@NotNull final TypeEvalContext context) {
return HighlightSeverity.WEAK_WARNING;
}
@@ -49,7 +49,7 @@ public class PySubstitutionChunkReference extends PsiReferenceBase<PyStringLiter
return null;
}
private static TextRange getKeyWordRange(PyStringLiteralExpression element, PyStringFormatParser.SubstitutionChunk chunk) {
private static TextRange getKeyWordRange(@NotNull final PyStringLiteralExpression element, @NotNull final PyStringFormatParser.SubstitutionChunk chunk) {
final TextRange shifted = chunk.getTextRange().shiftRight(1);
if (chunk.getMappingKey() != null) {
final int start = shifted.getStartOffset() + chunk.getTextRange().substring(element.getStringValue()).indexOf(chunk.getMappingKey());
@@ -61,28 +61,25 @@ public class PySubstitutionChunkReference extends PsiReferenceBase<PyStringLiter
@Nullable
@Override
public PsiElement resolve() {
PsiElement result = null;
if (myChunk != null) {
result = resolveFormatString();
if (result == null) {
result = resolvePercentString();
}
PsiElement result = resolveFormatString();
if (result == null) {
result = resolvePercentString();
}
return result;
}
@Nullable
private PsiElement resolveFormatString() {
PyArgumentList argumentList = getArgumentList(getElement());
final PyArgumentList argumentList = getArgumentList(getElement());
if (argumentList != null) {
PyExpression[] arguments = argumentList.getArguments();
final PyExpression[] arguments = argumentList.getArguments();
if (myChunk.getMappingKey() != null) {
PyKeywordArgument argument = argumentList.getKeywordArgument(myChunk.getMappingKey());
PyExpression subStarExpression = getSubStarExpression(arguments);
final PyKeywordArgument argument = argumentList.getKeywordArgument(myChunk.getMappingKey());
final PyExpression subStarExpression = getSubStarExpression(arguments);
return ObjectUtils.chooseNotNull(argument, subStarExpression);
}
else {
int position = myChunk.getPosition() == null ? myPosition : myChunk.getPosition();
final int position = myChunk.getPosition() == null ? myPosition : myChunk.getPosition();
if (arguments.length == 1 && arguments[0] instanceof PyStarArgument) {
return arguments[0];
}
@@ -96,20 +93,20 @@ public class PySubstitutionChunkReference extends PsiReferenceBase<PyStringLiter
}
private PsiElement resolvePercentString() {
PyBinaryExpression binaryExpression = PsiTreeUtil.getParentOfType(getElement(), PyBinaryExpression.class);
final PyBinaryExpression binaryExpression = PsiTreeUtil.getParentOfType(getElement(), PyBinaryExpression.class);
final Class[] SIMPLE_RESULT_EXPRESSIONS = {
PyLiteralExpression.class, PySubscriptionExpression.class, PyBinaryExpression.class, PyConditionalExpression.class,
PyCallExpression.class, PySliceExpression.class, PyReferenceExpression.class
};
if (binaryExpression != null) {
PyExpression rightExpression = binaryExpression.getRightExpression();
final PyExpression rightExpression = binaryExpression.getRightExpression();
if (rightExpression instanceof PyParenthesizedExpression) {
PyParenthesizedExpression expression = (PyParenthesizedExpression)rightExpression;
PyExpression containedExpression = expression.getContainedExpression();
final PyParenthesizedExpression expression = (PyParenthesizedExpression)rightExpression;
final PyExpression containedExpression = expression.getContainedExpression();
if (containedExpression instanceof PyTupleExpression ) {
PyExpression[] elements = ((PySequenceExpression)containedExpression).getElements();
final PyExpression[] elements = ((PySequenceExpression)containedExpression).getElements();
if (elements.length > myPosition) {
return elements[myPosition];
}
@@ -117,9 +114,9 @@ public class PySubstitutionChunkReference extends PsiReferenceBase<PyStringLiter
}
else if (rightExpression instanceof PyDictLiteralExpression) {
if (myChunk.getMappingKey() != null) {
PyKeyValueExpression[] keyValueExpressions = ((PyDictLiteralExpression)rightExpression).getElements();
final PyKeyValueExpression[] keyValueExpressions = ((PyDictLiteralExpression)rightExpression).getElements();
for (PyKeyValueExpression keyValueExpression: keyValueExpressions) {
PyStringLiteralExpression key = (PyStringLiteralExpression)keyValueExpression.getKey();
final PyStringLiteralExpression key = (PyStringLiteralExpression)keyValueExpression.getKey();
if (key.getStringValue().equals(myChunk.getMappingKey())) {
return key;
}
@@ -134,8 +131,8 @@ public class PySubstitutionChunkReference extends PsiReferenceBase<PyStringLiter
}
@Nullable
private static PyArgumentList getArgumentList(PsiElement original) {
PsiElement pyReferenceExpression = PsiTreeUtil.getParentOfType(original, PyReferenceExpression.class);
private static PyArgumentList getArgumentList(final PsiElement original) {
final PsiElement pyReferenceExpression = PsiTreeUtil.getParentOfType(original, PyReferenceExpression.class);
return PsiTreeUtil.getNextSiblingOfType(pyReferenceExpression, PyArgumentList.class);
}
@@ -21,7 +21,7 @@ import com.intellij.psi.PsiReferenceProvider;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ProcessingContext;
import com.jetbrains.python.inspections.PyStringFormatParser;
import com.jetbrains.python.psi.StringLiteralExpression;
import com.jetbrains.python.psi.PyStringLiteralExpression;
import org.jetbrains.annotations.NotNull;
import java.util.List;
@@ -29,31 +29,31 @@ import java.util.List;
public class PythonFormattedStringReferenceProvider extends PsiReferenceProvider {
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
PsiReference[] referencesFromFormatString = getReferencesFromFormatString((StringLiteralExpression)element);
PsiReference[] referencesFromPercentString = getReferencesFromPercentString((StringLiteralExpression)element);
public PsiReference[] getReferencesByElement(@NotNull final PsiElement element, @NotNull final ProcessingContext context) {
final PsiReference[] referencesFromFormatString = getReferencesFromFormatString((PyStringLiteralExpression)element);
final PsiReference[] referencesFromPercentString = getReferencesFromPercentString((PyStringLiteralExpression)element);
return ArrayUtil.mergeArrays(referencesFromFormatString, referencesFromPercentString);
}
private static PsiReference[] getReferencesFromFormatString(@NotNull StringLiteralExpression element) {
List<PyStringFormatParser.SubstitutionChunk> chunks = PyStringFormatParser.filterSubstitutions(
private static PsiReference[] getReferencesFromFormatString(@NotNull final PyStringLiteralExpression element) {
final List<PyStringFormatParser.SubstitutionChunk> chunks = PyStringFormatParser.filterSubstitutions(
PyStringFormatParser.parseNewStyleFormat(element.getStringValue()));
return getReferencesFromChunks(element, chunks);
}
private static PsiReference[] getReferencesFromPercentString(@NotNull StringLiteralExpression element) {
List<PyStringFormatParser.SubstitutionChunk>
private static PsiReference[] getReferencesFromPercentString(@NotNull final PyStringLiteralExpression element) {
final List<PyStringFormatParser.SubstitutionChunk>
chunks = PyStringFormatParser.filterSubstitutions(PyStringFormatParser.parsePercentFormat(element.getStringValue()));
return getReferencesFromChunks(element, chunks);
}
@NotNull
private static PsiReference[] getReferencesFromChunks(@NotNull StringLiteralExpression element,
List<PyStringFormatParser.SubstitutionChunk> chunks) {
PsiReference[] result = new PsiReference[chunks.size()];
private static PsiReference[] getReferencesFromChunks(@NotNull final PyStringLiteralExpression element,
@NotNull final List<PyStringFormatParser.SubstitutionChunk> chunks) {
final PsiReference[] result = new PsiReference[chunks.size()];
for (int i = 0; i < chunks.size(); i++) {
PyStringFormatParser.SubstitutionChunk chunk = chunks.get(i);
final PyStringFormatParser.SubstitutionChunk chunk = chunks.get(i);
result[i] = new PySubstitutionChunkReference(element, chunk, i);
}
return result;
@@ -64,15 +64,15 @@ public class PyStringFormatCompletionContributor extends CompletionContributor {
final PyExpression[] arguments = getFormatFunctionKeyWordArguments(original);
for (PyExpression argument : arguments) {
result = result.withPrefixMatcher(getPrefix(parameters.getOffset(), argument.getContainingFile()));
tryToAddKeysFromStarArgument(result, argument);
tryToAddKeyWordArgument(result, argument);
addKeysFromStarArgument(result, argument);
addKeyWordArgument(result, argument);
}
}
}
else if (PyUtil.instanceOf(parent, PyKeywordArgument.class, PyReferenceExpression.class)) {
final PyArgumentList argumentList = PsiTreeUtil.getParentOfType(original, PyArgumentList.class);
result = result.withPrefixMatcher(getPrefix(parameters.getOffset(), parent.getContainingFile()));
tryToAddElementsFromFormattedString(result, argumentList);
addElementsFromFormattedString(result, argumentList);
}
}
}
@@ -98,7 +98,7 @@ public class PyStringFormatCompletionContributor extends CompletionContributor {
return PyExpression.EMPTY_ARRAY;
}
private static void tryToAddKeysFromStarArgument(@NotNull final CompletionResultSet result, @NotNull final PyExpression arg) {
private static void addKeysFromStarArgument(@NotNull final CompletionResultSet result, @NotNull final PyExpression arg) {
if (arg instanceof PyStarArgument) {
final PyDictLiteralExpression dict = ObjectUtils.chooseNotNull(PsiTreeUtil.getChildOfType(arg, PyDictLiteralExpression.class),
getDictFromReference(arg));
@@ -125,7 +125,7 @@ public class PyStringFormatCompletionContributor extends CompletionContributor {
return null;
}
private static void tryToAddKeyWordArgument(@NotNull final CompletionResultSet result, @NotNull final PyExpression arg) {
private static void addKeyWordArgument(@NotNull final CompletionResultSet result, @NotNull final PyExpression arg) {
if (arg instanceof PyKeywordArgument) {
final String keyword = ((PyKeywordArgument)arg).getKeyword();
if (keyword!= null) {
@@ -142,8 +142,8 @@ public class PyStringFormatCompletionContributor extends CompletionContributor {
.withAutoCompletionPolicy(AutoCompletionPolicy.ALWAYS_AUTOCOMPLETE);
}
private static void tryToAddElementsFromFormattedString(@NotNull final CompletionResultSet result,
@Nullable final PyArgumentList argumentList) {
private static void addElementsFromFormattedString(@NotNull final CompletionResultSet result,
@Nullable final PyArgumentList argumentList) {
if (argumentList != null) {
final PyReferenceExpression pyReferenceExpression = PsiTreeUtil.getPrevSiblingOfType(argumentList, PyReferenceExpression.class);
final PyStringLiteralExpression formattedString = PsiTreeUtil.getChildOfType(pyReferenceExpression, PyStringLiteralExpression.class);