Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a single empty line as the last line in the code - clang-format

I am using clang-format extension in vscode to format my c++ code. I was looking for a configuration that could add a single empty line as the last line in the code. But found none.

If I have a case:

#include <iostream>
using namespace std;

int main() {
  string s = "asfasdf";
  cout << "Type IIanything: ";
  // cin >> s;
  for (int i = s.length() - 1; i > -1; i--) {
    cout << s[i];
  }
  cout << endl;
  cout << "NOT using build 2" << endl;
}
---empty line1---
---empty line2---
---empty line3---

Then when i hit save, the empty line 2 and 3 rows are gone. And only 1 is left. This is fine for me. But if none of 1, 2 and 3 were there, and i hit save, the closing brace is the last line in the code. What can i do to add an empty line after the closing brace in such a situation.

like image 643
Ankit Koirala Avatar asked Mar 27 '19 09:03

Ankit Koirala


1 Answers

Adding the following two entries to your settings.json should fix it:

{
    "files.insertFinalNewline": true,
    "files.trimFinalNewlines": true
}

This will cause VS Code to either insert a final newline if none is present, or trim multiple final newlines so only one is present.

like image 106
HaaLeo Avatar answered Oct 05 '22 11:10

HaaLeo