Skip to content

Commit

Permalink
Show used keys when you ask for arg description inside func declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
klesun committed Feb 4, 2018
1 parent 592fc72 commit 83bf1cb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
30 changes: 25 additions & 5 deletions src/org/klesun/deep_assoc_completion/entry/ShowDocs.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@
import com.intellij.ui.EditorTextField;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.ui.components.JBScrollPane;
import com.jetbrains.php.lang.psi.elements.Function;
import com.jetbrains.php.lang.psi.elements.Parameter;
import com.jetbrains.php.lang.psi.elements.ParameterList;
import com.jetbrains.php.lang.psi.elements.PhpExpression;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import org.klesun.deep_assoc_completion.DeepType;
import org.klesun.deep_assoc_completion.helpers.FuncCtx;
import org.klesun.deep_assoc_completion.helpers.FuncCtx;
import org.klesun.deep_assoc_completion.helpers.SearchContext;
import org.klesun.deep_assoc_completion.resolvers.KeyUsageResolver;
import org.klesun.lang.Lang;
import org.klesun.lang.Opt;
import org.klesun.lang.Tls;

import javax.swing.*;
import java.awt.*;
import java.util.List;

import static org.klesun.lang.Lang.L;
import static org.klesun.lang.Lang.opt;
import static org.klesun.lang.Lang.*;

public class ShowDocs extends AnAction
{
Expand All @@ -32,9 +37,24 @@ public static List<DeepType> findPsiType(PsiElement psi)
SearchContext search = new SearchContext().setDepth(20);
FuncCtx funcCtx = new FuncCtx(search);

return Tls.cast(PhpExpression.class, psi)
.map(expr -> funcCtx.findExprType(expr).types)
.def(Lang.list());
return list(
Tls.cast(PhpExpression.class, psi)
.fap(expr -> funcCtx.findExprType(expr).types),
Tls.cast(Parameter.class, psi)
.fap(par -> {
int order = opt(par.getParent()).fop(toCast(ParameterList.class))
.map(lst -> L(lst.getParameters()).indexOf(par)).def(-1);
return opt(par.getParent())
.map(lst -> lst.getParent())
.fop(toCast(Function.class))
.fap(func -> {
DeepType arrt = new DeepType(par, PhpType.ARRAY);
L<String> keys = new KeyUsageResolver(funcCtx).resolveArgUsedKeys(func, order);
keys.fch(k -> arrt.addKey(k, psi));
return list(arrt);
});
})
).fap(a -> a);
}

public void actionPerformed(AnActionEvent e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static L<ArrayIndex> findUsedIndexes(Function meth, String varName)
.map(varUsage -> acc.getIndex()));
}

private L<String> resolveArgUsedKeys(Function meth, int argOrder)
public L<String> resolveArgUsedKeys(Function meth, int argOrder)
{
return L(meth.getParameters()).gat(argOrder)
.fop(toCast(ParameterImpl.class))
Expand Down Expand Up @@ -78,7 +78,7 @@ private static Opt<Function> resolveFunc(ParameterList argList)
)) .fop(toCast(Function.class)));
}

private static L<Variable> findVarReferences(Variable caretVar)
private static L<Variable> findVarReferences(PhpNamedElement caretVar)
{
return Tls.findParent(caretVar, Function.class, a -> true)
.fap(meth -> Tls.findChildren(
Expand All @@ -104,7 +104,7 @@ private L<String> findKeysUsedOnExpr(PhpExpression arrCtor)
});
}

private L<String> findKeysUsedOnVar(Variable caretVar)
private L<String> findKeysUsedOnVar(PhpNamedElement caretVar)
{
return findVarReferences(caretVar)
.flt(ref -> ref.getTextOffset() > caretVar.getTextOffset())
Expand Down
2 changes: 2 additions & 0 deletions tests/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,8 @@ private static function testBriefValueBuiltInFuncBug($split)
$result = [
'type' => 'flight',
'couponNumber' => intval($split['D']),
'from' => intval($split['F']),
'to' => intval($split['T']),
];
$result['']; // 'couponNumber' brief value should be `intval($split['D'])`, not `function intval($var, $base = null) {}`
}
Expand Down

0 comments on commit 83bf1cb

Please sign in to comment.