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")));
37 this.package_cache.add_all(
38 this.loadPackages(Path.get_dirname (context.get_vapi_path("gee-1.0")));
41 // various loader methods..
45 //this.comments = { };
49 public override void load () {
51 this.loadUsageFile(BuilderApplication.configDirectory() + "/resources/GtkUsage.txt");
56 public string doc(string what) {
57 var ns = what.split(".")[0];
58 var gir = Gir.factory(ns);
61 //return typeof(this.comments[ns][what]) == 'undefined' ? '' : this.comments[ns][what];
64 // does not handle implements...
65 public override GirObject? getClass(string ename)
68 var es = ename.split(".");
69 var gir = Gir.factory(es[0]);
71 return gir.classes.get(es[1]);
75 public override Gee.HashMap<string,GirObject> getPropertiesFor(string ename, string type)
77 //print("Loading for " + ename);
81 // if (typeof(this.proplist[ename]) != 'undefined') {
82 //print("using cache");
83 // return this.proplist[ename][type];
85 // use introspection to get lists..
87 var es = ename.split(".");
88 var gir = Gir.factory(es[0]);
90 var cls = gir.classes.get(es[1]);
92 var ret = new Gee.HashMap<string,GirObject>();
94 //throw new Error.INVALID_VALUE( "Could not find class: " + ename);
99 //cls.parseSignals(); // ?? needed for add handler..
100 //cls.parseMethods(); // ?? needed for ??..
101 //cls.parseConstructors(); // ?? needed for ??..
115 throw new Error.INVALID_VALUE( "getPropertiesFor called with: " + type);
116 //var ret = new Gee.HashMap<string,GirObject>();
122 //cls.overlayInterfaces(gir);
127 public string[] getInheritsFor(string ename)
131 var cls = Gir.factoryFqn(ename);
133 if (cls == null || cls.nodetype != "Class") {
134 print("getInheritsFor:could not find cls: %s\n", ename);
138 return cls.inheritsToStringArray();
143 public override void fillPack(JsRender.Node node,JsRender.Node parent)
146 string inherits = string.joinv(" ",
147 this.getInheritsFor (node.fqn())) + " ";
148 inherits += node.fqn() + " ";
149 //print ("fillPack:Inherits : %s\n", inherits);
150 // parent.fqn() method ( node.fqn()
151 var methods = this.getPropertiesFor (parent.fqn(), "methods");
153 var res = new Gee.HashMap<string,string>();
154 var map = methods.map_iterator();
157 var n = map.get_key();
158 //print ("fillPack:checking method %s\n", n);
160 var meth = map.get_value();
161 if (meth.paramset == null || meth.paramset.params.size < 1) {
162 print ("fillPack:c -- no params\n");
166 var fp = meth.paramset.params.get(0);
168 var type = Gir.fqtypeLookup(fp.type, meth.ns);
169 print ("fillPack:first param type is %s\n", type);
172 if (!inherits.contains(" " + type + " ")) {
177 var pack = meth.name;
178 for(var i =1; i < meth.paramset.params.size; i++) {
179 var ty = Gir.fqtypeLookup(meth.paramset.params.get(i).type, meth.ns);
180 pack += "," + Gir.guessDefaultValueForType(ty);
183 print ("fillPack:add pack: -- %s\n",pack );
185 res.set(meth.name, pack);
193 if (res.has_key("pack_start")) {
194 node.props.set("* pack", res.get("pack_start"));
197 if (res.has_key("add")) {
198 node.props.set("* pack", res.get("add"));
201 var riter = res.map_iterator();
202 while(riter.next()) {
203 node.props.set("* pack", riter.get_value());
209 public Gee.ArrayList<string> packages(Project.Gtk gproject)
211 var vapidirs = gproject.vapidirs();
212 var ret = new Gee.ArrayList<string>();
213 ret.add_all(this.package_cache);
214 for(var i = 0; i < vapidirs.length;i++) {
215 var add = this.loadPackages(vapidirs[i]);
216 for (var j=0; j < add.size; j++) {
217 if (ret.contains(add.get(j))) {
228 public Gee.ArrayList<string> loadPackages(string dirname)
231 var ret = new Gee.ArrayList<string>();
232 //this.package_cache = new Gee.ArrayList<string>();
234 if (!GLib.FileUtils.test(dirname, FileTest.IS_DIR)) {
235 print("opps package directory %s does not exist", dirname);
239 var dir = File.new_for_path(dirname);
243 var file_enum = dir.enumerate_children(
244 GLib.FileAttribute.STANDARD_DISPLAY_NAME,
245 GLib.FileQueryInfoFlags.NONE,
251 while ((next_file = file_enum.next_file(null)) != null) {
252 var fn = next_file.get_display_name();
253 if (!Regex.match_simple("\\.vapi$", fn)) {
256 ret.add(Path.get_basename(fn).replace(".vapi", ""));
259 print("oops - something went wrong scanning the packages\n");
265 public override bool typeOptions(string fqn, string key, string type, out string[] opts)
268 print("get typeOptions %s (%s)%s", fqn, type, key);
269 if (type.up() == "BOOL" || type.up() == "BOOLEAN") {
270 opts = { "true", "false" };
273 var gir= Gir.factoryFqn(type) ;
275 print("could not find Gir data for %s\n", key);
278 print ("Got type %s", gir.asJSONString());
279 if (gir.nodetype != "Enum") {
283 var iter = gir.consts.map_iterator();
286 ret += (type + "." + iter.get_value().name);
289 if (ret.length > 0) {
299 public override List<SourceCompletionItem> suggestComplete(
300 JsRender.JsRender file,
304 string complete_string
307 var ret = new List<SourceCompletionItem>();
308 // completion rules??
310 // make sure data is loaded
315 // this. (based on the node type)
316 // this.xxx // Node and any determination...
318 if (complete_string.index_of(".",0) < 0) {
319 // string does not have a '.'
320 // offer up vala keywords... / _this .. / look for var string = .. in the code..
322 var max = (int)Vala.TokenType.YIELD +1;
323 for (var i =0; i < max;i++) {
324 var m = (Vala.TokenType)i;
325 var s = m.to_string();
326 var ss = s.slice(1,-1);
327 if (s[0] == '`' && GLib.Regex.match_simple("^[a-z]+$", ss) &&
328 complete_string != ss && ss.index_of(complete_string,0) == 0 ) {
329 ret.append(new SourceCompletionItem (ss, ss, null, "vala : " + ss));
332 var miter = Gir.cache.map_iterator();
333 while (miter.next()) {
334 var ss = miter.get_key();
336 if (complete_string != ss && ss.index_of(complete_string,0) == 0 ) {
337 ret.append(new SourceCompletionItem (ss, ss, null, "vala namespace : " + ss));
342 if (complete_string != "_this" && "_this".index_of(complete_string,0) == 0 ) { // should we ignore exact matches... ???
343 ret.append(new SourceCompletionItem ("_this - the top level element", "_this", null, "Top level element"));
351 // got at least one ".".
352 var parts = complete_string.split(".");
354 var cur_instance = false;
355 if (parts[0] == "this") {
356 // work out from the node, what the type is...
358 print("node is empty - no return\n");
359 return ret; // no idea..
361 curtype = "*" + node.fqn();
364 if (Gir.cache.get(parts[0]) == null) {
369 // all Gtk.... etc.. types...
372 //if (parts[0] == "Roo") {
374 // cur_instance = false;
377 var prevbits = parts[0] + ".";
378 for(var i =1; i < parts.length; i++) {
379 print("matching %d/%d\n", i, parts.length);
380 var is_last = i == parts.length -1;
384 // look up all the properties of the type...
385 var cls = Gir.factoryFqn(curtype);
386 if (cls == null && curtype[0] != '*') {
387 print("could not get class of curtype %s\n", curtype);
393 if (curtype[0] == '*' && parts[i] == "el") {
394 curtype = curtype.substring(1);
395 prevbits += parts[i] + ".";
399 // only exact matches from here on...
404 if (cls.props.has_key(parts[i])) {
405 var prop = cls.props.get(parts[i]);
406 if (prop.type.index_of(".",0) > -1) {
407 // type is another roo object..
409 prevbits += parts[i] + ".";
417 // check methods?? - we do not export that at present..
418 return ret; //no idea...
420 var look = prevbits + parts[i];
421 var scls = Gir.factoryFqn(look);
426 prevbits += parts[i] + ".";
431 // got to the last element..
432 print("Got last element\n");
433 if (curtype == "") { // should not happen.. we would have returned already..
436 print("Got last element type %s\n",curtype);
438 print("matching instance");
439 // it's a static reference..
440 var citer = cls.classes.map_iterator();
441 while (citer.next()) {
442 var scls = citer.get_key();
444 if (parts[i].length > 0 && scls.index_of(parts[i],0) != 0) {
447 // got a starting match..
448 ret.append(new SourceCompletionItem (
455 citer = cls.methods.map_iterator();
456 while (citer.next()) {
457 var scls = citer.get_key();
459 if (parts[i].length > 0 && scls.index_of(parts[i],0) != 0) {
462 // got a starting match..
463 ret.append(new SourceCompletionItem (
464 prevbits + scls + citer.get_value().sig ,
471 citer = cls.consts.map_iterator();
472 while (citer.next()) {
473 var scls = citer.get_key();
475 if (parts[i].length > 0 && scls.index_of(parts[i],0) != 0) {
478 // got a starting match..
479 ret.append(new SourceCompletionItem (
480 prevbits + scls + citer.get_value().sig ,
489 print("matching property");
495 var citer = cls.methods.map_iterator();
496 while (citer.next()) {
497 var prop = citer.get_value();
498 // does the name start with ...
499 if (parts[i].length > 0 && prop.name.index_of(parts[i],0) != 0) {
502 // got a matching property...
504 ret.append(new SourceCompletionItem (
505 prop.name + prop.sig + " : ("+ prop.propertyof + ")",
506 prevbits + prop.name + "(",
511 // get the properties / methods and subclasses.. of cls..
512 // we have cls.. - see if the string matches any of the properties..
513 citer = cls.props.map_iterator();
514 while (citer.next()) {
515 var prop = citer.get_value();
516 // does the name start with ...
517 if (parts[i].length > 0 && prop.name.index_of(parts[i],0) != 0) {
520 // got a matching property...
522 ret.append(new SourceCompletionItem (
523 prop.name + " : " + prop.type + " ("+ prop.propertyof + ")",
524 prevbits + prop.name,