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

Show expanded code of freestanding macro expansions in hover #755

Open
adam-fowler opened this issue Jun 9, 2023 · 12 comments · May be fixed by #1436
Open

Show expanded code of freestanding macro expansions in hover #755

adam-fowler opened this issue Jun 9, 2023 · 12 comments · May be fixed by #1436

Comments

@adam-fowler
Copy link
Contributor

Xcode has a nice feature where you can expand macros to see the code they generate. It'd be cool if you could do this with VSCode as well. Could we add a CodeAction or something similar to provide the code expansion?

@ahoppen
Copy link
Collaborator

ahoppen commented Jun 9, 2023

Tracked in Apple’s issue tracker as rdar://110516432

@ahoppen
Copy link
Collaborator

ahoppen commented Jun 27, 2023

I think we do have a code action that does this. If you create a new macro template and build it, you can right click on stringify in main.swift and select Refactor… -> Inline Macro.

It’s not quite what Xcode does with the code roll. If you know if VSCode has an option to show the expanded source code inline, we should be able to figure something out how to give you the necessary expansion information.

@adam-fowler
Copy link
Contributor Author

I haven't looked into how VS Code would display the expanded macros but other extensions have added additions to the source view, so it should be possible. For example the GitHub extension allows you to see PR comments.

@ahoppen
Copy link
Collaborator

ahoppen commented Jun 27, 2023

I suspect that that would probably require dedicated support in the Swift VSCode extension. If you figure out how to display those inline text hint, I’m happy to create an LSP request that gives you the text to display.

@ahoppen
Copy link
Collaborator

ahoppen commented Jul 7, 2023

AFAICT there’s nothing actionable for SourceKit-LSP here, so I’m closing this issue. Could you file another issue once you’ve got support in the VSCode plugin to display the macro expansion and then we can discuss what kind of SourceKit-LSP request you need?

@ahoppen ahoppen closed this as completed Jul 7, 2023
@adam-fowler
Copy link
Contributor Author

What's your thoughts on including the macro expansion in the textDocument/hover output. eg

### Expands to
(x + y, "x + y")

@ahoppen
Copy link
Collaborator

ahoppen commented Jul 7, 2023

I don’t think that scales. In a lot of cases the generated code will span over multiple lines and I don’t think that makes for a good hover response. Also, for attached macros, you would want to show where the generated code is being inserted (e.g. in case of a peer macro) and that also doesn’t fit with the design of hover.

@adam-fowler
Copy link
Contributor Author

I don't think spanning over multiple lines is a huge issue. The discussion part of a hover comment can cover many lines. Obviously the positioning is a different issue.

We might not be given the UI capabilities in VSCode to insert read only text sections into a text document. So this maybe the only option.

@ahoppen
Copy link
Collaborator

ahoppen commented Jul 7, 2023

Do you know which other plugins do show read-only code inline and how they do it? Is it only supported for first-party plugins by Microsoft?

@adam-fowler
Copy link
Contributor Author

This is how cpp-tools does it

ie as part of the hover window

I can't find anything that adds read only sections to text windows. They have added support for a full comments UI with threads and inline editing of comments. But nothing as simple add this block of text.

@ahoppen
Copy link
Collaborator

ahoppen commented Jul 10, 2023

Ah, OK. Thanks for double-checking. Let’s use this issue to track showing the expanded code for freestanding macro expansions. Attached macro expansions are more tricky because I’m still not sure where to display them, so let’s just skip them for now and not let it stop us doing the simple case.

@ahoppen ahoppen reopened this Jul 10, 2023
@ahoppen ahoppen changed the title Is it possible for Sourcekit-LSP to provide macro expansions? Show expanded code of freestanding macro expansions in hover Jul 10, 2023
@bnbarham
Copy link
Contributor

How is documentation displayed, is that hover?

For all but accessor macros, we could use the insertion position to add some context to the provided generated code. That could be a comment or some extra source. For accessor we'd really need the kind included in the response as well, but there's no reason we couldn't add that.

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

Successfully merging a pull request may close this issue.

3 participants