Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions spark/src/main/java/org/apache/zeppelin/spark/SparkInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@
import scala.collection.Iterator;
import scala.collection.JavaConversions;
import scala.collection.JavaConverters;
import scala.collection.convert.WrapAsJava;
import scala.collection.Seq;
import scala.collection.convert.WrapAsJava$;
import scala.collection.convert.WrapAsScala;
import scala.collection.mutable.HashMap;
import scala.collection.mutable.HashSet;
import scala.reflect.io.AbstractFile;
Expand Down Expand Up @@ -114,7 +112,7 @@ public class SparkInterpreter extends Interpreter {
/**
* completer - org.apache.spark.repl.SparkJLineCompletion (scala 2.10)
*/
private Object completer;
private Object completer = null;

private Map<String, Object> binder;
private SparkVersion sparkVersion;
Expand Down Expand Up @@ -723,11 +721,25 @@ public void open() {
logger.error(e.getMessage(), e);
}
}
}

if (Utils.findClass("org.apache.spark.repl.SparkJLineCompletion", true) != null) {
completer = Utils.instantiateClass(
"org.apache.spark.repl.SparkJLineCompletion",
new Class[]{Utils.findClass("org.apache.spark.repl.SparkIMain")},
new Object[]{intp});
} else if (Utils.findClass(
"scala.tools.nsc.interpreter.PresentationCompilerCompleter", true) != null) {
completer = Utils.instantiateClass(
"scala.tools.nsc.interpreter.PresentationCompilerCompleter",
new Class[]{ IMain.class },
new Object[]{ intp });
} else if (Utils.findClass(
"scala.tools.nsc.interpreter.JLineCompletion", true) != null) {
completer = Utils.instantiateClass(
"scala.tools.nsc.interpreter.JLineCompletion",
new Class[]{ IMain.class },
new Object[]{ intp });
}

if (Utils.isSpark2()) {
Expand Down Expand Up @@ -906,6 +918,11 @@ private List<File> classPath(ClassLoader cl) {

@Override
public List<InterpreterCompletion> completion(String buf, int cursor) {
if (completer == null) {
logger.warn("Can't find completer");
return new LinkedList<InterpreterCompletion>();
}

if (buf.length() < cursor) {
cursor = buf.length();
}
Expand All @@ -914,22 +931,18 @@ public List<InterpreterCompletion> completion(String buf, int cursor) {
completionText = "";
cursor = completionText.length();
}
if (Utils.isScala2_10()) {
ScalaCompleter c = (ScalaCompleter) Utils.invokeMethod(completer, "completer");
Candidates ret = c.complete(completionText, cursor);

List<String> candidates = WrapAsJava$.MODULE$.seqAsJavaList(ret.candidates());
List<InterpreterCompletion> completions = new LinkedList<InterpreterCompletion>();
ScalaCompleter c = (ScalaCompleter) Utils.invokeMethod(completer, "completer");
Candidates ret = c.complete(completionText, cursor);

for (String candidate : candidates) {
completions.add(new InterpreterCompletion(candidate, candidate));
}
List<String> candidates = WrapAsJava$.MODULE$.seqAsJavaList(ret.candidates());
List<InterpreterCompletion> completions = new LinkedList<InterpreterCompletion>();

return completions;
} else {
return new LinkedList<InterpreterCompletion>();
for (String candidate : candidates) {
completions.add(new InterpreterCompletion(candidate, candidate));
}

return completions;
}

private String getCompletionTargetString(String text, int cursor) {
Expand Down
8 changes: 7 additions & 1 deletion spark/src/main/java/org/apache/zeppelin/spark/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ static Object invokeStaticMethod(Class c, String name) {
}

static Class findClass(String name) {
return findClass(name, false);
}

static Class findClass(String name, boolean silence) {
try {
return Utils.class.forName(name);
} catch (ClassNotFoundException e) {
logger.error(e.getMessage(), e);
if (!silence) {
logger.error(e.getMessage(), e);
}
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@

import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.File;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import org.apache.spark.SparkConf;
import org.apache.spark.SparkContext;
import org.apache.spark.repl.SparkILoop;
import org.apache.zeppelin.display.AngularObjectRegistry;
import org.apache.zeppelin.interpreter.thrift.InterpreterCompletion;
import org.apache.zeppelin.resource.LocalResourcePool;
import org.apache.zeppelin.resource.WellKnownResourceName;
import org.apache.zeppelin.user.AuthenticationInfo;
Expand All @@ -42,7 +42,6 @@
import org.junit.runners.MethodSorters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.tools.nsc.interpreter.IMain;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SparkInterpreterTest {
Expand Down Expand Up @@ -282,4 +281,10 @@ public void testDisableImplicitImport() {
assertEquals(Code.ERROR, repl2.interpret(ddl, context).code());
repl2.close();
}

@Test
public void testCompletion() {
List<InterpreterCompletion> completions = repl.completion("sc.", "sc.".length());
assertTrue(completions.size() > 0);
}
}