mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-06-22 00:49:03 +02:00
feat!: make conform.nvim
and nvim-lint
the default formatters/linters
This commit is contained in:
parent
70f91956e7
commit
14c091b1fc
13 changed files with 148 additions and 64 deletions
|
@ -10,6 +10,7 @@ return {
|
||||||
optional = true,
|
optional = true,
|
||||||
opts = function(_, opts)
|
opts = function(_, opts)
|
||||||
local nls = require("null-ls")
|
local nls = require("null-ls")
|
||||||
|
opts.sources = opts.sources or {}
|
||||||
table.insert(opts.sources, nls.builtins.formatting.black)
|
table.insert(opts.sources, nls.builtins.formatting.black)
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|
|
@ -10,6 +10,7 @@ return {
|
||||||
optional = true,
|
optional = true,
|
||||||
opts = function(_, opts)
|
opts = function(_, opts)
|
||||||
local nls = require("null-ls")
|
local nls = require("null-ls")
|
||||||
|
opts.sources = opts.sources or {}
|
||||||
table.insert(opts.sources, nls.builtins.formatting.prettierd)
|
table.insert(opts.sources, nls.builtins.formatting.prettierd)
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|
|
@ -12,8 +12,7 @@ return {
|
||||||
optional = true,
|
optional = true,
|
||||||
opts = function(_, opts)
|
opts = function(_, opts)
|
||||||
local nls = require("null-ls")
|
local nls = require("null-ls")
|
||||||
opts.sources = opts.sources or {}
|
opts.sources = vim.list_extend(opts.sources or {}, {
|
||||||
vim.list_extend(opts.sources, {
|
|
||||||
nls.builtins.diagnostics.cmake_lint,
|
nls.builtins.diagnostics.cmake_lint,
|
||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
|
|
|
@ -19,8 +19,7 @@ return {
|
||||||
optional = true,
|
optional = true,
|
||||||
opts = function(_, opts)
|
opts = function(_, opts)
|
||||||
local nls = require("null-ls")
|
local nls = require("null-ls")
|
||||||
opts.sources = opts.sources or {}
|
opts.sources = vim.list_extend(opts.sources or {}, {
|
||||||
vim.list_extend(opts.sources, {
|
|
||||||
nls.builtins.diagnostics.hadolint,
|
nls.builtins.diagnostics.hadolint,
|
||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
|
|
|
@ -37,8 +37,7 @@ return {
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local nls = require("null-ls")
|
local nls = require("null-ls")
|
||||||
opts.sources = opts.sources or {}
|
opts.sources = vim.list_extend(opts.sources or {}, {
|
||||||
vim.list_extend(opts.sources, {
|
|
||||||
nls.builtins.diagnostics.credo,
|
nls.builtins.diagnostics.credo,
|
||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
|
|
|
@ -86,14 +86,12 @@ return {
|
||||||
"nvimtools/none-ls.nvim",
|
"nvimtools/none-ls.nvim",
|
||||||
optional = true,
|
optional = true,
|
||||||
opts = function(_, opts)
|
opts = function(_, opts)
|
||||||
if type(opts.sources) == "table" then
|
local nls = require("null-ls")
|
||||||
local nls = require("null-ls")
|
opts.sources = vim.list_extend(opts.sources or {}, {
|
||||||
vim.list_extend(opts.sources, {
|
nls.builtins.code_actions.gomodifytags,
|
||||||
nls.builtins.code_actions.gomodifytags,
|
nls.builtins.code_actions.impl,
|
||||||
nls.builtins.code_actions.impl,
|
nls.builtins.formatting.goimports,
|
||||||
nls.builtins.formatting.goimports,
|
})
|
||||||
})
|
|
||||||
end
|
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -13,6 +13,7 @@ return {
|
||||||
optional = true,
|
optional = true,
|
||||||
opts = function(_, opts)
|
opts = function(_, opts)
|
||||||
local nls = require("null-ls")
|
local nls = require("null-ls")
|
||||||
|
opts.sources = opts.sources or {}
|
||||||
table.insert(opts.sources, nls.builtins.formatting.csharpier)
|
table.insert(opts.sources, nls.builtins.formatting.csharpier)
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|
|
@ -28,13 +28,11 @@ return {
|
||||||
"nvimtools/none-ls.nvim",
|
"nvimtools/none-ls.nvim",
|
||||||
optional = true,
|
optional = true,
|
||||||
opts = function(_, opts)
|
opts = function(_, opts)
|
||||||
if type(opts.sources) == "table" then
|
local null_ls = require("null-ls")
|
||||||
local null_ls = require("null-ls")
|
opts.sources = vim.list_extend(opts.sources or {}, {
|
||||||
vim.list_extend(opts.sources, {
|
null_ls.builtins.formatting.terraform_fmt,
|
||||||
null_ls.builtins.formatting.terraform_fmt,
|
null_ls.builtins.diagnostics.terraform_validate,
|
||||||
null_ls.builtins.diagnostics.terraform_validate,
|
})
|
||||||
})
|
|
||||||
end
|
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
45
lua/lazyvim/plugins/extras/lsp/none-ls.lua
Normal file
45
lua/lazyvim/plugins/extras/lsp/none-ls.lua
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
local Util = require("lazyvim.util")
|
||||||
|
|
||||||
|
return {
|
||||||
|
-- none-ls
|
||||||
|
{
|
||||||
|
"nvimtools/none-ls.nvim",
|
||||||
|
event = "LazyFile",
|
||||||
|
dependencies = { "mason.nvim" },
|
||||||
|
init = function()
|
||||||
|
Util.on_very_lazy(function()
|
||||||
|
-- register the formatter with LazyVim
|
||||||
|
require("lazyvim.util").format.register({
|
||||||
|
name = "none-ls.nvim",
|
||||||
|
priority = 200, -- set higher than conform, the builtin formatter
|
||||||
|
primary = true,
|
||||||
|
format = function(buf)
|
||||||
|
return Util.lsp.format({
|
||||||
|
bufnr = buf,
|
||||||
|
filter = function(client)
|
||||||
|
return client.name == "null-ls"
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
end,
|
||||||
|
sources = function(buf)
|
||||||
|
local ret = require("null-ls.sources").get_available(vim.bo[buf].filetype, "NULL_LS_FORMATTING") or {}
|
||||||
|
return vim.tbl_map(function(source)
|
||||||
|
return source.name
|
||||||
|
end, ret)
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
end)
|
||||||
|
end,
|
||||||
|
opts = function(_, opts)
|
||||||
|
local nls = require("null-ls")
|
||||||
|
opts.root_dir = opts.root_dir
|
||||||
|
or require("null-ls.utils").root_pattern(".null-ls-root", ".neoconf.json", "Makefile", ".git")
|
||||||
|
opts.sources = vim.list_extend(opts.sources or {}, {
|
||||||
|
nls.builtins.formatting.fish_indent,
|
||||||
|
nls.builtins.diagnostics.fish,
|
||||||
|
nls.builtins.formatting.stylua,
|
||||||
|
nls.builtins.formatting.shfmt,
|
||||||
|
})
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
}
|
67
lua/lazyvim/plugins/linting.lua
Normal file
67
lua/lazyvim/plugins/linting.lua
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
return {
|
||||||
|
{
|
||||||
|
"mfussenegger/nvim-lint",
|
||||||
|
event = "LazyFile",
|
||||||
|
opts = {
|
||||||
|
-- Event to trigger linters
|
||||||
|
events = { "BufWritePost", "BufReadPost", "InsertLeave" },
|
||||||
|
linters_by_ft = {
|
||||||
|
fish = { "fish" },
|
||||||
|
},
|
||||||
|
-- LazyVim extension to easily override linter options
|
||||||
|
-- or add custom linters.
|
||||||
|
---@type table<string,table>
|
||||||
|
linters = {
|
||||||
|
-- -- Example of using selene only when a selene.toml file is present
|
||||||
|
-- selene = {
|
||||||
|
-- -- `condition` is another LazyVim extension that allows you to
|
||||||
|
-- -- dynamically enable/disable linters based on the context.
|
||||||
|
-- condition = function(ctx)
|
||||||
|
-- return vim.fs.find({ "selene.toml" }, { path = ctx.filename, upward = true })[1]
|
||||||
|
-- end,
|
||||||
|
-- },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
config = function(_, opts)
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
local lint = require("lint")
|
||||||
|
for name, linter in pairs(opts.linters) do
|
||||||
|
if type(linter) == "table" and type(lint.linters) == "table" then
|
||||||
|
lint.linters[name] = vim.tbl_deep_extend("force", lint.linters[name], linter)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
lint.linters_by_ft = opts.linters_by_ft
|
||||||
|
|
||||||
|
function M.debounce(ms, fn)
|
||||||
|
local timer = vim.loop.new_timer()
|
||||||
|
return function(...)
|
||||||
|
local argv = { ... }
|
||||||
|
timer:start(ms, 0, function()
|
||||||
|
timer:stop()
|
||||||
|
vim.schedule_wrap(fn)(unpack(argv))
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.lint()
|
||||||
|
local names = lint.linters_by_ft[vim.bo.filetype] or {}
|
||||||
|
local ctx = { filename = vim.api.nvim_buf_get_name(0) }
|
||||||
|
ctx.dirname = vim.fn.fnamemodify(ctx.filename, ":h")
|
||||||
|
names = vim.tbl_filter(function(name)
|
||||||
|
local linter = lint.linters[name]
|
||||||
|
return linter and not (type(linter) == "table" and linter.condition and not linter.condition(ctx))
|
||||||
|
end, names)
|
||||||
|
|
||||||
|
if #names > 0 then
|
||||||
|
lint.try_lint(names)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.api.nvim_create_autocmd(opts.events, {
|
||||||
|
group = vim.api.nvim_create_augroup("nvim-lint", { clear = true }),
|
||||||
|
callback = M.debounce(100, M.lint),
|
||||||
|
})
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
}
|
|
@ -200,49 +200,6 @@ return {
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|
||||||
-- formatters
|
|
||||||
{
|
|
||||||
"nvimtools/none-ls.nvim",
|
|
||||||
event = "LazyFile",
|
|
||||||
dependencies = { "mason.nvim" },
|
|
||||||
opts = function()
|
|
||||||
local nls = require("null-ls")
|
|
||||||
return {
|
|
||||||
root_dir = require("null-ls.utils").root_pattern(".null-ls-root", ".neoconf.json", "Makefile", ".git"),
|
|
||||||
sources = {
|
|
||||||
nls.builtins.formatting.fish_indent,
|
|
||||||
nls.builtins.diagnostics.fish,
|
|
||||||
nls.builtins.formatting.stylua,
|
|
||||||
nls.builtins.formatting.shfmt,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
end,
|
|
||||||
config = function(_, opts)
|
|
||||||
require("null-ls").setup(opts)
|
|
||||||
|
|
||||||
-- register the formatter with LazyVim
|
|
||||||
require("lazyvim.util").format.register({
|
|
||||||
name = "none-ls.nvim",
|
|
||||||
priority = 50,
|
|
||||||
primary = true,
|
|
||||||
format = function(buf)
|
|
||||||
return Util.lsp.format({
|
|
||||||
bufnr = buf,
|
|
||||||
filter = function(client)
|
|
||||||
return client.name == "null-ls"
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
end,
|
|
||||||
sources = function(buf)
|
|
||||||
local ret = require("null-ls.sources").get_available(vim.bo[buf].filetype, "NULL_LS_FORMATTING") or {}
|
|
||||||
return vim.tbl_map(function(source)
|
|
||||||
return source.name
|
|
||||||
end, ret)
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
end,
|
|
||||||
},
|
|
||||||
|
|
||||||
-- cmdline tools and lsp servers
|
-- cmdline tools and lsp servers
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
|
@ -124,7 +124,26 @@ function M.format(opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.health()
|
||||||
|
local Config = require("lazy.core.config")
|
||||||
|
local has_plugin = Config.spec.plugins["none-ls.nvim"]
|
||||||
|
local has_extra = vim.tbl_contains(Config.spec.modules, "lazyvim.plugins.extras.lsp.none-ls")
|
||||||
|
if has_plugin and not has_extra then
|
||||||
|
Util.warn({
|
||||||
|
"`conform.nvim` and `nvim-lint` are now the default forrmatters and linters in LazyVim.",
|
||||||
|
"",
|
||||||
|
"You can use those plugins together with `none-ls.nvim`,",
|
||||||
|
"but you need to enable the `lazyvim.plugins.extras.lsp.none-ls` extra,",
|
||||||
|
"for formatting to work correctly.",
|
||||||
|
"",
|
||||||
|
"In case you no longer want to use `none-ls.nvim`, just remove the spec from your config.",
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function M.setup()
|
function M.setup()
|
||||||
|
M.health()
|
||||||
|
|
||||||
-- Autoformat autocmd
|
-- Autoformat autocmd
|
||||||
vim.api.nvim_create_autocmd("BufWritePre", {
|
vim.api.nvim_create_autocmd("BufWritePre", {
|
||||||
group = vim.api.nvim_create_augroup("LazyFormat", {}),
|
group = vim.api.nvim_create_augroup("LazyFormat", {}),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue