mirror of
https://github.com/laravel-ls/protocol.git
synced 2026-06-16 03:54:56 +02:00
README.md: fix the code example
This commit is contained in:
parent
39fb18c7ea
commit
9988e59b6c
1 changed files with 83 additions and 36 deletions
119
README.md
119
README.md
|
|
@ -11,51 +11,98 @@ The package does not include JSON RPC handling as that is better left to other p
|
||||||
Example code using [github.com/sourcegraph/jsonrpc2](https://github.com/sourcegraph/jsonrpc2)
|
Example code using [github.com/sourcegraph/jsonrpc2](https://github.com/sourcegraph/jsonrpc2)
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"context"
|
||||||
"io"
|
"encoding/json"
|
||||||
"context"
|
"fmt"
|
||||||
"encoding/json"
|
"io"
|
||||||
"github.com/sourcegraph/jsonrpc2"
|
"log"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/laravel-ls/protocol"
|
||||||
|
"github.com/sourcegraph/jsonrpc2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func handleInitialize(params protocol.InitializeParams) (protocol.InitializeResult, error) {
|
||||||
|
// Initialization logic
|
||||||
|
// Read more at: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#initialize
|
||||||
|
return protocol.InitializeResult{
|
||||||
|
Capabilities: protocol.ServerCapabilities{
|
||||||
|
TextDocumentSync: protocol.TextDocumentSyncKindFull,
|
||||||
|
},
|
||||||
|
ServerInfo: &protocol.ServerInfo{
|
||||||
|
Name: "My LSP Server",
|
||||||
|
Version: "1.0.0",
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
func handler(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (any, error) {
|
func handler(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (any, error) {
|
||||||
|
switch req.Method {
|
||||||
switch req.Method {
|
case protocol.MethodInitialize:
|
||||||
case protocol.MethodInitialize:
|
var params protocol.InitializeParams
|
||||||
var params protocol.InitializeParams
|
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
|
||||||
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
|
return nil, err
|
||||||
return nil, err
|
}
|
||||||
}
|
return handleInitialize(params)
|
||||||
// handle initialize
|
// ... add more methods
|
||||||
case protocol.MethodTextDocumentDidChange:
|
default:
|
||||||
var params protocol.DidChangeTextDocumentParams
|
// Respond with a method not found error
|
||||||
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
|
return nil, &jsonrpc2.Error{
|
||||||
return nil, err
|
Code: jsonrpc2.CodeMethodNotFound,
|
||||||
}
|
Message: fmt.Sprintf("Method %s not found", req.Method),
|
||||||
// handle did change
|
}
|
||||||
default:
|
}
|
||||||
// Respond with a method not found error
|
|
||||||
return nil, &jsonrpc2.Error{
|
|
||||||
Code: jsonrpc2.CodeMethodNotFound,
|
|
||||||
Message: fmt.Sprintf("Method %s not found", req.Method),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Server) Run(ctx context.Context, conn io.ReadWriteCloser) error {
|
func handleConn(ctx context.Context, conn io.ReadWriteCloser) error {
|
||||||
stream := jsonrpc2.NewBufferedStream(conn, jsonrpc2.VSCodeObjectCodec{})
|
stream := jsonrpc2.NewBufferedStream(conn, jsonrpc2.VSCodeObjectCodec{})
|
||||||
rpc := jsonrpc2.NewConn(ctx, stream, jsonrpc2.HandlerWithError(s.dispatch))
|
rpc := jsonrpc2.NewConn(ctx, stream, jsonrpc2.HandlerWithError(handler))
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return fmt.Errorf("context closed")
|
return fmt.Errorf("context closed")
|
||||||
case <-rpc.DisconnectNotify():
|
case <-rpc.DisconnectNotify():
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// example TCP server.
|
||||||
|
ctx := context.Background()
|
||||||
|
logger := log.New(os.Stderr, "[tcp-lsp] ", log.LstdFlags|log.Lmicroseconds)
|
||||||
|
|
||||||
|
addr := "127.0.0.1:4389"
|
||||||
|
l, err := net.Listen("tcp", addr)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
logger.Println("Listening on", addr)
|
||||||
|
|
||||||
|
for {
|
||||||
|
conn, err := l.Accept()
|
||||||
|
if err != nil {
|
||||||
|
logger.Println("accept error:", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Println("Client connected:", conn.RemoteAddr())
|
||||||
|
|
||||||
|
go func(c net.Conn) {
|
||||||
|
defer c.Close()
|
||||||
|
handleConn(ctx, c)
|
||||||
|
}(conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
You can test your server with the following command on linux:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
printf 'Content-Length: 58\r\n\r\n{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}' | nc 127.0.0.1 4389
|
||||||
```
|
```
|
||||||
|
|
||||||
# Author
|
# Author
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue