X-Git-Url: http://nitlanguage.org diff --git a/src/nitx.nit b/src/nitx.nit index 84c0815..218f47b 100644 --- a/src/nitx.nit +++ b/src/nitx.nit @@ -16,8 +16,7 @@ module nitx import model_utils -import modelize_property -import frontend +import modelize # Main class of the nit index tool # NitIndex build the model using the toolcontext argument @@ -46,21 +45,16 @@ class NitIndex init(toolcontext: ToolContext) do # We need a model to collect stufs self.toolcontext = toolcontext - self.toolcontext.option_context.options.clear self.arguments = toolcontext.option_context.rest - if arguments.is_empty or arguments.length > 2 then - print "usage: ni path/to/module.nit [expression]" - toolcontext.option_context.usage + if arguments.length > 2 then + print toolcontext.tooldescription exit(1) end model = new Model mbuilder = new ModelBuilder(model, toolcontext) - # Here we load an process std modules - #var dir = "NIT_DIR".environ - #var mmodules = modelbuilder.parse_and_build(["{dir}/lib/standard/standard.nit"]) var mmodules = mbuilder.parse([arguments.first]) if mmodules.is_empty then return mbuilder.run_phases @@ -85,7 +79,7 @@ class NitIndex print "Loaded modules:" var mmodules = new Array[MModule] mmodules.add_all(model.mmodules) - var sorter = new MModuleNameSorter + var sorter = new MEntityNameSorter sorter.sort(mmodules) for m in mmodules do print "\t{m.name}" @@ -107,7 +101,7 @@ class NitIndex fun prompt do printn ">> " - search(stdin.read_line) + search(sys.stdin.read_line) end fun search(entry: String) do @@ -159,7 +153,7 @@ class NitIndex else var category = parts[0] var keyword = parts[1] - if keyword.first == ' ' then keyword = keyword.substring_from(1) + if keyword.chars.first == ' ' then keyword = keyword.substring_from(1) return new IndexQueryPair(str, keyword, category) end end @@ -265,7 +259,7 @@ end # Code Analysis redef class ToolContext - private var nitx_phase: NitxPhase = new NitxPhase(self, [typing_phase]) + private var nitx_phase: NitxPhase = new NitxPhase(self, [modelize_property_phase]) end # Compiler phase for nitx @@ -336,7 +330,7 @@ private class PagerMatchesRenderer cats[key].add(mprop) end #sort groups - var sorter = new MModuleNameSorter + var sorter = new MEntityNameSorter var sorted = new Array[MModule] sorted.add_all(cats.keys) sorter.sort(sorted) @@ -344,8 +338,7 @@ private class PagerMatchesRenderer for mmodule in sorted do var mprops = cats[mmodule] pager.add("# matches in module {mmodule.namespace.bold}") - var sorterp = new MPropertyNameSorter - sorterp.sort(mprops) + sorter.sort(mprops) for mprop in mprops do end @@ -355,7 +348,7 @@ private class PagerMatchesRenderer end private class Pager - var content = new Buffer + var content = new FlatBuffer var indent = 0 fun add(text: String) do add_indent @@ -384,27 +377,23 @@ redef class MModule end redef fun preview(index, pager) do - if index.mbuilder.mmodule2nmodule.has_key(self) then - var node = index.mbuilder.mmodule2nmodule[self] - if node.n_moduledecl != null and not node.n_moduledecl.n_doc == null and not node.n_moduledecl.n_doc.short_comment.is_empty then - pager.add(node.n_moduledecl.n_doc.short_comment.green) - end + var mdoc = self.mdoc + if mdoc != null then + pager.add(mdoc.short_comment.green) end pager.add(prototype) pager.add("{namespace}".bold.gray + " (lines {location.lines})".gray) end redef fun content(index, pager) do - if index.mbuilder.mmodule2nmodule.has_key(self) then - var node = index.mbuilder.mmodule2nmodule[self] - if node.n_moduledecl != null and not node.n_moduledecl.n_doc == null and not node.n_moduledecl.n_doc.comment.is_empty then - for comment in node.n_moduledecl.n_doc.comment do pager.add(comment.green) - end + var mdoc = self.mdoc + if mdoc != null then + for comment in mdoc.content do pager.add(comment.green) end pager.add(prototype) pager.add("{namespace}".bold.gray + " (lines {location.lines})".gray) pager.indent = pager.indent + 1 - var sorter = new MModuleNameSorter + var sorter = new MEntityNameSorter # imported modules var imports = new Array[MModule] for mmodule in in_importation.direct_greaters.to_a do @@ -422,7 +411,6 @@ redef class MModule pager.indent = pager.indent - 1 end # mclassdefs - var csorter = new MClassDefNameSorter var intros = new Array[MClassDef] var redefs = new Array[MClassDef] for mclassdef in mclassdefs do @@ -434,7 +422,7 @@ redef class MModule end # introductions if not intros.is_empty then - csorter.sort(intros) + sorter.sort(intros) pager.add("") pager.add("== introduced classes".bold) pager.indent = pager.indent + 1 @@ -446,7 +434,7 @@ redef class MModule end # refinements if not redefs.is_empty then - csorter.sort(redefs) + sorter.sort(redefs) pager.add("") pager.add("== refined classes".bold) pager.indent = pager.indent + 1 @@ -465,7 +453,7 @@ redef class MClass # return the generic signature of the class # [E, F] private fun signature: String do - var res = new Buffer + var res = new FlatBuffer if arity > 0 then res.append("[") for i in [0..intro.parameter_names.length[ do @@ -481,7 +469,7 @@ redef class MClass # class name is displayed with colors depending on visibility # abstract interface Foo[E] private fun prototype: String do - var res = new Buffer + var res = new FlatBuffer res.append("{kind} ") if visibility.to_s == "public" then res.append("{name}{signature}".bold.green) if visibility.to_s == "private" then res.append("{name}{signature}".bold.red) @@ -499,11 +487,10 @@ redef class MClass redef fun content(index, pager) do # intro comment - if index.mbuilder.mclassdef2nclassdef.has_key(intro) then - var node = index.mbuilder.mclassdef2nclassdef[intro] - if node isa AStdClassdef and not node.n_doc == null and not node.n_doc.comment.is_empty then - for comment in node.n_doc.comment do pager.add(comment.green) - end + var sorter = new MEntityNameSorter + var mdoc = intro.mdoc + if mdoc != null then + for comment in mdoc.content do pager.add(comment.green) end pager.add(intro.to_console) pager.add("{intro.namespace}".bold.gray + " (lines {intro.location.lines})".gray) @@ -511,8 +498,7 @@ redef class MClass # parents var supers = self.in_hierarchy(index.mainmodule).direct_greaters.to_a if not supers.is_empty then - var csorter = new MClassNameSorter - csorter.sort(supers) + sorter.sort(supers) pager.add("") pager.add("== supers".bold) pager.indent = pager.indent + 1 @@ -534,13 +520,12 @@ redef class MClass pager.indent = pager.indent - 1 end # intro mproperties - var psorter = new MPropDefNameSorter var mpropdefs = intro.mpropdefs index.mainmodule.linearize_mpropdefs(mpropdefs) for cat in intro.cats2mpropdefs.keys do var defs = intro.cats2mpropdefs[cat].to_a if defs.is_empty then continue - psorter.sort(defs) + sorter.sort(defs) pager.add("") pager.add("== {cat}".bold) pager.indent = pager.indent + 1 @@ -577,39 +562,35 @@ redef class MClassDef end fun to_console: String do - var res = new Buffer + var res = new FlatBuffer if not is_intro then res.append("redef ") res.append(mclass.prototype) return res.to_s end redef fun preview(index, pager) do - if index.mbuilder.mclassdef2nclassdef.has_key(self) then - var node = index.mbuilder.mclassdef2nclassdef[self] - if node isa AStdClassdef and not node.n_doc == null and not node.n_doc.short_comment.is_empty then - pager.add(node.n_doc.short_comment.green) - end + var mdoc = self.mdoc + if mdoc != null then + pager.add(mdoc.short_comment.green) end pager.add(to_console) pager.add("{namespace}".bold.gray + " (lines {location.lines})".gray) end redef fun content(index, pager) do - if index.mbuilder.mclassdef2nclassdef.has_key(self) then - var node = index.mbuilder.mclassdef2nclassdef[self] - if node isa AStdClassdef and not node.n_doc == null and not node.n_doc.comment.is_empty then - for comment in node.n_doc.comment do pager.add(comment.green) - end + var mdoc = self.mdoc + if mdoc != null then + for comment in mdoc.content do pager.add(comment.green) end pager.add(to_console) pager.add("{namespace}".bold.gray + " (lines {location.lines})".gray) pager.indent = pager.indent + 1 var mpropdefs = self.mpropdefs - var psorter = new MPropDefNameSorter + var sorter = new MEntityNameSorter index.mainmodule.linearize_mpropdefs(mpropdefs) for cat in cats2mpropdefs.keys do var defs = cats2mpropdefs[cat].to_a - psorter.sort(defs) + sorter.sort(defs) if defs.is_empty then continue pager.add("") pager.add("== {cat}".bold) @@ -685,22 +666,18 @@ redef class MPropDef end redef fun preview(index, pager) do - if index.mbuilder.mpropdef2npropdef.has_key(self) then - var nprop = index.mbuilder.mpropdef2npropdef[self] - if not nprop.n_doc == null and not nprop.n_doc.short_comment.is_empty then - pager.add(nprop.n_doc.short_comment.green) - end + var mdoc = self.mdoc + if mdoc != null then + pager.add(mdoc.short_comment.green) end pager.add(to_console) pager.add("{namespace}".bold.gray + " (lines {location.lines})".gray) end redef fun content(index, pager) do - if index.mbuilder.mpropdef2npropdef.has_key(self) then - var nprop = index.mbuilder.mpropdef2npropdef[self] - if not nprop.n_doc == null and not nprop.n_doc.comment.is_empty then - for comment in nprop.n_doc.comment do pager.add(comment.green) - end + var mdoc = self.mdoc + if mdoc != null then + for comment in mdoc.content do pager.add(comment.green) end pager.add(to_console) pager.add("{namespace}".bold.gray + " (lines {location.lines})".gray) @@ -709,22 +686,21 @@ end redef class MMethodDef redef fun to_console do - var res = new Buffer + var res = new FlatBuffer if not is_intro then res.append("redef ") if not mproperty.is_init then res.append("fun ") res.append(mproperty.to_console.bold) if msignature != null then res.append(msignature.to_console) - # FIXME: modifiers should be accessible via the model - #if self isa ADeferredMethPropdef then ret = "{ret} is abstract" - #if self isa AInternMethPropdef then ret = "{ret} is intern" - #if self isa AExternMethPropdef then ret = "{ret} is extern" + if is_abstract then res.append " is abstract" + if is_intern then res.append " is intern" + if is_extern then res.append " is extern" return res.to_s end end redef class MVirtualTypeDef redef fun to_console do - var res = new Buffer + var res = new FlatBuffer res.append("type ") res.append(mproperty.to_console.bold) res.append(": {bound.to_console}") @@ -734,7 +710,7 @@ end redef class MAttributeDef redef fun to_console do - var res = new Buffer + var res = new FlatBuffer res.append("var ") res.append(mproperty.to_console.bold) res.append(": {static_mtype.to_console}") @@ -744,7 +720,7 @@ end redef class MSignature redef fun to_console do - var res = new Buffer + var res = new FlatBuffer if not mparameters.is_empty then res.append("(") for i in [0..mparameters.length[ do @@ -762,7 +738,7 @@ end redef class MParameter fun to_console: String do - var res = new Buffer + var res = new FlatBuffer res.append("{name}: {mtype.to_console}") if is_vararg then res.append("...") return res.to_s @@ -779,7 +755,7 @@ end redef class MGenericType redef fun to_console do - var res = new Buffer + var res = new FlatBuffer res.append("{mclass.name}[") for i in [0..arguments.length[ do res.append(arguments[i].to_console) @@ -798,17 +774,9 @@ redef class MVirtualType redef fun to_console do return mproperty.name end -redef class ADoc - private fun comment: List[String] do - var res = new List[String] - for t in n_comment do - res.add(t.text.replace("\n", "")) - end - return res - end - +redef class MDoc private fun short_comment: String do - return n_comment.first.text.replace("\n", "") + return content.first end end @@ -862,8 +830,8 @@ redef class String private fun escape: String do - var b = new Buffer - for c in self do + var b = new FlatBuffer + for c in self.chars do if c == '\n' then b.append("\\n") else if c == '\0' then @@ -892,7 +860,8 @@ end # Create a tool context to handle options and paths var toolcontext = new ToolContext -toolcontext.process_options +toolcontext.tooldescription = "Usage: nitx [OPTION]... [query]\nDisplays specific pieces of API information from Nit source files." +toolcontext.process_options(args) # Here we launch the nit index var ni = new NitIndex(toolcontext)