[PATCH] Correctly indent with tabs when whitespace starts from the column not divisible by TabWidth.

Alexander Kornienko alexfh at google.com
Thu Sep 26 05:46:24 PDT 2013


Hi klimek, djasper,

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.

http://llvm-reviews.chandlerc.com/D1763

Files:
  lib/Format/WhitespaceManager.cpp
  lib/Format/WhitespaceManager.h
  unittests/Format/FormatTest.cpp

Index: lib/Format/WhitespaceManager.cpp
===================================================================
--- lib/Format/WhitespaceManager.cpp
+++ lib/Format/WhitespaceManager.cpp
@@ -225,7 +225,7 @@
                           C.PreviousEndOfTokenColumn, C.EscapedNewlineColumn);
       else
         appendNewlineText(ReplacementText, C.NewlinesBefore);
-      appendIndentText(ReplacementText, C.Spaces);
+      appendIndentText(ReplacementText, C.Spaces, C.StartOfTokenColumn - C.Spaces);
       ReplacementText.append(C.CurrentLinePrefix);
       storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
     }
@@ -264,10 +264,18 @@
   }
 }
 
-void WhitespaceManager::appendIndentText(std::string &Text, unsigned Spaces) {
+void WhitespaceManager::appendIndentText(std::string &Text, unsigned Spaces,
+                                         unsigned WhitespaceStartColumn) {
   if (!Style.UseTab) {
     Text.append(std::string(Spaces, ' '));
   } else {
+    unsigned FirstTabWidth =
+        Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
+    // Indent with tabs only when there's at least one full tab.
+    if (FirstTabWidth + Style.TabWidth <= Spaces) {
+      Spaces -= FirstTabWidth;
+      Text.append("\t");
+    }
     Text.append(std::string(Spaces / Style.TabWidth, '\t'));
     Text.append(std::string(Spaces % Style.TabWidth, ' '));
   }
Index: lib/Format/WhitespaceManager.h
===================================================================
--- lib/Format/WhitespaceManager.h
+++ lib/Format/WhitespaceManager.h
@@ -157,7 +157,8 @@
   void appendNewlineText(std::string &Text, unsigned Newlines,
                          unsigned PreviousEndOfTokenColumn,
                          unsigned EscapedNewlineColumn);
-  void appendIndentText(std::string &Text, unsigned Spaces);
+  void appendIndentText(std::string &Text, unsigned Spaces,
+                        unsigned WhitespaceStartColumn);
 
   SmallVector<Change, 16> Changes;
   SourceManager &SourceMgr;
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -5753,6 +5753,20 @@
   Tab.IndentWidth = 8;
   Tab.UseTab = true;
   Tab.AlignEscapedNewlinesLeft = true;
+
+  EXPECT_EQ("if (aaaaaaaa && // q\n"
+            "    bb)\t\t// w\n"
+            "\t;",
+            format("if (aaaaaaaa &&// q\n"
+                   "bb)// w\n"
+                   ";",
+                   Tab));
+  EXPECT_EQ("if (aaa && bbb) // w\n"
+            "\t;",
+            format("if(aaa&&bbb)// w\n"
+                   ";",
+                   Tab));
+
   verifyFormat("class X {\n"
                "\tvoid f() {\n"
                "\t\tsomeFunction(parameter1,\n"
@@ -5836,6 +5850,7 @@
                    " \t \t in multiple lines\t\n"
                    " \t  */",
                    Tab));
+
   Tab.UseTab = false;
   EXPECT_EQ("/*\n"
             "              a\t\tcomment\n"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1763.1.patch
Type: text/x-patch
Size: 2997 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130926/5246f34d/attachment.bin>


More information about the cfe-commits mailing list