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

x/tools/gopls: ctrl-click should jump to source, not to autogenerated code #65001

Open
guettli opened this issue Jan 7, 2024 · 9 comments
Open
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@guettli
Copy link

guettli commented Jan 7, 2024

gopls version

(does not matter here)

go env

(does not matter here)

What did you do?

I use Go, vscode and templ.guide

The tool templ creates Go code from templates.

I am looking at my Go file and I use a function which was auto-generated by templ:

If I ctrl-click on page() then ...

image

then I get to the autogenerated Go code:

image

It would be great if I could get to the templ-file instead:

image

As a user, the experience is not great, because I would like to jump to the original source (the templ template).

Does gpls provide hooks, which templ could implement, so that ctrl-click jumps directly to the templ template?

Related issue: a-h/templ#387

@guettli guettli added gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository. labels Jan 7, 2024
@gopherbot gopherbot added this to the Unreleased milestone Jan 7, 2024
@icholy
Copy link

icholy commented Jan 7, 2024

This would require something akin to sourcemaps.

@joerdav
Copy link

joerdav commented Jan 8, 2024

@icholy templ does in fact maintain sourcemaps, I think the main question is around whether templ can hook in to gopls to override the jump to definition, or if it would require using templ as the main lsp for go code (which I don't think would be ideal)

@findleyr
Copy link
Contributor

findleyr commented Jan 8, 2024

So, a while back gopls would maintain two positions for every position: the "compiled" position, and whichever position was referenced by //line directives. As you might imagine, passing two positions throughout the call stack lead to a lot of complication and misuse, and so //line directives were rather buggy. For that reason, we removed them in #55043.

A cleaner solution would be to only handle line directives at API boundaries, for example in a func(protocol.Location) []protocol.Location that expanded or replaced positions based on //line directives. Such a function would not be that hard to write, but we haven't done so yet. In part, we were waiting to see if anyone cared, because we didn't know how anyone could have been relying on the old buggy implementation.

Does templ produce //line directives? Could it?

@findleyr findleyr modified the milestones: Unreleased, gopls/v0.16.0 Jan 8, 2024
@joerdav
Copy link

joerdav commented Jan 8, 2024

Currently templ does not produce line directives. Though, I believe it could quite easily, as a source map is already maintained.

I imagine it would look something like this in a generated file, if I understand correctly (index_templ.go):

//line index.templ:20
func Index() templ.Component {
...

@findleyr
Copy link
Contributor

findleyr commented Jan 8, 2024

@joerdav yes, that looks right.

@joerdav
Copy link

joerdav commented Jan 9, 2024

I'll give this a go in templ. Given that this isn't currently supported in gopls does the compiler directive have any effect at the moment?

@findleyr
Copy link
Contributor

findleyr commented Jan 9, 2024

The compiler directive may affect the location of error messages. Since I wouldn't expect generated code to have errors, this may not matter.

Can you point me to an example of templ's current sourcemaps?

@joerdav
Copy link

joerdav commented Jan 9, 2024

Cool makes sense, ah I think you're talking about some sourcemap files, whereas templ only really holds it in memory for LSP purposes: https://github.com/a-h/templ/blob/main/parser/v2/sourcemap.go

@joerdav
Copy link

joerdav commented Jan 31, 2024

@findleyr We currently are processing a PR in templ to add //line directives to generated code. To support the potential release of this feature in gopls. a-h/templ#476

I've also done a proof-of-concept to check if this is possible in gopls, I fully would intend to tidy this up and add tests, but just wanted to check if I was on the right track here: joerdav/tools@63456ed

This does work on an example project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants