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
73
README.md
73
README.md
|
|
@ -11,30 +11,44 @@ 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)
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/laravel-ls/protocol"
|
||||
"github.com/sourcegraph/jsonrpc2"
|
||||
)
|
||||
|
||||
func handler(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (any, error) {
|
||||
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) {
|
||||
switch req.Method {
|
||||
case protocol.MethodInitialize:
|
||||
var params protocol.InitializeParams
|
||||
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// handle initialize
|
||||
case protocol.MethodTextDocumentDidChange:
|
||||
var params protocol.DidChangeTextDocumentParams
|
||||
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// handle did change
|
||||
return handleInitialize(params)
|
||||
// ... add more methods
|
||||
default:
|
||||
// Respond with a method not found error
|
||||
return nil, &jsonrpc2.Error{
|
||||
|
|
@ -42,12 +56,11 @@ func handler(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (a
|
|||
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{})
|
||||
rpc := jsonrpc2.NewConn(ctx, stream, jsonrpc2.HandlerWithError(s.dispatch))
|
||||
rpc := jsonrpc2.NewConn(ctx, stream, jsonrpc2.HandlerWithError(handler))
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
|
|
@ -56,6 +69,40 @@ func (s Server) Run(ctx context.Context, conn io.ReadWriteCloser) error {
|
|||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue