Skip to content

Commit

Permalink
feat: add option to rewrite the config on cwd change
Browse files Browse the repository at this point in the history
also moves from params.cwd to vim.fn.getcwd(-1,-1) (the global cwd)
because params.cwd is only set once to the first working directory
encountered and is never updated afterwards, no matter what happens to
the global cwd
  • Loading branch information
KotarouX committed Jan 19, 2025
1 parent 6281b3d commit 9dec139
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
5 changes: 1 addition & 4 deletions lua/cspell/code_actions/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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 @@ -55,8 +54,6 @@ return make_builtin({
---@param params GeneratorParams
---@return table<number, CodeAction>
fn = function(params)
params.cwd = params.cwd or u.get_root()

---@type CSpellSourceConfig
local code_action_config =
vim.tbl_extend("force", { read_config_synchronously = true }, params:get_config())
Expand All @@ -65,7 +62,7 @@ return make_builtin({
local cspell_config_paths = {}

local cspell_config_directories = code_action_config.cspell_config_dirs or {}
table.insert(cspell_config_directories, params.cwd)
table.insert(cspell_config_directories, vim.fn.getcwd(-1, -1))

for _, cspell_config_directory in pairs(cspell_config_directories) do
local cspell_config_path = h.get_config_path(params, cspell_config_directory)
Expand Down
5 changes: 2 additions & 3 deletions lua/cspell/diagnostics/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ return h.make_builtin({
params.ft,
"stdin://" .. params.bufname,
}
params.cwd = params.cwd or vim.loop.cwd()

---@type CSpellSourceConfig
local diagnostics_config = params and params:get_config() or {}
Expand All @@ -34,7 +33,7 @@ return h.make_builtin({
local cspell_config_paths = {}

local cspell_config_directories = diagnostics_config.cspell_config_dirs or {}
table.insert(cspell_config_directories, params.cwd)
table.insert(cspell_config_directories, vim.fn.getcwd(-1, -1))

for _, cspell_config_directory in pairs(cspell_config_directories) do
local cspell_config_path = helpers.get_config_path(params, cspell_config_directory)
Expand All @@ -60,7 +59,7 @@ return h.make_builtin({

if helpers.matching_configs(code_action_config, diagnostics_config) then
-- warm up the config cache so we have the config ready by the time we call the code action
helpers.async_get_config_info(params, cspell_config_paths[params.cwd])
helpers.async_get_config_info(params, cspell_config_paths[vim.fn.getcwd(-1, -1)])
elseif needs_warning then
needs_warning = false
vim.notify(
Expand Down
30 changes: 19 additions & 11 deletions lua/cspell/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ local CONFIG_INFO_BY_PATH = {}
---@type table<string, string|nil>
local PATH_BY_DIRECTORY = {}

local last_working_directory = ""

local cspell_config_read_languages = function(cspell_config_path)
local language_list = {}
local ok, cspell_config = pcall(vim.json.decode, Path:new(cspell_config_path):read())
Expand Down Expand Up @@ -97,10 +99,14 @@ end
---@return CSpellConfigInfo
M.create_merged_cspell_json = function(params, cspell_config_mapping)
local merged_config_path = M.get_merged_cspell_json_path(params)
local force_rewrite = params:get_config().reload_on_cwd_change and last_working_directory ~= vim.fn.getcwd(-1, -1)
if force_rewrite then
last_working_directory = vim.fn.getcwd(-1, -1)
end

local cspell_config_paths = {}

if CONFIG_INFO_BY_PATH[merged_config_path] ~= nil then
if not force_rewrite and CONFIG_INFO_BY_PATH[merged_config_path] ~= nil then
return CONFIG_INFO_BY_PATH[merged_config_path]
end

Expand Down Expand Up @@ -135,16 +141,18 @@ M.create_merged_cspell_json = function(params, cspell_config_mapping)
flagWords = {},
import = cspell_config_paths,
}

local existing_config = M.get_cspell_config(params, merged_config_path)

if existing_config ~= nil then
local existing_import_set = set_create(existing_config.config.import)
local new_import_set = set_create(cspell_json.import)

if set_compare(existing_import_set, new_import_set) and set_compare(new_import_set, existing_import_set) then
CONFIG_INFO_BY_PATH[merged_config_path] = existing_config
return CONFIG_INFO_BY_PATH[merged_config_path]
if not force_rewrite then
local existing_config = M.get_cspell_config(params, merged_config_path)
if existing_config ~= nil then
local existing_import_set = set_create(existing_config.config.import)
local new_import_set = set_create(cspell_json.import)

if
set_compare(existing_import_set, new_import_set) and set_compare(new_import_set, existing_import_set)
then
CONFIG_INFO_BY_PATH[merged_config_path] = existing_config
return CONFIG_INFO_BY_PATH[merged_config_path]
end
end
end

Expand Down

0 comments on commit 9dec139

Please sign in to comment.