Skip to content

feat(lsp): Support gotoDefinition from go->templ#932

Closed
tris203 wants to merge 3 commits into
a-h:mainfrom
tris203:def
Closed

feat(lsp): Support gotoDefinition from go->templ#932
tris203 wants to merge 3 commits into
a-h:mainfrom
tris203:def

Conversation

@tris203
Copy link
Copy Markdown
Contributor

@tris203 tris203 commented Sep 24, 2024

Support goToDefinition from a go file to a templ file.

Steps to test:

  1. Attach both gopls and templ lsp to a main.go buffer
  2. GoToDefinition
  3. Profit

Caveats, because gopls and templ are attached, both references will be returned. Editor settings can be used to set preference to select from a list or go to the preferred option (probably the templ file)

When and if this was merged, it would need further adjustments to neovim/lspconfig to attach templ to go files too and would probably need a change in the vscode extension for the same

Closes #387

@joerdav
Copy link
Copy Markdown
Collaborator

joerdav commented Jan 27, 2025

Sorry for a delay on this one. This is a great addition, many have said a pain point is gotodefinition from go to templ and this is the first step!

Editor settings can be used to set preference to select from a list or go to the preferred option (probably the templ file)

I wonder if we should add the docs for how to do the above along side this change?

@a-h
Copy link
Copy Markdown
Owner

a-h commented Jan 27, 2025

I looked at this the other day, and couldn't get it to work. Probably because I hadn't told my editor to use the templ LSP for Go files too, so I'd say we do need to do that.

@tris203
Copy link
Copy Markdown
Contributor Author

tris203 commented Jan 27, 2025

I have done it in my config for neovim, when it comes to other editors:

Neovim: lspconfig change
VSCode: I assume an extension change

helix/emacs, will need PRs to the relevant repos

Am i missing anything?

@joerdav
Copy link
Copy Markdown
Collaborator

joerdav commented Jan 28, 2025

I think that's correct yes. If we just update those things and there are no user config changes required apart from updating configs then maybe it's okay to merge this now at this point? Then we can gradually update the extensions etc?

@tris203
Copy link
Copy Markdown
Contributor Author

tris203 commented May 31, 2025

Is there any blockers for this, or anything further I can do to help move this along?

Edit: will address the test failure that was easy, was just logging change and test file name changes

@Napam
Copy link
Copy Markdown

Napam commented Apr 20, 2026

I have done it in my config for neovim, when it comes to other editors:

Neovim: lspconfig change VSCode: I assume an extension change

helix/emacs, will need PRs to the relevant repos

Am i missing anything?

Just wondering, so with this approach one uses templ lsp for everything go, or does some dynamic handling to detect "if templ detected then templ lsp else gopls"?

@joerdav
Copy link
Copy Markdown
Collaborator

joerdav commented Apr 20, 2026

@Napam The templ lsp itself connects to a version of gopls, so you don't have to tell your editor to run both explicitly. The "if templ ..." stuff actually happens within templ itself. Therefore, with this PR if you configure neovim to run templ lsp for .go files it will handle it. There's nothing stopping you running both, but you might get multiple results for some actions.

@a-h
Copy link
Copy Markdown
Owner

a-h commented May 11, 2026

Thanks a lot for your work on this. I eventually got around to thinking about this, and put together two PRs at:

I don't think I would have got to it without your nudge.

@a-h a-h closed this May 11, 2026
@tris203 tris203 deleted the def branch May 11, 2026 21:30
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 this pull request may close these issues.

lsp: Support goToDefinition from a Go file to a templ file

5 participants