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

Filename evaluated *after* buffer is closed #13

Open
YodaEmbedding opened this issue Jul 25, 2024 · 1 comment
Open

Filename evaluated *after* buffer is closed #13

YodaEmbedding opened this issue Jul 25, 2024 · 1 comment

Comments

@YodaEmbedding
Copy link

YodaEmbedding commented Jul 25, 2024

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:

  1. 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.
  2. Just tell the user to always use "utf-8" since that's the most common encoding anyways.
  3. 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",
@YodaEmbedding

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant