Skip to content

Commit 886f25f

Browse files
committed
Merge branch 'cosmos72-autocomplete'
2 parents b399361 + 88c885d commit 886f25f

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
gophernotes
2+
.ipynb_checkpoints
3+
Untitled.ipynb

complete.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
interp "github.com/cosmos72/gomacro/fast"
5+
)
6+
7+
type Completion struct {
8+
class,
9+
name,
10+
typ string
11+
}
12+
13+
type CompletionResponse struct {
14+
partial int
15+
completions []Completion
16+
}
17+
18+
/************************************************************
19+
* entry function
20+
************************************************************/
21+
func handleCompleteRequest(ir *interp.Interp, receipt msgReceipt) error {
22+
// Extract the data from the request.
23+
reqcontent := receipt.Msg.Content.(map[string]interface{})
24+
code := reqcontent["code"].(string)
25+
cursorPos := int(reqcontent["cursor_pos"].(float64))
26+
27+
// autocomplete the code at the cursor position
28+
prefix, matches, _ := ir.CompleteWords(code, cursorPos)
29+
30+
// prepare the reply
31+
content := make(map[string]interface{})
32+
33+
if len(matches) == 0 {
34+
content["ename"] = "ERROR"
35+
content["evalue"] = "no completions found"
36+
content["traceback"] = nil
37+
content["status"] = "error"
38+
} else {
39+
partialWord := interp.TailIdentifier(prefix)
40+
content["cursor_start"] = float64(len(prefix) - len(partialWord))
41+
content["cursor_end"] = float64(cursorPos)
42+
content["matches"] = matches
43+
content["status"] = "ok"
44+
}
45+
46+
return receipt.Reply("complete_reply", content)
47+
}

kernel.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ func handleShellMsg(ir *interp.Interp, receipt msgReceipt) {
259259
if err := sendKernelInfo(receipt); err != nil {
260260
log.Fatal(err)
261261
}
262+
case "complete_request":
263+
if err := handleCompleteRequest(ir, receipt); err != nil {
264+
log.Fatal(err)
265+
}
262266
case "execute_request":
263267
if err := handleExecuteRequest(ir, receipt); err != nil {
264268
log.Fatal(err)

0 commit comments

Comments
 (0)