alker0/chezmoi.vim

Breaks formatter plugins

Closed this issue · 1 comments

I just want to say first that I appreciate this plugin. Thanks very much for putting it together.

Overall it works great, but formatting plugins break when editing template files. Obviously this is because formatters are expecting a syntactically correct input for their expected language. Feeding the whole buffer or file as input to a formatter will of course cause failures as the template syntax is fully incompatible.

This isn't life-changing because probably no one needs to reformat their template files frequently enough to need a plugin that does it for them. However, it would still be nice to have right?

I would suppose this is a feature enabled by treesitter, but we probably need to write a custom grammar that combines the base filetype grammars with the chezmoitmpl grammar. I have no idea how to do this yet, but I'm interested in trying to help get this off the ground.

It sounds simple from a high-level distant perspective, but the devil is in the details. Feel free to reject this request as out of scope.

Just like you, I think how nice we would be to get a format support. I have had a question since publishing this plugin at the first time. To begin with, what tool should support our needs that integration between original dotfile language and go-template?

IMO, it would be better to if what isn't limited to any editor supports that, for example, LSP or tree-sitter. However, the both doesn't show documents and examples for running language feature for multiple languages, as much as it obviously seems to support our usage.

A chezmoi template file has some difficult requirements unlike Markdown that seems to be a good sample, as follows:

  • The main contents is usually an other language and partially injected with go-template. In opposite, Markdown usually includes other language as just snippets.
  • The templating mark {{ ... }} can be used even in the middle of line, so language can be switched between chezmoi template and another language in a same line. In Markdown, you need to insert line breaks around an code block with specifying language like ```sh.
  • It needs to support a dotfile that doesn't have an extention like .config/git/config, and needs to know its language in some way. In Markdown, any code blocks are clearly stated what language is written in together.

Any tools we choose should do the following tasks for get a language feature work for editing a chezmoi template:

  1. Detect file type for a target path that can not exist.
  2. Separate text regions by whether is go-template.
  3. Run language feature of both a target language (from 1st) and go-template, for separated regions (from 2nd), respectively.

Luckily, Vim supports all of these only for highlighting feature. The Vim builtin highlighting feature can not only support integration of multiple languages but also officially document enough for that niche feature as well. So it helped me to think "I could create it for just personally usage" and to make it really work. It was so amazing for me that I would publish it for sharing it with someone who curious like me.

I couldn't have maintained this plugin without excluding other language features (e.g. linting and formatting) from the scope of my supporting. Therefore for formatting, this plugin wouldn't actively support it anytime soon, unless someone forks this project. However we can use variables that this plugin set at filetype detection (e.g. b:chezmoi_original_filetype) for telling other tools what language is another one for the main contents of a chezmoi template file.

One of the "other tools" is the neovim plugin conform.nvim. In according to its advanced_topics, it seems to support injection of another language via separating contents by languages with tree-sitter and formatting each parts with LSP. It could work for even a chezmoi template with using a variable given by this plugin without creating new tree-sitter plugin.

If someone creates new tree-sitter plugin as you have suggested, it should be in another project but not in this one.

Thank you for opening the discussion. This give me a good good opportunity to put my frustration into words. It has been on my mind for long time. I will close this as out of scope for little time I can make currently.