Skip to content
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

wish: add key to kill-ring-save the inferred type #1708

Closed
nahuel opened this issue May 20, 2020 · 9 comments
Closed

wish: add key to kill-ring-save the inferred type #1708

nahuel opened this issue May 20, 2020 · 9 comments

Comments

@nahuel
Copy link

nahuel commented May 20, 2020

Many times I want to type an inferred type, it will be nice to have a shorcut to copy the type below the cursor. Typescript example:

type SomeType = 'q' | 'w'
const b = () => 'q' as SomeType

function a(arg /* here I want to type " : " and then paste "SomeType" */ ) {
  const x = b() 
     // ^ Cursor here
}

Here cursor is over x, so the inferred type is shown in the minibuffer as const x: SomeType. Now it will be nice to press a (new) shorcut that copies the x type name to the clipboard, so then you can move the cursor after arg, type : and then C-Y to paste SomeType, to declare the type of arg as the same of x.

@yyoncho
Copy link
Member

yyoncho commented May 21, 2020

so the inferred type is shown in the minibuffer as const x: SomeType

The issue is that this information is not structured and might not be the type itself. I think that the only reliable server-side method for that matter is documentSymbols call(corresponds to imenu). We could use it to copy the name of the wrapping function, copy the name of the wrapping class, etc.

@nahuel
Copy link
Author

nahuel commented May 21, 2020

Can the mechanism behind lsp-rust-analyzer-inlay-hints-mode be reused for getting the expression type in a way generic for all LSP backends? (note, I didn't read the LSP spec)

@nahuel
Copy link
Author

nahuel commented May 21, 2020

Note, I filled a similar wish for vscode: microsoft/vscode#98267

@yyoncho
Copy link
Member

yyoncho commented May 21, 2020

Can the mechanism behind lsp-rust-analyzer-inlay-hints-mode be reused for getting the expression type in a way generic for all LSP backends?

This is RA specific. We could provide a mechanism to copy the inlay-hint on the line, this will be easy, but it will work only for rust analyzer.

@nahuel
Copy link
Author

nahuel commented May 21, 2020

Another option maybe is to parse what the minibuffer shows, at least for const / let declarations, and extract the type from here. FYI just found a proposal from the Rust people to add inlay hints to the LSP spec: microsoft/language-server-protocol#956 . But maybe the LSP spec needs a more general "get the type at position" functionality than that?

@yyoncho
Copy link
Member

yyoncho commented May 21, 2020

But maybe the LSP spec needs a more general "get the type at position" functionality than that?

Yep, in order to work consistently.

Another option maybe is to parse what the minibuffer shows, at least for const / let declarations,

I tend to avoid such ad-hoc solutions because they cause trouble. After all, lsp protocol is to avoid client side parsing.

@nahuel
Copy link
Author

nahuel commented May 21, 2020

ok, I understand. Note, others are finding this same LSP limitation and for a more general usage, as getting the type of a selected expression instead of only of the symbol at cursor:
rust-lang/rust-analyzer#389
https://github.com/digital-asset/ghcide/issues/70

@yyoncho
Copy link
Member

yyoncho commented May 21, 2020

yep, unfortunately, the spec is designed for point-and-click usage.

@yyoncho
Copy link
Member

yyoncho commented May 20, 2021

closing based on the previous comment. Once the spec adds support for it we can implement the client-side code.

@yyoncho yyoncho closed this as completed May 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants