nitpretty: does not force `do` inlining on APropdefs 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 # Check formatting instead of pretty printing.
43 #
44 # This option create a tempory pretty printed file then check if
45 # the output of the diff command on the source file and the pretty
46 # printed one is empty.
47 var opt_check = new OptionBool("Check format of Nit source files", "--check")
48 end
49
50 # Return result from diff between `file1` and `file2`.
51 private fun diff(file1, file2: String): String do
52 var p = new IProcess("diff", "-u", file1, file2)
53 var res = p.read_all
54 p.wait
55 p.close
56 return res
57 end
58
59 # process options
60 var toolcontext = new ToolContext
61
62 var opts = toolcontext.option_context
63 opts.add_option(toolcontext.opt_dir, toolcontext.opt_output)
64 opts.add_option(toolcontext.opt_diff, toolcontext.opt_meld, toolcontext.opt_check)
65 opts.add_option(toolcontext.opt_break_str, toolcontext.opt_inline_do)
66
67 toolcontext.tooldescription = "Usage: nitpretty [OPTION]... <file.nit>\n" +
68 "Pretty print Nit code from Nit source files."
69
70 toolcontext.process_options args
71 var arguments = toolcontext.option_context.rest
72 # build model
73 var model = new Model
74 var mbuilder = new ModelBuilder(model, toolcontext)
75 var mmodules = mbuilder.parse(arguments)
76 mbuilder.run_phases
77
78 if mmodules.is_empty then
79 print "Error: no module to pretty print"
80 return
81 end
82
83 if not toolcontext.opt_check.value and mmodules.length > 1 then
84 print "Error: only --check option allow multiple modules"
85 return
86 end
87
88 var dir = toolcontext.opt_dir.value or else ".nitpretty"
89 if not dir.file_exists then dir.mkdir
90 var v = new PrettyPrinterVisitor
91
92 if toolcontext.opt_break_str.value then
93 v.break_strings = true
94 end
95 if toolcontext.opt_inline_do.value then
96 v.inline_do = true
97 end
98
99 for mmodule in mmodules do
100 if not mbuilder.mmodule2nmodule.has_key(mmodule) then
101 print " Error: no source file for module {mmodule}"
102 return
103 end
104
105 var nmodule = mbuilder.mmodule2nmodule[mmodule]
106 var file = "{dir}/{mmodule.name}.nit"
107 var tpl = v.pretty_nmodule(nmodule)
108 tpl.write_to_file file
109
110 if toolcontext.opt_check.value then
111 var res = diff(nmodule.location.file.filename, file)
112
113 if not res.is_empty then
114 print "Wrong formating for module {nmodule.location.file.filename}"
115 toolcontext.info(res, 1)
116
117 if toolcontext.opt_meld.value then
118 sys.system "meld {nmodule.location.file.filename} {file}"
119 end
120 else
121 toolcontext.info("[OK] {nmodule.location.file.filename}", 1)
122 end
123 else
124 # write to file
125 var out = toolcontext.opt_output.value
126 if out != null then sys.system "cp {file} {out}"
127
128 # open in meld
129 if toolcontext.opt_meld.value then
130 sys.system "meld {arguments.first} {file}"
131 return
132 end
133
134 # show diff
135 if toolcontext.opt_diff.value then
136 var res = diff(arguments.first, file)
137 if not res.is_empty then print res
138 return
139 end
140
141 # show pretty
142 if not toolcontext.opt_quiet.value then tpl.write_to sys.stdout
143 end
144 end