Skip to content

Commit

Permalink
fix: read the code actions config synchronously
Browse files Browse the repository at this point in the history
fixes: #25
  • Loading branch information
David Mejorado authored and David Mejorado committed Nov 13, 2023
1 parent 335080b commit eb243e1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ local config = {
find_json = function(cwd)
end,

-- Will find and read the cspell config file synchronously, as soon as the
-- code actions generator gets called.
--
-- If you experience UI-blocking during the first run of this code action, try
-- setting this option to false.
-- See: https://github.com/davidmh/cspell.nvim/issues/25
read_config_synchronously = true,

---@param cspell string The contents of the CSpell config file
---@return table
decode_json = function(cspell_str)
Expand Down
25 changes: 21 additions & 4 deletions lua/cspell/code_actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ local cspell_diagnostics = function(bufnr, lnum, cursor_col)
return diagnostics
end

---@param params GeneratorParams
local get_config_info = function(params)
---@type CSpellSourceConfig
local code_action_config = vim.tbl_extend(
'force',
{ read_config_synchronously = true },
params:get_config()
)

-- In theory, the call to async_get_config_info in the diagnostics source
-- should already have been loaded, that's why we're defaulting reading the
-- config synchronously here.
if code_action_config.read_config_synchronously then
return h.sync_get_config_info(params)
end

return h.async_get_config_info(params)
end

return make_builtin({
name = "cspell",
meta = {
Expand All @@ -42,11 +61,11 @@ return make_builtin({
fn = function(params)
params.cwd = params.cwd or vim.loop.cwd()

local cspell = get_config_info(params)

---@type table<number, CodeAction>
local actions = {}

local cspell = h.async_get_config_info(params)

local diagnostics = cspell_diagnostics(params.bufnr, params.row - 1, params.col)
if vim.tbl_isempty(diagnostics) then
return actions
Expand All @@ -60,8 +79,6 @@ return make_builtin({
action = function()
h.set_word(diagnostic, suggestion)

---@type CSpellSourceConfig
local code_action_config = params:get_config()
local on_success = code_action_config.on_success

if on_success then
Expand Down
20 changes: 16 additions & 4 deletions lua/cspell/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,7 @@ M.async_get_config_info = function(params)
---@type uv_async_t|nil
local async
async = vim.loop.new_async(function()
if CONFIG_INFO_BY_CWD[params.cwd] == nil then
local config = M.get_cspell_config(params)
CONFIG_INFO_BY_CWD[params.cwd] = config
end
M.sync_get_config_info(params)
async:close()
end)

Expand All @@ -147,6 +144,14 @@ M.async_get_config_info = function(params)
return CONFIG_INFO_BY_CWD[params.cwd]
end

M.sync_get_config_info = function(params)
if CONFIG_INFO_BY_CWD[params.cwd] == nil then
local config = M.get_cspell_config(params)
CONFIG_INFO_BY_CWD[params.cwd] = config
end
return CONFIG_INFO_BY_CWD[params.cwd]
end

M.get_config_path = function(params)
if PATH_BY_CWD[params.cwd] == nil then
local code_action_config = params:get_config()
Expand Down Expand Up @@ -239,6 +244,13 @@ return M

---@class CSpellSourceConfig
---@field config_file_preferred_name string|nil
--- Will find and read the cspell config file synchronously, as soon as the
--- code actions generator gets called.
---
--- If you experience UI-blocking during the first run of this code action, try
--- setting this option to false.
--- See: https://github.com/davidmh/cspell.nvim/issues/25
---@field read_config_synchronously boolean|nil
---@field find_json function|nil
---@field decode_json function|nil
---@field encode_json function|nil
Expand Down

0 comments on commit eb243e1

Please sign in to comment.