bmatcuk / stylelint-lsp

A stylelint Language Server
MIT License
46 stars 4 forks source link

`autoFixOnSave` didn't work with nvim lsp #29

Closed ceigh closed 3 weeks ago

ceigh commented 2 years ago

Hi, I've been using stylelintplus for a long time, thank you! I used to use coc-stylelintplus, but now I'm trying to transfer my old infrastructure to native nvim-lsp and lua. So, started with https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#stylelint_lsp but i can't turn on auto-formatting when saving a file, here's the config:

return {
  filetypes = {
    'css',
    'scss',
    'vue',
  },

  settings = {
    stylelintplus = {
      autoFixOnSave = true,
      -- autoFixOnFormat = true,
    }
  }
}

Moreover, autoFixOnFormat works, everything is ok (when I call vim.lsp.buf.formatting(), but not when saving.. Do you have any idea why this is happening? Thanks again

bmatcuk commented 2 years ago

Hi @ceigh, glad you enjoy stylelintplus! Unfortunately, I don't use nvim's built-in lsp support, so I'm not 100% sure what the problem might be. About 7 months ago someone else ran into this same problem and it turned out to be a configuration issue. The code you posted above looks correct, though, and, looking through nvim-lspconfig's docs, it doesn't look like anything changed.

What if you tried changing a setting that would have an obvious effect? Like enable = false. That would completely disable stylelintplus. If you restart nvim after making that change and find that stylelintplus is still running, that would mean that your settings aren't getting applied and might help you track down why. If that works but autoFixOnSave still isn't working then we may need to dig deeper...

ceigh commented 2 years ago

Tried it with enable = false - works correctly, server is disabled. It's just about autoFixOnSave

For now i managed this with autoFixOnFormat = true and vim.cmd 'au BufWritePre <buffer> lua vim.lsp.buf.formatting_sync()' (autoformat on save)

So I don't know what to do next yet, it might be worth opening the issue in nvim-lspconfig itself.

bmatcuk commented 2 years ago

Hmm, I'm sorry it's still not working for you; that's very strange! I'm glad you found a work-around, though. I wonder if the save event isn't getting sent for some reason. 'Fraid I'm out of ideas, though.

kirill-martynov commented 2 years ago

Same issue with me.

enable = false / true - works autoFixOnSave = true - doesn't work

kirill-martynov commented 2 years ago

@ceigh This is my workaround:

Using styleling_lsp for linting Added null-ls plugin and turn on formatting for stylelint and it works.

JamyGolden commented 2 years ago

Same issue with me, enable option works correctly, but autoFixOnSave has no effect. eslint autoFixOnSave is working correctly so it doesn't seem related to save events not being fired.

@bmatcuk doesn't nvim require an autocmd, like @ceigh added above, to apply the fix to the current buffer? The eslint instructions mention that an autocmd line should be added. Stylelint functions in a similar way, is that instruction missing from https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#stylelint_lsp?

bennypowers commented 2 years ago

I tried setting up stylelint lsp with this lsp configuration, based on the eslint config:

settings = {
  stylelintplus = {
    autoFixOnSave = true,
    autoFixOnFormat = true,
    cssInJs = false,
  },
},
commands = {
  StylelintFixAll = {
    function()
      local util = require 'lspconfig.util'
      local opts = { sync = true, bufnr = 0 }
      local bufnr = util.validate_bufnr(opts.bufnr or 0)

      local stylelint_lsp_client = util.get_active_client_by_name(bufnr, 'stylelint_lsp')
      if stylelint_lsp_client == nil then
        return
      end

      local request
      if opts.sync then
        request = function(bufnr, method, params)
          stylelint_lsp_client.request_sync(method, params, nil, bufnr)
        end
      else
        request = function(bufnr, method, params)
          stylelint_lsp_client.request(method, params, nil, bufnr)
        end
      end

      request(bufnr, 'workspace/executeCommand', {
        command = 'stylelint.applyAutoFixes',
        arguments = {
          {
            uri = vim.uri_from_bufnr(bufnr),
            version = vim.lsp.util.buf_versions[bufnr],
          },
        },
      })
    end,
    description = 'Fix all stylelint problems for this buffer',
  },
}

I can then setup an autocommand:

local lsp_should_handle_this_but_doesnt = ag('lsp-workaround', { clear = true })
---For reasons unclear to me, eslint ls and stylelint ls
---doesn't autoFixOnSave, so execute `*FixAll` instead
--
au('BufWritePre', {
  group = lsp_should_handle_this_but_doesnt,
  pattern = { '*.tsx', '*.ts', '*.jsx', '*.js', },
  command = 'EslintFixAll',
})
au('BufWritePre', {
  group = lsp_should_handle_this_but_doesnt,
  pattern = { '*.css', '*.scss', },
  command = 'StylelintFixAll',
})

Note that I receive this message when saving the file:

[LSP] Format request failed, no matching language servers.
tjex commented 1 year ago

Also not working out of the box for me. Could it be due to neovims on_attach function that seems to have a lot of precedence for plugin functionality? Bit green behind the ears with this level of detail, but particularly with lsp, quite a few plugins I'm using have also had broken functionality due to on_attach.

sijad commented 10 months ago

I'm using https://github.com/nvim-lua/kickstart.nvim/blob/master/lua/kickstart/plugins/autoformat.lua with following settings and it works:

settings = {
  stylelintplus = {
    autoFixOnSave = true,
    autoFixOnFormat = true,
  },
},
JamyGolden commented 10 months ago

Yeah it's working for me with those settings now too. Perhaps it's been fixed since the ticket was first created?

bmatcuk commented 9 months ago

When this issue was opened, I was not using neovim, so it was hard for me to debug this issue or figure out what the problem was. It turns out the issue was that neovim's language server implementation did not support the functionality that made fix-on-save work, specifically, it did not support the language server message textDocument/willSaveWaitUntil (or the related textDocument/willSave).

That feature was added to neovim in this PR, which made it into the neovim v0.9.0 release in April of this year (2023). It should work now, without any additional configuration.

ceigh commented 3 weeks ago

Hello after a couple of years :) I think we can close this issue, since it has been working for a long time:

require("lspconfig").stylelint_lsp.setup({
  on_attach = function(_, buffer)
    -- Call lsp format on save
    vim.api.nvim_create_autocmd("BufWritePre", {
      buffer = buffer,
      callback = function()
        vim.lsp.buf.format()
      end,
    })
  end,

  settings = {
    stylelintplus = {
      autoFixOnFormat = true,
    },
  },
})

It seems there is no point in autoFixOnSave.