vim_autocomplete: remove ModelView dependency
[nit.git] / src / doc / commands / commands_catalog.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Commands to retrieve Catalog related data
16 module commands_catalog
17
18 import commands_model
19
20 # A DocCommand based on a Catalog
21 abstract class CmdCatalog
22 super DocCommand
23
24 autoinit(model, catalog, filter)
25
26 # Catalog to query at
27 var catalog: Catalog
28 end
29
30 # A CmdSearch command using a Catalog
31 class CmdCatalogSearch
32 super CmdCatalog
33 super CmdSearch
34
35 autoinit(model, catalog, filter, query, limit, page, count, max)
36
37 redef fun init_results do
38 if results != null then return new CmdSuccess
39
40 var res = super
41 if not res isa CmdSuccess then return res
42
43 var query = self.query
44 if query == null then return new ErrorNoQuery
45 sorter = null
46
47 var index = model.index
48
49 # lookup by name prefix
50 var matches = index.find_by_name_prefix(query).uniq.
51 sort(lname_sorter, name_sorter, kind_sorter)
52 matches = matches.rerank.sort(vis_sorter, score_sorter)
53
54 # lookup by tags
55 var malus = matches.length
56 if catalog.tag2proj.has_key(query) then
57 for mpackage in catalog.tag2proj[query] do
58 matches.add new IndexMatch(mpackage, malus)
59 malus += 1
60 end
61 matches = matches.uniq.rerank.sort(vis_sorter, score_sorter)
62 end
63
64 # lookup by full_name prefix
65 malus = matches.length
66 var full_matches = new IndexMatches
67 for match in index.find_by_full_name_prefix(query).
68 sort(lfname_sorter, fname_sorter) do
69 match.score += 1
70 full_matches.add match
71 end
72 matches = matches.uniq
73
74 # lookup by similarity
75 malus = matches.length
76 var sim_matches = new IndexMatches
77 for match in index.find_by_similarity(query).sort(score_sorter, lname_sorter, name_sorter) do
78 if match.score > query.length then break
79 match.score += 1
80 sim_matches.add match
81 end
82 matches.add_all sim_matches
83 matches = matches.uniq
84 results = matches.rerank.sort(vis_sorter, score_sorter).mentities
85 return res
86 end
87
88 private var score_sorter = new ScoreComparator
89 private var vis_sorter = new VisibilityComparator
90 private var name_sorter = new NameComparator
91 private var lname_sorter = new NameLengthComparator
92 private var fname_sorter = new FullNameComparator
93 private var lfname_sorter = new FullNameLengthComparator
94 private var kind_sorter = new MEntityComparator
95 end
96
97 # Retrieve the catalog metadata for a MPackage
98 class CmdMetadata
99 super CmdEntity
100
101 # MPackage metadata retrieved
102 var metadata: nullable MPackageMetadata = null is optional, writable
103
104 redef fun init_command do
105 if metadata != null then return new CmdSuccess
106
107 var res = super
108 if not res isa CmdSuccess then return res
109 var mentity = self.mentity.as(not null)
110
111 if mentity isa MPackage then
112 metadata = mentity.metadata
113 else
114 return new WarningNoMetadata(mentity)
115 end
116 return res
117 end
118 end
119
120 # No metadata for `mentity`
121 class WarningNoMetadata
122 super CmdWarning
123
124 # MEntity provided
125 var mentity: MEntity
126
127 redef fun to_s do return "No metadata for `{mentity.full_name}`"
128 end
129
130 # Retrieve the packages in the catalog
131 class CmdCatalogPackages
132 super CmdCatalog
133 super CmdEntities
134
135 autoinit(model, catalog, filter, limit, page, count, max)
136
137 redef var sorter = new CatalogScoreSorter(catalog) is lazy
138
139 redef fun init_results do
140 if results != null then return new CmdSuccess
141
142 var res = super
143 if not res isa CmdSuccess then return res
144
145 results = catalog.mpackages.values.to_a
146 return res
147 end
148 end
149
150 # Retrieve the catalog stats
151 class CmdCatalogStats
152 super CmdCatalog
153
154 # Retrieved catalog statistics
155 var stats: nullable CatalogStats = null is optional, writable
156
157 redef fun init_command do
158 super
159 self.stats = catalog.catalog_stats
160 return new CmdSuccess
161 end
162 end
163
164 # Retrieve the catalog tags list
165 class CmdCatalogTags
166 super CmdCatalog
167
168 # Sorter to sort tags alphabetically
169 var tags_sorter = new CatalogTagsSorter is optional, writable
170
171 # Count of packages by tag
172 var packages_count_by_tags: nullable ArrayMap[String, Int] = null is optional, writable
173
174 redef fun init_command do
175 super
176 var tags_to_projects = new ArrayMap[String, Int]
177 var tags = catalog.tag2proj.keys.to_a
178 tags_sorter.sort(tags)
179 for tag in tags do
180 if not catalog.tag2proj.has_key(tag) then continue
181 tags_to_projects[tag] = catalog.tag2proj[tag].length
182 end
183 packages_count_by_tags = tags_to_projects
184 return new CmdSuccess
185 end
186 end
187
188 # Retrieve the packages for a tag
189 class CmdCatalogTag
190 super CmdCatalogPackages
191
192 autoinit(model, catalog, filter, tag, limit, page, count, max)
193
194 # The tag to retrieve
195 var tag: nullable String = null is optional, writable
196
197 redef fun init_command do
198 var tag = self.tag
199 if tag == null then return new ErrorNoTag
200
201 if not catalog.tag2proj.has_key(tag) then return new ErrorTagNotFound(tag)
202 return super
203 end
204
205 redef fun init_results do
206 if results != null then return new CmdSuccess
207
208 var res = super
209 if not res isa CmdSuccess then return res
210
211 results = catalog.tag2proj[tag].to_a
212 return res
213 end
214 end
215
216 # No tag name provided
217 class ErrorNoTag
218 super CmdError
219
220 redef fun to_s do return "No tag name provided"
221 end
222
223 # No tag with this name in the catalog
224 class ErrorTagNotFound
225 super CmdError
226
227 # The tag that was not found
228 var tag: String
229
230 redef fun to_s do return "No tag found for `{tag}`"
231 end
232
233 # Retrieve a person from the catalog
234 class CmdCatalogPerson
235 super CmdCatalog
236
237 # Person to retrieve
238 #
239 # You can also pass a `person_name`.
240 var person: nullable Person = null is optional, writable
241
242 # Name of the person to retrieve
243 #
244 # You can also pass a `person` instance.
245 var person_name: nullable String = null is optional, writable
246
247 # Initialize the `person` result
248 fun init_person: CmdMessage do
249 var person = self.person
250 if person != null then
251 person_name = person.name
252 return new CmdSuccess
253 end
254
255 var name = self.person_name
256 if name == null then return new ErrorNoPerson
257 if not catalog.name2person.has_key(name) then return new ErrorPersonNotFound(name)
258 self.person = catalog.name2person[name]
259 return new CmdSuccess
260 end
261
262 redef fun init_command do
263 init_person
264 return super
265 end
266 end
267
268 # No person instance or name provided
269 class ErrorNoPerson
270 super CmdError
271
272 redef fun to_s do return "No person provided"
273 end
274
275 # No person found with this name
276 class ErrorPersonNotFound
277 super CmdError
278
279 # Name of the person that was not found
280 var name: String
281
282 redef fun to_s do return "No person found for `{name}`"
283 end
284
285 # Retrieve the packages maintained by a person
286 class CmdCatalogMaintaining
287 super CmdCatalogPerson
288 super CmdCatalogPackages
289
290 autoinit(model, catalog, filter, person, person_name, limit, page, count, max)
291
292 redef fun init_command do return super
293
294 redef fun init_results do
295 if results != null then return new CmdSuccess
296 var res = super
297 if not res isa CmdSuccess then return res
298 var person = self.person.as(not null)
299
300 if not catalog.maint2proj.has_key(person) then return res
301 results = catalog.maint2proj[person]
302 return res
303 end
304 end
305
306 # Retrieve the packages contributed by a person
307 class CmdCatalogContributing
308 super CmdCatalogPerson
309 super CmdCatalogPackages
310
311 autoinit(model, catalog, filter, person, person_name, limit, page, count, max)
312
313 # Include maintained packages?
314 #
315 # Default is `false`.
316 var maintaining = false is optional, writable
317
318 # FIXME linearization
319 redef fun init_command do return super
320
321 redef fun init_results do
322 if results != null then return new CmdSuccess
323
324 var res = super
325 if not res isa CmdSuccess then return res
326 var person = self.person.as(not null)
327
328 if not catalog.contrib2proj.has_key(person) then return res
329
330 var maint2proj = null
331 if catalog.maint2proj.has_key(person) then
332 maint2proj = catalog.maint2proj[person]
333 end
334
335 var results = new Array[MPackage]
336 for mpackage in catalog.contrib2proj[person] do
337 if not maintaining and maint2proj != null and maint2proj.has(mpackage) then continue
338 results.add mpackage
339 end
340 self.results = results
341 return res
342 end
343 end