|
| 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 | +} |
0 commit comments