Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit acf8e90

Browse files
committed
Correctly indent with tabs when whitespace starts from the column not divisible by TabWidth.
Summary: The width of the first inserted tab character depends on the initial column, so we need to handle the first tab in a special manner. Reviewers: klimek, djasper Reviewed By: klimek CC: cfe-commits Differential Revision: http://llvm-reviews.chandlerc.com/D1763 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@191497 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 6b3ff8c commit acf8e90

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

lib/Format/WhitespaceManager.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ void WhitespaceManager::generateChanges() {
225225
C.PreviousEndOfTokenColumn, C.EscapedNewlineColumn);
226226
else
227227
appendNewlineText(ReplacementText, C.NewlinesBefore);
228-
appendIndentText(ReplacementText, C.Spaces);
228+
appendIndentText(ReplacementText, C.Spaces, C.StartOfTokenColumn - C.Spaces);
229229
ReplacementText.append(C.CurrentLinePrefix);
230230
storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
231231
}
@@ -264,10 +264,18 @@ void WhitespaceManager::appendNewlineText(std::string &Text, unsigned Newlines,
264264
}
265265
}
266266

267-
void WhitespaceManager::appendIndentText(std::string &Text, unsigned Spaces) {
267+
void WhitespaceManager::appendIndentText(std::string &Text, unsigned Spaces,
268+
unsigned WhitespaceStartColumn) {
268269
if (!Style.UseTab) {
269270
Text.append(std::string(Spaces, ' '));
270271
} else {
272+
unsigned FirstTabWidth =
273+
Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
274+
// Indent with tabs only when there's at least one full tab.
275+
if (FirstTabWidth + Style.TabWidth <= Spaces) {
276+
Spaces -= FirstTabWidth;
277+
Text.append("\t");
278+
}
271279
Text.append(std::string(Spaces / Style.TabWidth, '\t'));
272280
Text.append(std::string(Spaces % Style.TabWidth, ' '));
273281
}

lib/Format/WhitespaceManager.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ class WhitespaceManager {
157157
void appendNewlineText(std::string &Text, unsigned Newlines,
158158
unsigned PreviousEndOfTokenColumn,
159159
unsigned EscapedNewlineColumn);
160-
void appendIndentText(std::string &Text, unsigned Spaces);
160+
void appendIndentText(std::string &Text, unsigned Spaces,
161+
unsigned WhitespaceStartColumn);
161162

162163
SmallVector<Change, 16> Changes;
163164
SourceManager &SourceMgr;

unittests/Format/FormatTest.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -5760,6 +5760,20 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
57605760
Tab.IndentWidth = 8;
57615761
Tab.UseTab = true;
57625762
Tab.AlignEscapedNewlinesLeft = true;
5763+
5764+
EXPECT_EQ("if (aaaaaaaa && // q\n"
5765+
" bb)\t\t// w\n"
5766+
"\t;",
5767+
format("if (aaaaaaaa &&// q\n"
5768+
"bb)// w\n"
5769+
";",
5770+
Tab));
5771+
EXPECT_EQ("if (aaa && bbb) // w\n"
5772+
"\t;",
5773+
format("if(aaa&&bbb)// w\n"
5774+
";",
5775+
Tab));
5776+
57635777
verifyFormat("class X {\n"
57645778
"\tvoid f() {\n"
57655779
"\t\tsomeFunction(parameter1,\n"
@@ -5843,6 +5857,7 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
58435857
" \t \t in multiple lines\t\n"
58445858
" \t */",
58455859
Tab));
5860+
58465861
Tab.UseTab = false;
58475862
EXPECT_EQ("/*\n"
58485863
" a\t\tcomment\n"

0 commit comments

Comments
 (0)