Skip to content

Filename evaluated *after* buffer is closed #13

@YodaEmbedding

Description

@YodaEmbedding

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",

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions