vscode-clang-format
vscode-clang-format copied to clipboard
An additional new line is being inserted (windows)
A new line is being inserted when by the plugin involontary by using the following clang-format file:
IncludeBlocks: Regroup
SortIncludes: 'true'
with this simple c++ code.
#include <string>
int main() {}
I reproduced this issue with clang-format 7.0.1 and 8.0. The xml being generated by clang-format is the following:
<?xml version='1.0'?>
<replacements xml:space='preserve' incomplete_format='false'>
<replacement offset='0' length='18'>#include <string> </replacement>
</replacements>
Clang-format is trying to replace the first 18 characters. The last character removed is the carriage returns but the replacement string also ends with the carriage returns. This is technically the same result from the input. By using the vscode-clang-format plugin though a new line is inserted after this section.
I have traced the issue up to where it seems problematic (extension.ts).
The vscode.TextDocument.positionAt()
function does not seems to allow selection of character between the carriage return and line feed (CR / LF). It then position the entity before the carriage return. The new string only contains a carriage returns but vscode also insert a line feed after, which creates two consecutive combo of CR/LF instead of one. Of course this issue only occurs on Windows.
Funny thing is that when formatting this section it will generate two blank lines after the include block, and when formatting back that section again it will be back to one blank line.
Happens to me as well, but does not happen when invoking clang-format manually in my terminal (Windows).
This happens to me when my line endings are set to CRLF. Setting them to LF for the entire file resolves the issue.
There's also a UseCRLF
in clang-format, do you have set both on either CRLF or LF?
I previously experienced this issue in a CRLF codebase (only with this extension, not MS's C/C++ extension), but now that we've upgraded to clang-format 10 and added UseCRLF: true
, it no longer happens.