Skip to content

feat: add experimental support for Go files#1384

Merged
a-h merged 3 commits into
mainfrom
cross_file_support
Jul 15, 2026
Merged

feat: add experimental support for Go files#1384
a-h merged 3 commits into
mainfrom
cross_file_support

Conversation

@a-h

@a-h a-h commented May 10, 2026

Copy link
Copy Markdown
Owner

See #387

LSP proxy: .go file support and bug fixes

Summary

The proxy now handles requests from .go files (not just .templ), enabling cross-file navigation (e.g. "Go to Definition" on a templ component from a .go file). Several broken LSP methods are fixed.

How it works

Routing: proxyPositionRequest

Replaces the old updatePosition method. For .templ files, it converts the cursor position via source maps to the generated _templ.go position. For .go files, it passes the position through unchanged. Returns ok=false only when a .templ source map lookup fails, causing the handler to return nil.

Navigation methods (Definition, Declaration, References, TypeDefinition, Implementation, Rename, PrepareRename, PrepareCallHierarchy) all use this helper. After gopls responds, _templ.go URIs and ranges in results are converted back to .templ using convertLocationResults or convertCallHierarchyItem.

Duplicative features return nil for .go files

Hover, Completion, Formatting, RangeFormatting, CodeAction, DocumentHighlight, DocumentLink, SignatureHelp, OnTypeFormatting, FoldingRanges, SemanticTokens, and Moniker return nil when the request URI is a non-templ .go file. The Go extension already provides these for .go files.

Document sync forwards .go files

DidChange, DidOpen, and DidClose forward .go file notifications to gopls so it maintains accurate workspace state.

Shared conversion helpers (rewrite.go)

  • convertGoRangeToTemplRange -- uses the source map cache to map a Go range back to templ source.
  • convertLocationResults -- converts _templ.go URIs/ranges in []lsp.Location results.
  • convertCallHierarchyItem -- converts a call hierarchy item's URI, Range, and SelectionRange.
  • convertWorkspaceEdit -- converts _templ.go URIs/ranges in both DocumentChanges and Changes maps.
  • isNonTemplGoURI -- returns true for .go files that are not _templ.go.

Client-side fixes (client.go)

  • ApplyEdit now calls convertWorkspaceEdit before forwarding to the editor.
  • PublishDiagnostics silently drops diagnostics for regular .go files (the Go extension publishes those) instead of returning an error.

Bug fixes

Method Fix
Rename Now converts request position and response workspace edit (was pass-through)
TypeDefinition Now converts response locations (was missing)
Implementation Uses convertLocationResults instead of hardcoding request URI
DocumentHighlight Implemented (was stubbed to return empty)
RangeFormatting Converts range parameter before forwarding (was missing)
CodeAction Enabled "Organize Imports"; uses convertWorkspaceEdit for edits (was empty allowlist)
PrepareCallHierarchy Converts response items (was pass-through)
IncomingCalls/OutgoingCalls Converts response items (was pass-through)
Symbols Converts _templ.go locations in response (was pass-through)

Capability negotiation

The proxy advertises experimental.templ.goFileSupport: true in its Initialize response. Editors can check this before routing .go files to the proxy, ensuring safe independent upgrades of extension and templ binary.

Testing

New unit tests in server_test.go, client_test.go, and rewrite_test.go verify each behavior using mock servers/clients. The integration test for CodeAction now asserts "Organize Imports" is returned.

Comment thread cmd/templ/lspcmd/proxy/rewrite.go Outdated
Comment thread cmd/templ/lspcmd/proxy/server.go Outdated
Comment thread cmd/templ/lspcmd/lsp_test.go
@joerdav

joerdav commented May 12, 2026

Copy link
Copy Markdown
Collaborator

No game-changing comments here, happy to approve if we decide that they don't need to be addressed!

@a-h
a-h merged commit 3eb1420 into main Jul 15, 2026
8 checks passed
@a-h
a-h deleted the cross_file_support branch July 15, 2026 13:04
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.

2 participants