diff --git a/lua/null-ls/builtins/code_actions/cspell.lua b/lua/null-ls/builtins/code_actions/cspell.lua index 3d320928a..9aa791919 100644 --- a/lua/null-ls/builtins/code_actions/cspell.lua +++ b/lua/null-ls/builtins/code_actions/cspell.lua @@ -13,27 +13,6 @@ local cspell_diagnostics = function(bufnr, lnum, cursor_col) return diagnostics end -local CSPELL_CONFIG_FILES = { - "cspell.json", - ".cspell.json", - "cSpell.json", - ".Sspell.json", - ".cspell.config.json", -} - --- find the first cspell.json file in the directory tree -local find_cspell_config = function(cwd) - local cspell_json_file = nil - for _, file in ipairs(CSPELL_CONFIG_FILES) do - local path = vim.fn.findfile(file, (cwd or vim.loop.cwd()) .. ";") - if path ~= "" then - cspell_json_file = path - break - end - end - return cspell_json_file -end - -- create a bare minimum cspell.json file local create_cspell_json = function(cwd, file_name) local cspell_json = { @@ -65,13 +44,13 @@ return h.make_builtin({ fn = function(params) local actions = {} local config = params:get_config() - local find_json = config.find_json or find_cspell_config + local find_json = config.find_json or h.cspell.find_cspell_config -- create_config_file if nil defaults to true local create_config_file = config.create_config_file ~= false local create_config_file_name = config.create_config_file_name or "cspell.json" - if not vim.tbl_contains(CSPELL_CONFIG_FILES, create_config_file_name) then + if not vim.tbl_contains(h.cspell.CSPELL_CONFIG_FILES, create_config_file_name) then vim.notify( "Invalid default file name for cspell json file: " .. create_config_file_name diff --git a/lua/null-ls/builtins/diagnostics/cspell.lua b/lua/null-ls/builtins/diagnostics/cspell.lua index 44c79d86a..dc5e008ee 100644 --- a/lua/null-ls/builtins/diagnostics/cspell.lua +++ b/lua/null-ls/builtins/diagnostics/cspell.lua @@ -32,8 +32,18 @@ return h.make_builtin({ generator_opts = { command = "cspell", args = function(params) + local config = params:get_config() + local find_json = config.find_json or h.cspell.find_cspell_config + local cspell_json_file = find_json(params.cwd) + + if cspell_json_file == nil or cspell_json_file == "" then + cspell_json_file = "cspell.json" -- default for the -c option + end + local cspell_args = { "lint", + "-c", + cspell_json_file, "--language-id", params.ft, "stdin", diff --git a/lua/null-ls/helpers/cspell.lua b/lua/null-ls/helpers/cspell.lua new file mode 100644 index 000000000..93786fc01 --- /dev/null +++ b/lua/null-ls/helpers/cspell.lua @@ -0,0 +1,25 @@ +local CSPELL_CONFIG_FILES = { + "cspell.json", + ".cspell.json", + "cSpell.json", + ".Sspell.json", + ".cspell.config.json", +} + +-- find the first cspell.json file in the directory tree +local find_cspell_config = function(cwd) + local cspell_json_file = nil + for _, file in ipairs(CSPELL_CONFIG_FILES) do + local path = vim.fn.findfile(file, (cwd or vim.loop.cwd()) .. ";") + if path ~= "" then + cspell_json_file = path + break + end + end + return cspell_json_file +end + +return { + CSPELL_CONFIG_FILES = CSPELL_CONFIG_FILES, + find_cspell_config = find_cspell_config, +} diff --git a/lua/null-ls/helpers/init.lua b/lua/null-ls/helpers/init.lua index b956c8fc4..0ef140818 100644 --- a/lua/null-ls/helpers/init.lua +++ b/lua/null-ls/helpers/init.lua @@ -1,5 +1,6 @@ return { cache = require("null-ls.helpers.cache"), + cspell = require("null-ls.helpers.cspell"), diagnostics = require("null-ls.helpers.diagnostics"), formatter_factory = require("null-ls.helpers.formatter_factory"), generator_factory = require("null-ls.helpers.generator_factory"),