Skip to content

Document reference search for constructiors/initializations #10725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
umanwizard opened this issue Nov 8, 2021 · 8 comments
Open

Document reference search for constructiors/initializations #10725

umanwizard opened this issue Nov 8, 2021 · 8 comments
Labels
A-documentation docs for rust-analyzer usage/inner working A-ide general IDE features E-easy S-actionable Someone could pick this issue up and work on it right now

Comments

@umanwizard
Copy link
Contributor

struct S {
    pub x: usize,
}

fn print_s(s: S) {
    println!("{}", s.x)
}

fn main() {
    let s = S { x: 42 };
    print_s(s);
}

"find references" on S in the above program will find both the usage of S in the type of the parameter to print_s, as well as the use of S in the instantiation of s in main. However, I often want to see all the places where a struct is created, without having to hunt through the list of all references for those specific ones.

@Veykril
Copy link
Member

Veykril commented Nov 8, 2021

We actually already have this, though rather obsure(and possibly not really documented).
If you invoke reference search with $0 being the cursor like

struct S$0 {
    pub x: usize,
}

fn print_s(s: S) {
    println!("{}", s.x)
}

fn main() {
    let s = S { x: 42 };
    print_s(s);
}

(that is the cursor at the far end of the definition's ident)
you will get all constructor usages returned. The UX is obviously not really that great but we are somewhat limited by the language server protocol in this regard here.

Nevertheless we should document this behaviour better, assuming we don't yet.

@Veykril Veykril added the A-ide general IDE features label Nov 8, 2021
@umanwizard
Copy link
Contributor Author

The UX is obviously not really that great but we are somewhat limited by the language server protocol in this regard here.

Doesn't rust-analyzer have many extensions to the LSP already?

I could imagine r-a decides to supports thi, and then upstreams a proposal to the folks maintaining LSP (I guess Microsoft). Is this not a good model for how the process works?

In the meantime, thanks for the workaround.

@Veykril
Copy link
Member

Veykril commented Nov 8, 2021

The main problem here is the question how to even expose this, we could make an extension out of this but that would require each client to somehow expose this special search as well. And in VSCode for example I don't think there is a decent way to offer more "kinds" of reference searches than the default.

@umanwizard
Copy link
Contributor Author

The workaround doesn't work for me on version 04f03a360 2021-11-02 dev. It still finds all the references. Relevant parts of the server/client communication below:

[Trace - 12:37:12 PM] Sending request 'textDocument/references - (1396)'.
Params: {
  "textDocument": {
    "uri": "file:///Users/brennan/test_rust/src/main.rs"
  },
  "position": {
    "line": 0,
    "character": 8
  },
  "context": {
    "includeDeclaration": false
  }
}


[Trace - 12:37:12 PM] Received response 'textDocument/references - (1396)' in 1ms.
Result: [
  {
    "range": {
      "end": {
        "character": 15,
        "line": 4
      },
      "start": {
        "character": 14,
        "line": 4
      }
    },
    "uri": "file:///Users/brennan/test_rust/src/main.rs"
  },
  {
    "range": {
      "end": {
        "character": 13,
        "line": 9
      },
      "start": {
        "character": 12,
        "line": 9
      }
    },
    "uri": "file:///Users/brennan/test_rust/src/main.rs"
  }
]

@umanwizard
Copy link
Contributor Author

The main problem here is the question how to even expose this, we could make an extension out of this but that would require each client to somehow expose this special rename as well. And in VSCode for example I don't think there is a decent way to offer more "kinds" of renames than the default.

Did you mean to leave this comment on my other issue, #10713 ?

@Veykril
Copy link
Member

Veykril commented Nov 8, 2021

Ah my bad, I meant after the last character, not on the last(So in this case the whitespace char after the identifier).

Did you mean to leave this comment on my other issue, #10713 ?

Ah I typed rename by accident, meant reference search of course.

@Veykril
Copy link
Member

Veykril commented Nov 8, 2021

Wrong again, its on the { char apparently(I rarely used this myself and with no docs I actually had to go check 😅)
So

struct S $0{
   ...
}

should show you all constructor usages, same applies for ( and ; for the different struct and enum variant variations.

@Veykril Veykril changed the title Feature request: find specific types of references (e.g., instantiations) Document reference search for constructiors/initializations Nov 8, 2021
@umanwizard
Copy link
Contributor Author

Thanks, that works.

@Veykril Veykril added E-easy S-actionable Someone could pick this issue up and work on it right now labels Nov 8, 2021
@Veykril Veykril added the A-documentation docs for rust-analyzer usage/inner working label Mar 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-documentation docs for rust-analyzer usage/inner working A-ide general IDE features E-easy S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

No branches or pull requests

2 participants