From d0f14ed4a19dd392938ea8ef3bd4e5ae559ecfd2 Mon Sep 17 00:00:00 2001 From: pablu Date: Tue, 28 Apr 2026 17:07:18 +0200 Subject: [PATCH] pyproject dependent linters --- dot_config/nvim/lua/plugins/conform.lua | 2 +- dot_config/nvim/lua/plugins/linter.lua | 40 +++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/dot_config/nvim/lua/plugins/conform.lua b/dot_config/nvim/lua/plugins/conform.lua index c10e6fa..6386f27 100644 --- a/dot_config/nvim/lua/plugins/conform.lua +++ b/dot_config/nvim/lua/plugins/conform.lua @@ -6,7 +6,7 @@ return { conform.setup { formatters_by_ft = { lua = { "stylua" }, - python = { "isort", "black", lsp_format = "fallback" }, + python = { "isort", "black", "ruff", lsp_format = "fallback" }, gotmpl = { "djlint" } }, format_on_save = { diff --git a/dot_config/nvim/lua/plugins/linter.lua b/dot_config/nvim/lua/plugins/linter.lua index 1628ca6..653e268 100644 --- a/dot_config/nvim/lua/plugins/linter.lua +++ b/dot_config/nvim/lua/plugins/linter.lua @@ -2,9 +2,43 @@ return { 'mfussenegger/nvim-lint', config = function() local lint = require("lint") - lint.linters_by_ft = { - python = { 'mypy' } - } + -- lint.linters_by_ft = { + -- python = { 'mypy' } + -- } + + local function read_pyproject() + local results = vim.fs.find("pyproject.toml", { upward = true, stop = vim.loop.os_homedir() }) + + local path = results[1] + if not path then + return nil + end + + local ok, lines = pcall(vim.fn.readfile, path) + if not ok then + return nil + end + + return table.concat(lines, "\n") + end + + local function python_linters() + local linters = {} + local pyproject = read_pyproject() + + if pyproject then + if pyproject:match("%[tool%.ruff%]") then + table.insert(linters, "ruff") + end + if pyproject:match("%[tool%.mypy%]") then + table.insert(linters, "mypy") + end + end + + return linters + end + + lint.linters_by_ft.python = python_linters() local lint_augroup = vim.api.nvim_create_augroup("lint", { clear = true }) vim.api.nvim_create_autocmd({ "BufEnter", "BufWritePost", "InsertLeave" }, {