9 public class Introspect.El : Object
23 public class Gtk : Palete {
25 public Gee.ArrayList<string> package_cache;
34 var context = new Vala.CodeContext ();
36 this.package_cache = this.loadPackages(Path.get_dirname (context.get_vapi_path("glib-2.0")));
38 // various loader methods..
42 //this.comments = { };
46 public override void load () {
48 this.loadUsageFile(BuilderApplication.configDirectory() + "/resources/GtkUsage.txt");
53 public string doc(string what) {
54 var ns = what.split(".")[0];
55 var gir = Gir.factory(ns);
58 //return typeof(this.comments[ns][what]) == 'undefined' ? '' : this.comments[ns][what];
61 // does not handle implements...
62 public override GirObject? getClass(string ename)
65 var es = ename.split(".");
66 var gir = Gir.factory(es[0]);
68 return gir.classes.get(es[1]);
72 public override Gee.HashMap<string,GirObject> getPropertiesFor(string ename, string type)
74 //print("Loading for " + ename);
78 // if (typeof(this.proplist[ename]) != 'undefined') {
79 //print("using cache");
80 // return this.proplist[ename][type];
82 // use introspection to get lists..
84 var es = ename.split(".");
85 var gir = Gir.factory(es[0]);
87 var cls = gir.classes.get(es[1]);
89 var ret = new Gee.HashMap<string,GirObject>();
91 //throw new Error.INVALID_VALUE( "Could not find class: " + ename);
96 //cls.parseSignals(); // ?? needed for add handler..
97 //cls.parseMethods(); // ?? needed for ??..
98 //cls.parseConstructors(); // ?? needed for ??..
112 throw new Error.INVALID_VALUE( "getPropertiesFor called with: " + type);
113 //var ret = new Gee.HashMap<string,GirObject>();
119 //cls.overlayInterfaces(gir);
124 public string[] getInheritsFor(string ename)
128 var cls = Gir.factoryFqn(ename);
130 if (cls == null || cls.nodetype != "Class") {
131 print("getInheritsFor:could not find cls: %s\n", ename);
135 return cls.inheritsToStringArray();
140 public override void fillPack(JsRender.Node node,JsRender.Node parent)
143 string inherits = string.joinv(" ",
144 this.getInheritsFor (node.fqn())) + " ";
145 inherits += node.fqn() + " ";
146 //print ("fillPack:Inherits : %s\n", inherits);
147 // parent.fqn() method ( node.fqn()
148 var methods = this.getPropertiesFor (parent.fqn(), "methods");
150 var res = new Gee.HashMap<string,string>();
151 var map = methods.map_iterator();
154 var n = map.get_key();
155 //print ("fillPack:checking method %s\n", n);
157 var meth = map.get_value();
158 if (meth.paramset == null || meth.paramset.params.size < 1) {
159 print ("fillPack:c -- no params\n");
163 var fp = meth.paramset.params.get(0);
165 var type = Gir.fqtypeLookup(fp.type, meth.ns);
166 print ("fillPack:first param type is %s\n", type);
169 if (!inherits.contains(" " + type + " ")) {
174 var pack = meth.name;
175 for(var i =1; i < meth.paramset.params.size; i++) {
176 var ty = Gir.fqtypeLookup(meth.paramset.params.get(i).type, meth.ns);
177 pack += "," + Gir.guessDefaultValueForType(ty);
180 print ("fillPack:add pack: -- %s\n",pack );
182 res.set(meth.name, pack);
190 if (res.has_key("pack_start")) {
191 node.props.set("* pack", res.get("pack_start"));
194 if (res.has_key("add")) {
195 node.props.set("* pack", res.get("add"));
198 var riter = res.map_iterator();
199 while(riter.next()) {
200 node.props.set("* pack", riter.get_value());
206 public Gee.ArrayList<string> packages(Project.Gtk gproject)
208 var vapidirs = gproject.vapidirs();
209 var ret = new Gee.ArrayList<string>();
210 ret.add_all(this.package_cache);
211 for(var i = 0; i < vapidirs.length;i++) {
212 var add = this.loadPackages(vapidirs[i]);
213 for (var j=0; j < add.size; j++) {
214 if (ret.contains(add.get(j))) {
225 public Gee.ArrayList<string> loadPackages(string dirname)
228 var ret = new Gee.ArrayList<string>();
229 //this.package_cache = new Gee.ArrayList<string>();
231 if (!GLib.FileUtils.test(dirname, FileTest.IS_DIR)) {
232 print("opps package directory %s does not exist", dirname);
236 var dir = File.new_for_path(dirname);
240 var file_enum = dir.enumerate_children(
241 GLib.FileAttribute.STANDARD_DISPLAY_NAME,
242 GLib.FileQueryInfoFlags.NONE,
248 while ((next_file = file_enum.next_file(null)) != null) {
249 var fn = next_file.get_display_name();
250 if (!Regex.match_simple("\\.vapi$", fn)) {
253 ret.add(Path.get_basename(fn).replace(".vapi", ""));
256 print("oops - something went wrong scanning the packages\n");
262 public override bool typeOptions(string fqn, string key, string type, out string[] opts)
265 print("get typeOptions %s (%s)%s", fqn, type, key);
266 if (type.up() == "BOOL" || type.up() == "BOOLEAN") {
267 opts = { "true", "false" };
270 var gir= Gir.factoryFqn(type) ;
272 print("could not find Gir data for %s\n", key);
275 print ("Got type %s", gir.asJSONString());
276 if (gir.nodetype != "Enum") {
280 var iter = gir.consts.map_iterator();
283 ret += (type + "." + iter.get_value().name);
286 if (ret.length > 0) {
296 public override List<SourceCompletionItem> suggestComplete(
297 JsRender.JsRender file,
301 string complete_string
304 var ret = new List<SourceCompletionItem>();
305 // completion rules??
307 // make sure data is loaded
312 // this. (based on the node type)
313 // this.xxx // Node and any determination...
315 if (complete_string.index_of(".",0) < 0) {
316 // string does not have a '.'
317 // offer up vala keywords... / _this .. / look for var string = .. in the code..
319 var max = (int)Vala.TokenType.YIELD +1;
320 for (var i =0; i < max;i++) {
321 var m = (Vala.TokenType)i;
322 var s = m.to_string();
323 var ss = s.slice(1,-1);
324 if (s[0] == '`' && GLib.Regex.match_simple("^[a-z]+$", ss) &&
325 complete_string != ss && ss.index_of(complete_string,0) == 0 ) {
326 ret.append(new SourceCompletionItem (ss, ss, null, "vala : " + ss));
329 var miter = Gir.cache.map_iterator();
330 while (miter.next()) {
331 var ss = miter.get_key();
333 if (complete_string != ss && ss.index_of(complete_string,0) == 0 ) {
334 ret.append(new SourceCompletionItem (ss, ss, null, "vala namespace : " + ss));
339 if (complete_string != "_this" && "_this".index_of(complete_string,0) == 0 ) { // should we ignore exact matches... ???
340 ret.append(new SourceCompletionItem ("_this - the top level element", "_this", null, "Top level element"));
348 // got at least one ".".
349 var parts = complete_string.split(".");
351 var cur_instance = false;
352 if (parts[0] == "this") {
353 // work out from the node, what the type is...
355 print("node is empty - no return\n");
356 return ret; // no idea..
358 curtype = "*" + node.fqn();
361 if (Gir.cache.get(parts[0]) == null) {
366 // all Gtk.... etc.. types...
369 //if (parts[0] == "Roo") {
371 // cur_instance = false;
374 var prevbits = parts[0] + ".";
375 for(var i =1; i < parts.length; i++) {
376 print("matching %d/%d\n", i, parts.length);
377 var is_last = i == parts.length -1;
381 // look up all the properties of the type...
382 var cls = Gir.factoryFqn(curtype);
383 if (cls == null && curtype[0] != '*') {
384 print("could not get class of curtype %s\n", curtype);
390 if (curtype[0] == '*' && parts[i] == "el") {
391 curtype = curtype.substring(1);
392 prevbits += parts[i] + ".";
396 // only exact matches from here on...
401 if (cls.props.has_key(parts[i])) {
402 var prop = cls.props.get(parts[i]);
403 if (prop.type.index_of(".",0) > -1) {
404 // type is another roo object..
406 prevbits += parts[i] + ".";
414 // check methods?? - we do not export that at present..
415 return ret; //no idea...
417 var look = prevbits + parts[i];
418 var scls = Gir.factoryFqn(look);
423 prevbits += parts[i] + ".";
428 // got to the last element..
429 print("Got last element\n");
430 if (curtype == "") { // should not happen.. we would have returned already..
433 print("Got last element type %s\n",curtype);
435 print("matching instance");
436 // it's a static reference..
437 var citer = cls.classes.map_iterator();
438 while (citer.next()) {
439 var scls = citer.get_key();
441 if (parts[i].length > 0 && scls.index_of(parts[i],0) != 0) {
444 // got a starting match..
445 ret.append(new SourceCompletionItem (
452 citer = cls.methods.map_iterator();
453 while (citer.next()) {
454 var scls = citer.get_key();
456 if (parts[i].length > 0 && scls.index_of(parts[i],0) != 0) {
459 // got a starting match..
460 ret.append(new SourceCompletionItem (
461 prevbits + scls + citer.get_value().sig ,
468 citer = cls.consts.map_iterator();
469 while (citer.next()) {
470 var scls = citer.get_key();
472 if (parts[i].length > 0 && scls.index_of(parts[i],0) != 0) {
475 // got a starting match..
476 ret.append(new SourceCompletionItem (
477 prevbits + scls + citer.get_value().sig ,
486 print("matching property");
492 var citer = cls.methods.map_iterator();
493 while (citer.next()) {
494 var prop = citer.get_value();
495 // does the name start with ...
496 if (parts[i].length > 0 && prop.name.index_of(parts[i],0) != 0) {
499 // got a matching property...
501 ret.append(new SourceCompletionItem (
502 prop.name + prop.sig + " : ("+ prop.propertyof + ")",
503 prevbits + prop.name + "(",
508 // get the properties / methods and subclasses.. of cls..
509 // we have cls.. - see if the string matches any of the properties..
510 citer = cls.props.map_iterator();
511 while (citer.next()) {
512 var prop = citer.get_value();
513 // does the name start with ...
514 if (parts[i].length > 0 && prop.name.index_of(parts[i],0) != 0) {
517 // got a matching property...
519 ret.append(new SourceCompletionItem (
520 prop.name + " : " + prop.type + " ("+ prop.propertyof + ")",
521 prevbits + prop.name,