r191497 - Correctly indent with tabs when whitespace starts from the column not divisible by TabWidth.

Alexander Kornienko alexfh at google.com
Fri Sep 27 02:45:40 PDT 2013


Author: alexfh
Date: Fri Sep 27 04:45:40 2013
New Revision: 191497

URL: http://llvm.org/viewvc/llvm-project?rev=191497&view=rev
Log:
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

Modified:
    cfe/trunk/lib/Format/WhitespaceManager.cpp
    cfe/trunk/lib/Format/WhitespaceManager.h
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/WhitespaceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/WhitespaceManager.cpp?rev=191497&r1=191496&r2=191497&view=diff
==============================================================================
--- cfe/trunk/lib/Format/WhitespaceManager.cpp (original)
+++ cfe/trunk/lib/Format/WhitespaceManager.cpp Fri Sep 27 04:45:40 2013
@@ -225,7 +225,7 @@ void WhitespaceManager::generateChanges(
                           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::appendNewlineTex
   }
 }
 
-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, ' '));
   }

Modified: cfe/trunk/lib/Format/WhitespaceManager.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/WhitespaceManager.h?rev=191497&r1=191496&r2=191497&view=diff
==============================================================================
--- cfe/trunk/lib/Format/WhitespaceManager.h (original)
+++ cfe/trunk/lib/Format/WhitespaceManager.h Fri Sep 27 04:45:40 2013
@@ -157,7 +157,8 @@ private:
   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;

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=191497&r1=191496&r2=191497&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Sep 27 04:45:40 2013
@@ -5760,6 +5760,20 @@ TEST_F(FormatTest, ConfigurableUseOfTab)
   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"
@@ -5843,6 +5857,7 @@ TEST_F(FormatTest, ConfigurableUseOfTab)
                    " \t \t in multiple lines\t\n"
                    " \t  */",
                    Tab));
+
   Tab.UseTab = false;
   EXPECT_EQ("/*\n"
             "              a\t\tcomment\n"





More information about the cfe-commits mailing list