Skip to content

Commit 66f674f

Browse files
committed
Switch to a new REPL API
1 parent 873e547 commit 66f674f

File tree

14 files changed

+168
-129
lines changed

14 files changed

+168
-129
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ configurations {
155155
dependencies {
156156
compile project(":jupyter-lib")
157157
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
158-
compile "org.jetbrains.kotlin:kotlin-scripting-jvm-host-embeddable:$kotlinVersion"
158+
compile "org.jetbrains.kotlin:kotlin-scripting-ide-services-impl-embeddable:$kotlinVersion"
159159
compile "org.jetbrains.kotlin:kotlin-scripting-common:$kotlinVersion"
160160
compile "org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:$kotlinVersion"
161161
compile "org.jetbrains.kotlin:kotlin-compiler-embeddable:$kotlinVersion"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package jupyter.kotlin.receivers
2+
3+
class ConstReceiver(val value: Int)

src/main/kotlin/org/jetbrains/kotlin/jupyter/receivers.kt jupyter-lib/src/main/kotlin/jupyter/kotlin/receivers/TypeProviderReceiver.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
package org.jetbrains.kotlin.jupyter
2-
3-
class ConstReceiver(val value: Int)
1+
package jupyter.kotlin.receivers
42

53
class TypeProviderReceiver {
64

75
fun generateCode(values: List<Int>): List<String> {
86
val properties = (0 until values.size)
97
.map { "val value$it : Int get() = list[$it]" }
10-
.joinToLines()
8+
.joinToString("\n")
119

1210
val classDeclaration = """
1311
class TypedIntList###(val list: List<Int>): List<Int> by list {

resources/notebook-extension/kernel.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ define(function(){
153153
return -1;
154154
}
155155

156-
function getTokenBounds(buf, cursor) {
156+
function getTokenBounds(buf, cursor, editor) {
157157
if (cursor > buf.length) {
158158
throw new Error("Position " + cursor + " does not exist in code snippet <" + buf + ">");
159159
}
@@ -174,7 +174,9 @@ define(function(){
174174
tokenBeforeCursor: buf.substring(start, cursor),
175175
after: buf.substring(end, buf.length),
176176
start: start,
177-
end: end
177+
end: end,
178+
posStart: editor.posFromIndex(start),
179+
posEnd: editor.posFromIndex(end)
178180
}
179181
}
180182

@@ -211,7 +213,7 @@ define(function(){
211213
cursor_pos = utils.js_idx_to_char_idx(cursor_pos, text);
212214

213215
var prevBounds = this.tokenBounds;
214-
var bounds = getTokenBounds(text, cursor_pos);
216+
var bounds = getTokenBounds(text, cursor_pos, this.editor);
215217
this.tokenBounds = bounds;
216218
if (prevBounds && this.raw_result) {
217219
if (bounds.before === prevBounds.before &&
@@ -223,6 +225,10 @@ define(function(){
223225
if (displayName[0] === '`')
224226
displayName = displayName.substring(1, displayName.length - 1);
225227
return displayName.startsWith(bounds.tokenBeforeCursor)
228+
}).map((completion) => {
229+
completion.from = bounds.posStart;
230+
completion.to = bounds.posEnd;
231+
return completion;
226232
});
227233

228234
if (newResult.length > 0) {
@@ -542,7 +548,7 @@ define(function(){
542548
var optionsLen;
543549
var index;
544550
var prevIndex;
545-
if (code == keycodes.enter) {
551+
if (code == keycodes.enter && !event.shiftKey) {
546552
event.codemirrorIgnore = true;
547553
event._ipkmIgnore = true;
548554
event.preventDefault();
@@ -608,7 +614,7 @@ define(function(){
608614
};
609615

610616
function _isCompletionKey(key) {
611-
return key.length === 1;
617+
return /^[A-Z0-9.:"]$/i.test(key);
612618
}
613619

614620
Completer.prototype.keypress = function (event) {
@@ -675,12 +681,16 @@ define(function(){
675681
.forEach(it => it.clear());
676682
}
677683

678-
CodeCell.prototype._handle_change = function(cm, changes) {
679-
this.notebook.get_cells().forEach((cell) =>{
684+
function clearAllErrors(notebook) {
685+
notebook.get_cells().forEach((cell) =>{
680686
if (cell.code_mirror) {
681687
clearErrors(cell.code_mirror);
682688
}
683689
});
690+
}
691+
692+
CodeCell.prototype._handle_change = function(cm, changes) {
693+
clearAllErrors(this.notebook);
684694
this.kernel.listErrors(cm.getValue(), (msg) => {
685695
var content = msg.content;
686696
console.log(content);
@@ -832,6 +842,7 @@ define(function(){
832842
};
833843

834844
CodeCell.prototype._handle_execute_reply = function (msg) {
845+
clearAllErrors(this.notebook)
835846
this.bindEvents();
836847

837848
this.set_input_prompt(msg.content.execution_count);

src/main/kotlin/org/jetbrains/kotlin/jupyter/config.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ val LocalSettingsPath = Paths.get(System.getProperty("user.home"), ".jupyter_kot
2323
val GitHubApiHost = "api.github.com"
2424
val GitHubRepoOwner = "kotlin"
2525
val GitHubRepoName = "kotlin-jupyter"
26-
val GitHubBranchName = "demo-jan2020"
26+
val GitHubBranchName = "psi_completion_with_typing"
2727
val GitHubApiPrefix = "https://$GitHubApiHost/repos/$GitHubRepoOwner/$GitHubRepoName"
2828

2929
val LibraryDescriptorExt = "json"

src/main/kotlin/org/jetbrains/kotlin/jupyter/protocol.kt

+10-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import java.io.PrintStream
1313
import java.lang.reflect.InvocationTargetException
1414
import java.util.concurrent.atomic.AtomicLong
1515
import kotlin.concurrent.timer
16+
import kotlin.script.experimental.api.ScriptDiagnostic
1617

1718
enum class ResponseState {
1819
Ok, Error, Abort
@@ -356,17 +357,18 @@ fun JupyterConnection.evalWithIO(config: OutputConfig, srcMessage: Message, body
356357
forkedOut.flush()
357358
forkedError.flush()
358359

359-
val additionalInfo = ex.errorResult.location?.let {
360-
val errorMessage = ex.errorResult.message
361-
jsonObject("lineStart" to it.line, "colStart" to it.column,
362-
"lineEnd" to it.lineEnd, "colEnd" to it.columnEnd,
360+
val firstDiagnostic = ex.firstDiagnostics
361+
val additionalInfo = firstDiagnostic?.location?.let {
362+
val errorMessage = firstDiagnostic.message
363+
jsonObject("lineStart" to it.start.line, "colStart" to it.start.col,
364+
"lineEnd" to (it.end?.line ?: -1), "colEnd" to (it.end?.col ?: -1),
363365
"message" to errorMessage,
364-
"path" to it.path)
366+
"path" to firstDiagnostic.sourcePath.orEmpty())
365367
} ?: jsonObject()
366368

367369
ErrorResponseWithMessage(
368370
textResult("Error!"),
369-
ex.errorResult.message,
371+
ex.message,
370372
ex.javaClass.canonicalName,
371373
ex.message ?: "",
372374
ex.stackTrace.map { it.toString() },
@@ -376,8 +378,8 @@ fun JupyterConnection.evalWithIO(config: OutputConfig, srcMessage: Message, body
376378

377379
val stdErr = StringBuilder()
378380
with(stdErr) {
379-
val cause = ex.errorResult.cause
380-
if (cause == null) appendln(ex.errorResult.message)
381+
val cause = ex.cause
382+
if (cause == null) appendln(ex.message)
381383
else {
382384
when (cause) {
383385
is InvocationTargetException -> appendln(cause.targetException.toString())

0 commit comments

Comments
 (0)