Skip to content

Commit 50e97c4

Browse files
committed
Added textDocument/codeAction
1 parent 71d1280 commit 50e97c4

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

Diff for: handler/handler.go

+35
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ func (handler *InoHandler) transformClangdResult(method string, uri lsp.Document
262262
case "textDocument/completion":
263263
r := result.(*lsp.CompletionList)
264264
handler.cpp2inoCompletionList(r, uri)
265+
case "textDocument/codeAction":
266+
r := result.(*[]CodeAction)
267+
for index := range *r {
268+
handler.cpp2inoCodeAction(&(*r)[index], uri)
269+
}
265270
case "textDocument/hover":
266271
r := result.(*Hover)
267272
handler.cpp2inoHover(r, uri)
@@ -297,6 +302,36 @@ func (handler *InoHandler) cpp2inoCompletionList(list *lsp.CompletionList, uri l
297302
}
298303
}
299304

305+
func (handler *InoHandler) cpp2inoCodeAction(codeAction *CodeAction, uri lsp.DocumentURI) {
306+
newEdit := lsp.WorkspaceEdit{Changes: make(map[string][]lsp.TextEdit)}
307+
for uri, edit := range codeAction.Edit.Changes {
308+
if data, ok := handler.data[lsp.DocumentURI(uri)]; ok {
309+
newValue := make([]lsp.TextEdit, len(edit))
310+
for index := range edit {
311+
r := edit[index].Range
312+
newValue[index] = lsp.TextEdit{
313+
NewText: edit[index].NewText,
314+
Range: lsp.Range{
315+
Start: lsp.Position{Line: data.sourceLineMap[r.Start.Line], Character: r.Start.Character},
316+
End: lsp.Position{Line: data.sourceLineMap[r.End.Line], Character: r.End.Character},
317+
},
318+
}
319+
}
320+
newEdit.Changes[string(data.sourceURI)] = newValue
321+
} else {
322+
newEdit.Changes[uri] = edit
323+
}
324+
}
325+
codeAction.Edit = &newEdit
326+
if data, ok := handler.data[uri]; ok {
327+
for index := range codeAction.Diagnostics {
328+
r := &codeAction.Diagnostics[index].Range
329+
r.Start.Line = data.sourceLineMap[r.Start.Line]
330+
r.End.Line = data.sourceLineMap[r.End.Line]
331+
}
332+
}
333+
}
334+
300335
func (handler *InoHandler) cpp2inoHover(hover *Hover, uri lsp.DocumentURI) {
301336
if data, ok := handler.data[uri]; ok {
302337
r := hover.Range

Diff for: handler/protocol.go

+15
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ func sendRequest(ctx context.Context, conn *jsonrpc2.Conn, method string, params
6868
result := new(lsp.CompletionList)
6969
err := conn.Call(ctx, method, params, result)
7070
return result, err
71+
case "textDocument/codeAction":
72+
result := new([]CodeAction)
73+
err := conn.Call(ctx, method, params, result)
74+
return result, err
7175
case "completionItem/resolve":
7276
result := new(lsp.CompletionItem)
7377
err := conn.Call(ctx, method, params, result)
@@ -100,11 +104,22 @@ func sendRequest(ctx context.Context, conn *jsonrpc2.Conn, method string, params
100104
return result, err
101105
}
102106

107+
// CodeAction structure according to LSP
108+
type CodeAction struct {
109+
Title string `json:"title"`
110+
Kind string `json:"kind,omitempty"`
111+
Diagnostics []lsp.Diagnostic `json:"diagnostics,omitempty"`
112+
Edit *lsp.WorkspaceEdit `json:"edit,omitempty"`
113+
Command *lsp.Command `json:"command,omitempty"`
114+
}
115+
116+
// Hover structure according to LSP
103117
type Hover struct {
104118
Contents MarkupContent `json:"contents"`
105119
Range *lsp.Range `json:"range,omitempty"`
106120
}
107121

122+
// MarkupContent structure according to LSP
108123
type MarkupContent struct {
109124
Kind string `json:"kind"`
110125
Value string `json:"value"`

Diff for: handler/stream.go

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type StreamReadWrite struct {
1212
outLog io.Writer
1313
}
1414

15+
// Read reads from the stream.
1516
func (srw StreamReadWrite) Read(p []byte) (int, error) {
1617
n, err := srw.inStream.Read(p)
1718
if n > 0 && srw.inLog != nil {
@@ -20,13 +21,15 @@ func (srw StreamReadWrite) Read(p []byte) (int, error) {
2021
return n, err
2122
}
2223

24+
// Write writes to the stream.
2325
func (srw StreamReadWrite) Write(p []byte) (int, error) {
2426
if srw.outLog != nil {
2527
srw.outLog.Write(p)
2628
}
2729
return srw.outStream.Write(p)
2830
}
2931

32+
// Close closes the stream.
3033
func (srw StreamReadWrite) Close() error {
3134
err1 := srw.inStream.Close()
3235
err2 := srw.outStream.Close()

0 commit comments

Comments
 (0)