enable fmt on save for python with ruff

master
Jordan Orelli 3 months ago
parent 0fba8d146c
commit db4e0da978

@ -3,6 +3,8 @@ autoload -Uz compinit && compinit
autoload -U colors && colors autoload -U colors && colors
autoload -Uz vcs_info autoload -Uz vcs_info
export EDITOR=nvim
# adds the cargo environment, which also adds rust binaries. We want to do this # adds the cargo environment, which also adds rust binaries. We want to do this
# early in case anything else depends on a rust binary, such as direnv # early in case anything else depends on a rust binary, such as direnv
if [[ -f "$HOME/.cargo/env" ]]; then if [[ -f "$HOME/.cargo/env" ]]; then

@ -17,12 +17,12 @@ local buf = vim.api.nvim_get_current_buf()
local root_dir = vim.fs.root(buf, root_files) local root_dir = vim.fs.root(buf, root_files)
local git_dir = vim.fs.root(buf, {'.git'}) local git_dir = vim.fs.root(buf, {'.git'})
local lsp_server = vim.lsp.start({ local pyright = vim.lsp.start({
name = 'pyright', name = 'pyright',
cmd = {'pyright-langserver', '--stdio'}, cmd = {'pyright-langserver', '--stdio'},
filetypes = { 'python' }, filetypes = { 'python' },
root_dir = root_dir, root_dir = root_dir,
single_file_support = false, single_file_support = true,
settings = { settings = {
python = { python = {
analysis = { analysis = {
@ -34,4 +34,15 @@ local lsp_server = vim.lsp.start({
}, },
}) })
vim.lsp.buf_attach_client(buf, lsp_server) vim.lsp.buf_attach_client(buf, pyright)
local ruff = vim.lsp.start({
name = 'ruff',
cmd = {'ruff', 'server'},
filetypes = { 'python' },
root_dir = root_dir,
single_file_support = true,
settings = {},
})
vim.lsp.buf_attach_client(buf, ruff)

@ -0,0 +1,26 @@
function enable_autocomplete(client, buf)
if client.supports_method('textDocument/completion') then
vim.lsp.completion.enable(true, client.id, buf, {autotrigger = true})
end
end
function enable_autoformat(client, buf)
if client.supports_method('textDocument/formatting') then
vim.api.nvim_create_autocmd('BufWritePre', {
buffer = buf,
callback = function()
vim.lsp.buf.format({bufnr = buf, id = client.id})
end,
})
end
end
vim.api.nvim_create_autocmd('LspAttach', {
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
local buf = args.buf
-- enable_autocomplete(client, buf)
enable_autoformat(client, buf)
end,
})
Loading…
Cancel
Save