clangd / vscode-clangd

Visual Studio Code extension for clangd
https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd
MIT License
592 stars 97 forks source link

Incorrect results about Go-to-definition and Go-to-declaration #634

Open tisble opened 1 month ago

tisble commented 1 month ago

Hi, I found a bug about the go to definition and go to declaration. Please see the code example below:

#include  <stdio.h>
struct str {
    int v;
};
int main() {
    str s;
    s.v = 10;
    return 0;
}

I performed Go-to-def/decl on the variable s in declaration statement at line 6, but it jumps to the variable s at line 7. Actually, I think this is Go to Use or Find References, rather than Go-to-def or decl (should be unchanged at line 6). So, this is a bug.

System information

Clangd version: Apple clangd version 15.0.0 (clang-1500.3.9.4) Features: mac+xpc Platform: x86_64-apple-darwin23.5.0; target=arm64-apple-darwin23.5.0

clangd extension version: Version 0.1.28: March 20, 2024

Operating system: MacOS

HighCommander4 commented 1 month ago

It looks like the client sides a textDocument/references request if the response to textDocument/definition was the same location as the input.

This is a behaviour of the vscode-languageclient component that lives upstream at https://github.com/Microsoft/vscode-languageserver-node/, could you file an issue there please?

tisble commented 1 month ago

Hi, I have submitted the issue and the link is: https://github.com/microsoft/vscode-languageserver-node/issues/1486

HighCommander4 commented 1 month ago

This is a behaviour of the vscode-languageclient component

It turns out I was mistaken about this: this is actually a behaviour of vscode itself.

Moreover, it is configurable as follows:

  1. Open vscode settings
  2. Search for the setting name editor.gotoLocation.alternativeDefinitionCommand. Note the setting's description: "Alternative command id that is being executed when the result of 'Go to Definition' is the current location.", and default value, editor.action.goToReferences.
  3. Change the setting value in the dropdown to empty.

Now, go-to-def on s on line 6 will leave the cursor at the same location.