Skip to content

Commit

Permalink
Provide completion of array_column key name argument
Browse files Browse the repository at this point in the history
  • Loading branch information
klesun committed Aug 29, 2017
1 parent cc3216e commit dcd1b97
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 22 deletions.
84 changes: 84 additions & 0 deletions src/org/klesun/deep_keys/entry/ArrayColumnPvdr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.klesun.deep_keys.entry;

import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.PhpIcons;
import com.jetbrains.php.lang.psi.elements.*;
import com.jetbrains.php.lang.psi.elements.impl.FunctionReferenceImpl;
import org.jetbrains.annotations.NotNull;
import org.klesun.deep_keys.DeepType;
import org.klesun.deep_keys.helpers.FuncCtx;
import org.klesun.deep_keys.helpers.IFuncCtx;
import org.klesun.deep_keys.helpers.MultiType;
import org.klesun.deep_keys.helpers.SearchContext;

import java.util.HashSet;
import java.util.Set;

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

/**
* provides completion in `array_column($segments, '')`
*/
public class ArrayColumnPvdr extends CompletionProvider<CompletionParameters>
{
private static LookupElement makeLookupBase(String keyName, String type)
{
return LookupElementBuilder.create(keyName)
.bold()
.withIcon(PhpIcons.FIELD)
.withTypeText(type);
}

private static LookupElement makeLookup(DeepType.Key keyEntry)
{
String type = keyEntry.getTypes().gat(0).map(t -> t.briefType.toString()).def("unknown");
return makeLookupBase(keyEntry.name, type);
}

private static L<LookupElement> makeOptions(MultiType mt)
{
L<LookupElement> result = L();
Set<String> suggested = new HashSet<>();
for (DeepType type: mt.types) {
for (DeepType.Key keyEntry: type.keys.values()) {
String key = keyEntry.name;
if (suggested.contains(key)) continue;
suggested.add(key);
result.add(makeLookup(keyEntry));
}
L(type.indexTypes).gat(0).flt(t -> type.keys.size() == 0).thn(t -> {
for (int k = 0; k < 10; ++k) {
result.add(makeLookupBase(k + "", t.briefType.toString()));
}
});
}
return result;
}

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext processingContext, @NotNull CompletionResultSet result)
{
SearchContext search = new SearchContext().setDepth(35);
IFuncCtx funcCtx = new FuncCtx(search, L());

long startTime = System.nanoTime();
MultiType mt = opt(parameters.getPosition().getParent())
.map(literal -> literal.getParent())
.map(literal -> literal.getParent())
.fap(toCast(FunctionReferenceImpl.class))
.flt(call -> "array_column".equals(call.getName()))
.fap(call -> L(call.getParameters()).gat(0))
.fap(toCast(PhpExpression.class))
.map(arr -> funcCtx.findExprType(arr).getEl())
.def(MultiType.INVALID_PSI);

makeOptions(mt).fch(result::addElement);
result.addLookupAdvertisement("Press <Page Down> few times to skip built-in suggestions");

long elapsed = System.nanoTime() - startTime;
System.out.println("Resolved in " + (elapsed / 1000000000.0) + " seconds");
}
}
8 changes: 8 additions & 0 deletions src/org/klesun/deep_keys/entry/DeepKeysCbtr.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,13 @@ public DeepKeysCbtr()
.withSuperParent(2, PhpDocParamTag.class),
new DocFqnPvdr()
);
this.extend(
CompletionType.BASIC,
PlatformPatterns.psiElement()
.withSuperParent(1, StringLiteralExpression.class)
//.withSuperParent(2, ParameterList.class)
,
new ArrayColumnPvdr()
);
}
}
2 changes: 1 addition & 1 deletion src/org/klesun/deep_keys/entry/DeepKeysGoToDecl.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static void removeDuplicates(List<PsiElement> psiTargets)
@Override
public PsiElement[] getGotoDeclarationTargets(PsiElement psiElement, int i, Editor editor)
{
SearchContext search = new SearchContext().setDepth(40);
SearchContext search = new SearchContext().setDepth(35);
IFuncCtx funcCtx = new FuncCtx(search, L());

List<PsiElement> psiTargets = new ArrayList<>();
Expand Down
2 changes: 1 addition & 1 deletion src/org/klesun/deep_keys/entry/DeepKeysPvdr.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static LookupElement makeLookup(DeepType.Key keyEntry, Project project)
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext processingContext, @NotNull CompletionResultSet result)
{
SearchContext search = new SearchContext().setDepth(40);
SearchContext search = new SearchContext().setDepth(35);
IFuncCtx funcCtx = new FuncCtx(search, L());

long startTime = System.nanoTime();
Expand Down
16 changes: 0 additions & 16 deletions src/org/klesun/deep_keys/entry/DocFqnPvdr.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,6 @@
*/
public class DocFqnPvdr extends CompletionProvider<CompletionParameters>
{
private static LookupElement makeLookup(DeepType.Key keyEntry, Project project)
{
LookupElementBuilder result = LookupElementBuilder.create(keyEntry.name)
.bold()
.withIcon(PhpIcons.FIELD);

if (keyEntry.getTypes().size() > 0) {
DeepType type = keyEntry.getTypes().get(0);
result = result.withTypeText(type.briefType.toString());
} else {
result = result.withTypeText("unknown");
}

return result;
}

private static String makeMethOrderValue(Method meth)
{
String result = "";
Expand Down
30 changes: 26 additions & 4 deletions tests/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ private static function testTupleAccess()
$restoredB[''];
}

//============================
// not implemented follow
//============================

private static function testLambdaAccess()
{
$subjects = self::makeRecord()['chosenSubSubjects'];
Expand All @@ -97,6 +93,32 @@ private static function testLambdaAccess()
$mapped[2][''];
}

private static function testArrayColumn()
{
$segments = [
['from' => 'MOW', 'to' => 'LON'],
['from' => 'LON', 'to' => 'PAR'],
['from' => 'PAR', 'to' => 'MOW'],
];
// should suggest: "from", "to"
$segments[0][''];
// should suggest: "from", "to"
array_column($segments, '');

$pnrs = [];
$pnrs[] = [
'ptcInfo' => ['ptc' => 'C05', 'quantity' => 1],
'paxes' => [['name' => 'vova', 'surname' => 'turman']],
'itinerary' => $segments,
];
array_column($pnrs, '');
array_column(array_column($pnrs, 'ptcInfo'), '');
}

//============================
// not implemented follow
//============================

private static function testGenericAccess()
{
$records = [];
Expand Down

0 comments on commit dcd1b97

Please sign in to comment.