OmniSharp / omnisharp-vim

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

Analyzer reports missing empty line at end of file if `endofline` is set. #782

Closed DasOhmoff closed 2 years ago

DasOhmoff commented 2 years ago

Hey :wave:, thank you for your help

I setup some c# analyzers in my project. One of which makes sure that there is always a blank line at the end the file. Ignore the following source code:

namespace Test
{
    internal class TestClass
    {
    }
}

Notice the blank line at the end of the file. When I open up this file in vim, it shows as follows: image Notice that vim does not show the last blank line of the file even though it is there. The analyzers seems to get confused and shows warnings in that scenario, message: File is required to end with a single newline character.

Vim automatically hides the last line when the buffer has an empty line at the end of it, it also sets the endofline setting. I tried to disable this setting manually through autocmds and etc, but without success.

For example this does not help:

au BufReadPre * set noeol

Also this does not help, as it only saves the file correctly, but does not show it correctly in vim:

au BufWritePre * set binary | set noeol
au BufWritePost * set nobinary | set eol

If the content of the file is correct, shouldn't there be no warning even if vim shows the file differently? How could this be solved?

nickspoons commented 2 years ago

I'm not really sure. Are you just asking advice, or do you think there's something that OmniSharp-vim should be doing in this situation? We just pass the analysis through from OmniSharp-roslyn so this seems like an issue with how vim and the lanuage server operate?

DasOhmoff commented 2 years ago

I assume that omnisharp-vim passes vims buffer to omnisharp-roslyn, which in turn responds with the warnings and errors. Therefore I think that omnisharp-vim is maybe not passing the last empty new line at the end of the file to omnisharp-roslyn, which in turn thinks that there is no new line at the end of the file, and therefore sends the warning back. Basically I think that maybe not the real content of the file is passed to the roslyn server, but the one that is displayed by vim, in which the last blank line is trimmed away.

If that is the case, I think that omnisharp-vim should act as if there is a blank line and the end and pass that to the roslyn server. I think that one could do that by adding the last new line character to the buffer that is passed to the server if the endofline setting is enabled.

nickspoons commented 2 years ago

Hmm OK I see what you mean. It sounds ... fiddly though.

DasOhmoff commented 2 years ago

Yeah, for sure. Sounds pretty "edgecasy".

Maybe this is important to know: I think vim itself fixes this issue with the fixendofline setting. When saving the buffer, it automatically adds the last end of line character to the buffer before saving it. But when viewing the text in the editor, it does not seem to do that.

nickspoons commented 2 years ago

Do you want this analyzer you've installed to actually do anything? That is, if we just add an extra blank line at the end of every request when 'endofline' is true, then the analyzer will never report an error.

nickspoons commented 2 years ago

Are you able to make a demo repo with the analyzer included so I can understand exactly what is happening? I assume it also is related to fileformat?

DasOhmoff commented 2 years ago

Here is the demo project: DemoProject.zip You should receive the following message in the Program.cs file: image

DasOhmoff commented 2 years ago

Do you want this analyzer you've installed to actually do anything?

Yeah, I would like the analyzer to work properly.

That is, if we just add an extra blank line at the end of every request when 'endofline' is true, then the analyzer will never report an error.

I don't understand, why will it never report an error? If I think about it, endofline is only true if there is actually an empty line at the very end. If there is no empty line, then endofline would be false, therefore the request would not have an empty line at the end of the buffer, shouldn't therefore the analyzer return a warning?

I assume it also is related to fileformat?

I thought so too, but I was not able to find anything related to it.

DasOhmoff commented 2 years ago

I think the following might need consideration: If there is no blank line at the end, and fixeol is enabled, when one save the file, then vim automatically adds a blank line at the end. But endofline does NOT get set to true, even though now there is a blank line at the end (because vim automatically added it). endofline becomes outdated, it only holds the state of the file at the opening time, not at the current time after saving.

nickspoons commented 2 years ago

@DasOhmoff are you able to try out PR #784 to see if it resolves the issue for your analyzer? In the test project I found it went like this:

  1. Open Program.cs and see that the analyzer complains, and 'endofline' is off. xxd Program.cs verifies that there is no trailing EOL on the last line.
  2. :set fixendofline then :w adds the final EOL which can be verified again with xxd Program.cs, but the analyzer still complains (because vim hasn't changed 'endofline' yet).
  3. :e causes vim to re-read the file and now 'endofline' is true, and the linting error goes away
DasOhmoff commented 2 years ago

Yes, it works! Thank you :)