nvimtools / none-ls.nvim

null-ls.nvim reloaded / Use Neovim as a language server to inject LSP diagnostics, code actions, and more via Lua.
The Unlicense
2.43k stars 72 forks source link

Auto formatting not working for svelte files on save #43

Closed diegoulloao closed 9 months ago

diegoulloao commented 9 months ago

FAQ

Issues

Neovim Version

0.9.4

Dev Version?

Operating System

macOs sonoma

Minimal Config

null_ls.setup({
  sources = {
    formatting.prettier,
    formatting.stylua,
    diagnostics.eslint_d,
  },
  -- configure format on save
  on_attach = function(current_client, bufnr)
    if current_client.supports_method("textDocument/formatting") then
      vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
      vim.api.nvim_create_autocmd("BufWritePre", {
        group = augroup,
        buffer = bufnr,
        callback = function()
          vim.lsp.buf.format({
            filter = function(client)
              -- only use null-ls for formatting instead of lsp server
              return client.name == "null-ls"
            end,
            bufnr = bufnr,
          })
        end,
      })
    end
  end,
})

Steps to Reproduce

Create a .svelte file and try saving for auto formatting.

Reproducibility Check

Expected Behavior

Files should get formatted automatically before write changes.

Actual Behavior

Auto formatting doesn't work.

Debug Log

none.

Help

Yes

Implementation Help

No response

Requirements

dennisschoepf commented 9 months ago

Svelte Files are not registered automatically by formatting.prettier (as you can read here: https://github.com/nvimtools/none-ls.nvim/blob/main/doc/BUILTINS.md#notes-39)

So you would have to enable it like this:

null_ls.setup({
  sources = {
    formatting.prettier.with({ extra_filetypes = { "svelte" } }),
    formatting.stylua,
    diagnostics.eslint_d,
  },
  -- configure format on save
  on_attach = function(current_client, bufnr)
    if current_client.supports_method("textDocument/formatting") then
      vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
      vim.api.nvim_create_autocmd("BufWritePre", {
        group = augroup,
        buffer = bufnr,
        callback = function()
          vim.lsp.buf.format({
            filter = function(client)
              -- only use null-ls for formatting instead of lsp server
              return client.name == "null-ls"
            end,
            bufnr = bufnr,
          })
        end,
      })
    end
  end,
})