Skip to content

fix: Handle empty fileencoding by falling back to global encoding#346

Merged
zbirenbaum merged 1 commit intozbirenbaum:masterfrom
Syu-fu:fix_issue_344
Dec 23, 2024
Merged

fix: Handle empty fileencoding by falling back to global encoding#346
zbirenbaum merged 1 commit intozbirenbaum:masterfrom
Syu-fu:fix_issue_344

Conversation

@Syu-fu
Copy link
Copy Markdown
Contributor

@Syu-fu Syu-fu commented Dec 15, 2024

Description

This fix addresses an issue where an error occurs if the buffer's fileencoding option is empty during certain operations, such as applying text edits in the LSP. (#344)

Context

While similar issues regarding file encodings have been reported and resolved, this specific case occurs when the fileencoding of a buffer is not explicitly set. In such cases, the empty string ("") is returned, leading to failures in functions that expect a valid encoding (e.g., vim.lsp.util.apply_text_edits).

Changes

  • Added a fallback mechanism to use the global encoding option (vim.o.encoding) if fileencoding is empty.
  • Ensured that the user’s configuration for encoding is respected before defaulting to "utf-8".

Steps to Reproduce

  1. Open a new file in Neovim (e.g., nvim newfile.txt).
  2. Without saving the file or setting fileencoding, trigger an operation that depends on the file's encoding, such as applying LSP edits.
  3. Observe the error due to the empty fileencoding.

Fix Explanation

The fix retrieves the fileencoding value for the buffer and checks if it is empty. If it is, the global encoding setting is used as a fallback. This ensures that the operation continues with a valid encoding, avoiding errors.

@fserb
Copy link
Copy Markdown

fserb commented Dec 18, 2024

Thanks so much for this. I've been getting those errors and have to force set encoding a lot.

Comment on lines +490 to +492
local encoding = vim.api.nvim_get_option_value("fileencoding", { buf = bufnr }) ~= ""
and vim.api.nvim_get_option_value("fileencoding", { buf = bufnr })
or vim.api.nvim_get_option_value("encoding", { scope = "global" })
Copy link
Copy Markdown

@unlimitedsola unlimitedsola Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally find this easier to read.

Suggested change
local encoding = vim.api.nvim_get_option_value("fileencoding", { buf = bufnr }) ~= ""
and vim.api.nvim_get_option_value("fileencoding", { buf = bufnr })
or vim.api.nvim_get_option_value("encoding", { scope = "global" })
local encoding = vim.api.nvim_get_option_value("fileencoding", { buf = bufnr })
if encoding == "" then
encoding = vim.api.nvim_get_option_value("fileencoding", { scope = "global" })
end

@zbirenbaum
Copy link
Copy Markdown
Owner

LGTM thanks for the fix!

@zbirenbaum zbirenbaum merged commit 886ee73 into zbirenbaum:master Dec 23, 2024
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

Successfully merging this pull request may close these issues.

4 participants