orta / vscode-twoslash-queries

VS Code extension which adds support for twoslash queries into typescript projects
https://marketplace.visualstudio.com/items?itemName=Orta.vscode-twoslash-queries
MIT License
419 stars 15 forks source link

Add the ability to copy the query as text #21

Open DanKaplanSES opened 1 year ago

DanKaplanSES commented 1 year ago

This is a feature request. Often, I'm using the query to document a code example so I can get help on it. But if I copy the code sample and paste it, it copies only // ^?. I would like it to copy the full expanded text with it, that way, when I share my code, other people can see the types.

orta commented 1 year ago

Afraid that's not possible without vscode support for the feature - maybe a right click command might be possible, you'd have to look into the APIs

DanKaplanSES commented 1 year ago

Clarifying question: are you saying that this can't be done with an InlayHintsProvider?

What follows are assumptions built on top of assumptions:

Assuming that's what you meant, hypothetically, could an extension directly write the text in the editor instead of using a InlayHintsProvider? (I'm not trying to put the onus of this research on your plate, I just thought you might know off the top of your head.)

And assuming the answer to that question is yes, would you consider changing the extension to do that / adding a setting to do that?

jasikpark commented 1 year ago

Maybe the better option would be a code action to display the type of an identifier / to copy it to the clipboard?

DanKaplanSES commented 1 year ago

@jasikpark When you say "it," do you mean the text of the InlayHintsProvider? I could see that being useful. It's definitely better than typing out the content of the InlayHintsProvider by hand.

So, with that feature, these are the steps to share some code that includes two slash on stack overflow or slack etc.

  1. Copy the text of the code
  2. Paste it in a textbox
  3. Return to vs code.
  4. Copy the text of a // ^?
  5. Paste it in the appropriate line in the textbox
  6. If there are more lines with // ^?, repeat step 4.

Am I understanding correctly?

jasikpark commented 1 year ago

moreso that you could highlight an identifier to interact with it to perform those actions, rather than copying text, which would not be viable

DanKaplanSES commented 1 year ago

@jasikpark Apologies for my confusion. Assuming you could highlight an identifier to interact with it, what would be the steps to perform this use case:

I would like it to share a section of my code outside of VSCode (e.g., pasted into my browser to ask a stack overflow question) while allowing others to see the twoslash types.

jerone commented 1 year ago

It looks like this is possible with InlayHintsProvider. The InlayHint interface has an property label which can be an string (as in this extension), but it can also be an InlayHintLabelPart interface, which provides again the label property, but now also an optional command of interface Command, allowing you to execute your own command to copy whats inside your hint.

jasikpark commented 1 year ago

oooh https://github.com/JoshuaKGoldberg/eslint-plugin-expect-type/pull/47 from https://github.com/microsoft/TypeScript/issues/52839 seems like a good solution for your purposes @DanKaplanSES

DanKaplanSES commented 1 year ago

@jasikpark https://github.com/microsoft/TypeScript/issues/52839 sounds perfect to me. Using https://github.com/JoshuaKGoldberg/eslint-plugin-expect-type/pull/47 is an interesting idea; are you suggesting I intentionally create invalid twoslash comment types and let the plug-in fill in the correct comment string? That'd work nicely, so thanks for letting me know.

Question: will I have to uninstall the "vscode twoslash queries" plug-in to use this? It seems like they would fight with each other.

Regardless, IMHO it would be even better if the twoslash comment type automatically kept up to date, but since the linter will catch errors, your idea is like 95% of the way there. eslint / vscode may even have an automated way of fixing these on file change, which is like 100% of the way there.

I'm not sure where this leaves this issue.

jasikpark commented 1 year ago

even with this solution i've found it annoying that I can't copy the inferred type for use in a satisfies clause