Fix layout width in ninja_syntax.py.
authorNico Weber <nicolasweber@gmx.de>
Wed, 2 May 2012 01:26:51 +0000 (18:26 -0700)
committerNico Weber <nicolasweber@gmx.de>
Wed, 2 May 2012 01:26:51 +0000 (18:26 -0700)
The last line would sometimes be needlessly longer than the layout
width. One example is line 67 in the build.ninja generated by
ninja's own configure.py: Before this patch, ninja_syntax would
create a 81 character line.

misc/ninja_syntax.py
misc/ninja_test.py

index 97bd82b278009df5ce13fbb2680c6061e104334b..3ecbcee25d4a3d6980cf1fe83f98f8e7a9e59e90 100644 (file)
@@ -101,7 +101,7 @@ class Writer(object):
     def _line(self, text, indent=0):
         """Write 'text' word-wrapped at self.width characters."""
         leading_space = '  ' * indent
-        while len(text) > self.width:
+        while len(leading_space) + len(text) > self.width:
             # The text is too wide; wrap if possible.
 
             # Find the rightmost space that would obey our width constraint and
index 897de72ad1792bce10650ebf393029d870ba08a1..b56033e3c8a14853c58cb365bf5a6fba43c8bbb4 100755 (executable)
@@ -41,6 +41,18 @@ class TestLineWordWrap(unittest.TestCase):
                                       INDENT + 'y']) + '\n',
                          self.out.getvalue())
 
+    def test_short_words_indented(self):
+        # Test that indent is taking into acount when breaking subsequent lines.
+        # The second line should not be '    to tree', as that's longer than the
+        # test layout width of 8.
+        self.n._line('line_one to tree')
+        self.assertEqual('''\
+line_one $
+    to $
+    tree
+''',
+                         self.out.getvalue())
+
     def test_few_long_words_indented(self):
         # Check wrapping in the presence of indenting.
         self.n._line(' '.join(['x', LONGWORD, 'y']), indent=1)