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: Handle empty fileencoding by falling back to global encoding #346

Merged
merged 1 commit into from
Dec 23, 2024

Conversation

Syu-fu
Copy link
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

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

@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
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