Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow conform.nvim and nvim-lint to work alongside mason-null-ls #756

Merged
merged 2 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lua/astrocommunity/editing-support/conform-nvim/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
return {
{ "AstroNvim/astrolsp", opts = { formatting = { disabled = true } } },
{ "jay-babu/mason-null-ls.nvim", optional = true, opts = { methods = { formatting = false } } },
{
"stevearc/conform.nvim",
event = "User AstroFile",
Expand Down
87 changes: 45 additions & 42 deletions lua/astrocommunity/lsp/nvim-lint/init.lua
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
---@type LazySpec
return {
"mfussenegger/nvim-lint",
event = "User AstroFile",
dependencies = { "williamboman/mason.nvim" },
opts = {},
config = function(_, opts)
local lint = require "lint"
{ "jay-babu/mason-null-ls.nvim", optional = true, opts = { methods = { diagnostics = false } } },
{
"mfussenegger/nvim-lint",
event = "User AstroFile",
dependencies = { "williamboman/mason.nvim" },
opts = {},
config = function(_, opts)
local lint = require "lint"

lint.linters_by_ft = opts.linters_by_ft or {}
for name, linter in pairs(opts.linters or {}) do
local base = lint.linters[name]
lint.linters[name] = (type(linter) == "table" and type(base) == "table")
and vim.tbl_deep_extend("force", base, linter)
or linter
end
lint.linters_by_ft = opts.linters_by_ft or {}
for name, linter in pairs(opts.linters or {}) do
local base = lint.linters[name]
lint.linters[name] = (type(linter) == "table" and type(base) == "table")
and vim.tbl_deep_extend("force", base, linter)
or linter
end

local function try_lint()
local names = lint._resolve_linter_by_ft(vim.bo.filetype)
local function try_lint()
local names = lint._resolve_linter_by_ft(vim.bo.filetype)

-- Add fallback linters and global linters.
if #names == 0 then names = lint.linters_by_ft["_"] or {} end
vim.list_extend(names, lint.linters_by_ft["*"] or {})
-- Add fallback linters and global linters.
if #names == 0 then names = lint.linters_by_ft["_"] or {} end
vim.list_extend(names, lint.linters_by_ft["*"] or {})

-- Filter out linters that don't exist or don't match the condition.
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 vim.fn.executable(linter.cmd) == 1
and not (type(linter) == "table" and linter.condition and not linter.condition(ctx))
end, names)
-- Filter out linters that don't exist or don't match the condition.
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 vim.fn.executable(linter.cmd) == 1
and not (type(linter) == "table" and linter.condition and not linter.condition(ctx))
end, names)

lint.try_lint(names)
end
lint.try_lint(names)
end

try_lint() -- start linter immediately
local timer = vim.loop.new_timer()
vim.api.nvim_create_autocmd({ "BufWritePost", "BufReadPost", "InsertLeave", "TextChanged" }, {
group = vim.api.nvim_create_augroup("auto_lint", { clear = true }),
desc = "Automatically try linting",
callback = function()
timer:start(100, 0, function()
timer:stop()
vim.schedule(try_lint)
end)
end,
})
end,
try_lint() -- start linter immediately
local timer = vim.loop.new_timer()
vim.api.nvim_create_autocmd({ "BufWritePost", "BufReadPost", "InsertLeave", "TextChanged" }, {
group = vim.api.nvim_create_augroup("auto_lint", { clear = true }),
desc = "Automatically try linting",
callback = function()
timer:start(100, 0, function()
timer:stop()
vim.schedule(try_lint)
end)
end,
})
end,
},
}
Loading