The filename() function is called once after the buffer is opened, and once after buffer is closed. This is problematic since the docs suggest:
local encoding = vim.api.nvim_buf_get_option(0, "fileencoding")
...but that's taking the encoding of the new buffer, not the one that was just closed!
Possible fixes:
- Evaluate
filename() only when the buffer is opened, then save this filename to an internal hash table that maps the buffer number/id to the filename. Also, update it if the user switches languages or encoding.
- Just tell the user to always use "utf-8" since that's the most common encoding anyways.
- Pass in an
encoding argument into filename(). But the encoding should be determined before the file is closed.
opts = {
dictionary = {
filename = function(lang, encoding)
-- For example:
-- lang = "en-US"
-- lang_prefix = "en"
-- encoding = "utf-8"
-- filename = "en.utf-8.add"
local lang_prefix = string.match(lang, "^(%a+)-")
encoding = encoding or "utf-8"
local filename = lang_prefix .. "." .. encoding .. ".add"
return filename
},
},
Quick n' dirty patch:
diff --git a/lua/ltex-utils/words_cache.lua b/lua/ltex-utils/words_cache.lua
index 0f9ca28..3d8b920 100644
--- a/lua/ltex-utils/words_cache.lua
+++ b/lua/ltex-utils/words_cache.lua
@@ -53,8 +53,9 @@ function M:apply_cache(bufnr)
for lang, dict in pairs(dicts) do
---@type string
+ local encoding = vim.api.nvim_get_option_value("fileencoding", { buf = bufnr })
local filename = Config.dictionary.path ..
- Config.dictionary.filename(lang)
+ Config.dictionary.filename(lang, encoding)
settings_io.write(
filename,
table.concat(dict, "\n") .. "\n",
The
filename()function is called once after the buffer is opened, and once after buffer is closed. This is problematic since the docs suggest:...but that's taking the encoding of the new buffer, not the one that was just closed!
Possible fixes:
filename()only when the buffer is opened, then save this filename to an internal hash table that maps the buffer number/id to the filename. Also, update it if the user switches languages or encoding.encodingargument intofilename(). But the encoding should be determined before the file is closed.Quick n' dirty patch: