Merge: Nitpretty: make the tool more customizable
authorJean Privat <jean@pryen.org>
Wed, 14 Jan 2015 01:16:48 +0000 (20:16 -0500)
committerJean Privat <jean@pryen.org>
Wed, 14 Jan 2015 01:16:48 +0000 (20:16 -0500)
Added some options to control behavior.

Interesting features:

* option --break-strings to enable literal string breaks
* option --inline-do to enable do inlining
* option --skip-empty to enable empty line skipping

Pull-Request: #1088
Reviewed-by: Jean Privat <jean@pryen.org>
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>

1  2 
src/nitpretty.nit

diff --combined src/nitpretty.nit
@@@ -32,6 -32,20 +32,20 @@@ redef class ToolContex
        var opt_meld = new OptionBool("Show diff between source and output using meld",
           "--meld")
  
+       # Break too long string literals.
+       var opt_break_str = new OptionBool("Break too long string literals", "--break-strings")
+       # Force `do` on the same line as the method signature.
+       var opt_inline_do = new OptionBool("Force do keyword on the same line as the method signature",
+               "--inline-do")
+       # Force formatting on empty lines.
+       #
+       # By default empty lines are kept as they were typed in the file.
+       # When enabling this option, `nitpretty` will decide where to break lines
+       # and will put empty lines to separate properties and code blocks.
+       var opt_skip_empty = new OptionBool("Force formatting of empty lines", "--skip-empty")
        # Check formatting instead of pretty printing.
        #
        # This option create a tempory pretty printed file then check if
@@@ -52,9 -66,11 +66,11 @@@ en
  # process options
  var toolcontext = new ToolContext
  
- toolcontext.option_context.
-    add_option(toolcontext.opt_dir, toolcontext.opt_output, toolcontext.opt_diff,
-    toolcontext.opt_meld, toolcontext.opt_check)
+ 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_skip_empty)
  
  toolcontext.tooldescription = "Usage: nitpretty [OPTION]... <file.nit>\n" +
        "Pretty print Nit code from Nit source files."
@@@ -81,12 -97,23 +97,22 @@@ var dir = toolcontext.opt_dir.value or 
  if not dir.file_exists then dir.mkdir
  var v = new PrettyPrinterVisitor
  
+ if toolcontext.opt_break_str.value then
+       v.break_strings = true
+ end
+ if toolcontext.opt_inline_do.value then
+       v.inline_do = true
+ end
+ if toolcontext.opt_skip_empty.value then
+       v.skip_empty = true
+ end
  for mmodule in mmodules do
 -      if not mbuilder.mmodule2nmodule.has_key(mmodule) then
 +      var nmodule = mbuilder.mmodule2node(mmodule)
 +      if nmodule == null then
                print " Error: no source file for module {mmodule}"
                return
        end
 -
 -      var nmodule = mbuilder.mmodule2nmodule[mmodule]
        var file = "{dir}/{mmodule.name}.nit"
        var tpl = v.pretty_nmodule(nmodule)
        tpl.write_to_file file