Automatic fixes with Oxlint LSP? #16494
|
Hello everyone, Oxlint supports autofixes with the --fix flag, but the LSP doesn’t seem to provide any formatting capabilities, even though the source.fixAll.oxc command is available. Did I miss something, or is this a feature that could be added to the LSP? |
Replies: 5 comments 9 replies
|
It should work 🤞 , we have a test for it: oxc/editors/vscode/tests/code_actions.spec.ts Lines 85 to 112 in 8c85e08 |
|
Ok thank you, I'm sorry for the confusion I should have emphasized on the fact that my question was specifically about the LS and not about the VSCode extension. I'm using Neovim native LSP client. I now understand that the LS provides a fixAll comand that is to be used by the editor, not a formatting capability in the LSP sense. So I could replicate the VSCode extension feature using a simple autocmd : vim.api.nvim_create_autocmd("BufWritePre", {
desc = "Format before save",
pattern = "*",
group = vim.api.nvim_create_augroup("FormatConfig", { clear = true }),
callback = function(ev)
local client = vim.lsp.get_clients({ name = "oxlint", bufnr = ev.buf })[1]
if not client then
return
end
local request_result = client:request_sync("workspace/executeCommand", {
command = "oxc.fixAll",
arguments = {
{
uri = vim.uri_from_bufnr(ev.buf),
},
},
})
if request_result and request_result.err then
vim.notify(request_result.err.message, vim.log.levels.ERROR)
return
end
end,
})
So now it works as expected, only issue I get now is how can I change the LS settings? vim.lsp.config("oxlint", {
cmd = { "oxc_language_server" },
cmd_env = {
RUST_LOG = "debug",
},
settings = {
initializationOptions = {
{
workspaceUri = "file://home/francois/Projects/Personal/project",
options = {
["fmt.experimental"] = true,
},
},
},
},
})I'm not understand why it complains, in the VSCode Oxc channel the settings are passed with the same format : Any idea of the format expected? |
|
Ok so I figured out, here is my config for oxlint : {
cmd = { 'oxlint', '--lsp' },
filetypes = {
'javascript',
'javascriptreact',
'javascript.jsx',
'typescript',
'typescriptreact',
'typescript.tsx',
},
workspace_required = true,
on_attach = function(client, bufnr)
vim.api.nvim_buf_create_user_command(bufnr, 'LspOxlintFixAll', function()
client:exec_cmd({
title = 'Apply Oxlint automatic fixes',
command = 'oxc.fixAll',
arguments = { { uri = vim.uri_from_bufnr(bufnr) } },
})
end, {
desc = 'Apply Oxlint automatic fixes',
})
end,
root_dir = function(bufnr, on_dir)
local fname = vim.api.nvim_buf_get_name(bufnr)
-- Oxlint resolves configuration by walking upward and using the nearest config file
-- to the file being processed. We therefore compute the root directory by locating
-- the closest `.oxlintrc.json` (or `package.json` fallback) above the buffer.
local root_markers = util.insert_package_json({ '.oxlintrc.json' }, 'oxlint', fname)[1]
on_dir(vim.fs.dirname(vim.fs.find(root_markers, { path = fname, upward = true })[1]))
end,
init_options = {
{
workspaceUri = 'file://' .. vim.fs.dirname(vim.fs.find('.git', { path = fname, upward = true })[1]),
options = {
-- run = 'onType',
-- configPath = null,
-- tsConfigPath = null,
-- unusedDisableDirectives = 'allow',
-- typeAware = false,
-- disableNestedConfig = false,
fixKind = 'all',
},
},
},
}And for oxfmt : {
cmd = { 'oxfmt', '--lsp' },
filetypes = {
'javascript',
'javascriptreact',
'javascript.jsx',
'typescript',
'typescriptreact',
'typescript.tsx',
},
workspace_required = true,
root_dir = function(bufnr, on_dir)
local fname = vim.api.nvim_buf_get_name(bufnr)
-- Oxfmt resolves configuration by walking upward and using the nearest config file
-- to the file being processed. We therefore compute the root directory by locating
-- the closest `.oxfmtrc.json` (or `package.json` fallback) above the buffer.
local root_markers = util.insert_package_json({ '.oxfmtrc.json' }, 'oxfmt', fname)[1]
on_dir(vim.fs.dirname(vim.fs.find(root_markers, { path = fname, upward = true })[1]))
end,
init_options = {
{
workspaceUri = 'file://' .. vim.fs.dirname(vim.fs.find('.git', { path = fname, upward = true })[1]),
options = {
['fmt.experimental'] = true, -- if disable the Oxfmt LS won't do anything
-- ['fmt.configPath'] = null,
},
},
},
}I made a PR to lspconfig, if it accepted and I didn't mess up, both Oxlint and Oxfmt LSs will be fully functional using native Neovim LSP integration : neovim/nvim-lspconfig#4242 |
|
Hi do you guys have an example setting for oxlint language server with monorepo? I'm using neovim |

Ok so I figured out, here is my config for oxlint :
{ cmd = { 'oxlint', '--lsp' }, filetypes = { 'javascript', 'javascriptreact', 'javascript.jsx', 'typescript', 'typescriptreact', 'typescript.tsx', }, workspace_required = true, on_attach = function(client, bufnr) vim.api.nvim_buf_create_user_command(bufnr, 'LspOxlintFixAll', function() client:exec_cmd({ title = 'Apply Oxlint automatic fixes', command = 'oxc.fixAll', arguments = { { uri = vim.uri_from_bufnr(bufnr) } }, }) end, { desc = 'Apply Oxlint automatic fixes', }) end, root_dir = function(bufnr, on_dir) local fname = vim.api.nvim_buf_get_…