-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Unable to set lineLength from nvim lua config #12778
Comments
What does the The language server settings will only be accounted for when running the formatter via the server aka |
I have set a nvim user command triggering the formater. Mostly copied from conform.nvim recipy. -- Add format command
vim.api.nvim_create_user_command('Format', function(args)
local range = nil
if args.count ~= -1 then
local end_line = vim.api.nvim_buf_get_lines(0, args.line2 - 1, args.line2, true)[1]
range = {
start = { args.line1, 0 },
['end'] = { args.line2, end_line:len() },
}
end
require('conform').format { async = true, lsp_format = 'fallback', range = range }
end, { range = true }) Below is my conform formatter set up, mostly copied from kickstart vim -- kickstart config
return {
{ -- Autoformat
'stevearc/conform.nvim',
event = { 'BufWritePre' },
cmd = { 'ConformInfo' },
keys = {
{
'<leader>f',
function()
require('conform').format { async = true, lsp_fallback = true }
end,
mode = '',
desc = '[F]ormat buffer',
},
},
opts = {
notify_on_error = false,
format_on_save = function(bufnr)
-- Define default behaviour here (default disable autoformat)
if (vim.g.disable_autoformat == nil) or (vim.b[bufnr].disable_autoformat == nil) then
return
end
-- Disable autoformat with a global or buffer-local variable
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
-- Disable "format_on_save lsp_fallback" for languages that don't
-- have a well standardized coding style. You can add additional
-- languages here or re-enable it for the disabled ones.
local disable_filetypes = { c = true, cpp = true }
return {
timeout_ms = 500,
lsp_fallback = not disable_filetypes[vim.bo[bufnr].filetype],
}
end,
formatters_by_ft = {
lua = { 'stylua' },
python = { 'ruff_fix', 'ruff_organize_imports', 'ruff_format' },
sql = { 'sqlfluff' },
markdown = { 'prettier', 'markdown-toc', 'markdownlint-cli2' },
-- Conform can also run multiple formatters sequentially
-- python = { "isort", "black" },
--
-- You can use 'stop_after_first' to run the first available formatter from the list
-- javascript = { "prettierd", "prettier", stop_after_first = true },
},
},
},
}
|
There's some context here: #12514 (comment) (and following replies) but the gist is that |
Note that |
You can configure require('conform').setup {
formatters_by_ft = {
python = { 'ruff_fix', 'ruff_organize_imports', lsp_format = 'first' },
}
} This way the server settings will be considered during formatting via the Ruff language server. You can verify whether
Note The |
Thanks for the help @dhruvmanila require('conform').setup {
formatters_by_ft = {
python = { lsp_format = 'first' },
}
} *without ruff_fix and ruff_organize_imports I tried to change the sequence but couldnt solve the issue. For now i couldnt find a way getting both "line length" and "ruff_fix + ruff_ogranize_import" to work together. |
-- in my lspconfig.lua
ruff = {
init_options = {
settings = {
lineLength = 1,
organizeImports = true,
}
}
}, -- in my conform.lua
require('conform').setup {
formatters_by_ft = {
python = { lsp_format = 'first' },
}
} I still quite new to neovim/lua/ruff and not quite sure what ruff_fix does. Will check on the "fixAll" option in ruff config. |
Huh, that's interesting. What do you mean by "have got organize imports working"? I'm asking because the default value for that setting is As noted above, the
|
Sorry my bad. Upon futher inspection, it seems ruff organize imports is not working despite explisitly setting organizeImports = true, lineLength setting works fine though. I have also tried added select = {'I'} but it still doesnt work ruff = {
init_options = {
settings = {
lineLength = 120,
organizeImports = true,
fixAll = true,
lint = {
select = { 'I' },
},
},
},
}, |
I have fixed the issue by falling back to using ruff CLI args as i could not get it working via language server. -- Add this line to conform.lua
require('conform').formatters.ruff_format = { append_args = { '--line-length', '120' } }
-- python formaters in conform.lua
python = { 'ruff_fix', 'ruff_organize_imports', 'ruff_format' },
|
As mentioned before, all these formatter configuration ( |
Not sure if I did this correctly but i couldnt set --line-length arg in my neovim config (setting it to 1 just for testing). I am using kickstart vim.
Inside my neovim/nvim-lspconfig
The formatting runs fine on ":Format" command but ignores my line length setting. The only way I could get it work is to run "!ruff format --line-length 1" manually.
Version:
ruff 0.5.7
NVIM v0.10.1
The text was updated successfully, but these errors were encountered: