[clang-tools-extra] [clangd] Do not offer extraction to variable for decl init expression (PR #69477)

kadir çetinkaya via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 19 05:38:31 PST 2024


================
@@ -490,6 +491,13 @@ bool eligibleForExtraction(const SelectionTree::Node *N) {
         BO->getRHS() == OuterImplicit.ASTNode.get<Expr>())
       return false;
   }
+  if (const auto *Decl = Parent->ASTNode.get<VarDecl>()) {
+    if (!Decl->isInitCapture() &&
----------------
kadircet wrote:

sorry I don't think you can get that kind of behavior from a regular clangd. can you add a test case demonstrating how that comes to be?

here's a sample clangd test proving my point:
```
Content-Length: 1290

{"id":1,"jsonrpc":"2.0","method":"initialize","params":{"capabilities":{"textDocument":{"codeAction":{"codeActionLiteralSupport":{"codeActionKind":{"valueSet":["","quickfix","refactor","refactor.extract","refactor.inline","refactor.rewrite","source","source.organizeImports"]}}},"completion":{"completionItem":{"documentationFormat":["plaintext","markdown"]},"completionItemKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]}},"documentSymbol":{"hierarchicalDocumentSymbolSupport":false,"labelSupport":false,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]}},"hover":{"contentFormat":["plaintext","markdown"]},"signatureHelp":{"signatureInformation":{"documentationFormat":["plaintext","markdown"],"parameterInformation":{"labelOffsetSupport":true}}},"synchronization":{"didSave":true}},"workspace":{"applyEdit":true,"didChangeWatchedFiles":{"dynamicRegistration":true},"symbol":{"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]}},"workspaceEdit":{"documentChanges":true}}},"initializationOptions":{"clangdFileStatus":true},"processId":4179957,"rootPath":"/usr/local/google/home/kadircet/repos/tmp","rootUri":"file:///usr/local/google/home/kadircet/repos/tmp"}}Content-Length: 52

{"jsonrpc":"2.0","method":"initialized","params":{}}Content-Length: 109

{"jsonrpc":"2.0","method":"workspace/didChangeConfiguration","params":{"settings":{"clangdFileStatus":true}}}Content-Length: 193

{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"languageId":"cpp","text":"void foo() {\n  int x = 1 + 2 + 3;\n  return;\n}\n","uri":"file:///tmp/a.cc","version":1}}}Content-Length: 217

{"id":2,"jsonrpc":"2.0","method":"textDocument/codeAction","params":{"context":{"diagnostics":[]},"range":{"end":{"character":18,"line":1},"start":{"character":14,"line":1}},"textDocument":{"uri":"file:///tmp/a.cc"}}}Content-Length: 251

{"id":3,"jsonrpc":"2.0","method":"workspace/executeCommand","params":{"arguments":[{"file":"file:///tmp/a.cc","selection":{"end":{"character":18,"line":1},"start":{"character":14,"line":1}},"tweakID":"ExtractVariable"}],"command":"clangd.applyTweak"}}Content-Length: 50

{"id":0,"jsonrpc":"2.0","result":{"applied":true}}Content-Length: 232

{"jsonrpc":"2.0","method":"textDocument/didChange","params":{"contentChanges":[{"text":"void foo() {\n  auto placeholder = 1 + 2 + 3;\n  int x = placeholder;\n  return;\n}\n"}],"textDocument":{"uri":"file:///tmp/a.cc","version":2}}}Content-Length: 115

{"jsonrpc":"2.0","method":"textDocument/didClose","params":{"textDocument":{"uri":"file:///tmp/a.cc","version":2}}}Content-Length: 58

{"id":4,"jsonrpc":"2.0","method":"shutdown","params":null}Content-Length: 47

{"jsonrpc":"2.0","method":"exit","params":null}
```

some notable points here are:
- original file contents: `"text":"void foo() {\n  int x = 1 + 2 + 3;\n  return;\n}\n"`
- code action request range: `,"range":{"end":{"character":18,"line":1},"start":{"character":14,"line":1}}`, namely `[[2 + 3]]`.
- code action response: `"contentChanges":[{"text":"void foo() {\n  auto placeholder = 1 + 2 + 3;\n  int x = placeholder;\n  return;\n}\n"}]`

that's my clangd version string: `clangd version 19.0.0git (git at github.com:llvm/llvm-project.git 7e3fb372b0e8899958ec7e9241797e7e136a7a23)`

you can feed in the json request above into clangd with ` /path/to/clangd -log=verbose -sync < /tmp/clangd_input.mirror`.

https://github.com/llvm/llvm-project/pull/69477


More information about the cfe-commits mailing list