Skip to content

Commit

Permalink
Go To actual string value when completing array created like that
Browse files Browse the repository at this point in the history
$arr[$varKey] = 123
  • Loading branch information
klesun committed Mar 26, 2018
1 parent 3c4516d commit 03f8027
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
34 changes: 26 additions & 8 deletions src/org/klesun/deep_assoc_completion/helpers/KeyType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,52 @@

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.klesun.deep_assoc_completion.DeepType;
import org.klesun.lang.Lang;

import static org.klesun.lang.Lang.list;
import static org.klesun.lang.Lang.opt;

/** is there actually a point in having this and MultiType as two separate classes? */
public class KeyType
{
public enum EKeyType {STRING, INTEGER, UNKNOWN}

final public EKeyType keyType;
final public @Nullable
Lang.L<String> names;
final private Lang.L<DeepType> types;

private KeyType(EKeyType keyType, @Nullable Lang.L<String> name)
private KeyType(EKeyType keyType, @NotNull Lang.L<DeepType> types)
{
this.keyType = keyType;
this.names = name;
this.types = types;
}

public static KeyType string(@NotNull Lang.L<String> name)
public static KeyType mt(MultiType mt)
{
return new KeyType(EKeyType.STRING, name);
return mt.getStringValues().size() > 0
? new KeyType(EKeyType.STRING, mt.types)
: (mt.isInt()
? KeyType.integer()
: KeyType.unknown());
}

public static KeyType integer()
{
return new KeyType(EKeyType.INTEGER, null);
return new KeyType(EKeyType.INTEGER, list());
}

public static KeyType unknown()
{
return new KeyType(EKeyType.UNKNOWN, null);
return new KeyType(EKeyType.UNKNOWN, list());
}

public Lang.L<String> getNames()
{
return new MultiType(types).getStringValues();
}

public Lang.Dict<Lang.L<DeepType>> getNameToMt()
{
return types.gop(t -> opt(t.stringValue));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ private static MultiType makeType(L<KeyType> keys, S<MultiType> getType, PsiElem
KeyType nextKey = keys.get(0);
L<KeyType> furtherKeys = keys.sub(1);
if (nextKey.keyType == KeyType.EKeyType.STRING) {
nextKey.names.fch(name ->
arr.addKey(name, psi).addType(() ->
makeType(furtherKeys, getType, psi, briefType), briefType));
nextKey.getNameToMt().fch((types, name) ->
types.fch(t -> arr.addKey(name, t.definition).addType(() ->
makeType(furtherKeys, getType, psi, briefType), briefType)));
} else if (nextKey.keyType == KeyType.EKeyType.INTEGER) {
arr.hasIntKeys = true;
arr.listElTypes = makeType(furtherKeys, getType, psi, briefType).types;
Expand Down Expand Up @@ -75,11 +75,7 @@ private Opt<T2<List<KeyType>, S<MultiType>>> collectKeyAssignment(AssignmentExpr
.map(index -> index.getValue())
.fop(toCast(PhpExpression.class))
.map(key -> ctx.findExprType(key))
.map(mt -> mt.getStringValues().size() > 0
? KeyType.string(mt.getStringValues())
: (mt.isInt()
? KeyType.integer()
: KeyType.unknown()))
.map(mt -> KeyType.mt(mt))
.def(KeyType.integer());
reversedKeys.add(name);

Expand Down
2 changes: 1 addition & 1 deletion tests/src/DeepTest/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1717,7 +1717,7 @@ public static function provideObjectInAKeyMethod()
foreach ($zhopa as $color) {
$colorToNum[$color] = rand(0,100);
}
$colorToNum['black'];
$colorToNum['green'];

$storRecord = [
'capacity' => '256mb',
Expand Down

0 comments on commit 03f8027

Please sign in to comment.