Skip to content

Commit

Permalink
fix: use none-ls root in the code actions (#71)
Browse files Browse the repository at this point in the history
And make sure we update the merged config to reference a possibly new
file.
  • Loading branch information
davidmh authored Nov 21, 2024
1 parent 2c29bf5 commit 8fe923f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lua/cspell/code_actions/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local make_builtin = require("null-ls.helpers").make_builtin
local u = require("null-ls.utils")
local methods = require("null-ls.methods")
local h = require("cspell.helpers")
local make_add_to_json = require("cspell.code_actions.make_add_to_json")
Expand Down Expand Up @@ -54,7 +55,7 @@ return make_builtin({
---@param params GeneratorParams
---@return table<number, CodeAction>
fn = function(params)
params.cwd = params.cwd or vim.loop.cwd()
params.cwd = params.cwd or u.get_root()

---@type CSpellSourceConfig
local code_action_config =
Expand Down
4 changes: 4 additions & 0 deletions lua/cspell/code_actions/make_add_to_json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ return function(opts)

cspell = cspell or h.create_default_cspell_json(opts.params, cspell_config_path)

if not path_exists then
pcall(h.update_merged_config_imports, opts.params, cspell_config_path)
end

if not cspell.config.words then
cspell.config.words = {}
end
Expand Down
29 changes: 24 additions & 5 deletions lua/cspell/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,23 @@ local set_compare = function(expected_values, new_values)
return true
end

--- create a merged cspell.json file that imports all cspell configs defined in cspell_config_dirs
---@param params GeneratorParams
---@param cspell_config_mapping table<number|string, string>
---@return CSpellConfigInfo
M.create_merged_cspell_json = function(params, cspell_config_mapping)
M.get_merged_cspell_json_path = function(params)
local vim_cache = vim.fn.stdpath("cache")
local plugin_name = "cspell.nvim"

local merged_config_key = Path:new(params.cwd):joinpath("cspell.json"):absolute():gsub("/", "%%")
local merged_config_path = Path:new(vim_cache):joinpath(plugin_name):joinpath(merged_config_key):absolute()

return merged_config_path
end

--- create a merged cspell.json file that imports all cspell configs defined in cspell_config_dirs
---@param params GeneratorParams
---@param cspell_config_mapping table<number|string, string>
---@return CSpellConfigInfo
M.create_merged_cspell_json = function(params, cspell_config_mapping)
local merged_config_path = M.get_merged_cspell_json_path(params)

local cspell_config_paths = {}

if CONFIG_INFO_BY_PATH[merged_config_path] ~= nil then
Expand Down Expand Up @@ -111,6 +117,19 @@ M.create_merged_cspell_json = function(params, cspell_config_mapping)
return create_cspell_json(params, cspell_json, merged_config_path)
end

--- Update the import field from a merged config file
---@param params GeneratorParams
---@param cspell_config_path string
function M.update_merged_config_imports(params, cspell_config_path)
local merged_config_path = M.get_merged_cspell_json_path(params)
local merged_config = vim.json.decode(Path:new(merged_config_path):read())

merged_config.import = merged_config.import or {}
table.insert(merged_config.import, cspell_config_path)

Path:new(merged_config_path):write(vim.json.encode(merged_config), "w")
end

--- create a bare minimum cspell.json file
---@param params GeneratorParams
---@param cspell_json_file_path string
Expand Down

0 comments on commit 8fe923f

Please sign in to comment.