a-h / templ

A language for writing HTML user interfaces in Go.
https://templ.guide/
MIT License
7.14k stars 236 forks source link

fix: LSP CodeAction sometimes results in "error: column is beyond end of line" #817

Closed a-h closed 6 days ago

a-h commented 1 week ago

In #801 we saw a report of "error: column is beyond end of line" coming back from the LSP.

This turned out to be hard to reproduce without the specific set of files, and also because different editors, extensions, and versions of gopls interact differently with the LSP.

However, investigating the issue, I spotted that CodeAction doesn't do any rewriting of the templ source positions to the target Go positions. This means that sometimes, the templ source position would indeed be past the end of a Go source code line, but the behaviour would be hard to reproduce, since it would require the specific CodeAction to have been executed, and it may require a specific version of gopls etc.

I use Neovim where to run a code action I have to press space followed by c and a. However, in VS Code, it's likely that some extensions are calling this LSP feature regularly - maybe even every time a cursor moves position, or a file is edited, meaning that those users are much more likely to spot this issue.

It looks to me like CodeAction can't ever have worked up until this point, since the rewrites weren't properly done, and also, because gopls ignores actions on generated code, based on magic comments in the file.

This PR does a few things:

I think that this will fix #801