# This file is part of NIT ( http://www.nitlanguage.org ).
#
-# Copyright 2008 Jean Privat <jean@pryen.org>
-#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# See the License for the specific language governing permissions and
# limitations under the License.
+# ni or nit index, is a command tool used to display documentation
module ni
import model_utils
private class Pager
- var content: String = ""
+ var content = new Buffer
fun add(text: String) do addn("{text}\n")
- fun addn(text: String) do content += text.escape
+ fun addn(text: String) do content.append(text.escape)
fun add_rule do add("\n---\n")
fun render do sys.system("echo \"{content}\" | pager -r")
end
+# Main class of the nit index tool
+# NitIndex build the model using the toolcontext argument
+# then wait for query on std in to display documentation
class NitIndex
private var toolcontext: ToolContext
private var model: Model
#var mmodules = modelbuilder.parse_and_build(["{dir}/lib/standard/standard.nit"])
var mmodules = mbuilder.parse_and_build([arguments.first])
if mmodules.is_empty then return
- mbuilder.full_propdef_semantic_analysis
assert mmodules.length == 1
self.mainmodule = mmodules.first
end
end
fun welcome do
- print "Welcome in Nit Index.\n"
- print "Loaded modules"
- for m in mbuilder.nmodules do
- print " - {m.mmodule.name}"
+ print "Welcome in the Nit Index."
+ print "\nCommands:"
+ print "\tname\t\tlookup module, class and property with the corresponding 'name'"
+ print "\tparam: Type\tlookup methods using the corresponding 'Type' as parameter"
+ print "\treturn: Type\tlookup methods returning the corresponding 'Type'"
+ print "\tEnter a blank line to exit.\n"
+ print "\nLoaded modules:"
+ var mmodules = new Array[MModule]
+ mmodules.add_all(model.mmodules)
+ var sorter = new MModuleNameSorter
+ sorter.sort(mmodules)
+ for m in mmodules do
+ print "\t{m.name}"
end
- print "\nEnter the module, class or property name you want to look up."
- print "Enter a blank line to exit.\n"
end
fun prompt do
pager.add("known clients: ".bold + "{mmodule.in_importation.direct_smallers.join(", ")}\n")
end
pager.add_rule
- pager.addn(nmodule.comment.green)
+ pager.addn(nmodule.n_moduledecl.n_doc.comment.green)
pager.add_rule
var cats = new HashMap[String, Collection[MClass]]
#sort list
var sorted = new Array[MClass]
sorted.add_all(list)
- var sorter = new ComparableSorter[MClass]
+ var sorter = new MClassNameSorter
sorter.sort(sorted)
for mclass in sorted do
var nclass = mbuilder.mclassdef2nclassdef[mclass.intro].as(AStdClassdef)
pager.add("")
- if not nclass.short_comment.is_empty then
- pager.add("\t# {nclass.short_comment}")
+ if not nclass.n_doc == null and not nclass.n_doc.short_comment.is_empty then
+ pager.add("\t# {nclass.n_doc.short_comment}")
end
if cat == "refined classes" then
pager.add("\tredef {mclass.short_doc}")
pager.add("# {mclass.namespace}\n".bold)
pager.add("{mclass.short_doc}")
- pager.add_rule
- pager.addn(nclass.comment.green)
+ if not nclass.n_doc == null then
+ pager.add_rule
+ pager.addn(nclass.n_doc.comment.green)
+ end
pager.add_rule
if not mclass.parameter_types.is_empty then
pager.add("# formal types".bold)
pager.add("# virtual types".bold)
for vt in mclass.virtual_types do
pager.add("")
- vt_fulldoc(pager, vt)
+ mpropdef_fulldoc(pager, vt.intro)
end
end
pager.add_rule
#sort list
var sorted = new Array[MMethod]
sorted.add_all(list)
- var sorter = new ComparableSorter[MMethod]
+ var sorter = new MPropertyNameSorter
sorter.sort(sorted)
pager.add("\n# {cat}".bold)
for mprop in sorted do
pager.add("")
- method_fulldoc(pager, mprop)
+ mpropdef_fulldoc(pager, mprop.intro)
end
end
end
cats[mclass].add(mprop)
end
#sort groups
- var sorter = new ComparableSorter[MClass]
+ var sorter = new MClassNameSorter
var sorted = new Array[MClass]
sorted.add_all(cats.keys)
sorter.sort(sorted)
for mclass in sorted do
var mprops = cats[mclass]
pager.add("# {mclass.namespace}".bold)
- var sorterp = new ComparableSorter[MProperty]
+ var sorterp = new MPropertyNameSorter
sorterp.sort(mprops)
for mprop in mprops do
- if mprop isa MMethod and mbuilder.mpropdef2npropdef.has_key(mprop.intro) then
- pager.add("")
- method_fulldoc(pager, mprop)
- else if mprop isa MVirtualTypeProp then
- pager.add("")
- vt_fulldoc(pager, mprop)
- end
+ pager.add("")
+ mpropdef_fulldoc(pager, mprop.intro)
end
pager.add_rule
end
return matches
end
- private fun method_fulldoc(pager: Pager, mprop: MMethod) do
- if mbuilder.mpropdef2npropdef.has_key(mprop.intro) then
- var nprop = mbuilder.mpropdef2npropdef[mprop.intro]
- if not nprop.short_comment.is_empty then
- pager.add("\t# {nprop.short_comment}")
- end
- if nprop isa AAttrPropdef then
- pager.add("\t{nprop.read_accessor}")
- pager.add("\t{nprop.write_accessor}")
- else if nprop isa AMethPropdef then
- pager.add("\t{nprop}")
- end
- pager.add("\t\t" + "introduced in {mprop.intro_mclassdef.namespace}".gray)
- for mpropdef in mprop.mpropdefs do
- if mpropdef != mprop.intro then
- pager.add("\t\t" + "refined in {mpropdef.mclassdef.namespace}".gray)
- end
+ private fun mpropdef_fulldoc(pager: Pager, mpropdef: MPropDef) do
+ if mbuilder.mpropdef2npropdef.has_key(mpropdef) then
+ var nprop = mbuilder.mpropdef2npropdef[mpropdef]
+ if not nprop.n_doc == null and not nprop.n_doc.short_comment.is_empty then
+ pager.add("\t# {nprop.n_doc.short_comment}")
end
end
- end
-
- private fun vt_fulldoc(pager: Pager, vt: MVirtualTypeProp) do
- pager.add("\t{vt.short_doc}")
- pager.add("\t\t" + "introduced in {vt.intro_mclassdef.namespace}::{vt}".gray)
- for mpropdef in vt.mpropdefs do
- if mpropdef != vt.intro then
- pager.add("\t\t" + "refined in {mpropdef.mclassdef.namespace}".gray)
+ pager.add("\t{mpropdef}")
+ pager.add("\t\t" + "introduced in {mpropdef.mproperty.intro_mclassdef.namespace}".gray)
+ for mpdef in mpropdef.mproperty.mpropdefs do
+ if not mpdef.is_intro then
+ pager.add("\t\t" + "refined in {mpdef.mclassdef.namespace}".gray)
end
end
end
# Printing facilities
redef class MModule
- super Comparable
- redef type OTHER: MModule
- redef fun <(other: OTHER): Bool do return self.name < other.name
-
private fun namespace: String do
return full_name
end
end
redef class MClass
- super Comparable
- redef type OTHER: MClass
- redef fun <(other: OTHER): Bool do return self.name < other.name
-
redef fun to_s: String do
if arity > 0 then
return "{name}[{intro.parameter_names.join(", ")}]"
end
private fun namespace: String do
- return "{intro_mmodule.public_owner.name}::{name}"
+ if not intro_mmodule.public_owner == null then
+ return "{intro_mmodule.public_owner.name}::{name}"
+ else
+ return "{intro_mmodule.name}::{name}"
+ end
end
end
end
end
-redef class MProperty
- super Comparable
- redef type OTHER: MProperty
- redef fun <(other: OTHER): Bool do return self.name < other.name
+redef class MMethodDef
+ redef fun to_s do
+ var res = new Buffer
+ if not is_intro then res.append("redef ")
+ if not mproperty.is_init then res.append("fun ")
+ if mproperty.visibility.to_s == "public" then res.append(mproperty.name.green)
+ if mproperty.visibility.to_s == "private" then res.append(mproperty.name.red)
+ if mproperty.visibility.to_s == "protected" then res.append(mproperty.name.yellow)
+ if msignature != null then res.append(msignature.to_s)
+ # 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"
+ return res.to_s
+ end
end
-redef class MVirtualTypeProp
- private fun short_doc: String do
- var ret = ""
- if visibility.to_s == "public" then ret = "{to_s.green}: {intro.bound.to_s}"
- if visibility.to_s == "private" then ret = "\t{to_s.red}: {intro.bound.to_s}"
- if visibility.to_s == "protected" then ret = "\t{to_s.yellow}: {intro.bound.to_s}"
- return ret
+redef class MVirtualTypeDef
+ redef fun to_s do
+ var res = new Buffer
+ if mproperty.visibility.to_s == "public" then res.append(mproperty.name.green)
+ if mproperty.visibility.to_s == "private" then res.append(mproperty.name.red)
+ if mproperty.visibility.to_s == "protected" then res.append(mproperty.name.yellow)
+ res.append(": {bound.to_s}")
+ return res.to_s
end
end
-redef class AModule
- private fun comment: String do
- var ret = ""
- for t in n_moduledecl.n_doc.n_comment do
- ret += "{t.text.replace("# ", "")}"
+redef class MSignature
+ redef fun to_s do
+ var res = new Buffer
+ if not mparameters.is_empty then
+ res.append("(")
+ for i in [0..mparameters.length[ do
+ res.append(mparameters[i].to_s)
+ if i < mparameters.length - 1 then res.append(", ")
+ end
+ res.append(")")
end
- return ret
+ if return_mtype != null then
+ res.append(": {return_mtype.to_s}")
+ end
+ return res.to_s
end
end
-redef class AStdClassdef
- private fun comment: String do
- var ret = ""
- if n_doc != null then
- for t in n_doc.n_comment do
- var txt = t.text.replace("# ", "")
- txt = txt.replace("#", "")
- ret += "{txt}"
- end
- end
- return ret
+redef class MParameter
+ redef fun to_s do
+ var res = new Buffer
+ res.append("{name}: {mtype}")
+ if is_vararg then res.append("...")
+ return res.to_s
end
+end
- private fun short_comment: String do
- var ret = ""
- if n_doc != null then
- var txt = n_doc.n_comment.first.text
- txt = txt.replace("# ", "")
- txt = txt.replace("\n", "")
- ret += txt
+redef class MNullableType
+ redef fun to_s do return "nullable {mtype}"
+end
+
+redef class MGenericType
+ redef fun to_s do
+ var res = new Buffer
+ res.append("{mclass.name}[")
+ for i in [0..arguments.length[ do
+ res.append(arguments[i].to_s)
+ if i < arguments.length - 1 then res.append(", ")
end
- return ret
+ res.append("]")
+ return res.to_s
end
end
-redef class APropdef
- private fun short_comment: String is abstract
+redef class MParameterType
+ redef fun to_s do return mclass.intro.parameter_names[rank]
end
-redef class AAttrPropdef
- redef fun short_comment do
- var ret = ""
- if n_doc != null then
- var txt = n_doc.n_comment.first.text
- txt = txt.replace("# ", "")
- txt = txt.replace("\n", "")
- ret += txt
+redef class MVirtualType
+ redef fun to_s do return mproperty.intro.to_s
+end
+
+redef class ADoc
+ private fun comment: String do
+ var res = new Buffer
+ for t in n_comment do
+ res.append(t.text.replace("# ", "").replace("#", ""))
end
- return ret
+ return res.to_s
end
+ private fun short_comment: String do
+ return n_comment.first.text.replace("# ", "").replace("\n", "")
+ end
+end
+
+redef class AAttrPropdef
private fun read_accessor: String do
var ret = "fun "
#FIXME bug with standard::stream::FDStream::fd
end
end
-redef class AMethPropdef
- redef fun short_comment do
- var ret = ""
- if n_doc != null then
- var txt = n_doc.n_comment.first.text
- txt = txt.replace("# ", "")
- txt = txt.replace("\n", "")
- ret += txt
- end
- return ret
- end
-
- redef fun to_s do
- var ret = ""
- if not mpropdef.mproperty.is_init then
- ret = "fun "
- end
- if mpropdef.mproperty.visibility.to_s == "public" then ret = "{ret}{mpropdef.mproperty.name.green}"
- if mpropdef.mproperty.visibility.to_s == "private" then ret = "{ret}{mpropdef.mproperty.name.red}"
- if mpropdef.mproperty.visibility.to_s == "protected" then ret = "{ret}{mpropdef.mproperty.name.yellow}"
- if n_signature != null then ret = "{ret}{n_signature.to_s}"
- if n_kwredef != null then ret = "redef {ret}"
- 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"
- return ret
- end
-end
-
-redef class ASignature
- redef fun to_s do
- #TODO closures
- var ret = ""
- if not n_params.is_empty then
- ret = "{ret}({n_params.join(", ")})"
- end
- if n_type != null then ret += ": {n_type.to_s}"
- return ret
- end
-end
-
-redef class AParam
- redef fun to_s do
- var ret = "{n_id.text}"
- if n_type != null then
- ret = "{ret}: {n_type.to_s}"
- if n_dotdotdot != null then ret = "{ret}..."
- end
- return ret
- end
-end
-
-redef class AType
- redef fun to_s do
- var ret = n_id.text
- if n_kwnullable != null then ret = "nullable {ret}"
- if not n_types.is_empty then ret = "{ret}[{n_types.join(", ")}]"
- return ret
- end
-end
-
# Redef String class to add a function to color the string
redef class String