Move linter and formater to one config. Add mason tool autoinstallerr

This commit is contained in:
CronyAkatsuki 2023-12-24 21:22:04 +01:00
parent c3aa716388
commit 949dde4a91
5 changed files with 61 additions and 38 deletions

View File

@ -0,0 +1,57 @@
-- [[ Formatters Setup ]]
local conform = require("conform")
conform.setup({
formatters_by_ft = {
python = { "isort", "black" },
lua = { "stylua" },
sh = { "shfmt" },
bash = { "shfmt" },
},
})
-- only format with keybinding, don't like code chaning everytime I save
vim.keymap.set({ "n", "v" }, "<leader>f", function()
conform.format({
lsp_fallback = true,
})
end, { desc = "Format file or range (in visual mode)" })
-- [[ Linter's Setup ]]
local lint = require("lint")
-- fix mypy import's error
lint.linters.mypy.args = {
function()
local virtual = os.getenv("VIRTUAL_ENV") or os.getenv("CONDA_PREFIX") or "/usr"
return "--python-executable", virtual .. "/bin/python3"
end,
}
lint.linters_by_ft = {
python = { "mypy", "ruff" },
}
-- setup auto linting
local lint_augroup = vim.api.nvim_create_augroup("lint", { clear = true })
vim.api.nvim_create_autocmd({ "BufEnter", "BufWritePost", "InsertLeave" }, {
group = lint_augroup,
callback = function()
lint.try_lint()
end,
})
-- [[ Mason Tools Installer ]]
local mason_tool_installer = require("mason-tool-installer")
mason_tool_installer.setup({
ensure_installed = {
"isort",
"black",
"stylua",
"shfmt",
"mypy",
"ruff",
},
})

View File

@ -1,16 +0,0 @@
local conform = require("conform")
conform.setup({
formatters_by_ft = {
python = { "isort", "black" },
lua = { "stylua" },
sh = { "shfmt" },
bash = { "shfmt" },
},
})
vim.keymap.set({ "n", "v" }, "<leader>f", function()
conform.format({
lsp_fallback = true,
})
end, { desc = "Format file or range (in visual mode)" })

View File

@ -75,7 +75,7 @@ vim.diagnostic.config({
require("mason").setup({}) require("mason").setup({})
require("mason-lspconfig").setup({ require("mason-lspconfig").setup({
ensure_installed = { "lua_ls" }, ensure_installed = { "lua_ls", "jsonls", "bashls", "pyright" },
handlers = { handlers = {
lsp.default_setup, lsp.default_setup,
}, },

View File

@ -1,21 +0,0 @@
local lint = require("lint")
lint.linters.mypy.args = {
function()
local virtual = os.getenv("VIRTUAL_ENV") or os.getenv("CONDA_PREFIX") or "/usr"
return "--python-executable", virtual .. "/bin/python3"
end,
}
lint.linters_by_ft = {
python = { "mypy", "ruff" },
}
local lint_augroup = vim.api.nvim_create_augroup("lint", { clear = true })
vim.api.nvim_create_autocmd({ "BufEnter", "BufWritePost", "InsertLeave" }, {
group = lint_augroup,
callback = function()
lint.try_lint()
end,
})

View File

@ -152,6 +152,9 @@ require("lazy").setup({
-- additional linter support -- additional linter support
"mfussenegger/nvim-lint", "mfussenegger/nvim-lint",
-- mason autoinstaller for formatter's and linter's
"WhoIsSethDaniel/mason-tool-installer.nvim"
}, { }, {
install = { install = {
colorscheme = { "catppuccin" }, colorscheme = { "catppuccin" },