Skip to content

Commit

Permalink
Fix: do not suggest instance methods on a class reference
Browse files Browse the repository at this point in the history
  • Loading branch information
klesun committed Feb 15, 2019
1 parent f2b453e commit 56ab8c5
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.lang.psi.elements.*;
import com.jetbrains.php.lang.psi.elements.impl.ClassReferenceImpl;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import org.jetbrains.annotations.NotNull;
import org.klesun.deep_assoc_completion.contexts.ExprCtx;
import org.klesun.deep_assoc_completion.contexts.FuncCtx;
import org.klesun.deep_assoc_completion.contexts.IExprCtx;
import org.klesun.deep_assoc_completion.contexts.SearchCtx;
import org.klesun.deep_assoc_completion.structures.DeepType;
import org.klesun.deep_assoc_completion.helpers.*;
import org.klesun.deep_assoc_completion.helpers.Mt;
import org.klesun.deep_assoc_completion.resolvers.ArrCtorRes;
import org.klesun.deep_assoc_completion.resolvers.UsageResolver;
import org.klesun.deep_assoc_completion.structures.DeepType;
import org.klesun.lang.It;
import org.klesun.lang.L;
import org.klesun.lang.*;

import java.util.HashSet;
Expand Down Expand Up @@ -97,21 +100,22 @@ private static Opt<LookupElement> makeMagicLookup(DeepType t)
.withTypeText("from __get()"));
}

private It<? extends LookupElement> resolveObj(PhpExpression ref, FuncCtx funcCtx)
private It<? extends LookupElement> resolveObj(PhpExpression ref, FuncCtx funcCtx, boolean isStaticRef)
{
Mt mt = funcCtx.findExprType(ref).wap(Mt::new);
return ArrCtorRes.resolveMtCls(mt, ref.getProject())
.fap(cls -> list(
It(cls.getMethods()).flt(m -> !m.getName().startsWith("__")),
It(cls.getFields())
).fap(a -> a)
.flt(fld -> fld.getModifier().isPublic()
|| ref.getText().equals("$this"))
.flt(fld -> !fld.getModifier().isStatic())
.fap(cls -> {
It<? extends PhpClassMember> mems = list(
It(cls.getMethods()).flt(m -> !m.getName().startsWith("__")),
It(cls.getFields())
).fap(a -> a);
return mems
.flt(fld -> fld.getModifier().isPublic())
.flt(fld -> isStaticRef == fld.getModifier().isStatic())
.map(m -> makeLookup(m))
.cct(getMagicProps(cls, funcCtx)
.fap(t -> makeMagicLookup(t)))
).cct(getAssignedProps(mt).map(a -> a));
.fap(t -> makeMagicLookup(t)));
}).cct(getAssignedProps(mt).map(a -> a));
}

@Override
Expand All @@ -127,13 +131,17 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi

Dict<Long> times = new Dict<>(list());
It<? extends LookupElement> options = It(list());
if (builtIns.size() == 0 || !parameters.isAutoPopup()) {
Boolean hasBuiltIns = builtIns.any(b -> !"class".equals(b.getLookupString()));
if (!hasBuiltIns || !parameters.isAutoPopup()) {
FuncCtx funcCtx = new FuncCtx(search);
// IDEA did not resolve the class on it's own - worth trying Deep resolution
options = opt(parameters.getPosition().getParent())
.fop(toCast(MemberReference.class))
.map(mem -> mem.getClassReference())
// IDEA did not resolve the class on it's own - worth trying Deep resolution
.fap(ref -> resolveObj(ref, funcCtx));
.fap(mem -> opt(mem.getClassReference())
// no point in deep resolution if it's an explicit class
.flt(objRef -> !(objRef instanceof ClassReferenceImpl)
|| objRef.getText().equals("$this"))
.fap(ref -> resolveObj(ref, funcCtx, mem.isStatic())));
times.put("iteratorDone", System.nanoTime() - startTime);
}
Set<String> suggested = new HashSet<>(builtIns.map(l -> l.getLookupString()).arr());
Expand Down
9 changes: 5 additions & 4 deletions src/org/klesun/deep_assoc_completion/contexts/SearchCtx.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,12 @@ public Iterable<DeepType> findExprType(PhpExpression expr, ExprCtx exprCtx)
}
}

/** @debug */
//System.out.println("zalupa expr " + expr.getText() + " self is " + result.fap(a -> a).rdc((sum,el) -> sum + "|" +
// el.clsRefType.map(t -> t.toString()).def("(no cls)"), "") + " trace: " + funcCtx.func());

return It(result.def(It.non()))
.btw(t -> {
if (debug) {
System.out.println(Tls.repeat(" ", exprCtx.depth) + "| " + Tls.singleLine(expr.getText(), 60) + " | " + t);
}
})
.thn(cnt -> exprCtx.typeCnt = som(cnt))
;
}
Expand Down
6 changes: 3 additions & 3 deletions src/org/klesun/deep_assoc_completion/structures/DeepType.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,14 @@ public static String varExport(List<DeepType> types, int level, Set<DeepType> ci
public Opt<String> getBriefVal()
{
String typeInfo = null;
if (stringValue != null) {
if (clsRefType.has()) {
typeInfo = clsRefType.unw() + "::class";
} else if (stringValue != null) {
typeInfo = "'" + stringValue + "'";
} else if (keys.has()) {
typeInfo = "[" + keys.fap(k -> k.getBriefKey()).unq().str() + "]";
} else if (returnTypeGetters.has()) {
typeInfo = "(...) ==> {...}";
} else if (clsRefType.has()) {
typeInfo = clsRefType.unw() + "::class";
} else if (props.size() > 0) {
typeInfo = "obj(" + props.kys().str() + ")";
}
Expand Down
9 changes: 9 additions & 0 deletions src/org/klesun/lang/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ public interface C2<Tin1, Tin2> extends BiConsumer<Tin1, Tin2> {}
// following methods are supposed to be used after extending
// this class to be able to use them without `new ` or `ClassName.`

public static String repeat(String str, int n)
{
String result = "";
for (int i = 0; i < n; ++i) {
result += str;
}
return result;
}

public static String substr(String str, int startIndex, int endIndex)
{
if (startIndex < 0) {
Expand Down
7 changes: 6 additions & 1 deletion tests/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -1165,4 +1165,9 @@ function __construct(array $options = array())
preg_match_all('/asd/', 'asd', $matches, '');
simplexml_load_string('<root/>', null, '2.9.1');
simplexml_load_file('<root/>', null, '0');
preg_last_error() === '';
preg_last_error() === '';
// should not suggest: 'hates', 'getAngry'
Blanc::;
(new Blanc())->;
$blancCls = Blanc::class;
(new $blancCls)->hates;
17 changes: 17 additions & 0 deletions tests/src/NeptuniaNs/Blanc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
class Blanc {
private $loves = ['Rom', 'Ram'];
protected $likes = ['Neptune', 'B-Sha'];
public $hates = ['Vert'];
public static $dream = 'Big Boobs';

public function getAngry()
{
$this->hates[] = self::beAngry();
}

public static function beAngry()
{
return 'everyone';
}
}

0 comments on commit 56ab8c5

Please sign in to comment.