- printn ">> "
- search(sys.stdin.read_line)
- end
-
- fun search(entry: String) do
- if entry.is_empty then
- prompt
- return
- end
- if entry == ":h" then
- help
- prompt
- return
- end
- if entry == ":q" then return
-
- # Parse query string
- var query = parse_query(entry)
-
- # search in index
- var matches = new HashSet[IndexMatch]
- if query isa IndexQueryPair then
- if query.category == "return" then
- # seek return types
- matches.add_all(search_returns(query))
- else if query.category == "param" then
- # seek param types
- matches.add_all(search_params(query))
- else if query.category == "new" then
- # seek type inits
- matches.add_all(search_inits(query))
- end
- else
- matches.add_all(search_modules(query))
- matches.add_all(search_classes(query))
- matches.add_all(search_properties(query))
- end
- # no matches
- if matches.is_empty then
- print "Nothing known about '{query.string}', type ':h' for help"
- else
- renderer.render_matches(query, matches)
- end
- if arguments.length == 1 then prompt
- end
-
- private fun parse_query(str: String): IndexQuery do
- var parts = str.split_with(":")
- if parts.length == 1 then
- return new IndexQuery(str, parts[0])
- else
- var category = parts[0]
- var keyword = parts[1]
- if keyword.chars.first == ' ' then keyword = keyword.substring_from(1)
- return new IndexQueryPair(str, keyword, category)
- end
- end
-
- # search for modules
- private fun search_modules(query: IndexQuery): Set[MModule] do
- var matches = new HashSet[MModule]
- for mmodule in model.mmodules do
- if mmodule.name == query.keyword then matches.add(mmodule)
- end
- return matches
- end
-
- # search for classes
- private fun search_classes(query: IndexQuery): Set[MClass] do
- var matches = new HashSet[MClass]
- for mclass in model.mclasses do
- if mclass.name == query.keyword then matches.add(mclass)
- end
- return matches
- end
-
- # search for properties
- private fun search_properties(query: IndexQuery): Set[MProperty] do
- var matches = new HashSet[MProperty]
- for mproperty in model.mproperties do
- if mproperty.name == query.keyword then matches.add(mproperty)
- end
- return matches
- end
-
- # search for mpropdef returning keyword
- private fun search_returns(query: IndexQuery): Set[MProperty] do
- var matches = new HashSet[MProperty]
- for mproperty in model.mproperties do
- var intro = mproperty.intro
- if intro isa MMethodDef then
- if intro.msignature.return_mtype != null and intro.msignature.return_mtype.to_console.has_prefix(query.keyword) then matches.add(mproperty)
- else if intro isa MAttributeDef then
- if intro.static_mtype.to_console.has_prefix(query.keyword) then matches.add(mproperty)
- end
- end
- return matches
- end
-
- # search for mpropdef taking keyword as parameter
- private fun search_params(query: IndexQuery): Set[MProperty] do
- var matches = new HashSet[MProperty]
- for mproperty in model.mproperties do
- var intro = mproperty.intro
- if intro isa MMethodDef then
- var mparameters = intro.msignature.mparameters
- for mparameter in mparameters do
- if mparameter.mtype.to_console.has_prefix(query.keyword) then matches.add(mproperty)
- end
- else if intro isa MAttributeDef then
- if intro.static_mtype.to_console.has_prefix(query.keyword) then matches.add(mproperty)
- end
- end
- return matches
- end
-
- # search for mpropdef creating new instance of keyword
- private fun search_inits(query: IndexQuery): Set[MPropDef] do
- var mtype2mpropdefs = toolcontext.nitx_phase.mtype2mpropdefs
- var matches = new HashSet[MPropDef]
- for mtype in mtype2mpropdefs.keys do
- if mtype.to_console.has_prefix(query.keyword) then
- for mpropdef in mtype2mpropdefs[mtype] do
- matches.add(mpropdef)
- end
- end
- end
- return matches
- end
-end
-
-private class IndexQuery
- var string: String
- var keyword: String
- init(string: String, keyword: String) do
- self.string = string
- self.keyword = keyword
- end
-end
-
-private class IndexQueryPair
- super IndexQuery
- var category: String
- init(string: String, keyword: String, category: String) do
- super(string, keyword)
- self.category = category
- end
-end
-
-# A match to a query in the nit index
-private interface IndexMatch
- # Short preview of the result for result list display
- fun preview(index: NitIndex, output: Pager) is abstract
- fun content(index: NitIndex, output: Pager) is abstract
-end
-
-# Code Analysis
-
-redef class ToolContext
- private var nitx_phase: NitxPhase = new NitxPhase(self, [modelize_property_phase])
-end
-
-# Compiler phase for nitx
-private class NitxPhase
- super Phase
-
- var mtype2mpropdefs = new HashMap[MType, Set[MPropDef]]
- redef fun process_npropdef(npropdef) do
- var visitor = new TypeInitVisitor
- visitor.enter_visit(npropdef)
- for mtype in visitor.inits do
- if not mtype2mpropdefs.has_key(mtype) then
- mtype2mpropdefs[mtype] = new HashSet[MPropDef]
- end
- mtype2mpropdefs[mtype].add(npropdef.mpropdef.as(not null))
- end
- end
-end
-
-# Visitor looking for initialized mtype (new T)
-private class TypeInitVisitor
- super Visitor
-
- var inits = new HashSet[MType]
- redef fun visit(node)
- do
- node.visit_all(self)
- # look for init
- if not node isa ANewExpr then return
- var mtype = node.n_type.mtype
- if mtype != null then inits.add(mtype)
- end
-end
-
-# Pager output for console
-
-private class PagerMatchesRenderer
- var index: NitIndex
- init(index: NitIndex) do self.index = index
-
- fun render_matches(query: IndexQuery, matches: Collection[IndexMatch]) do
- var pager = new Pager
- if matches.length == 1 then
- pager.add("= result for '{query.string}'".bold)
- pager.add("")
- pager.indent = pager.indent + 1
- matches.first.content(index, pager)
- pager.indent = pager.indent - 1