Page MenuHomePhabricator

Switching from spaces to tabs
Closed, ResolvedPublic

Asked by ardunster on Dec 13 2020, 1:57 PM.

Details

In reference to this comment by @jcmcdonald on D354: Transformation Refactor,

Switch from spaces to tabs across all your files, please. Visual Studio Code can do this for you, and then clangformat can help with the rest of the formatting. Talk to @ardunster on how to do that.

How do we set up VSCode successfully to use tabs automatically in C++ files, and get clang-format working properly?

Answers

ardunster
Updated 611 Days Ago

I had a couple hurdles I encountered when getting this to work. It was actually super frustrating to get working properly and consistently, because I thought I was changing everywhere that the setting existed, and VSCode persisted in using spaces instead of tabs. (There may or may not have been yelling at the computer involved....) And, even after I installed clang-format ( sudo apt-get install clang-format) and pointed VSCode at the correct location for the executable, I kept getting errors trying to run the formatter, which was also quite frustrating.

So here are the things I finally found to fix it, in case someone else is having similar trouble.

First, regarding tabs vs spaces, there are a lot more places that VSCode looks for formatting information than are immediately obvious. The most obvious two places are in the user settings. The relevant bits in the .json are:

	"editor.insertSpaces": false,
	"editor.detectIndentation": false,

although you can access it via the Settings UI also. There are also some options via the status bar, which will say something like "Tab Size: 4" in one of the fields in the bottom right, depending on the settings for that individual file, but that only changes settings for that file during that session. Better than nothing, but still frustrating to mess with each and every time.

Finally I discovered there are additional places in the settings.json file included in the SIMPLEXpress repository that were throwing me off. In SIMPLEXpress, they start at line 14; other projects may have them elsewhere in the settings file, but there are language specific settings:

"[c]": {
    "editor.detectIndentation": false,
    "editor.insertSpaces": false,
    "editor.tabSize": 4,
},

"[cpp]": {
    "editor.detectIndentation": false,
    "editor.insertSpaces": false,
    "editor.tabSize": 4,
},

I switched the editor.insertSpaces option in both cases from true to false and no longer have any issues with VSCode deciding to use spaces in any file in SIMPLEXpress. (Each project's settings file will need to be adjusted individually in this case)

This doesn't change the existing tabs and spaces in the document however, for that you need format. As I mentioned above, running the format option wasn't working for me even after installing clang-format, and I couldn't figure out why, because the .clang-format file looked right. Eventually I realized that my error resulted from a copy-paste issue when I had originally created the file, and instead of being properly not indented at all, all the lines were indented with a form of indentation that clang-format doesn't parse. (I don't now remember if it was spaces or tabs, but it should have been neither, and it had one of the two) Recopying the raw text data from P56 (instead of from the web formatted version I originally grabbed) got it working for me together with the install, and now I can format either an entire file or a selected piece of code with a right click or a keyboard shortcut, which helps keep everything tidy. Except where it introduces weird formatting that makes things confusing, of course ;)

New Answer

Answer

This question has been marked as closed, but you can still leave a new answer.