Skip to content
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

fix(dictionary): avoid overwrite corruption if filenames are the same #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions lua/ltex-utils/settings_io.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,21 @@ end
function M.update_dictionary_files(dictionaries)
---@type string[]
local used_langs = {}
local used_filenames = {}
for lang, dict in pairs(dictionaries) do
---@type string
local filename = Config.dictionary.path ..
Config.dictionary.filename(lang)
---@type string[]|nil
local saved_dict = M.read_dictionary(filename)
-- if there is already a saved dictionary merge it with current one
if saved_dict then
dict = table_utils.merge_lists_unique(dict, saved_dict)
if not vim.tbl_contains(used_filenames, filename) then
---@type string[]|nil
local saved_dict = M.read_dictionary(filename)
-- if there is already a saved dictionary merge it with current one
if saved_dict then
dict = table_utils.merge_lists_unique(dict, saved_dict)
end
M.write(filename, table.concat(dict, "\n") .. "\n")
end
M.write(filename, table.concat(dict, "\n") .. "\n")
table.insert(used_filenames, filename)
table.insert(used_langs, lang)
end

Expand Down
2 changes: 2 additions & 0 deletions lua/ltex-utils/table_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ function M.merge_lists_unique(list1, list2, changes)
---@type string[]
local merged_list = vim.tbl_keys(unique_elements)

table.sort(merged_list)

return merged_list
end

Expand Down