vscode-clang-format icon indicating copy to clipboard operation
vscode-clang-format copied to clipboard

An additional new line is being inserted (windows)

Open BigSeb opened this issue 5 years ago • 4 comments

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 &lt;string>&#13;</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).

Capture

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.

format

BigSeb avatar Apr 19 '19 22:04 BigSeb

Happens to me as well, but does not happen when invoking clang-format manually in my terminal (Windows).

williambulin avatar May 22 '19 22:05 williambulin

This happens to me when my line endings are set to CRLF. Setting them to LF for the entire file resolves the issue.

nbn22385 avatar Oct 31 '19 17:10 nbn22385

There's also a UseCRLF in clang-format, do you have set both on either CRLF or LF?

xaverh avatar Feb 02 '20 12:02 xaverh

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.

StephanTLavavej avatar Jul 27 '20 23:07 StephanTLavavej