1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # `nitpretty` is a tool able to pretty print Nit files.
17 # See `man nitpretty` for more infos.
20 redef class ToolContext
21 # The working directory used to store temp files.
22 var opt_dir
= new OptionString("Working directory (default is '.nitpretty')", "--dir")
24 # Output pretty printed code with this filename.
25 var opt_output
= new OptionString("Output name (default is pretty.nit)", "-o",
28 # Show diff between source and pretty printed code.
29 var opt_diff
= new OptionBool("Show diff between source and output", "--diff")
31 # Show diff between source and pretty printed code using meld.
32 var opt_meld
= new OptionBool("Show diff between source and output using meld",
36 var opt_line_width
= new OptionInt("Maximum length of lines (use 0 to disable automatic line breaks)", 80, "--line-width")
38 # Break too long string literals.
39 var opt_break_str
= new OptionBool("Break too long string literals", "--break-strings")
41 # Force `do` on the same line as the method signature.
42 var opt_inline_do
= new OptionBool("Force do keyword on the same line as the method signature",
45 # Force formatting on empty lines.
47 # By default empty lines are kept as they were typed in the file.
48 # When enabling this option, `nitpretty` will decide where to break lines
49 # and will put empty lines to separate properties and code blocks.
50 var opt_skip_empty
= new OptionBool("Force formatting of empty lines", "--skip-empty")
52 # Check formatting instead of pretty printing.
54 # This option creates a temporary pretty printed file then checks if the
55 # output of the diff command on the source file and the pretty printed one is
57 var opt_check
= new OptionBool("Check format of Nit source files", "--check")
60 # Return result from diff between `file1` and `file2`.
61 private fun diff
(file1
, file2
: String): String do
62 var p
= new ProcessReader("diff", "-u", file1
, file2
)
70 var toolcontext
= new ToolContext
72 var opts
= toolcontext
.option_context
73 opts
.add_option
(toolcontext
.opt_dir
, toolcontext
.opt_output
)
74 opts
.add_option
(toolcontext
.opt_diff
, toolcontext
.opt_meld
, toolcontext
.opt_check
)
75 opts
.add_option
(toolcontext
.opt_line_width
, toolcontext
.opt_break_str
, toolcontext
.opt_inline_do
)
76 opts
.add_option
(toolcontext
.opt_skip_empty
)
78 toolcontext
.tooldescription
= "Usage: nitpretty [OPTION]... <file.nit>\n" +
79 "Pretty print Nit code from Nit source files."
81 toolcontext
.process_options args
82 var arguments
= toolcontext
.option_context
.rest
85 var mbuilder
= new ModelBuilder(model
, toolcontext
)
86 var mmodules
= mbuilder
.parse
(arguments
)
89 if mmodules
.is_empty
then
90 print
"Error: no module to pretty print"
94 if not toolcontext
.opt_check
.value
and mmodules
.length
> 1 then
95 print
"Error: only --check option allow multiple modules"
99 var dir
= toolcontext
.opt_dir
.value
or else ".nitpretty"
100 if not dir
.file_exists
then dir
.mkdir
101 var v
= new PrettyPrinterVisitor
103 v
.max_size
= toolcontext
.opt_line_width
.value
104 if toolcontext
.opt_break_str
.value
then
105 v
.break_strings
= true
107 if toolcontext
.opt_inline_do
.value
then
110 if toolcontext
.opt_skip_empty
.value
then
114 for mmodule
in mmodules
do
115 var nmodule
= mbuilder
.mmodule2node
(mmodule
)
116 if nmodule
== null then
117 print
" Error: no source file for module {mmodule}"
120 var file
= "{dir}/{mmodule.name}.nit"
121 var tpl
= v
.pretty_nmodule
(nmodule
)
122 tpl
.write_to_file file
124 if toolcontext
.opt_check
.value
then
125 var res
= diff
(nmodule
.location
.file
.filename
, file
)
127 if not res
.is_empty
then
128 print
"Wrong formating for module {nmodule.location.file.filename}"
129 toolcontext
.info
(res
, 1)
131 if toolcontext
.opt_meld
.value
then
132 sys
.system
"meld {nmodule.location.file.filename} {file}"
135 toolcontext
.info
("[OK] {nmodule.location.file.filename}", 1)
139 var out
= toolcontext
.opt_output
.value
140 if out
!= null then sys
.system
"cp {file} {out}"
143 if toolcontext
.opt_meld
.value
then
144 sys
.system
"meld {arguments.first} {file}"
149 if toolcontext
.opt_diff
.value
then
150 var res
= diff
(arguments
.first
, file
)
151 if not res
.is_empty
then print res
156 if not toolcontext
.opt_quiet
.value
then tpl
.write_to sys
.stdout