Skip to content

Commit

Permalink
[vim] Use ruff-lsp instead of efm for Ruff
Browse files Browse the repository at this point in the history
Unfortunately the `lsp_fix_all_and_format()` function doesn't seem to
apply the code actions consistently.
  • Loading branch information
rouge8 committed Oct 25, 2023
1 parent d4dd241 commit 55bd3cb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
16 changes: 0 additions & 16 deletions .config/efm-langserver/config.yaml.symlink
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,6 @@ tools:
format-command: black -
format-stdin: true

ruff: &ruff
lint-command: "ruff -n ${INPUT}"
lint-stdin: true
lint-formats:
- "%f:%l:%c: %m"
lint-ignore-exit-code: true
format-command: ruff --exit-zero --fix -n --stdin-filename ${INPUT} - --unfixable F401,F841
format-stdin: true

ruff-format: &ruff-format
format-command: ruff format --stdin-filename ${INPUT} -
format-stdin: true

vint: &vint
lint-command: vint -
lint-stdin: true
Expand All @@ -56,9 +43,6 @@ languages:
- <<: *djhtml
lua:
- <<: *stylua
python:
- <<: *ruff
- <<: *ruff-format
vim:
- <<: *vint
yaml:
Expand Down
2 changes: 1 addition & 1 deletion .config/nvim.symlink/init.vim
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function! MaybeLSPFormat()
if exists('b:no_autoformat')
return
endif
lua vim.lsp.buf.format()
lua lsp_fix_all_and_format()
endfunction

" ~/forks
Expand Down
23 changes: 21 additions & 2 deletions .config/nvim.symlink/lua/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ cmp.setup({
})

-- LSP
lsp_fix_all_and_format = function()
vim.lsp.buf.code_action({
apply = true,
context = {
only = { "source.fixAll.ruff" },
},
})
vim.lsp.buf.format({ async = false, timeout_ms = 5000 })
end

local nvim_lsp = require("lspconfig")

-- Use an on_attach function to only map the following keys
Expand All @@ -119,10 +129,10 @@ local on_attach = function(client, bufnr)
vim.keymap.set("n", "<leader>r", vim.lsp.buf.references, bufopts)
-- Formatting
vim.keymap.set("n", "<leader>f", function()
vim.lsp.buf.format({ async = false, timeout_ms = 5000 })
lsp_fix_all_and_format()
end, bufopts)
vim.keymap.set("v", "<leader>f", function()
vim.lsp.buf.format({ async = false })
lsp_fix_all_and_format()
end, bufopts)
-- Code actions
vim.keymap.set({ "n", "v" }, "<leader>ca", vim.lsp.buf.code_action, bufopts)
Expand Down Expand Up @@ -171,6 +181,15 @@ nvim_lsp.pyright.setup({
},
},
})
nvim_lsp.ruff_lsp.setup({
capabilities = capabilities,
on_attach = on_attach,
settings = {
lint = {
args = { "--unfixable", "F401,F841" },
},
},
})

-- Lua
nvim_lsp.lua_ls.setup({
Expand Down
2 changes: 2 additions & 0 deletions Brewfile
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ brew "rich-cli"
brew "ripgrep"
# Extremely fast Python linter, written in Rust
brew "ruff"
# Language Server Protocol implementation for Ruff
brew "ruff-lsp"
# Fast and accurate code counter with complexity and COCOMO estimates
brew "scc"
# Intuitive find & replace CLI
Expand Down

0 comments on commit 55bd3cb

Please sign in to comment.