latex-lsp / texlab

An implementation of the Language Server Protocol for LaTeX
GNU General Public License v3.0
1.55k stars 53 forks source link

documentFormatting clears out my file (using nvim-lsp) #437

Closed dirksierd closed 3 years ago

dirksierd commented 3 years ago

Saving my latex-file cleared out all its contents. I'm using nvim-lspconfig and nvim-lspinstall with default config. I did upgrade to nvim-lspinstall today, so that might have something to do with it.

The contents of the file are of no influence, everything gets cleared out. It seems to me the LSP is returning an empty string, or null-value perhaps?

I enabled the debug-logging for the nvim-lsp, but that didn't give me anything I could use. I could post the issue on the nvim-lsp-repo if you think this is something to investigate on their end.

For now I've disabled the following line, that used to work. This gives me no problems on other languages (even .bib-files for that matter).

autocmd BufWritePre *.ex lua vim.lsp.buf.formatting_sync(nil, 1000)
autocmd BufWritePre *.bib lua vim.lsp.buf.formatting_sync(nil, 1000)
" autocmd BufWritePre *.tex lua vim.lsp.buf.formatting_sync(nil, 1000)
clason commented 3 years ago

Texlab doesn't support textDocument/formatting for LaTeX files -- only for bib files. So you shouldn't have been calling this in the first place ;)

Nevertheless, the server should return an error (like it did in versions prior to 3.0.0). Instead, I get this in the log:

[ DEBUG ] 2021-05-31T08:48:26+0200 ] /usr/local/share/nvim/runtime/lua/vim/lsp/handlers.lua:434 ]   "default_handler"   "textDocument/formatting"   {  client_id = 1,  params = { {      newText = "",      range = {        end = {          character = 0,          line = 4        },        start = {          character = 0,          line = 0        }      }    } }}
dirksierd commented 3 years ago

Oh wow, that's interesting 😄, I could've sworn that the language server fixed some indentation-errors earlier. Well that explains it, I suppose we could close the ticket in that case…

clason commented 3 years ago

No, that's definitely a bug and a regression in 3.0: the language server should not return an empty(ing) textEdit for an unsupported request.

clason commented 3 years ago

Do you have a small example where this happens? I cannot reproduce this for every file, only for some.

dirksierd commented 3 years ago

It happens with all files on my end, even when they're empty. I haven't added any specific settings and am using latexmk. Also I have the lervag/vimtex-Plug installed, that probably doesn't matter though, but so you know.

clason commented 3 years ago

No, I don't think that matters. It happens with most files for me, too, but there are some where nothing happens. Bisecting the contents gives inconclusive results.

pfoerster commented 3 years ago

Texlab doesn't support textDocument/formatting for LaTeX files

Actually, TexLab uses latexindent to format LaTeX files but it looks like I broke something with the refactoring. I think writing to a process' stdin from Rust somehow breaks the process sometimes (same thing with ChkTeX) so I think it is better to use the file-based approach like with ChkTeX instead (this involves copying the file and the latexindent config to a temp directory). I will work on a fix. Maybe, it will be (finally) time to start working on a LaTeX formatter.

clason commented 3 years ago

Oh, right, I was thinking of internal support (just remembering the table of supported capabilities on netlify.app...)

But I don't have latexindent enabled (or configured)?

pfoerster commented 3 years ago

(just remembering the table of supported capabilities on netlify.app...)

This table is unfortunately horribly outdated. I am thinking of replacing the website and redirecting it to the GitHub page instead. I think the GitHub page has more visibility and it is easier to contribute documentation here.

But I don't have latexindent enabled (or configured)?

At the moment, there is no setting to enable latexindent. It is automatically used when it is available on the PATH. Perhaps, we should add a setting similar to texlab.bibtexFormatter to control the behavior.

clason commented 3 years ago

This table is unfortunately horribly outdated. I am thinking of replacing the website and redirecting it to the GitHub page instead. I think the GitHub page has more visibility and it is easier to contribute documentation here.

👍🏻

At the moment, there is no setting to enable latexindent. It is automatically used when it is available on the PATH. Perhaps, we should add a setting similar to texlab.bibtexFormatter to control the behavior.

Yes, that would be good. latexindent is usually installed and available on PATH when installing texlive, but in the absence of a yaml config just produces errors. That seems a bit wasteful (and error prone; see OP ;)) ...

pfoerster commented 3 years ago

Can you give 9ab84bb8b0ff6223f1b565c07fe04aae631b17f6 a shot, please? I tried the same fix that seems to be working for ChkTeX (so no longer writing to stdin). However, I am still unsure on how to hide latexindent behind configuration. texlab.latexFormatter won't really work in this case as we do not have an internal formatter (yet) and disabling it by default is another breaking change, which I would like to avoid. Perhaps, add a dummy texlab formatter which does nothing but is not the default?

clason commented 3 years ago

Yep, that seems to have fixed it for me.

Regarding latexindent: technically, 3.0.0 allows breaking changes (and this option has already changed). But I'd be fine with introducing a non-default texlab placeholder that does nothing (for now).

pfoerster commented 3 years ago

But I'd be fine with introducing a non-default texlab placeholder that does nothing (for now).

The placeholder has been added. In addition to that, one can now configure latexindent with the --local flag as well to allow a global configuration file. I think it is time for a new version (3.1.0 since we added more configuration options).

pfoerster commented 3 years ago

@dirksierd texlab 3.1.0, which fixes this issue, has been released now.