Merge: doc: fixed some typos and other misc. corrections
[nit.git] / src / nitpretty.nit
index af3fae0..17cc649 100644 (file)
@@ -32,17 +32,37 @@ 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")
+
+       # --no-inline
+       var opt_no_inline = new OptionBool("Disable automatic one-liners", "--no-inline")
+
+       # 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
-       # the output of the diff command on the source file and the pretty
-       # printed one is empty.
+       # This option creates a temporary pretty printed file then checks if the
+       # output of the diff command on the source file and the pretty printed one is
+       # empty.
        var opt_check = new OptionBool("Check format of Nit source files", "--check")
 end
 
 # Return result from diff between `file1` and `file2`.
 private fun diff(file1, file2: String): String do
-       var p = new IProcess("diff", "-u", file1, file2)
+       var p = new ProcessReader("diff", "-u", file1, file2)
        var res = p.read_all
        p.wait
        p.close
@@ -52,9 +72,12 @@ end
 # 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_line_width, toolcontext.opt_break_str, toolcontext.opt_inline_do)
+opts.add_option(toolcontext.opt_no_inline)
+opts.add_option(toolcontext.opt_skip_empty)
 
 toolcontext.tooldescription = "Usage: nitpretty [OPTION]... <file.nit>\n" +
        "Pretty print Nit code from Nit source files."
@@ -64,7 +87,7 @@ var arguments = toolcontext.option_context.rest
 # build model
 var model = new Model
 var mbuilder = new ModelBuilder(model, toolcontext)
-var mmodules = mbuilder.parse(arguments)
+var mmodules = mbuilder.parse_full(arguments)
 mbuilder.run_phases
 
 if mmodules.is_empty then
@@ -81,6 +104,20 @@ 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
+if toolcontext.opt_inline_do.value then
+       v.inline_do = true
+end
+if toolcontext.opt_skip_empty.value then
+       v.skip_empty = true
+end
+if toolcontext.opt_no_inline.value then
+       v.no_inline = true
+end
+
 for mmodule in mmodules do
        var nmodule = mbuilder.mmodule2node(mmodule)
        if nmodule == null then