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

Stop changing 'encoding' #186

Merged
merged 3 commits into from
Oct 30, 2022
Merged

Conversation

k-takata
Copy link
Contributor

@k-takata k-takata commented Jan 21, 2022

Changing 'encoding' has many side effects and that should be avoided.
Strip BOM manually.

Fix #144
Fix #145
Fix #147

Copy link
Member

@cxw42 cxw42 left a comment

Choose a reason for hiding this comment

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

Thanks for this PR! Would you be willing to add some test cases of files with different encodings? I can help with the test-case plumbing if that would be helpful.

let l:lines = readfile(a:config_filename)
if &encoding !=? 'utf-8'
" strip BOM
if len(l:lines) > 0 && l:lines[0][:2] ==# "\xEF\xBB\xBF"
Copy link
Member

Choose a reason for hiding this comment

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

Will the first three bytes always come through as the UTF-8 BOM values? E.g., what happens when the file is UTF-16?

Copy link
Contributor Author

@k-takata k-takata Jun 20, 2022

Choose a reason for hiding this comment

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

This part strips the UTF-8 BOM only when it exists. Otherwise it is kept as is.

(The editorconfig spec says that "EditorConfig files should be UTF-8 encoded", so I don't think we need to care about UTF-16.)

Changing 'encoding' has many side effects and that should be avoided.
Strip BOM manually.

Fix editorconfig#144
Fix editorconfig#147
`readfile()` doesn't convert the encoding of the file.
Convert it manually when 'encoding' is not UTF-8.
@k-takata
Copy link
Contributor Author

Would you be willing to add some test cases of files with different encodings?

Do we need to add some test cases with a .editorconfig file with UTF-8 BOM? Then, should https://github.com/editorconfig/editorconfig-plugin-tests be updated?

@cxw42
Copy link
Member

cxw42 commented Aug 2, 2022

Would you be willing to add some test cases of files with different encodings?

Do we need to add some test cases with a .editorconfig file with UTF-8 BOM? Then, should https://github.com/editorconfig/editorconfig-plugin-tests be updated?

@k-takata Good question! I don't think we need to update the main plugin tests, since this problem is specific to this plugin. If you or @h3xx can send me some test cases, I'll figure out where to put them.

@k-takata
Copy link
Contributor Author

k-takata commented Aug 2, 2022

How about creating a copy of https://github.com/editorconfig/editorconfig-plugin-tests/tree/master/test_files with just adding a UTF-8 BOM to test_files/.editorconfig?

@h3xx
Copy link

h3xx commented Aug 3, 2022

+1 for this PR. Tested these changes against the issue in my PR #194, and it in fact fixes the issue I described.

This PR seems more comprehensive than my change so I closed #194.

Summary of issue from #194 (~12 lines)

For some reason, this plugin is causing :e to change the file's encoding option (&fileencoding) to utf-8, overriding whatever it's currently set to. This doesn't happen upon first opening the file, but only upon re-opening it with :e.

Steps to reproduce:

  1. Open and save a file in latin1 mode:
export LANG=en_US LC_ALL=en_US LC_CTYPE=en_US
vim foo.txt
:set fenc=latin1
:w
  1. Without closing Vim, reload the file:
:e
  1. Check the file encoding hasn't changed:
:echo &fenc

&fenc should still be latin1. If it was reset to utf-8 the issue persists.

@alkim0
Copy link

alkim0 commented Aug 9, 2022

This will also fix #196

@cxw42
Copy link
Member

cxw42 commented Oct 30, 2022

Merged --- let me know if you experience any issues!

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