1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Nitx related components
17 # This module is a place holder for `nitx` related services.
18 # No `doc_phase` can be found here, only components used by Nitx tool.
24 import doc
::console_templates
26 # Nitx handles console I/O.
28 # Using `prompt`, the command line can be turned on an interactive tool.
31 # ToolContext used to access options.
34 # DocModel that contains the informations to display.
37 # Comparator used to sort MEntities.
38 var sorter
= new MEntityNameSorter
40 # Displays the welcome message and start prompt.
46 # Displays the welcome message and the list of loaded modules.
48 print
"Welcome in the Nit Index."
50 print
"Loaded modules:"
51 var mmodules
= doc
.mmodules
.to_a
60 # Displays the list of available commands.
63 print
"\tname\t\t\tlookup module, class and property with the corresponding 'name'"
64 print
"\tdoc: <name::space>\tdisplay the documentation page of 'namespace'"
65 print
"\nType lookup:"
66 print
"\tparam: <Type>\t\tlookup methods using the corresponding 'Type' as parameter"
67 print
"\treturn: <Type>\t\tlookup methods returning the corresponding 'Type'"
68 print
"\tnew: <Type>\t\tlookup methods creating new instances of 'Type'"
69 print
"\tcall: <name>\t\tlookup methods calling 'name'"
70 print
"\nHierarchy lookup:"
71 print
"\tparents: <Class>\tlist direct parents of 'Class'"
72 print
"\tancestors: <Class>\tlist all ancestors of 'Class'"
73 print
"\tchildren: <Class>\tlist direct children of 'Class'"
74 print
"\tdescendants: <Class>\tlist all descendants of 'Class'"
75 print
"\nCode lookup:"
76 print
"\tcode: <name>\t\tdisplay the source code associated to the 'name' entity"
78 print
"\t:h\t\t\tdisplay this help message"
79 print
"\t:q\t\t\tquit interactive mode"
83 # Prompts the user for a command.
86 do_query
(sys
.stdin
.read_line
)
90 # Processes the query string and performs it.
91 fun do_query
(str
: String) do
92 var query
= new DocCommand(str
)
93 if query
isa NitxCommand then
97 var res
= query
.perform
(self, doc
)
98 var page
= query
.make_results
(self, res
)
99 print page
.write_to_string
103 redef interface DocCommand
105 redef new(query_string
) do
106 if query_string
== ":q" then
108 else if query_string
== ":h" then
111 var cmd
= super(query_string
)
112 if cmd
isa UnknownCommand then
113 return new CommentCommand("comment: {query_string}")
118 # Looks up the `doc` model and returns possible matches.
119 fun perform
(nitx
: Nitx, doc
: DocModel): Array[NitxMatch] is abstract
121 # Pretty prints the results for the console.
122 fun make_results
(nitx
: Nitx, results
: Array[NitxMatch]): DocPage do
123 var page
= new DocPage("results", "Results")
124 page
.root
.add_child
(new QueryResultArticle("results", "Results", self, results
))
129 # Something that matches a `DocCommand`.
130 abstract class NitxMatch
132 # Query matched by `self`.
133 var query
: DocCommand
135 # Pretty prints `self` for console.
136 fun make_list_item
: String is abstract
139 # A match between a `DocCommand` and a `MEntity`.
146 redef fun make_list_item
do return mentity
.cs_list_item
149 redef class CommentCommand
150 redef fun perform
(nitx
, doc
) do
151 var name
= args
.first
152 var res
= new Array[NitxMatch]
153 for mentity
in doc
.mentities_by_name
(name
) do
154 res
.add
new MEntityMatch(self, mentity
)
159 redef fun make_results
(nitx
, results
) do
160 var len
= results
.length
162 var res
= results
.first
.as(MEntityMatch)
163 var mentity
= res
.mentity
164 var page
= new DocPage("resultats", "Results")
165 var article
= new DefinitionArticle("results", "Results", mentity
)
166 article
.cs_title
= mentity
.name
167 article
.cs_subtitle
= mentity
.cs_declaration
168 page
.root
.add_child article
176 # A query to search signatures using a specific `MType` as parameter.
177 redef class ParamCommand
178 redef fun perform
(nitx
, doc
) do
179 var res
= new Array[NitxMatch]
180 var mtype_name
= args
.first
181 for mproperty
in doc
.mproperties
do
182 if not mproperty
isa MMethod then continue
183 var msignature
= mproperty
.intro
.msignature
184 if msignature
!= null then
185 for mparam
in msignature
.mparameters
do
186 if mparam
.mtype
.name
== mtype_name
then
187 res
.add
new MEntityMatch(self, mproperty
)
196 # A query to search signatures using a specific `MType` as return.
197 redef class ReturnCommand
198 redef fun perform
(nitx
, doc
) do
199 var res
= new Array[NitxMatch]
200 var mtype_name
= args
.first
201 for mproperty
in doc
.mproperties
do
202 if not mproperty
isa MMethod then continue
203 var msignature
= mproperty
.intro
.msignature
204 if msignature
!= null then
205 var mreturn
= msignature
.return_mtype
206 if mreturn
!= null and mreturn
.name
== mtype_name
then
207 res
.add
new MEntityMatch(self, mproperty
)
215 # A query to search methods creating new instances of a specific `MType`.
216 redef class NewCommand
217 redef fun perform
(nitx
, doc
) do
218 var res
= new Array[NitxMatch]
219 var mtype_name
= args
.first
220 for mpropdef
in doc
.mpropdefs
do
221 var visitor
= new TypeInitVisitor(mtype_name
)
222 var npropdef
= nitx
.ctx
.modelbuilder
.mpropdef2node
(mpropdef
)
223 if npropdef
== null then continue
224 visitor
.enter_visit
(npropdef
)
225 for i
in visitor
.inits
do
226 res
.add
new MEntityMatch(self, mpropdef
)
233 # A query to search methods calling a specific `MProperty`.
234 redef class CallCommand
235 redef fun perform
(nitx
, doc
) do
236 var res
= new Array[NitxMatch]
237 var mprop_name
= args
.first
238 for mpropdef
in doc
.mpropdefs
do
239 var visitor
= new MPropertyCallVisitor
240 var npropdef
= nitx
.ctx
.modelbuilder
.mpropdef2node
(mpropdef
)
241 if npropdef
== null then continue
242 visitor
.enter_visit
(npropdef
)
243 for mprop
in visitor
.calls
do
244 if mprop
.name
!= mprop_name
then continue
245 res
.add
new MEntityMatch(self, mpropdef
)
252 # A query to search a Nitdoc documentation page by its name.
253 redef class ArticleCommand
254 redef fun perform
(nitx
, doc
) do
255 var res
= new Array[NitxMatch]
256 var name
= args
.first
257 for page
in doc
.pages
.values
do
258 if name
== "*" then # FIXME dev only
259 res
.add
new PageMatch(self, page
)
260 else if page
.title
== name
then
261 res
.add
new PageMatch(self, page
)
262 else if page
isa MEntityPage and page
.mentity
.cs_namespace
== name
then
263 res
.add
new PageMatch(self, page
)
269 redef fun make_results
(nitx
, results
) do
270 var len
= results
.length
271 # FIXME how to render the pager for one worded namespaces like "core"?
273 var page
= results
.first
.as(PageMatch).page
274 var pager
= new Pager
275 pager
.add page
.write_to_string
284 # A match between a `DocPage` and a `MEntity`.
291 redef fun make_list_item
do
293 if page
isa MEntityPage then
294 return page
.mentity
.cs_list_item
296 return " * {page.title}"
300 # Search in class or module hierarchy of a `MEntity`.
302 # It actually searches for pages about the mentity and extracts the
303 # pre-calculated hierarchies by the `doc_post` phase.
304 abstract class HierarchiesQuery
307 redef fun make_results
(nitx
, results
) do
308 var page
= new DocPage("hierarchy", "Hierarchy")
309 for result
in results
do
310 if not result
isa PageMatch then continue
311 var rpage
= result
.page
312 if not rpage
isa MClassPage then continue
313 page
.root
.add_child build_article
(rpage
)
318 # Build an article containing the hierarchy list depending on subclasses.
319 private fun build_article
(page
: MClassPage): DocArticle is abstract
322 # List all parents of a `MClass`.
324 super HierarchiesQuery
326 redef fun build_article
(page
) do
327 return new MEntitiesListArticle(
329 "Ancestors for {page.mentity.name}",
334 # List direct parents of a `MClass`.
336 super HierarchiesQuery
338 redef fun build_article
(page
) do
339 return new MEntitiesListArticle(
341 "Parents for {page.mentity.name}",
346 # List direct children of a `MClass`.
348 super HierarchiesQuery
350 redef fun build_article
(page
) do
351 return new MEntitiesListArticle(
353 "Children for {page.mentity.name}",
358 # List all descendants of a `MClass`.
359 class DescendantsQuery
360 super HierarchiesQuery
362 redef fun build_article
(page
) do
363 return new MEntitiesListArticle(
365 "Descendants for {page.mentity.name}",
370 # A query to search source code from a file name.
371 redef class CodeCommand
372 # FIXME refactor this!
373 redef fun perform
(nitx
, doc
) do
374 var res
= new Array[NitxMatch]
375 var name
= args
.first
376 # if name is an existing sourcefile, opens it
377 if name
.file_exists
then
378 var fr
= new FileReader.open
(name
)
379 var content
= fr
.read_all
381 res
.add
new CodeMatch(self, name
, content
)
384 # else, lookup the model by name
385 for mentity
in doc
.mentities_by_name
(name
) do
386 if mentity
isa MClass then continue
387 if mentity
isa MProperty then continue
388 res
.add
new CodeMatch(self, mentity
.cs_location
, mentity
.cs_source_code
)
393 redef fun make_results
(nitx
, results
) do
394 var page
= new DocPage("results", "Code Results")
395 for res
in results
do
396 page
.add
new CodeQueryArticle("results", "Results", self, res
.as(CodeMatch))
402 # A match between a piece of code and a string.
406 # Location of the code match.
409 # Piece of code matched.
412 redef fun make_list_item
do return "* {location}"
416 # A query that contains a nitx command.
418 # These commands are prefixed with `:` and are used to control the execution of
419 # `nitx` like displaying the help or quiting.
420 interface NitxCommand
423 # Executes the command.
424 fun execute
(nitx
: Nitx) is abstract
431 redef fun execute
(nitx
) do exit
0
434 # Displays the help message.
438 redef fun execute
(nitx
) do nitx
.help
443 # Visitor looking for initialized `MType` (new T).
446 private class TypeInitVisitor
449 # `MType` name to look for.
450 var mtype_name
: String
452 var inits
= new HashSet[MType]
453 redef fun visit
(node
)
457 if not node
isa ANewExpr then return
458 var mtype
= node
.n_type
.mtype
459 if mtype
!= null and mtype
.name
== mtype_name
then inits
.add
(mtype
)
463 # Visitor looking for calls to a `MProperty` (new T).
466 private class MPropertyCallVisitor
469 var calls
= new HashSet[MProperty]
470 redef fun visit
(node
)
473 if not node
isa ASendExpr then return
474 calls
.add node
.callsite
.as(not null).mproperty
480 # A `DocArticle` that displays query results.
481 private class QueryResultArticle
484 # Query linked to the results to display.
485 var query
: DocCommand
487 # Results to display.
488 var results
: Array[NitxMatch]
490 redef fun render_title
do
491 var len
= results
.length
493 add
"No result found for '{query.string}'..."
495 add
"# {len} result(s) for '{query.string}'".green
.bold
499 redef fun render_body
do
501 for result
in results
do
503 addn result
.make_list_item
508 # An article that displays a piece of code.
509 private class CodeQueryArticle
512 # The query linked to the result to display.
513 var query
: DocCommand
515 # The result to display.
516 var result
: CodeMatch
518 redef fun render_body
do
520 addn
"in {result.location}".gray
.bold
526 # A Pager is used to display data into a unix `less` container.
529 # Content to display.
530 var content
= new FlatBuffer
532 # Adds text to the pager.
533 fun add
(text
: String) do
534 content
.append
(escape
(text
))
537 fun render
do sys
.system
("echo \"{content}\
" | less -r")
539 fun escape
(str
: String): String
541 var b
= new FlatBuffer
542 for c
in str
.chars
do
545 else if c
== '\0' then
547 else if c
== '"' then
549 else if c == '\\' then
551 else if c == '`' then
553 else if c.code_point < 32 then
554 b.append("\\{c.code_point.to_base(8)}")