Skip to content

Commit bd5ee6b

Browse files
committed
Handle rename symbol
1 parent b3d9169 commit bd5ee6b

File tree

5 files changed

+78
-6
lines changed

5 files changed

+78
-6
lines changed

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ require (
99
github.com/pkg/errors v0.9.1
1010
github.com/stretchr/testify v1.7.0
1111
go.bug.st/json v1.15.6
12-
go.bug.st/lsp v0.0.0-20211130152916-c597b0a0439f
12+
go.bug.st/lsp v0.0.0-20211202163946-3ad3994172a0
1313
google.golang.org/grpc v1.42.0
1414
)

Diff for: go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ go.bug.st/json v1.15.6 h1:pvSpotu6f5JoCbx1TnKn6asVH7o9Tg2/GKsZSVzBOsc=
339339
go.bug.st/json v1.15.6/go.mod h1:bh58F9adz5ePlNqtvbuXuXcf9k6IrDLKH6lJUsHP3TI=
340340
go.bug.st/lsp v0.0.0-20211130152916-c597b0a0439f h1:Rj7FdBdROWh9mMra/16G/5d7u/QE0Wwq487NZt+Evjg=
341341
go.bug.st/lsp v0.0.0-20211130152916-c597b0a0439f/go.mod h1:oYTh1uf5hI1teV5crrWut41Pk8vD/NqIjs4zD+No5FE=
342+
go.bug.st/lsp v0.0.0-20211202163946-3ad3994172a0 h1:/SnZ7aZ3bmfUKWbhckiK6L4mv9vyf9HgwV6X0dm+/is=
343+
go.bug.st/lsp v0.0.0-20211202163946-3ad3994172a0/go.mod h1:oYTh1uf5hI1teV5crrWut41Pk8vD/NqIjs4zD+No5FE=
342344
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18 h1:F1qxtaFuewctYc/SsHRn+Q7Dtwi+yJGPgVq8YLtQz98=
343345
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18/go.mod h1:Cx1VqMtEhE9pIkEyUj3LVVVPkv89dgW8aCKrRPDR/uE=
344346
go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=

Diff for: ls/ls.go

+38-4
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,14 @@ func (ls *INOLanguageServer) TextDocumentCompletionReqFromIDE(ctx context.Contex
371371
ls.readLock(logger, true)
372372
defer ls.readUnlock(logger)
373373

374-
cppTextDocPositionParams, err := ls.ide2ClangTextDocumentPositionParams(logger, ideParams.TextDocumentPositionParams)
374+
clangTextDocPositionParams, err := ls.ide2ClangTextDocumentPositionParams(logger, ideParams.TextDocumentPositionParams)
375375
if err != nil {
376376
logger.Logf("Error: %s", err)
377377
return nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
378378
}
379379

380380
clangParams := &lsp.CompletionParams{
381-
TextDocumentPositionParams: cppTextDocPositionParams,
381+
TextDocumentPositionParams: clangTextDocPositionParams,
382382
Context: ideParams.Context,
383383
WorkDoneProgressParams: ideParams.WorkDoneProgressParams,
384384
PartialResultParams: ideParams.PartialResultParams,
@@ -637,14 +637,14 @@ func (ls *INOLanguageServer) TextDocumentImplementationReqFromIDE(ctx context.Co
637637
ls.readLock(logger, true)
638638
defer ls.readUnlock(logger)
639639

640-
cppTextDocumentPosition, err := ls.ide2ClangTextDocumentPositionParams(logger, ideParams.TextDocumentPositionParams)
640+
clangTextDocumentPosition, err := ls.ide2ClangTextDocumentPositionParams(logger, ideParams.TextDocumentPositionParams)
641641
if err != nil {
642642
logger.Logf("Error: %s", err)
643643
return nil, nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
644644
}
645645

646646
clangParams := &lsp.ImplementationParams{
647-
TextDocumentPositionParams: cppTextDocumentPosition,
647+
TextDocumentPositionParams: clangTextDocumentPosition,
648648
WorkDoneProgressParams: ideParams.WorkDoneProgressParams,
649649
PartialResultParams: ideParams.PartialResultParams,
650650
}
@@ -1212,6 +1212,40 @@ func (ls *INOLanguageServer) PublishDiagnosticsNotifFromClangd(logger jsonrpc.Fu
12121212
}
12131213
}
12141214

1215+
func (ls *INOLanguageServer) TextDocumentRenameReqFromIDE(ctx context.Context, logger jsonrpc.FunctionLogger, ideParams *lsp.RenameParams) (*lsp.WorkspaceEdit, *jsonrpc.ResponseError) {
1216+
ls.writeLock(logger, false)
1217+
defer ls.writeUnlock(logger)
1218+
1219+
clangTextDocPositionParams, err := ls.ide2ClangTextDocumentPositionParams(logger, ideParams.TextDocumentPositionParams)
1220+
if err != nil {
1221+
logger.Logf("Error: %s", err)
1222+
return nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
1223+
}
1224+
1225+
clangParams := &lsp.RenameParams{
1226+
TextDocumentPositionParams: clangTextDocPositionParams,
1227+
NewName: ideParams.NewName,
1228+
WorkDoneProgressParams: ideParams.WorkDoneProgressParams,
1229+
}
1230+
clangWorkspaceEdit, clangErr, err := ls.Clangd.conn.TextDocumentRename(ctx, clangParams)
1231+
if err != nil {
1232+
logger.Logf("clangd communication error: %v", err)
1233+
ls.Close()
1234+
return nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
1235+
}
1236+
if clangErr != nil {
1237+
logger.Logf("clangd response error: %v", clangErr.AsError())
1238+
return nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: clangErr.AsError().Error()}
1239+
}
1240+
1241+
ideWorkspaceEdit, err := ls.clang2IdeWorkspaceEdit(logger, clangWorkspaceEdit)
1242+
if err != nil {
1243+
logger.Logf("Error: %s", err)
1244+
return nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
1245+
}
1246+
return ideWorkspaceEdit, nil
1247+
}
1248+
12151249
func (ls *INOLanguageServer) ProgressNotifFromClangd(logger jsonrpc.FunctionLogger, progress *lsp.ProgressParams) {
12161250
var token string
12171251
if err := json.Unmarshal(progress.Token, &token); err != nil {

Diff for: ls/ls_clang_to_ide.go

+36
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ func (ls *INOLanguageServer) clangURIRefersToIno(clangURI lsp.DocumentURI) bool
1212
return clangURI.AsPath().EquivalentTo(ls.buildSketchCpp)
1313
}
1414

15+
// Convert Range and DocumentURI from Clang to IDE.
16+
// Returns:
17+
// - The IDE DocumentURI and Range
18+
// - a boolean that is true if the clang range is in the preprocessed area of the sketch
19+
// - an error
1520
func (ls *INOLanguageServer) clang2IdeRangeAndDocumentURI(logger jsonrpc.FunctionLogger, clangURI lsp.DocumentURI, clangRange lsp.Range) (lsp.DocumentURI, lsp.Range, bool, error) {
1621
// Sketchbook/Sketch/Sketch.ino <-> build-path/sketch/Sketch.ino.cpp
1722
// Sketchbook/Sketch/AnotherTab.ino <-> build-path/sketch/Sketch.ino.cpp (different section from above)
@@ -308,3 +313,34 @@ func (ls *INOLanguageServer) clang2IdeSymbolsInformation(logger jsonrpc.Function
308313
logger.Logf("SymbolInformation (%d elements):", len(clangSymbolsInformation))
309314
panic("not implemented")
310315
}
316+
317+
func (ls *INOLanguageServer) clang2IdeWorkspaceEdit(logger jsonrpc.FunctionLogger, clangWorkspaceEdit *lsp.WorkspaceEdit) (*lsp.WorkspaceEdit, error) {
318+
ideChanges := map[lsp.DocumentURI][]lsp.TextEdit{}
319+
for clangURI, clangChanges := range clangWorkspaceEdit.Changes {
320+
for _, clangTextEdit := range clangChanges {
321+
ideURI, ideTextEdit, isPreprocessed, err := ls.clang2IdeTextEdit(logger, clangURI, clangTextEdit)
322+
if isPreprocessed {
323+
logger.Logf("- ignore edit in preprocessed area")
324+
continue
325+
}
326+
if err != nil {
327+
return nil, err
328+
}
329+
ideChanges[ideURI] = append(ideChanges[ideURI], ideTextEdit)
330+
}
331+
}
332+
ideWorkspaceEdit := &lsp.WorkspaceEdit{
333+
Changes: ideChanges,
334+
ChangeAnnotations: clangWorkspaceEdit.ChangeAnnotations,
335+
}
336+
return ideWorkspaceEdit, nil
337+
}
338+
339+
func (ls *INOLanguageServer) clang2IdeTextEdit(logger jsonrpc.FunctionLogger, clangURI lsp.DocumentURI, clangTextEdit lsp.TextEdit) (lsp.DocumentURI, lsp.TextEdit, bool, error) {
340+
ideURI, ideRange, isPreprocessed, err := ls.clang2IdeRangeAndDocumentURI(logger, clangURI, clangTextEdit.Range)
341+
ideTextEdit := lsp.TextEdit{
342+
NewText: clangTextEdit.NewText,
343+
Range: ideRange,
344+
}
345+
return ideURI, ideTextEdit, isPreprocessed, err
346+
}

Diff for: ls/lsp_server_ide.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func (server *IDELSPServer) TextDocumentOnTypeFormatting(ctx context.Context, lo
155155
}
156156

157157
func (server *IDELSPServer) TextDocumentRename(ctx context.Context, logger jsonrpc.FunctionLogger, params *lsp.RenameParams) (*lsp.WorkspaceEdit, *jsonrpc.ResponseError) {
158-
panic("unimplemented")
158+
return server.ls.TextDocumentRenameReqFromIDE(ctx, logger, params)
159159
}
160160

161161
func (server *IDELSPServer) TextDocumentFoldingRange(ctx context.Context, logger jsonrpc.FunctionLogger, params *lsp.FoldingRangeParams) ([]lsp.FoldingRange, *jsonrpc.ResponseError) {

0 commit comments

Comments
 (0)