-
-
Notifications
You must be signed in to change notification settings - Fork 210
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
Set the cwd for each linter #646
Comments
The following config may help you: local opts = {
events = {
-- loading buffer after nvim-lint: event="BufReadPost"
"BufReadPost",
-- some linters require saving file
"BufWritePost",
"InsertLeave",
-- more aggresive (e.g., undo)
"TextChanged",
},
linters_by_ft = {
lua = {
"selene",
-- "luacheck",
},
github_action = {
-- Static checker for GitHub Actions workflow files
"actionlint",
-- general yaml linter
"yamllint",
},
},
root_patterns_by_ft = {
lua = {
selene = { "selene.toml", ".git" },
-- luacheck = { ".luacheckrc", ".git" },
},
github_action = {
yamllint = { ".yamllint.yaml", ".yamllint.yml", ".yamllint" },
},
}
}
local lint = require("lint")
local api = vim.api
local fs = vim.fs
lint.linters_by_ft = opts.linters_by_ft
---Set cwd for linters specified by opts.root_patterns_by_ft[filetype]
---@param linter_root_patterns table<string, string[]>? a mapping of linter to patterns
local function set_linters_cwd(linter_root_patterns)
for linter, root_patterns in pairs(linter_root_patterns or {}) do
local root_path = root_patterns and fs.root(0, root_patterns)
if root_path then
lint.linters[linter].cwd = root_path
-- you can set cwd to buffer's parent dir if no root is found
-- else
-- lint.linters[linter].cwd = fs.dirname(api.nvim_buf_get_name(0))
end
end
end
local lint_augroup = api.nvim_create_augroup("nvim-lint", { clear = true })
api.nvim_create_autocmd(opts.events, {
group = lint_augroup,
callback = function()
if filetype == "yaml" and string.find(path, ".github/workflows/") then
set_linters_cwd(opts.root_patterns_by_ft.github_action)
lint.try_lint(opts.linters_by_ft.github_action)
return
end
set_linters_cwd(opts.root_patterns_by_ft[filetype])
lint.try_lint()
end,
}) |
I have no plans to allow setting the cwd on a per linter level but |
I'm not sure I understand... it's already possible to set the Line 84 in 27f44d1
Line 366 in 968a35d
Note that I'm actually doing it myself for one linter I'm debugging at the moment. I can set the What's not currently supported is to provide the or... did I misunderstand something? |
Let's say I have a project with the following structure:
Currently,
nvim-lint
will set the cwd to the Neovim cwd,project
in this case, but when trying to editsubproject/src/init.lua
, the cwd should beproject/subproject
so selene will pick the correct configuration fileMy proposal is to add a
cwd
field to the linter configWould you be open to a PR to address this?
The text was updated successfully, but these errors were encountered: