fix: Handle empty fileencoding by falling back to global encoding#346
Merged
zbirenbaum merged 1 commit intozbirenbaum:masterfrom Dec 23, 2024
Merged
fix: Handle empty fileencoding by falling back to global encoding#346zbirenbaum merged 1 commit intozbirenbaum:masterfrom
zbirenbaum merged 1 commit intozbirenbaum:masterfrom
Conversation
|
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" }) |
There was a problem hiding this comment.
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 |
Owner
|
LGTM thanks for the fix! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This fix addresses an issue where an error occurs if the buffer's
fileencodingoption 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
fileencodingof 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
encodingoption (vim.o.encoding) iffileencodingis empty.encodingis respected before defaulting to"utf-8".Steps to Reproduce
nvim newfile.txt).fileencoding, trigger an operation that depends on the file's encoding, such as applying LSP edits.fileencoding.Fix Explanation
The fix retrieves the
fileencodingvalue for the buffer and checks if it is empty. If it is, the globalencodingsetting is used as a fallback. This ensures that the operation continues with a valid encoding, avoiding errors.