nitpretty: new option --line_width
authorJean Privat <jean@pryen.org>
Fri, 24 Apr 2015 13:54:17 +0000 (20:54 +0700)
committerJean Privat <jean@pryen.org>
Mon, 4 May 2015 20:08:12 +0000 (16:08 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/nitpretty.nit
src/pretty.nit

index 9667f81..71313c8 100644 (file)
@@ -32,6 +32,9 @@ redef class ToolContext
        var opt_meld = new OptionBool("Show diff between source and output using meld",
           "--meld")
 
+       # --line-width
+       var opt_line_width = new OptionInt("Maximum length of lines (use 0 to disable automatic line breaks)", 80, "--line-width")
+
        # Break too long string literals.
        var opt_break_str = new OptionBool("Break too long string literals", "--break-strings")
 
@@ -69,7 +72,7 @@ var toolcontext = new ToolContext
 var opts = toolcontext.option_context
 opts.add_option(toolcontext.opt_dir, toolcontext.opt_output)
 opts.add_option(toolcontext.opt_diff, toolcontext.opt_meld, toolcontext.opt_check)
-opts.add_option(toolcontext.opt_break_str, toolcontext.opt_inline_do)
+opts.add_option(toolcontext.opt_line_width, toolcontext.opt_break_str, toolcontext.opt_inline_do)
 opts.add_option(toolcontext.opt_skip_empty)
 
 toolcontext.tooldescription = "Usage: nitpretty [OPTION]... <file.nit>\n" +
@@ -97,6 +100,7 @@ var dir = toolcontext.opt_dir.value or else ".nitpretty"
 if not dir.file_exists then dir.mkdir
 var v = new PrettyPrinterVisitor
 
+v.max_size = toolcontext.opt_line_width.value
 if toolcontext.opt_break_str.value then
        v.break_strings = true
 end
index 20c3550..f037479 100644 (file)
@@ -124,7 +124,7 @@ class PrettyPrinterVisitor
                if n.must_be_inline then return true
                if n.must_be_block then return false
                # check length
-               if n.collect_length + current_length > max_size then return false
+               if max_size > 0 and n.collect_length + current_length > max_size then return false
                # check block is inlinable
                return n.is_inlinable
        end
@@ -219,7 +219,8 @@ class PrettyPrinterVisitor
        var tab_size = 8
 
        # Max line size.
-       var max_size = 80
+       # 0 (or negative) to disable.
+       var max_size = 80 is writable
 
        # Length of the current line.
        var current_length = 0
@@ -2118,7 +2119,7 @@ redef class AStringFormExpr
                        while i < text.length do
                                v.add text[i].to_s
 
-                               if v.current_length >= v.max_size and i <= text.length - 3 then
+                               if v.max_size > 0 and v.current_length >= v.max_size and i <= text.length - 3 then
                                        v.add "\" +"
                                        if was_inline then
                                                v.forcen