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
25 import model
::model_index
27 # Nitx handles console I/O.
29 # Using `prompt`, the command line can be turned on an interactive tool.
32 # ToolContext used to access options.
35 # DocModel that contains the informations to display.
38 # Comparator used to sort MEntities.
39 var sorter
= new MEntityNameSorter
41 # Displays the welcome message and start prompt.
47 # Displays the welcome message and the list of loaded modules.
49 print
"Welcome in the Nit Index."
51 print
"Loaded modules:"
52 var mmodules
= doc
.mmodules
.to_a
61 # Displays the list of available commands.
64 print
"\tname\t\t\tlookup module, class and property with the corresponding 'name'"
65 print
"\tdoc: <name::space>\tdisplay the documentation page of 'namespace'"
66 print
"\nType lookup:"
67 print
"\tparam: <Type>\t\tlookup methods using the corresponding 'Type' as parameter"
68 print
"\treturn: <Type>\t\tlookup methods returning the corresponding 'Type'"
69 print
"\tnew: <Type>\t\tlookup methods creating new instances of 'Type'"
70 print
"\tcall: <name>\t\tlookup methods calling 'name'"
71 print
"\nHierarchy lookup:"
72 print
"\tparents: <Class>\tlist direct parents of 'Class'"
73 print
"\tancestors: <Class>\tlist all ancestors of 'Class'"
74 print
"\tchildren: <Class>\tlist direct children of 'Class'"
75 print
"\tdescendants: <Class>\tlist all descendants of 'Class'"
76 print
"\nCode lookup:"
77 print
"\tcode: <name>\t\tdisplay the source code associated to the 'name' entity"
79 print
"\t:h\t\t\tdisplay this help message"
80 print
"\t:q\t\t\tquit interactive mode"
84 # Prompts the user for a command.
87 do_query
(sys
.stdin
.read_line
)
91 # Parser used to process doc commands
92 var parser
: DocCommandParser is lazy
do
93 var parser
= new DocCommandParser
94 parser
.allowed_commands
= ["doc", "comment", "list", "param", "return",
95 "new", "call", "code"]
99 # Processes the query string and performs it.
100 fun do_query
(str
: String) do
103 else if str
== ":h" then
107 var query
= parser
.parse
(str
)
108 if query
== null then
109 query
= new CommentCommand(str
)
112 var res
= query
.perform
(self, doc
)
115 suggest
= query
.suggest
(self, doc
)
117 var page
= query
.make_results
(self, res
, suggest
)
118 print page
.write_to_string
122 redef class DocCommand
124 # Looks up the `doc` model and returns possible matches.
125 fun perform
(nitx
: Nitx, doc
: DocModel): Array[NitxMatch] is abstract
127 # Looks up the `doc` model and returns possible suggestions.
128 fun suggest
(nitx
: Nitx, doc
: DocModel): nullable Array[MEntity] do
129 return find_suggestions
(doc
, arg
)
132 # Pretty prints the results for the console.
133 fun make_results
(nitx
: Nitx, results
: Array[NitxMatch], suggest
: nullable Array[MEntity]): DocPage do
134 var page
= new DocPage("results", "Results")
135 page
.root
.add_child
(new QueryResultArticle("results", "Results", self, results
, suggest
))
139 # Lookup mentities based on a `query` string.
141 # 1- lookup by first name (returns always one value)
142 # 2- lookup by name (can return conflicts)
143 fun find_mentities
(doc
: DocModel, query
: String): Array[MEntityMatch] do
144 var res
= new Array[MEntityMatch]
146 # First lookup by full_name
147 var mentity
= doc
.mentity_by_full_name
(query
)
148 if mentity
!= null then
149 res
.add
new MEntityMatch(self, mentity
)
153 # If no results, lookup by name
154 for m
in doc
.mentities_by_name
(query
) do
155 res
.add
new MEntityMatch(self, m
)
161 # Suggest some mentities based on a `query` string.
162 fun find_suggestions
(doc
: DocModel, query
: String): Array[MEntity] do
163 return doc
.find
(query
, 3)
167 # Something that matches a `DocCommand`.
168 abstract class NitxMatch
170 # Query matched by `self`.
171 var query
: DocCommand
173 # Pretty prints `self` for console.
174 fun make_list_item
: String is abstract
177 # A match between a `DocCommand` and a `MEntity`.
184 redef fun make_list_item
do return mentity
.cs_list_item
187 redef class CommentCommand
188 redef fun perform
(nitx
, doc
) do return find_mentities
(doc
, arg
)
190 redef fun make_results
(nitx
, results
, suggest
) do
191 var len
= results
.length
193 var res
= results
.first
.as(MEntityMatch)
194 var mentity
= res
.mentity
195 var page
= new DocPage("resultats", "Results")
196 var article
= new DefinitionArticle("results", "Results", mentity
)
197 article
.cs_title
= mentity
.name
198 article
.cs_subtitle
= mentity
.cs_declaration
199 page
.root
.add_child article
207 # A query to search signatures using a specific `MType` as parameter.
208 redef class ParamCommand
209 redef fun perform
(nitx
, doc
) do
210 var res
= new Array[NitxMatch]
212 for mproperty
in doc
.mproperties
do
213 if not mproperty
isa MMethod then continue
214 var msignature
= mproperty
.intro
.msignature
215 if msignature
!= null then
216 for mparam
in msignature
.mparameters
do
217 if mparam
.mtype
.name
== mtype_name
then
218 res
.add
new MEntityMatch(self, mproperty
)
227 # A query to search signatures using a specific `MType` as return.
228 redef class ReturnCommand
229 redef fun perform
(nitx
, doc
) do
230 var res
= new Array[NitxMatch]
232 for mproperty
in doc
.mproperties
do
233 if not mproperty
isa MMethod then continue
234 var msignature
= mproperty
.intro
.msignature
235 if msignature
!= null then
236 var mreturn
= msignature
.return_mtype
237 if mreturn
!= null and mreturn
.name
== mtype_name
then
238 res
.add
new MEntityMatch(self, mproperty
)
246 # A query to search methods creating new instances of a specific `MType`.
247 redef class NewCommand
248 redef fun perform
(nitx
, doc
) do
249 var res
= new Array[NitxMatch]
251 for mpropdef
in doc
.mpropdefs
do
252 var visitor
= new TypeInitVisitor(mtype_name
)
253 var npropdef
= nitx
.ctx
.modelbuilder
.mpropdef2node
(mpropdef
)
254 if npropdef
== null then continue
255 visitor
.enter_visit
(npropdef
)
256 for i
in visitor
.inits
do
257 res
.add
new MEntityMatch(self, mpropdef
)
264 # A query to search methods calling a specific `MProperty`.
265 redef class CallCommand
266 redef fun perform
(nitx
, doc
) do
267 var res
= new Array[NitxMatch]
269 for mpropdef
in doc
.mpropdefs
do
270 var visitor
= new MPropertyCallVisitor
271 var npropdef
= nitx
.ctx
.modelbuilder
.mpropdef2node
(mpropdef
)
272 if npropdef
== null then continue
273 visitor
.enter_visit
(npropdef
)
274 for mprop
in visitor
.calls
do
275 if mprop
.name
!= mprop_name
then continue
276 res
.add
new MEntityMatch(self, mpropdef
)
283 # A match between a `DocPage` and a `MEntity`.
290 redef fun make_list_item
do
292 if page
isa MEntityPage then
293 return page
.mentity
.cs_list_item
295 return " * {page.title}"
299 # Search in class or module hierarchy of a `MEntity`.
301 # It actually searches for pages about the mentity and extracts the
302 # pre-calculated hierarchies by the `doc_post` phase.
303 abstract class HierarchiesQuery
306 redef fun make_results
(nitx
, results
, suggest
) do
307 var page
= new DocPage("hierarchy", "Hierarchy")
308 for result
in results
do
309 if not result
isa PageMatch then continue
310 var rpage
= result
.page
311 if not rpage
isa MClassPage then continue
312 page
.root
.add_child build_article
(rpage
)
317 # Build an article containing the hierarchy list depending on subclasses.
318 private fun build_article
(page
: MClassPage): DocArticle is abstract
321 # List all parents of a `MClass`.
323 super HierarchiesQuery
325 redef fun build_article
(page
) do
326 return new MEntitiesListArticle(
328 "Ancestors for {page.mentity.name}",
333 # List direct parents of a `MClass`.
335 super HierarchiesQuery
337 redef fun build_article
(page
) do
338 return new MEntitiesListArticle(
340 "Parents for {page.mentity.name}",
345 # List direct children of a `MClass`.
347 super HierarchiesQuery
349 redef fun build_article
(page
) do
350 return new MEntitiesListArticle(
352 "Children for {page.mentity.name}",
357 # List all descendants of a `MClass`.
358 class DescendantsQuery
359 super HierarchiesQuery
361 redef fun build_article
(page
) do
362 return new MEntitiesListArticle(
364 "Descendants for {page.mentity.name}",
369 # A query to search source code from a file name.
370 redef class CodeCommand
371 # FIXME refactor this!
372 redef fun perform
(nitx
, doc
) do
373 var res
= new Array[NitxMatch]
375 # if name is an existing sourcefile, opens it
376 if name
.file_exists
then
377 var fr
= new FileReader.open
(name
)
378 var content
= fr
.read_all
380 res
.add
new CodeMatch(self, name
, content
)
383 # else, lookup the model by name
384 for match
in find_mentities
(doc
, name
) do
385 if match
.mentity
isa MClass then continue
386 if match
.mentity
isa MProperty then continue
387 res
.add
new CodeMatch(self, match
.mentity
.cs_location
, match
.mentity
.cs_source_code
)
392 redef fun make_results
(nitx
, results
, suggest
) do
393 var page
= new DocPage("results", "Code Results")
394 for res
in results
do
395 page
.add
new CodeQueryArticle("results", "Results", self, res
.as(CodeMatch))
401 # A match between a piece of code and a string.
405 # Location of the code match.
408 # Piece of code matched.
411 redef fun make_list_item
do return "* {location}"
416 # Visitor looking for initialized `MType` (new T).
419 private class TypeInitVisitor
422 # `MType` name to look for.
423 var mtype_name
: String
425 var inits
= new HashSet[MType]
426 redef fun visit
(node
)
430 if not node
isa ANewExpr then return
431 var mtype
= node
.n_type
.mtype
432 if mtype
!= null and mtype
.name
== mtype_name
then inits
.add
(mtype
)
436 # Visitor looking for calls to a `MProperty` (new T).
439 private class MPropertyCallVisitor
442 var calls
= new HashSet[MProperty]
443 redef fun visit
(node
)
446 if not node
isa ASendExpr then return
447 calls
.add node
.callsite
.as(not null).mproperty
453 # A `DocArticle` that displays query results.
454 private class QueryResultArticle
457 # Query linked to the results to display.
458 var query
: DocCommand
460 # Results to display.
461 var results
: Array[NitxMatch]
463 # Optional suggestion when no matches where found
464 var suggest
: nullable Array[MEntity] = null is optional
466 redef fun render_title
do
467 var len
= results
.length
469 addn
"No result found for '{query.string}'..."
470 var suggest
= self.suggest
471 if suggest
!= null and suggest
.not_empty
then
472 add
"\nDid you mean "
475 add
"`{s.full_name}`"
476 if i
== suggest
.length
- 2 then add
", "
477 if i
== suggest
.length
- 1 then add
" or "
483 add
"# {len} result(s) for '{query.string}'".green
.bold
487 redef fun render_body
do
489 for result
in results
do
491 addn result
.make_list_item
496 # An article that displays a piece of code.
497 private class CodeQueryArticle
500 # The query linked to the result to display.
501 var query
: DocCommand
503 # The result to display.
504 var result
: CodeMatch
506 redef fun render_body
do
508 addn
"in {result.location}".gray
.bold
514 # A Pager is used to display data into a unix `less` container.
517 # Content to display.
518 var content
= new FlatBuffer
520 # Adds text to the pager.
521 fun add
(text
: String) do
522 content
.append
(escape
(text
))
525 fun render
do sys
.system
("echo \"{content}\
" | less -r")
527 fun escape
(str
: String): String
529 var b
= new FlatBuffer
530 for c
in str
.chars
do
533 else if c
== '\0' then
535 else if c
== '"' then
537 else if c == '\\' then
539 else if c == '`' then
541 else if c.code_point < 32 then
542 b.append("\\{c.code_point.to_base(8)}")