Skip to content

Commit

Permalink
Handle complete_request in gojupyterscaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
yunabe committed Nov 8, 2017
1 parent bee24dc commit 2349d37
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/lgo-internal/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ func (h *handlers) HandleExecuteRequest(ctx context.Context, r *scaffold.Execute
}
}

func (*handlers) HandleComplete(req *scaffold.CompleteRequest) *scaffold.CompleteReply {
// Not implemented
return nil
}

func (h *handlers) HandleInspect(r *scaffold.InspectRequest) *scaffold.InspectReply {
index := -1
count := 0
Expand Down
9 changes: 9 additions & 0 deletions jupyter/gojupyterscaffold-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ loop:
return res
}

func (*handlers) HandleComplete(req *scaffold.CompleteRequest) *scaffold.CompleteReply {
return &scaffold.CompleteReply{
Status: "ok",
Matches: []string{"abc", "xyz", "123", "こんにちは"},
CursorStart: req.CursorPos,
CursorEnd: req.CursorPos,
}
}

func (*handlers) HandleInspect(req *scaffold.InspectRequest) *scaffold.InspectReply {
return &scaffold.InspectReply{
Status: "ok",
Expand Down
29 changes: 29 additions & 0 deletions jupyter/gojupyterscaffold/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type RequestHandlers interface {
req *ExecuteRequest,
writeStream func(name, text string),
writeDisplayData func(data *DisplayData)) *ExecuteResult
HandleComplete(req *CompleteRequest) *CompleteReply
HandleInspect(req *InspectRequest) *InspectReply
}

Expand Down Expand Up @@ -55,6 +56,34 @@ type InspectReply struct {
Data map[string]interface{} `json:"data,omitempty"`
}

// http://jupyter-client.readthedocs.io/en/latest/messaging.html#completion
type CompleteRequest struct {
// The code context in which completion is requested
// this may be up to an entire multiline cell, such as
// 'foo = a.isal'
Code string `json:"code"`
// The cursor position within 'code' (in unicode characters) where completion is requested
CursorPos int `json:"cursor_pos"`
}

type CompleteReply struct {
// The list of all matches to the completion request, such as
// ['a.isalnum', 'a.isalpha'] for the above example.
Matches []string `json:"matches"`

// The range of text that should be replaced by the above matches when a completion is accepted.
// typically cursor_end is the same as cursor_pos in the request.
CursorStart int `json:"cursor_start"`
CursorEnd int `json:"cursor_end"`

// 'metadata' is omitted

// status should be 'ok' unless an exception was raised during the request,
// in which case it should be 'error', along with the usual error message content
// in other messages.
Status string `json:"status"`
}

// http://jupyter-client.readthedocs.io/en/latest/messaging.html#execution-results
type ExecuteResult struct {
Status string `json:"status"`
Expand Down
2 changes: 2 additions & 0 deletions jupyter/gojupyterscaffold/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func newContentForMsgType(header *messageHeader) interface{} {
switch header.MsgType {
case "execute_request":
return &ExecuteRequest{}
case "complete_request":
return &CompleteRequest{}
case "inspect_request":
return &InspectRequest{}
}
Expand Down
14 changes: 14 additions & 0 deletions jupyter/gojupyterscaffold/shelsocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,20 @@ func (s *shellSocket) handleMessages() error {
// TODO: Send shutdown_reply
case "execute_request":
s.execQueue.push(&msg, s)
case "complete_request":
go func() {
reply := s.handlers.HandleComplete(msg.Content.(*CompleteRequest))
res := newMessageWithParent(&msg)
res.Header.MsgType = "complete_reply"
if reply != nil {
res.Content = reply
} else {
res.Content = &CompleteReply{
Status: "ok",
}
}
s.pushResult(res)
}()
case "inspect_request":
go func() {
reply := s.handlers.HandleInspect(msg.Content.(*InspectRequest))
Expand Down

0 comments on commit 2349d37

Please sign in to comment.