tamasfe / taplo

A TOML toolkit written in Rust
https://taplo.tamasfe.dev
MIT License
1.26k stars 108 forks source link

Taplo lsp abandons formating and diagnostic if it cannot find a schema #580

Closed ndtoan96 closed 3 months ago

ndtoan96 commented 3 months ago

I use taplo with the helix editor. Upon opening a toml file, there's only one message at the top: "this document has been excluded". I guess because it cannot find the schema. That is fine. But I cannot perform formatting for the document, and even if there's toml syntax errors, the taplo lsp does not give any warning.

I cross-checked with VSCode "even better toml" extension and everything works fine in VSCode. What causes the difference in behavior even though both editors use the same taplo lsp?

JounQin commented 3 months ago

Did you try this config option?

https://taplo.tamasfe.dev/configuration/file.html#schema

ndtoan96 commented 3 months ago

Did you try this config option?

https://taplo.tamasfe.dev/configuration/file.html#schema

My file does not have any schema. The point is formatting and basic toml syntax diagnosis (something like brackets not matching...) shouldn't need a schema.

JounQin commented 3 months ago

Schema will be enabled with filename patterns by default. And why helix editor's behaviour should be related to taplo?

ndtoan96 commented 3 months ago

I still don't get your point. So, I will describe my use case in more detail.

I use helix to edit a random toml file. It has the below content:

[config_a]
name_1="value1"
name_2        =     4

helix does support taplo. I check it with Cargo.toml and it works fine. But for this random toml file, as you can guess from the content, it's just a random file with no schema, and I cannot perform formatting in helix (using :fmt command). However, I open the same file in VSCode with Even Better Toml extension installed, press Shift-Alt-F and I can format the file.

What's more, I intentionally create a syntax error like below

[config_a]
name_1="value1"
name_2        =
4

I open the file in helix, and there's no error message. I open the file in VSCode, and it says there's an error.

I hope now you can understand what I'm trying to say. Even both editors use the same taplo lsp, the helix one cannot work without a schema, while the VSCode still works without a schema. What is the difference? How can I make the helix one behaves like the VSCode one?

JounQin commented 3 months ago

I don't get your point neither, why didn't you post the issue to helix instead? That editor is out of our control?

ndtoan96 commented 3 months ago

My gut told me this is an issue on taplo side.

From my understanding, in LSP, to do formatting, the editor sends a request along with the current code to the LSP server, and the server sends a response back with the formatted code. Since the LSP logic in helix is implemented generally for all languages (it does not have a toml plugin, it doesn't even have plugin system yet), I can't imagine helix fails to send the request for toml alone. Which means it's highly likely that the server side, taplo in this case, somehow refutes the request or fails to send the response.

JounQin commented 3 months ago

It requires https://github.com/helix-editor/helix's contributors to debug in depth then.

As you found, it worked for VSCode extension, so you still need to reproduce without helix to approve that's something wrong with taplo's side.

The point is always: why it's taplo's issue instead of helix's, so if you can reproduce it with a failing test case, that would be great.

ndtoan96 commented 3 months ago

Found the problem. I got suggestion when looking into the nvim-lspconfig for taplo. For the lsp to work, we need to configure the lsp root. Both VSCode and NeoVim already set the root for taplo in their plugins. Meanwhile helix is just barebone without any config and it considers a git project as root. That's why a random file from a random place is ignored.

ndtoan96 commented 3 months ago

LSP root config issue.

For those who got the same problem, I was able to fix this by adding this config in languages.toml

[[language]]
name = "toml"
roots = ["."]