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

LSP completion not working in Templ control structures #1102

Open
axzilla opened this issue Mar 21, 2025 · 2 comments
Open

LSP completion not working in Templ control structures #1102

axzilla opened this issue Mar 21, 2025 · 2 comments

Comments

@axzilla
Copy link
Contributor

axzilla commented Mar 21, 2025

Before you begin
I've installed the latest version of the templ CLI (go install github.com/a-h/templ/cmd/templ@latest), and have upgraded my project to use the latest version of the templ runtime (go get -u github.com/a-h/templ@latest).

Describe the bug
LSP auto-completion doesn't trigger properly when initially writing control structures (if statements, for loops) in Templ components. Completions only appear after you've already written the control structure, then modify an existing variable.

To Reproduce

  1. Create a simple templ component with a variable
  2. Try to write an if statement or for loop using that variable
  3. Notice that LSP completions don't appear during initial typing
  4. After completing the control structure, delete part of a variable name and start typing again
  5. Now LSP completions will appear correctly

This happens in both VS Code and Neovim with nvim-cmp.

Expected behavior
LSP completions should appear during the initial writing of control structures, just like they do when modifying existing code.

Screenshots
I have a screen recording demonstrating this issue that I can provide if needed.

Desktop (please complete the following information):

  • OS: macOS 15.0
  • templ CLI version: v0.3.833
  • Go version: go1.24.0 darwin/arm64
  • gopls version: v0.18.1

Additional context
This issue affects both the latest release and the most recent commit of templ. The problem is consistent across different editors (VS Code and Neovim).

Screen.Recording.2025-03-21.at.7.44.30.PM.mov
@a-h
Copy link
Owner

a-h commented Mar 22, 2025

This and #1086 are likely the same issue.

I think it was introduced here when I changed the parsing to allow for statements in flowing text.

4c14ab0

The issue is probably that since that commit, when you type for , then it won't pick up that it's a for loop until you enter {\n. As a result, no autocomplete...

Options are:

  • Revert the commit and go back to a situation where if you happen to start a line with for, then you're in Go code.
  • Update the templ language to force use @for, @if etc. to avoid the problem (and also normalise the @component() syntax with it).
  • Give the parser the ability to parse a new node called "ForExpressionIncomplete" which isn't valid, but triggers autocompletion.

@a-h
Copy link
Owner

a-h commented Mar 22, 2025

It's likely that there's only an autocomplete LSP test for string expressions, hence not detecting it, and that a lot of people use snippets tools that auto-insert a {\n.

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

No branches or pull requests

2 participants