How to Configure Vim for Formatting .gohtml Files?

205 Views Asked by At

I use lazyvim default, it turns out that gohtml is not recognized as html, I ended up creating a filetype.lua file and setting .gohtml to .html

In this case, the LSP recognizes it through :LspInfo

However, when saved, it even appears that null-ls is formatting, but it does not format.

I've been researching for a few days and I haven't been able to get it to work yet, if I change it to .html it formats it, but that's a pain.

1

There are 1 best solutions below

0
c4rt0 On

Here's an example of a filetype.lua file:

local ft = vim.api.nvim_create_augroup('filetype_gohtml', { clear = true })
vim.api.nvim_create_autocmd('filetype', { group = ft, pattern = '.gohtml', callback = function()
  vim.bo.filetype = 'html'
end })

Once you have created the filetype.lua file, you need to load it into Vim. You can do this by adding the following line to your vimrc file:

lua require('gohtml-filetype')

After loading the filetype.lua file, you should be able to use :LspInfo to see that the LSP recognizes .gohtml files as HTML. However, you may still encounter issues with formatting when saving the files.

This is because the null-ls formatter is not able to automatically format .gohtml files without additional configuration. To fix this issue, you need to manually configure null-ls to use the appropriate formatter for .gohtml files.

Here's an example of how to configure null-ls to use the prettier formatter for .gohtml files:

-- Enable null-ls
lua require('null-ls').setup({
  sources = {
    {
      name = 'prettier',
      command = 'prettier',
      filetypes = { 'html' },
    },
  },
})

With this configuration, null-ls should be able to automatically format .gohtml files when you save them.