diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -316,16 +316,21 @@ return CB(InpAST.takeError()); auto &AST = InpAST->AST; const auto &SM = AST.getSourceManager(); - SourceLocation Loc = - SM.getMacroArgExpandedLocation(getBeginningOfIdentifier( - Pos, AST.getSourceManager(), AST.getLangOpts())); - auto Range = getTokenRange(SM, AST.getLangOpts(), Loc); - if (!Range) - return CB(llvm::None); // "rename" is not valid at the position. + auto Loc = sourceLocationInMainFile(SM, Pos); + if (!Loc) + return CB(Loc.takeError()); + const auto *TouchingIdentifier = + spelledIdentifierTouching(*Loc, AST.getTokens()); + if (!TouchingIdentifier) + return CB(llvm::None); // no rename on non-identifiers. + + auto Range = halfOpenToRange( + SM, CharSourceRange::getCharRange(TouchingIdentifier->location(), + TouchingIdentifier->endLocation())); if (CrossFileRename) // FIXME: we now assume cross-file rename always succeeds, revisit this. - return CB(*Range); + return CB(Range); // Performing the local rename isn't substantially more expensive than // doing an AST-based check, so we just rename and throw away the results. @@ -338,7 +343,7 @@ // the message to users (VSCode does). return CB(Changes.takeError()); } - return CB(*Range); + return CB(Range); }; WorkScheduler.runWithAST("PrepareRename", File, std::move(Action)); } diff --git a/clang-tools-extra/clangd/test/rename.test b/clang-tools-extra/clangd/test/rename.test --- a/clang-tools-extra/clangd/test/rename.test +++ b/clang-tools-extra/clangd/test/rename.test @@ -21,12 +21,9 @@ # CHECK-NEXT: } --- {"jsonrpc":"2.0","id":2,"method":"textDocument/prepareRename","params":{"textDocument":{"uri":"test:///foo.cpp"},"position":{"line":0,"character":2}}} -# CHECK: "error": { -# CHECK-NEXT: "code": -32001, -# CHECK-NEXT: "message": "Cannot rename symbol: there is no symbol at the given location" -# CHECK-NEXT: }, -# CHECK-NEXT: "id": 2, -# CHECK-NEXT: "jsonrpc": "2.0" +# CHECK: "id": 2, +# CHECK-NEXT: "jsonrpc": "2.0", +# CHECK-NEXT: "result": null --- {"jsonrpc":"2.0","id":4,"method":"textDocument/rename","params":{"textDocument":{"uri":"test:///foo.cpp"},"position":{"line":0,"character":2},"newName":"bar"}} # CHECK: "error": {