-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Completion base support #22
Changes from 1 commit
4c9653e
5d6a8f5
e34eaac
09fcac7
d5abdf2
32e1bf5
5996792
1672771
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jetbrains.kotlin.jupyter.repl.building | ||
|
||
import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration | ||
import java.io.File | ||
import java.util.ArrayList | ||
import java.util.Collections | ||
import java.util.HashSet | ||
import org.jetbrains.kotlin.jupyter.repl.context.KotlinReceiver | ||
|
||
/** | ||
* Class that holds properties for Kotlin REPL creation, | ||
* namely implicit receiver, classpath, preloaded code, directory for class bytecode output, | ||
* max result limit and shortening types flag. | ||
* | ||
* Set its parameters by chaining corresponding methods, e.g. | ||
* properties.outputDir(dir).shortenTypes(false) | ||
* | ||
* Get its parameters via getters. | ||
*/ | ||
class KotlinReplProperties { | ||
|
||
val hostConf = defaultJvmScriptingHostConfiguration | ||
|
||
var receiver: KotlinReceiver? = null | ||
private set | ||
private val classpath: MutableSet<String> | ||
private val codeOnLoad: MutableList<String> | ||
var outputDir: String? = null | ||
private set | ||
var maxResult = 1000 | ||
private set | ||
var shortenTypes = true | ||
private set | ||
|
||
init { | ||
this.receiver = KotlinReceiver() | ||
|
||
this.classpath = HashSet() | ||
val javaClasspath = System.getProperty("java.class.path").split(File.pathSeparator.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | ||
Collections.addAll(classpath, *javaClasspath) | ||
|
||
this.codeOnLoad = ArrayList() | ||
} | ||
|
||
fun receiver(receiver: KotlinReceiver): KotlinReplProperties { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need in setter methods, all fields in Kotlin are properties and optional getters/setters can be added if needed (https://kotlinlang.org/docs/reference/properties.html) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I know this, but this was auto-converted from Java :) |
||
this.receiver = receiver | ||
return this | ||
} | ||
|
||
fun classPath(path: String): KotlinReplProperties { | ||
this.classpath.add(path) | ||
return this | ||
} | ||
|
||
fun classPath(paths: Collection<String>): KotlinReplProperties { | ||
this.classpath.addAll(paths) | ||
return this | ||
} | ||
|
||
fun codeOnLoad(code: String): KotlinReplProperties { | ||
this.codeOnLoad.add(code) | ||
return this | ||
} | ||
|
||
fun codeOnLoad(code: Collection<String>): KotlinReplProperties { | ||
this.codeOnLoad.addAll(code) | ||
return this | ||
} | ||
|
||
fun outputDir(outputDir: String): KotlinReplProperties { | ||
this.outputDir = outputDir | ||
return this | ||
} | ||
|
||
fun maxResult(maxResult: Int): KotlinReplProperties { | ||
this.maxResult = maxResult | ||
return this | ||
} | ||
|
||
fun shortenTypes(shortenTypes: Boolean): KotlinReplProperties { | ||
this.shortenTypes = shortenTypes | ||
return this | ||
} | ||
|
||
fun getClasspath(): Set<String> { | ||
return classpath | ||
} | ||
|
||
fun getCodeOnLoad(): List<String> { | ||
return codeOnLoad | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jetbrains.kotlin.jupyter.repl.building | ||
|
||
import org.jetbrains.kotlin.scripting.compiler.plugin.impl.KJvmReplCompilerImpl | ||
import java.io.File | ||
import java.util.StringJoiner | ||
import kotlin.script.experimental.api.* | ||
import kotlin.script.experimental.jvm.BasicJvmScriptEvaluator | ||
import kotlin.script.experimental.jvm.dependenciesFromCurrentContext | ||
import kotlin.script.experimental.jvm.jvm | ||
import kotlin.script.experimental.host.withDefaultsFrom | ||
import kotlin.script.experimental.jvmhost.repl.JvmReplCompiler | ||
import kotlin.script.experimental.jvmhost.repl.JvmReplEvaluator | ||
|
||
/** | ||
* Util class for building REPL components. | ||
*/ | ||
object ReplBuilding { | ||
fun buildCompiler(properties: KotlinReplProperties): JvmReplCompiler { | ||
val receiverClassPath = properties.receiver!!.javaClass | ||
.protectionDomain.codeSource.location.path | ||
properties.classPath(receiverClassPath) | ||
|
||
val compilerImpl = KJvmReplCompilerImpl(properties.hostConf) | ||
|
||
return JvmReplCompiler( | ||
buildCompilationConfiguration(properties), | ||
properties.hostConf, | ||
compilerImpl) | ||
} | ||
|
||
fun buildEvaluator(properties: KotlinReplProperties): JvmReplEvaluator { | ||
return JvmReplEvaluator( | ||
buildEvaluationConfiguration(properties), | ||
BasicJvmScriptEvaluator()) | ||
} | ||
|
||
private fun buildClassPath(p: KotlinReplProperties): String { | ||
val joiner = StringJoiner(File.pathSeparator) | ||
for (path in p.getClasspath()) { | ||
if (path != "") { | ||
joiner.add(path) | ||
} | ||
} | ||
return joiner.toString() | ||
} | ||
|
||
private fun buildCompilationConfiguration( | ||
p: KotlinReplProperties): ScriptCompilationConfiguration { | ||
return ScriptCompilationConfiguration { | ||
hostConfiguration.invoke(p.hostConf) | ||
|
||
val jvmBuilder = jvm | ||
jvmBuilder.dependenciesFromCurrentContext(wholeClasspath = true, unpackJarCollections = false) | ||
|
||
val compilerOptions = listOf("-classpath", buildClassPath(p)) | ||
|
||
this.compilerOptions.invoke(compilerOptions) | ||
|
||
val kt = KotlinType(p.receiver!!.javaClass.canonicalName) | ||
val receivers = listOf(kt) | ||
implicitReceivers.invoke(receivers) | ||
|
||
Unit | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to return Unit here |
||
} | ||
} | ||
|
||
private fun buildEvaluationConfiguration( | ||
p: KotlinReplProperties): ScriptEvaluationConfiguration { | ||
return ScriptEvaluationConfiguration { | ||
hostConfiguration.invoke(p.hostConf) | ||
|
||
val receivers = listOf<Any?>(p.receiver) | ||
implicitReceivers.invoke(receivers) | ||
|
||
Unit | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, remove this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed