Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

formatting - Visual Studio Code C++ Formatter: indent with tabs, align with spaces?

How would I go about configuring a formatter (in my case, the Visual Studio Code C/C++ formatter) to indent with tabs, but align with spaces?

I've looked at this post and this post, but they were asked more than 5 years ago and I'm wondering if anyone has some new information to share.

I don't mind manually typing out the spaces to align, I just don't want the formatter to convert 4 spaces into a tab.

For reference, I would like my code to be formatted like this, where ---> indicates a single tab and . indicates a single space:

if (condition) {
--->aLongFunction(something).// does something
--->a(b).....................// does something else
}

If I save this code, Visual Studio Code will format it as such:

if (condition) {
--->aLongFunction(something).// does something
--->a(b)--->--->--->--->--->.// does something else
}

This is bad because if someone else wants to look at my code and they have their tab-width set to 2, it would look like this:

if (condition) {
->aLongFunction(something).// does something
->a(b)->->->->->.// does something else
}

And now it is no longer aligned, just strangely spaced out!

I think I value alignment more than I value tabs, so if it comes to it, I will simply switch to use spaces over tabs, but it would be nice if the formatter could see what I'm trying to do!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So I talked to some friends and we came to the conclusion that mixing tabs and spaces could potentially be a pain to work with. They also pointed out to me that inline commenting is not as clear as commenting above the statement, invalidating the examples in my question.

I've also been told that aligning variable assignments can actually be harmful to code readability and especially maintainability.

Thus, I've come to the conclusion that "smart tabs" (that is what I've been told it is called) aren't necessary, so it's probably best to keep using tabs and just not be insistent on aligning.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...