Skip to content

Commit

Permalink
feat!: make conform.nvim and nvim-lint the default formatters/lin…
Browse files Browse the repository at this point in the history
…ters
  • Loading branch information
folke committed Oct 12, 2023
1 parent 70f9195 commit 14c091b
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 64 deletions.
1 change: 1 addition & 0 deletions lua/lazyvim/plugins/extras/formatting/black.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ return {
optional = true,
opts = function(_, opts)
local nls = require("null-ls")
opts.sources = opts.sources or {}
table.insert(opts.sources, nls.builtins.formatting.black)
end,
},
Expand Down
1 change: 1 addition & 0 deletions lua/lazyvim/plugins/extras/formatting/prettier.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ return {
optional = true,
opts = function(_, opts)
local nls = require("null-ls")
opts.sources = opts.sources or {}
table.insert(opts.sources, nls.builtins.formatting.prettierd)
end,
},
Expand Down
3 changes: 1 addition & 2 deletions lua/lazyvim/plugins/extras/lang/cmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ return {
optional = true,
opts = function(_, opts)
local nls = require("null-ls")
opts.sources = opts.sources or {}
vim.list_extend(opts.sources, {
opts.sources = vim.list_extend(opts.sources or {}, {
nls.builtins.diagnostics.cmake_lint,
})
end,
Expand Down
3 changes: 1 addition & 2 deletions lua/lazyvim/plugins/extras/lang/docker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ return {
optional = true,
opts = function(_, opts)
local nls = require("null-ls")
opts.sources = opts.sources or {}
vim.list_extend(opts.sources, {
opts.sources = vim.list_extend(opts.sources or {}, {
nls.builtins.diagnostics.hadolint,
})
end,
Expand Down
3 changes: 1 addition & 2 deletions lua/lazyvim/plugins/extras/lang/elixir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ return {
return
end
local nls = require("null-ls")
opts.sources = opts.sources or {}
vim.list_extend(opts.sources, {
opts.sources = vim.list_extend(opts.sources or {}, {
nls.builtins.diagnostics.credo,
})
end,
Expand Down
14 changes: 6 additions & 8 deletions lua/lazyvim/plugins/extras/lang/go.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,12 @@ return {
"nvimtools/none-ls.nvim",
optional = true,
opts = function(_, opts)
if type(opts.sources) == "table" then
local nls = require("null-ls")
vim.list_extend(opts.sources, {
nls.builtins.code_actions.gomodifytags,
nls.builtins.code_actions.impl,
nls.builtins.formatting.goimports,
})
end
local nls = require("null-ls")
opts.sources = vim.list_extend(opts.sources or {}, {
nls.builtins.code_actions.gomodifytags,
nls.builtins.code_actions.impl,
nls.builtins.formatting.goimports,
})
end,
},
{
Expand Down
1 change: 1 addition & 0 deletions lua/lazyvim/plugins/extras/lang/omnisharp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ return {
optional = true,
opts = function(_, opts)
local nls = require("null-ls")
opts.sources = opts.sources or {}
table.insert(opts.sources, nls.builtins.formatting.csharpier)
end,
},
Expand Down
12 changes: 5 additions & 7 deletions lua/lazyvim/plugins/extras/lang/terraform.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ return {
"nvimtools/none-ls.nvim",
optional = true,
opts = function(_, opts)
if type(opts.sources) == "table" then
local null_ls = require("null-ls")
vim.list_extend(opts.sources, {
null_ls.builtins.formatting.terraform_fmt,
null_ls.builtins.diagnostics.terraform_validate,
})
end
local null_ls = require("null-ls")
opts.sources = vim.list_extend(opts.sources or {}, {
null_ls.builtins.formatting.terraform_fmt,
null_ls.builtins.diagnostics.terraform_validate,
})
end,
},
{
Expand Down
45 changes: 45 additions & 0 deletions lua/lazyvim/plugins/extras/lsp/none-ls.lua
Original file line number Diff line number Diff line change
@@ -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,
},
}
File renamed without changes.
67 changes: 67 additions & 0 deletions lua/lazyvim/plugins/linting.lua
Original file line number Diff line number Diff line change
@@ -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,
},
}
43 changes: 0 additions & 43 deletions lua/lazyvim/plugins/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -200,49 +200,6 @@ return {
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
{

Expand Down
19 changes: 19 additions & 0 deletions lua/lazyvim/util/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,26 @@ function M.format(opts)
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()
M.health()

-- Autoformat autocmd
vim.api.nvim_create_autocmd("BufWritePre", {
group = vim.api.nvim_create_augroup("LazyFormat", {}),
Expand Down

0 comments on commit 14c091b

Please sign in to comment.