OmniSharp / omnisharp-vim

Vim omnicompletion (intellisense) and more for C#
http://www.omnisharp.net
MIT License
1.7k stars 169 forks source link

Use the file's encoding for new code-action files #762

Closed nickspoons closed 2 years ago

nickspoons commented 2 years ago

Closes #761

DasOhmoff commented 2 years ago

Hey, I was waiting for this feature. When will it be merged?

nickspoons commented 2 years ago

Hi @DasOhmoff, I never got any feedback on #761 so I don't really know if this solves that issue. I've never had a problem with encoding personally so I didn't really know if this PR is actually useful.

Have you tried it? What is the scenario that this PR fixes for you?

DasOhmoff commented 2 years ago

Hey. I tested it out, but it is not working. It still creates ansi files instead of utf-8 on my pc.

nickspoons commented 2 years ago

Can you tell me what vim outputs for the following, before and after creating a new file using a code action?

:set fileformat?
:set encoding?
:set fileencoding?
:set fileencodings?

Which code action are you using to create the new file? Does your file include any non-ascii characters?

nickspoons commented 2 years ago

@DasOhmoff I suspect what is happening is that your newly created files don't have any non-ascii characters, so it actually doesn't matter which encoding is used to write the file. When you next open the file, vim tries to find the correct encoding but if it is a simple file with no BOM or non-ascii characters then vim uses the first value of fileencodings.

When there are non-ascii characters, then this PR causes the fileformat to match that of the previous file. It seems to work and I haven't experienced any errors over the last few weeks so I'm going to merge it now.

DasOhmoff commented 2 years ago

Sorry for the long wait. Here is the output: image

vim tries to find the correct encoding but if it is a simple file with no BOM or non-ascii characters then vim uses the first value of fileencodings.

I see, but how can I make it generate utf-8-bom files? Because the other files in my projects are also with bom. But the newly generated file is not.

nickspoons commented 2 years ago

@DasOhmoff how would you normally create a utf-8-bom file in vim, outside of OmniSharp-vim?

DasOhmoff commented 2 years ago

I dont even know right now actually. The files get created through another program for us (Unity)

nickspoons commented 2 years ago

It would be very helpful for me to have an example file to play with

DasOhmoff commented 2 years ago

I know that opening a file, setting fileencoding=utf-8 bomb, and saving it, converts the encoding to utf-8 bom.

DasOhmoff commented 2 years ago

It would be very helpful for me to have an example file to play with

what do you mean? An utf-8 bom sample file?

nickspoons commented 2 years ago

I know that opening a file, setting fileencoding=utf-8 bomb, and saving it, converts the encoding to utf-8 bom.

Can you tell me the exact command? Because you can't include spaces in options so :set fileencoding=utf-8 bomb is not valid.

DasOhmoff commented 2 years ago

Does it not work on your machine? Using set fileencoding=utf-8 bomb (with the space) does work on my machine. Is it maybe because I use neovim instead of plain vim? Either way, I just noticed that doing this actually sets the bomb setting for the buffer, so I think one could just use set bomb instead, maybe this helps, or maybe its a problem, I don't know. Of course, to remove the bom one can use set nobomb

nickspoons commented 2 years ago

Right yes, the command :set fileencoding=utf-8 bomb is valid, but it is actually 2 commands. It sets 'fileencoding' to utf-8, and it enables 'bomb'.

So the relevant thing here is not the 'fileencoding' at all, but rather 'bomb', which causes the BOM to be added to the file on write. Is that actually the crux of the issue? That when the "parent" file has a BOM, the new file should also have a BOM, and so we should temporarily set 'bomb' before writing?

Do all unity-generated files have a BOM?

DasOhmoff commented 2 years ago

Yeah, that seems to be the issue indeed.

Do all unity-generated files have a BOM?

There is an option for this. If this option is enabled, then all newly generated files have it. I am not sure whether its is enabled by default on Windows, but not enabling it causes issues on windows and visual studio.

nickspoons commented 2 years ago

OK @DasOhmoff, I think that ought to do it. Please get the latest version of OmniSharp-vim and give it a try.

DasOhmoff commented 2 years ago

I tested it out. It seems to be working. Thank you!

DasGandlaf commented 2 years ago

This is great! Apologies for not responding to the previous issue, I somehow did not see you responding to it.