nitpretty: does not skip empty lines anymore
[nit.git] / src / nitpretty.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # `nitpretty` is a tool able to pretty print Nit files.
16 #
17 # See `man nitpretty` for more infos.
18 import pretty
19
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")
23
24 # Output pretty printed code with this filename.
25 var opt_output = new OptionString("Output name (default is pretty.nit)", "-o",
26 "--output")
27
28 # Show diff between source and pretty printed code.
29 var opt_diff = new OptionBool("Show diff between source and output", "--diff")
30
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",
33 "--meld")
34
35 # Break too long string literals.
36 var opt_break_str = new OptionBool("Break too long string literals", "--break-strings")
37
38 # Force `do` on the same line as the method signature.
39 var opt_inline_do = new OptionBool("Force do keyword on the same line as the method signature",
40 "--inline-do")
41
42 # Force formatting on empty lines.
43 #
44 # By default empty lines are kept as they were typed in the file.
45 # When enabling this option, `nitpretty` will decide where to break lines
46 # and will put empty lines to separate properties and code blocks.
47 var opt_skip_empty = new OptionBool("Force formatting of empty lines", "--skip-empty")
48
49 # Check formatting instead of pretty printing.
50 #
51 # This option create a tempory pretty printed file then check if
52 # the output of the diff command on the source file and the pretty
53 # printed one is empty.
54 var opt_check = new OptionBool("Check format of Nit source files", "--check")
55 end
56
57 # Return result from diff between `file1` and `file2`.
58 private fun diff(file1, file2: String): String do
59 var p = new IProcess("diff", "-u", file1, file2)
60 var res = p.read_all
61 p.wait
62 p.close
63 return res
64 end
65
66 # process options
67 var toolcontext = new ToolContext
68
69 var opts = toolcontext.option_context
70 opts.add_option(toolcontext.opt_dir, toolcontext.opt_output)
71 opts.add_option(toolcontext.opt_diff, toolcontext.opt_meld, toolcontext.opt_check)
72 opts.add_option(toolcontext.opt_break_str, toolcontext.opt_inline_do)
73 opts.add_option(toolcontext.opt_skip_empty)
74
75 toolcontext.tooldescription = "Usage: nitpretty [OPTION]... <file.nit>\n" +
76 "Pretty print Nit code from Nit source files."
77
78 toolcontext.process_options args
79 var arguments = toolcontext.option_context.rest
80 # build model
81 var model = new Model
82 var mbuilder = new ModelBuilder(model, toolcontext)
83 var mmodules = mbuilder.parse(arguments)
84 mbuilder.run_phases
85
86 if mmodules.is_empty then
87 print "Error: no module to pretty print"
88 return
89 end
90
91 if not toolcontext.opt_check.value and mmodules.length > 1 then
92 print "Error: only --check option allow multiple modules"
93 return
94 end
95
96 var dir = toolcontext.opt_dir.value or else ".nitpretty"
97 if not dir.file_exists then dir.mkdir
98 var v = new PrettyPrinterVisitor
99
100 if toolcontext.opt_break_str.value then
101 v.break_strings = true
102 end
103 if toolcontext.opt_inline_do.value then
104 v.inline_do = true
105 end
106 if toolcontext.opt_skip_empty.value then
107 v.skip_empty = true
108 end
109
110 for mmodule in mmodules do
111 if not mbuilder.mmodule2nmodule.has_key(mmodule) then
112 print " Error: no source file for module {mmodule}"
113 return
114 end
115
116 var nmodule = mbuilder.mmodule2nmodule[mmodule]
117 var file = "{dir}/{mmodule.name}.nit"
118 var tpl = v.pretty_nmodule(nmodule)
119 tpl.write_to_file file
120
121 if toolcontext.opt_check.value then
122 var res = diff(nmodule.location.file.filename, file)
123
124 if not res.is_empty then
125 print "Wrong formating for module {nmodule.location.file.filename}"
126 toolcontext.info(res, 1)
127
128 if toolcontext.opt_meld.value then
129 sys.system "meld {nmodule.location.file.filename} {file}"
130 end
131 else
132 toolcontext.info("[OK] {nmodule.location.file.filename}", 1)
133 end
134 else
135 # write to file
136 var out = toolcontext.opt_output.value
137 if out != null then sys.system "cp {file} {out}"
138
139 # open in meld
140 if toolcontext.opt_meld.value then
141 sys.system "meld {arguments.first} {file}"
142 return
143 end
144
145 # show diff
146 if toolcontext.opt_diff.value then
147 var res = diff(arguments.first, file)
148 if not res.is_empty then print res
149 return
150 end
151
152 # show pretty
153 if not toolcontext.opt_quiet.value then tpl.write_to sys.stdout
154 end
155 end