-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optional Go context to GLSP context
See #27 Also various cleanups and improvements to example server
- Loading branch information
Showing
22 changed files
with
203 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
package glsp | ||
|
||
import ( | ||
contextpkg "context" | ||
"encoding/json" | ||
) | ||
|
||
type NotifyFunc func(method string, params any) | ||
type CallFunc func(method string, params any, result any) | ||
|
||
type Context struct { | ||
Method string | ||
Params json.RawMessage | ||
Notify NotifyFunc | ||
Call CallFunc | ||
Method string | ||
Params json.RawMessage | ||
Notify NotifyFunc | ||
Call CallFunc | ||
Context contextpkg.Context // can be nil | ||
} | ||
|
||
type Handler interface { | ||
Handle(context *Context) (r any, validMethod bool, validParams bool, err error) | ||
Handle(context *Context) (result any, validMethod bool, validParams bool, err error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package server | ||
|
||
import ( | ||
contextpkg "context" | ||
"io" | ||
|
||
"github.com/gorilla/websocket" | ||
"github.com/sourcegraph/jsonrpc2" | ||
wsjsonrpc2 "github.com/sourcegraph/jsonrpc2/websocket" | ||
"github.com/tliron/commonlog" | ||
) | ||
|
||
func (self *Server) newStreamConnection(stream io.ReadWriteCloser) *jsonrpc2.Conn { | ||
handler := self.newHandler() | ||
connectionOptions := self.newConnectionOptions() | ||
|
||
context, cancel := contextpkg.WithTimeout(contextpkg.Background(), self.StreamTimeout) | ||
defer cancel() | ||
|
||
return jsonrpc2.NewConn(context, jsonrpc2.NewBufferedStream(stream, jsonrpc2.VSCodeObjectCodec{}), handler, connectionOptions...) | ||
} | ||
|
||
func (self *Server) newWebSocketConnection(socket *websocket.Conn) *jsonrpc2.Conn { | ||
handler := self.newHandler() | ||
connectionOptions := self.newConnectionOptions() | ||
|
||
context, cancel := contextpkg.WithTimeout(contextpkg.Background(), self.WebSocketTimeout) | ||
defer cancel() | ||
|
||
return jsonrpc2.NewConn(context, wsjsonrpc2.NewObjectStream(socket), handler, connectionOptions...) | ||
} | ||
|
||
func (self *Server) newConnectionOptions() []jsonrpc2.ConnOpt { | ||
if self.Debug { | ||
log := commonlog.NewScopeLogger(self.Log, "rpc") | ||
return []jsonrpc2.ConnOpt{jsonrpc2.LogMessages(&JSONRPCLogger{log})} | ||
} else { | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package server | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
) | ||
|
||
func (self *Server) RunStdio() error { | ||
self.Log.Notice("reading from stdin, writing to stdout") | ||
self.ServeStream(Stdio{}, nil) | ||
return nil | ||
} | ||
|
||
type Stdio struct{} | ||
|
||
// ([io.Reader] interface) | ||
func (Stdio) Read(p []byte) (int, error) { | ||
return os.Stdin.Read(p) | ||
} | ||
|
||
// ([io.Writer] interface) | ||
func (Stdio) Write(p []byte) (int, error) { | ||
return os.Stdout.Write(p) | ||
} | ||
|
||
// ([io.Closer] interface) | ||
func (Stdio) Close() error { | ||
return errors.Join(os.Stdin.Close(), os.Stdout.Close()) | ||
} |
Oops, something went wrong.