Skip to content

Commit

Permalink
Fixed autocompletion on instance members (fixes #108)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertlatacz committed Dec 2, 2016
1 parent a389c3c commit c8619d6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
10 changes: 6 additions & 4 deletions src/javarepl/completion/InstanceMemberCompleter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.googlecode.totallylazy.Option;
import com.googlecode.totallylazy.Sequence;
import com.googlecode.totallylazy.Sequences;
import com.googlecode.totallylazy.reflection.Types;
import javarepl.Evaluator;
import javarepl.reflection.ClassReflection;
import javarepl.reflection.MemberReflection;
Expand Down Expand Up @@ -33,16 +34,17 @@ public CompletionResult call(String expression) throws Exception {
final Boolean canComplete = packagePart.matches("[a-zA-Z0-9\\$_\\\\.\\(\\[\\]]*") && packagePart.contains(".");

final int beginIndex = packagePart.lastIndexOf('.') + 1;
Option<Type> aClass = canComplete
? evaluator.typeOfExpression(packagePart.substring(0, beginIndex - 1))
: none(Type.class);
Option<Class<?>> aClass = canComplete
? evaluator.typeOfExpression(packagePart.substring(0, beginIndex - 1)).map(Types::classOf)
: none();

if (aClass.isDefined()) {
ClassReflection classReflection = reflectionOf(aClass.get());

Sequence<MemberReflection> join = Sequences.empty(MemberReflection.class)
.join(classReflection.declaredFields())
.join(classReflection.declaredMethods());
.join(classReflection.declaredMethods())
.unique();

Sequence<CompletionCandidate> candidates = join
.filter(isPublic().and(not(isStatic())))
Expand Down
17 changes: 11 additions & 6 deletions test/javarepl/completion/InstanceMemberCompleterTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package javarepl.completion;

import javarepl.completion.helpers.SimpleCompletionInstance;
import org.junit.Test;

import static com.googlecode.totallylazy.Sequences.sequence;
import static javarepl.completion.CompleterTestHelper.*;
import static org.junit.Assert.assertThat;

public class InstanceMemberCompleterTest {

@Test
public void shouldCompleteStaticMembers() {
InstanceMemberCompleter completer = new InstanceMemberCompleter(evaluator());
String expression = TestOuterClass.class.getCanonicalName() + ".staticField.";
public void shouldCompleteInstance() {
String expression = "ci.over";
String instanceClassName = SimpleCompletionInstance.class.getCanonicalName();
InstanceMemberCompleter completer = new InstanceMemberCompleter(evaluator("ci = new "+instanceClassName+"()"));

// CompletionResult result = completer.apply(expression);
// assertThat(result,
// completesTo(candidates("staticField", "staticMethod("), position(expression.length())));
CompletionResult result = completer.apply(expression);

assertThat(result,
completesTo(candidates(new CompletionCandidate("overloadedMethod(", sequence("int overloadedMethod()", "int overloadedMethod(int)", "int overloadedMethod(int, int)"))),
position(3)));

assertThat(completer.apply("invalid"), completesTo(candidatesValues(), position(0)));
}
Expand Down
32 changes: 32 additions & 0 deletions test/javarepl/completion/helpers/SimpleCompletionInstance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package javarepl.completion.helpers;


import java.util.Date;


public class SimpleCompletionInstance {
public void voidMethod() {
}


public int primitiveTypeReturningMethod() {
return 0;
}

public Date complexTypeReturningMethod() {
return new Date();
}

public int overloadedMethod() {
return 0;
}

public int overloadedMethod(int param) {
return param;
}

public int overloadedMethod(int param1, int param2) {
return param1 + param2;
}

}

0 comments on commit c8619d6

Please sign in to comment.