From db4e0da978316b79e787541be0c93ac1ea246398 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 27 Jul 2024 13:03:05 -0500 Subject: [PATCH] enable fmt on save for python with ruff --- .zshrc | 2 ++ nvim/after/ftplugin/python.lua | 17 ++++++++++++++--- nvim/after/plugin/lsp.lua | 26 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/.zshrc b/.zshrc index 1f24307..4ff9c26 100644 --- a/.zshrc +++ b/.zshrc @@ -3,6 +3,8 @@ autoload -Uz compinit && compinit autoload -U colors && colors autoload -Uz vcs_info +export EDITOR=nvim + # 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 if [[ -f "$HOME/.cargo/env" ]]; then diff --git a/nvim/after/ftplugin/python.lua b/nvim/after/ftplugin/python.lua index 12bc83d..641507d 100644 --- a/nvim/after/ftplugin/python.lua +++ b/nvim/after/ftplugin/python.lua @@ -17,12 +17,12 @@ local buf = vim.api.nvim_get_current_buf() local root_dir = vim.fs.root(buf, root_files) local git_dir = vim.fs.root(buf, {'.git'}) -local lsp_server = vim.lsp.start({ +local pyright = vim.lsp.start({ name = 'pyright', cmd = {'pyright-langserver', '--stdio'}, filetypes = { 'python' }, root_dir = root_dir, - single_file_support = false, + single_file_support = true, settings = { python = { 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) diff --git a/nvim/after/plugin/lsp.lua b/nvim/after/plugin/lsp.lua index e69de29..dd7d4c7 100644 --- a/nvim/after/plugin/lsp.lua +++ b/nvim/after/plugin/lsp.lua @@ -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, +})