Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stylelint): add StylelintFixAll command #2089

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions lua/lspconfig/server_configurations/stylelint_lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,37 @@ if vim.fn.has 'win32' == 1 then
cmd = { 'cmd.exe', '/C', bin_name, '--stdio' }
end

local function fix_all(opts)
opts = opts or { 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
Copy link
Member

@justinmk justinmk Aug 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of thing is why in #1937 I'm very hesitant to add these special-case commands to each lspconfig. It's unpleasant for users to have a different interface or every random command. Here we have a function with an "async" flag. Looks like it's always true currently. This is a lot of code, and only for one command. I don't see how we can maintain 100s of configs with this much code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the record I don't like either of these commands and I think both are workarounds.

That being said, I just want to fix my stylelint errors on save. I have no idea why autoFixOnSave and autoFixOnFormat are not respected, but this config and an aucmd will will solve the problem for users.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bennypowers

require('lspconfig')['stylelint_lsp'].setup({
    settings = {
      stylelintplus = {
        autoFixOnFormat = true,
      },
    },
})

plus lukas-reineke/lsp-format.nvim seems to be fixing stylelint errors for me.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the link, but this pr is for support in lspconfig without an additional plugin

I'm definitely going to check that out for my own config tho

request = function(buf, method, params)
stylelint_lsp_client.request_sync(method, params, nil, buf)
end
else
request = function(buf, method, params)
stylelint_lsp_client.request(method, params, nil, buf)
end
end

request(bufnr, 'workspace/executeCommand', {
command = 'stylelint.applyAutoFixes',
arguments = {
{
uri = vim.uri_from_bufnr(bufnr),
version = vim.lsp.util.buf_versions[bufnr],
},
},
})
end

return {
default_config = {
cmd = cmd,
Expand All @@ -24,6 +55,14 @@ return {
},
root_dir = util.root_pattern('.stylelintrc', 'package.json'),
settings = {},
commands = {
StylelintFixAll = {
function()
fix_all { sync = true, bufnr = 0 }
end,
description = 'Fix all stylelint problems for this buffer',
},
},
},
docs = {
description = [[
Expand All @@ -46,6 +85,20 @@ require'lspconfig'.stylelint_lsp.setup{
}
}
```

`stylelint_lsp` provides a `StylelintFixAll` command that can be used to format a document on save:

```lua
local ag = vim.api.nvim_create_augroup
local au = vim.api.nvim_create_autocmd

local autoformat_workaround = ag('lsp_format_workaround', {})
au('BufWritePre', {
group = autoformat_workaround,
pattern = { '*.css', '*.scss', },
command = 'StylelintFixAll',
})
```
]],
default_config = {
root_dir = [[ root_pattern('.stylelintrc', 'package.json') ]],
Expand Down