Skip to content

Commit 5b0fb1e

Browse files
committed
Added textDocument/rename
1 parent ae7cbe0 commit 5b0fb1e

File tree

2 files changed

+61
-29
lines changed

2 files changed

+61
-29
lines changed

Diff for: handler/handler.go

+53-29
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ func (handler *InoHandler) transformClangdParams(method string, raw *json.RawMes
146146
p := params.(*lsp.DocumentSymbolParams)
147147
uri = p.TextDocument.URI
148148
err = handler.ino2cppTextDocumentIdentifier(&p.TextDocument)
149+
case "textDocument/rename":
150+
p := params.(*lsp.RenameParams)
151+
uri = p.TextDocument.URI
152+
err = handler.ino2cppRenameParams(p)
149153
}
150154
return
151155
}
@@ -290,6 +294,14 @@ func (handler *InoHandler) ino2cppDocumentOnTypeFormattingParams(params *lsp.Doc
290294
return nil
291295
}
292296

297+
func (handler *InoHandler) ino2cppRenameParams(params *lsp.RenameParams) error {
298+
handler.ino2cppTextDocumentIdentifier(&params.TextDocument)
299+
if data, ok := handler.data[params.TextDocument.URI]; ok {
300+
params.Position.Line = data.targetLineMap[params.Position.Line]
301+
}
302+
return nil
303+
}
304+
293305
func (handler *InoHandler) transformClangdResult(method string, uri lsp.DocumentURI, result interface{}) interface{} {
294306
switch method {
295307
case "textDocument/completion":
@@ -330,7 +342,10 @@ func (handler *InoHandler) transformClangdResult(method string, uri lsp.Document
330342
}
331343
case "textDocument/documentSymbol":
332344
r := result.(*[]DocumentSymbol)
333-
handler.cpp2inoDocumentSymbol(r, uri)
345+
result = handler.cpp2inoDocumentSymbols(*r, uri)
346+
case "textDocument/rename":
347+
r := result.(*lsp.WorkspaceEdit)
348+
result = handler.cpp2inoWorkspaceEdit(r)
334349
}
335350
return result
336351
}
@@ -348,8 +363,19 @@ func (handler *InoHandler) cpp2inoCompletionList(list *lsp.CompletionList, uri l
348363
}
349364

350365
func (handler *InoHandler) cpp2inoCodeAction(codeAction *CodeAction, uri lsp.DocumentURI) {
366+
codeAction.Edit = handler.cpp2inoWorkspaceEdit(codeAction.Edit)
367+
if data, ok := handler.data[uri]; ok {
368+
for index := range codeAction.Diagnostics {
369+
r := &codeAction.Diagnostics[index].Range
370+
r.Start.Line = data.sourceLineMap[r.Start.Line]
371+
r.End.Line = data.sourceLineMap[r.End.Line]
372+
}
373+
}
374+
}
375+
376+
func (handler *InoHandler) cpp2inoWorkspaceEdit(origEdit *lsp.WorkspaceEdit) *lsp.WorkspaceEdit {
351377
newEdit := lsp.WorkspaceEdit{Changes: make(map[string][]lsp.TextEdit)}
352-
for uri, edit := range codeAction.Edit.Changes {
378+
for uri, edit := range origEdit.Changes {
353379
if data, ok := handler.data[lsp.DocumentURI(uri)]; ok {
354380
newValue := make([]lsp.TextEdit, len(edit))
355381
for index := range edit {
@@ -367,14 +393,7 @@ func (handler *InoHandler) cpp2inoCodeAction(codeAction *CodeAction, uri lsp.Doc
367393
newEdit.Changes[uri] = edit
368394
}
369395
}
370-
codeAction.Edit = &newEdit
371-
if data, ok := handler.data[uri]; ok {
372-
for index := range codeAction.Diagnostics {
373-
r := &codeAction.Diagnostics[index].Range
374-
r.Start.Line = data.sourceLineMap[r.Start.Line]
375-
r.End.Line = data.sourceLineMap[r.End.Line]
376-
}
377-
}
396+
return &newEdit
378397
}
379398

380399
func (handler *InoHandler) cpp2inoHover(hover *Hover, uri lsp.DocumentURI) {
@@ -409,29 +428,34 @@ func (handler *InoHandler) cpp2inoTextEdit(edit *lsp.TextEdit, uri lsp.DocumentU
409428
}
410429
}
411430

412-
func (handler *InoHandler) cpp2inoDocumentSymbol(symbols *[]DocumentSymbol, uri lsp.DocumentURI) {
413-
if data, ok := handler.data[uri]; ok {
414-
for i := 0; i < len(*symbols); {
415-
symbol := &(*symbols)[i]
416-
symbol.Range.Start.Line = data.sourceLineMap[symbol.Range.Start.Line]
417-
symbol.Range.End.Line = data.sourceLineMap[symbol.Range.End.Line]
431+
func (handler *InoHandler) cpp2inoDocumentSymbols(origSymbols []DocumentSymbol, uri lsp.DocumentURI) []DocumentSymbol {
432+
data, ok := handler.data[uri]
433+
if !ok || len(origSymbols) == 0 {
434+
return origSymbols
435+
}
436+
newSymbols := make([]DocumentSymbol, len(origSymbols))
437+
j := 0
438+
for i := 0; i < len(origSymbols); i++ {
439+
symbol := &origSymbols[i]
440+
symbol.Range.Start.Line = data.sourceLineMap[symbol.Range.Start.Line]
441+
symbol.Range.End.Line = data.sourceLineMap[symbol.Range.End.Line]
442+
443+
duplicate := false
444+
for k := 0; k < j; k++ {
445+
if symbol.Name == newSymbols[k].Name && symbol.Range.Start.Line == newSymbols[k].Range.Start.Line {
446+
duplicate = true
447+
break
448+
}
449+
}
450+
if !duplicate {
418451
symbol.SelectionRange.Start.Line = data.sourceLineMap[symbol.SelectionRange.Start.Line]
419452
symbol.SelectionRange.End.Line = data.sourceLineMap[symbol.SelectionRange.End.Line]
420-
421-
duplicate := false
422-
for j := 0; j < i; j++ {
423-
if symbol.Name == (*symbols)[j].Name && symbol.Range.Start.Line == (*symbols)[j].Range.Start.Line {
424-
duplicate = true
425-
break
426-
}
427-
}
428-
if duplicate {
429-
*symbols = (*symbols)[:i+copy((*symbols)[i:], (*symbols)[i+1:])]
430-
} else {
431-
i++
432-
}
453+
symbol.Children = handler.cpp2inoDocumentSymbols(symbol.Children, uri)
454+
newSymbols[j] = *symbol
455+
j++
433456
}
434457
}
458+
return newSymbols[:j]
435459
}
436460

437461
// FromClangd handles a message received from clangd.

Diff for: handler/protocol.go

+8
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ func readParams(method string, raw *json.RawMessage) (interface{}, error) {
6666
params := new(lsp.DocumentSymbolParams)
6767
err := json.Unmarshal(*raw, params)
6868
return params, err
69+
case "textDocument/rename":
70+
params := new(lsp.RenameParams)
71+
err := json.Unmarshal(*raw, params)
72+
return params, err
6973
case "textDocument/publishDiagnostics":
7074
params := new(lsp.PublishDiagnosticsParams)
7175
err := json.Unmarshal(*raw, params)
@@ -122,6 +126,10 @@ func sendRequest(ctx context.Context, conn *jsonrpc2.Conn, method string, params
122126
result := new([]DocumentSymbol)
123127
err := conn.Call(ctx, method, params, result)
124128
return result, err
129+
case "textDocument/rename":
130+
result := new(lsp.WorkspaceEdit)
131+
err := conn.Call(ctx, method, params, result)
132+
return result, err
125133
case "window/showMessageRequest":
126134
result := new(lsp.MessageActionItem)
127135
err := conn.Call(ctx, method, params, result)

0 commit comments

Comments
 (0)