Search in sources :

Example 1 with KotlinVariableInfo

use of org.apache.zeppelin.kotlin.reflect.KotlinVariableInfo in project zeppelin by apache.

the class KotlinCompleter method completion.

public List<InterpreterCompletion> completion(String buf, int cursor, InterpreterContext interpreterContext) {
    if (ctx == null) {
        return new ArrayList<>(keywords);
    }
    List<InterpreterCompletion> result = new ArrayList<>();
    for (KotlinVariableInfo var : ctx.getVars()) {
        result.add(new InterpreterCompletion(var.getName(), var.getName(), shorten(var.getType())));
    }
    for (KotlinFunctionInfo fun : ctx.getFunctions()) {
        result.add(new InterpreterCompletion(fun.getName(), fun.getName(), fun.toString(true)));
    }
    result.addAll(keywords);
    return result;
}
Also used : InterpreterCompletion(org.apache.zeppelin.interpreter.thrift.InterpreterCompletion) KotlinVariableInfo(org.apache.zeppelin.kotlin.reflect.KotlinVariableInfo) KotlinFunctionInfo(org.apache.zeppelin.kotlin.reflect.KotlinFunctionInfo) ArrayList(java.util.ArrayList)

Example 2 with KotlinVariableInfo

use of org.apache.zeppelin.kotlin.reflect.KotlinVariableInfo in project zeppelin by apache.

the class KotlinInterpreterTest method testWrapper.

@Test
public void testWrapper() throws Exception {
    String code = "import org.jetbrains.kotlin.cli.common.repl.InvokeWrapper\n" + "var k = 0\n" + "val wrapper = object : InvokeWrapper {\n" + "    override operator fun <T> invoke(body: () -> T): T {\n" + "        println(\"START\")\n" + "        val result = body()\n" + "        println(\"END\")\n" + "        k = k + 1\n" + "        return result\n" + "    }\n" + "}\n" + "kc.setWrapper(wrapper)\n";
    interpreter.interpret(code, context);
    interpreter.interpret("println(\"hello!\")", context);
    List<KotlinVariableInfo> vars = interpreter.getVariables();
    for (KotlinVariableInfo v : vars) {
        if (v.getName().equals("k")) {
            assertEquals(1, v.getValue());
        }
    }
    InterpreterResult result = interpreter.interpret("kc.vars", context);
    assertTrue(result.message().get(0).getData().contains("k: kotlin.Int = 1"));
}
Also used : KotlinVariableInfo(org.apache.zeppelin.kotlin.reflect.KotlinVariableInfo) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Test(org.junit.Test)

Example 3 with KotlinVariableInfo

use of org.apache.zeppelin.kotlin.reflect.KotlinVariableInfo in project zeppelin by apache.

the class KotlinInterpreterTest method testVariables.

@Test
public void testVariables() throws Exception {
    interpreter.interpret("val x = 1", context);
    interpreter.interpret("val x = 2", context);
    List<KotlinVariableInfo> vars = interpreter.getVariables();
    assertEquals(2, vars.size());
    KotlinVariableInfo varX = vars.stream().filter(info -> info.getName().equals("x")).findFirst().orElseGet(() -> {
        Assert.fail();
        return null;
    });
    assertEquals(2, varX.getValue());
    assertEquals("kotlin.Int", varX.getType());
}
Also used : KotlinVariableInfo(org.apache.zeppelin.kotlin.reflect.KotlinVariableInfo) Test(org.junit.Test)

Aggregations

KotlinVariableInfo (org.apache.zeppelin.kotlin.reflect.KotlinVariableInfo)3 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)1 InterpreterCompletion (org.apache.zeppelin.interpreter.thrift.InterpreterCompletion)1 KotlinFunctionInfo (org.apache.zeppelin.kotlin.reflect.KotlinFunctionInfo)1