Skip to content

Commit

Permalink
feat: provide support for completion *TextEdit | *InsertReplaceEdit
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Jul 4, 2024
1 parent b4eec67 commit 1e461c1
Show file tree
Hide file tree
Showing 4 changed files with 362 additions and 32 deletions.
65 changes: 65 additions & 0 deletions connectionlogger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package protocol

import (
"context"
"encoding/json"
"fmt"
"io"

"go.lsp.dev/jsonrpc2"
)

func NewConnectionLogger(w io.Writer, next jsonrpc2.Conn) *ConnectionLogger {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return &ConnectionLogger{
w: w,
enc: enc,
next: next,
}
}

type ConnectionLogger struct {
w io.Writer
enc *json.Encoder
next jsonrpc2.Conn
}

func (cl *ConnectionLogger) Call(ctx context.Context, method string, params, result interface{}) (jsonrpc2.ID, error) {
io.WriteString(cl.w, fmt.Sprintf("-> %s\n", method))
cl.enc.Encode(params)
var res json.RawMessage
id, err := cl.next.Call(ctx, method, params, &res)
if err != nil {
io.WriteString(cl.w, fmt.Sprintf("<- %s %v\n", method, err))
return id, err
}
if res != nil {
io.WriteString(cl.w, fmt.Sprintf("<- %s\n", method))
cl.enc.Encode(res)
}
err = json.Unmarshal(res, &result)
return id, err
}

func (cl *ConnectionLogger) Notify(ctx context.Context, method string, params interface{}) error {
io.WriteString(cl.w, fmt.Sprintf("-> %s\n", method))
cl.enc.Encode(params)
return cl.next.Notify(ctx, method, params)
}

func (cl *ConnectionLogger) Go(ctx context.Context, handler jsonrpc2.Handler) {
cl.next.Go(ctx, handler)
}

func (cl *ConnectionLogger) Close() error {
return cl.next.Close()
}

func (cl *ConnectionLogger) Done() <-chan struct{} {
return cl.next.Done()
}

func (cl *ConnectionLogger) Err() error {
return cl.next.Err()
}
9 changes: 9 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@ func LoggerFromContext(ctx context.Context) *zap.Logger {
func WithClient(ctx context.Context, client Client) context.Context {
return context.WithValue(ctx, ctxClient, client)
}

// ClientFromContext extracts Client from context.
func ClientFromContext(ctx context.Context) Client {
client, ok := ctx.Value(ctxClient).(Client)
if !ok {
return nil
}
return client
}
51 changes: 50 additions & 1 deletion language.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package protocol

import (
"strconv"

"github.com/segmentio/encoding/json"
)

// CompletionParams params of Completion request.
Expand Down Expand Up @@ -257,7 +259,52 @@ type CompletionItem struct {
// contained and starting at the same position.
//
// @since 3.16.0 additional type "InsertReplaceEdit".
TextEdit *TextEdit `json:"textEdit,omitempty"` // *TextEdit | *InsertReplaceEdit
TextEdit *TextEditOrInsertReplaceEdit `json:"textEdit,omitempty"` // *TextEdit | *InsertReplaceEdit
}

type TextEditOrInsertReplaceEdit struct {
TextEdit *TextEdit
InsertReplaceEdit *InsertReplaceEdit
}

func (t *TextEditOrInsertReplaceEdit) MarshalJSON() ([]byte, error) {
if t.TextEdit != nil {
return json.Marshal(t.TextEdit)
}
return json.Marshal(t.InsertReplaceEdit)
}

type textEditAndInsertReplaceEdit struct {
// NewText is in both types.
NewText string `json:"newText"`

// Range is only present in TextEdit.
Range *Range `json:"range"`

// Insert is only present in InsertReplaceEdit.
Insert Range `json:"insert"`
// Replace is only present in InsertReplaceEdit.
Replace Range `json:"replace"`
}

func (t *TextEditOrInsertReplaceEdit) UnmarshalJSON(data []byte) error {
var teaire textEditAndInsertReplaceEdit
if err := json.Unmarshal(data, &teaire); err != nil {
return err
}
if teaire.Range != nil {
t.TextEdit = &TextEdit{
NewText: teaire.NewText,
Range: *teaire.Range,
}
return nil
}
t.InsertReplaceEdit = &InsertReplaceEdit{
NewText: teaire.NewText,
Insert: teaire.Insert,
Replace: teaire.Replace,
}
return nil
}

// CompletionItemKind is the completion item kind values the client supports. When this
Expand Down Expand Up @@ -324,6 +371,7 @@ const (
)

// String implements fmt.Stringer.
//
//nolint:cyclop
func (k CompletionItemKind) String() string {
switch k {
Expand Down Expand Up @@ -730,6 +778,7 @@ const (
)

// String implements fmt.Stringer.
//
//nolint:cyclop
func (k SymbolKind) String() string {
switch k {
Expand Down
Loading

0 comments on commit 1e461c1

Please sign in to comment.