Skip to content

Commit 4e516f1

Browse files
committed
PSI-based completion
1 parent d534c98 commit 4e516f1

File tree

12 files changed

+1101
-246
lines changed

12 files changed

+1101
-246
lines changed

build.gradle

+14-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import java.util.stream.Collectors
77

88
buildscript {
99
ext.shadowJarVersion = "5.2.0"
10-
ext.kotlinVersion = '1.3.70-eap-3'
10+
ext.kotlinVersion = '1.3-SNAPSHOT'
1111
ext.baseVersion = '0.7.41'
1212
repositories {
1313
jcenter()
@@ -62,8 +62,12 @@ allprojects {
6262
distributionPath = rootPath.resolve("distrib")
6363
//noinspection GroovyAssignabilityCheck
6464
distribBuildPath = rootPath.resolve("distrib-build")
65+
66+
resourcesDir = "resources"
6567
//noinspection GroovyAssignabilityCheck
66-
logosPath = getSubDir(rootPath, "resources", "logos")
68+
logosPath = getSubDir(rootPath, resourcesDir, "logos")
69+
//noinspection GroovyAssignabilityCheck
70+
nbExtensionPath = getSubDir(rootPath, resourcesDir, "notebook-extension")
6771

6872
if (project == rootProject) {
6973
isProtectedBranch = isProtectedBranch()
@@ -95,7 +99,6 @@ allprojects {
9599
debuggerConfig = "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$debugPort".toString()
96100

97101
mainSourceSetDir = "main"
98-
resourcesDir = "resources"
99102
runtimePropertiesFile = "runtime.properties"
100103

101104
jarsPath = "jars"
@@ -246,7 +249,9 @@ void createCleanTasks() {
246249
task(makeTaskName(cleanInstallDirTaskPrefix, local)) {
247250
group(local ? localGroup : distribGroup)
248251
doLast {
249-
dir.deleteDir()
252+
if(!dir.deleteDir()) {
253+
throw new Exception("Cannot delete $dir")
254+
}
250255
}
251256
}
252257
}
@@ -309,6 +314,7 @@ void createMainInstallTask(Boolean debug, Boolean local, String group, String sp
309314
def taskName = "$taskNamePrefix$taskNameMiddle$taskNameSuffix"
310315

311316
def dependencies = [
317+
makeTaskName(cleanInstallDirTaskPrefix, local),
312318
local ? copyRunKernelPy : prepareDistributionDir,
313319
makeTaskName(installKernelTaskPrefix, local),
314320
makeTaskName(installLibsTaskPrefix, local),
@@ -340,6 +346,10 @@ void makeKernelSpec(Path installPath, Boolean localInstall) {
340346
from logosPath
341347
into installPath
342348
}
349+
project.copy {
350+
from nbExtensionPath
351+
into installPath
352+
}
343353
}
344354

345355
void makeJarArgs(Path installPath, String kernelJarPath, List<String> classPath, String debuggerConfig = "") {

jupyter-lib/src/main/kotlin/jupyter/kotlin/completion/KotlinContext.kt

-38
This file was deleted.

jupyter-lib/src/main/kotlin/jupyter/kotlin/completion/KotlinFunctionInfo.kt

-37
This file was deleted.

jupyter-lib/src/main/kotlin/jupyter/kotlin/completion/KotlinReflectUtil.kt

-17
This file was deleted.

jupyter-lib/src/main/kotlin/jupyter/kotlin/completion/KotlinVariableInfo.kt

-25
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package jupyter.kotlin
2+
3+
import java.util.*
4+
import kotlin.reflect.KFunction
5+
import kotlin.reflect.KProperty
6+
7+
/**
8+
* Kotlin REPL has built-in context for getting user-declared functions and variables
9+
* and setting invokeWrapper for additional side effects in evaluation.
10+
* It can be accessed inside REPL by name `kc`, e.g. kc.showVars()
11+
*/
12+
class KotlinContext(val vars: HashMap<String, KotlinVariableInfo> = HashMap(),
13+
val functions: MutableSet<KotlinFunctionInfo> = TreeSet()) {
14+
15+
fun getVarsList(): List<KotlinVariableInfo> {
16+
return ArrayList(vars.values)
17+
}
18+
19+
fun getFunctionsList(): List<KotlinFunctionInfo> {
20+
return ArrayList(functions)
21+
}
22+
}
23+
24+
25+
26+
/**
27+
* The implicit receiver for lines in Kotlin REPL.
28+
* It is passed to the script as an implicit receiver, identical to:
29+
* with (context) {
30+
* ...
31+
* }
32+
*
33+
* KotlinReceiver can be inherited from and passed to REPL building properties,
34+
* so other variables and functions can be accessed inside REPL.
35+
* By default, it only has KotlinContext.
36+
* Inherited KotlinReceivers should be in separate java file, they can't be inner or nested.
37+
*/
38+
class KotlinReceiver {
39+
var kc: KotlinContext? = null
40+
}
41+
42+
fun functionSignature(function: KFunction<*>): String {
43+
return function.toString().replace("Line_\\d+\\.".toRegex(), "")
44+
}
45+
46+
fun shortenType(name: String): String {
47+
return name.replace("(\\b[_a-zA-Z$][_a-zA-Z0-9$]*\\b\\.)+".toRegex(), "")
48+
// kotlin.collections.List<kotlin.Int> -> List<Int>
49+
}
50+
51+
class KotlinFunctionInfo(private val function: KFunction<*>) : Comparable<KotlinFunctionInfo> {
52+
53+
val name: String
54+
get() = function.name
55+
56+
fun toString(shortenTypes: Boolean): String {
57+
return if (shortenTypes) {
58+
shortenType(toString())
59+
} else toString()
60+
}
61+
62+
override fun toString(): String {
63+
return functionSignature(function)
64+
}
65+
66+
override fun compareTo(other: KotlinFunctionInfo): Int {
67+
return this.toString().compareTo(other.toString())
68+
}
69+
70+
override fun hashCode(): Int {
71+
return this.toString().hashCode()
72+
}
73+
74+
override fun equals(other: Any?): Boolean {
75+
return if (other is KotlinFunctionInfo) {
76+
this.toString() == other.toString()
77+
} else false
78+
}
79+
}
80+
81+
class KotlinVariableInfo(private val value: Any?, private val descriptor: KProperty<*>) {
82+
83+
val name: String
84+
get() = descriptor.name
85+
86+
val type: String
87+
get() = descriptor.returnType.toString()
88+
89+
fun toString(shortenTypes: Boolean): String {
90+
var type: String = type
91+
if (shortenTypes) {
92+
type = shortenType(type)
93+
}
94+
return "$name: $type = $value"
95+
}
96+
97+
override fun toString(): String {
98+
return toString(false)
99+
}
100+
}

0 commit comments

Comments
 (0)