mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-51686: Overloadded access to arrays
This commit is contained in:
+20
-16
@@ -17,9 +17,9 @@
|
||||
package org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.path;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.psi.CommonClassNames;
|
||||
import com.intellij.psi.PsiArrayType;
|
||||
import com.intellij.psi.PsiType;
|
||||
import com.intellij.psi.CommonClassNames;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -27,9 +27,9 @@ import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.GrExpressionImpl;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType;
|
||||
|
||||
/**
|
||||
* @author ilyas
|
||||
@@ -64,13 +64,26 @@ public class GrIndexPropertyImpl extends GrExpressionImpl implements GrIndexProp
|
||||
PsiType thisType = selected.getType();
|
||||
|
||||
if (thisType != null) {
|
||||
if (thisType instanceof PsiArrayType) {
|
||||
PsiType componentType = ((PsiArrayType)thisType).getComponentType();
|
||||
return TypesUtil.boxPrimitiveType(componentType, getManager(), getResolveScope());
|
||||
}
|
||||
|
||||
GrArgumentList argList = getArgumentList();
|
||||
if (argList != null) {
|
||||
GrExpression[] arguments = argList.getExpressionArguments();
|
||||
PsiType[] argTypes = new PsiType[arguments.length];
|
||||
for (int i = 0; i < arguments.length; i++) {
|
||||
PsiType argType = arguments[i].getType();
|
||||
if (argType == null) argType = TypesUtil.getJavaLangObject(argList);
|
||||
argTypes[i] = argType;
|
||||
}
|
||||
|
||||
final PsiType overloadedOperatorType = TypesUtil.getOverloadedOperatorType(thisType, "getAt", this, argTypes);
|
||||
if (overloadedOperatorType!=null) {
|
||||
return overloadedOperatorType;
|
||||
}
|
||||
|
||||
if (thisType instanceof PsiArrayType) {
|
||||
PsiType componentType = ((PsiArrayType)thisType).getComponentType();
|
||||
return TypesUtil.boxPrimitiveType(componentType, getManager(), getResolveScope());
|
||||
}
|
||||
|
||||
if (thisType instanceof GrTupleType) {
|
||||
PsiType[] types = ((GrTupleType)thisType).getParameters();
|
||||
return types.length == 1 ? types[0] : null;
|
||||
@@ -84,15 +97,6 @@ public class GrIndexPropertyImpl extends GrExpressionImpl implements GrIndexProp
|
||||
if (InheritanceUtil.isInheritor(thisType, CommonClassNames.JAVA_UTIL_MAP)) {
|
||||
return PsiUtil.substituteTypeParameter(thisType, CommonClassNames.JAVA_UTIL_MAP, 1, true);
|
||||
}
|
||||
GrExpression[] arguments = argList.getExpressionArguments();
|
||||
PsiType[] argTypes = new PsiType[arguments.length];
|
||||
for (int i = 0; i < arguments.length; i++) {
|
||||
PsiType argType = arguments[i].getType();
|
||||
if (argType == null) argType = TypesUtil.getJavaLangObject(argList);
|
||||
argTypes[i] = argType;
|
||||
}
|
||||
|
||||
return TypesUtil.getOverloadedOperatorType(thisType, "getAt", this, argTypes);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -203,4 +203,6 @@ public class GroovyHighlightingTest extends LightCodeInsightFixtureTestCase {
|
||||
public void testStringAndGStringUpperBound() throws Exception {doTest();}
|
||||
|
||||
public void testWithMethod() throws Exception {doTest();}
|
||||
|
||||
public void testArrayLikeAccess() throws Exception {doTest();}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
def foo = [1, 2, 5]
|
||||
def bar = [00, 11, 22, 33, 44, 55, 66, 77, 88]
|
||||
|
||||
// highlights right side as 'Can not assign Integer to Collection'
|
||||
Collection<Integer> baz = bar[foo]
|
||||
assert baz == [11, 22, 55]
|
||||
|
||||
// highlights right side as 'Can not assign Integer to Collection'
|
||||
Collection<Integer> qux = bar[[1, 2, 5]]
|
||||
assert qux == [11, 22, 55]
|
||||
|
||||
// accepted as correct
|
||||
Collection<Integer> quux = [bar[1], bar[5], bar[2]]
|
||||
assert quux == [11, 22, 55]
|
||||
|
||||
// highlights right side as 'Can not assign Integer to Collection'
|
||||
|
||||
Collection<Integer> quuux = bar[2..5]
|
||||
assert quuux == [22, 33, 44, 55]
|
||||
Reference in New Issue
Block a user