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>();
232 var dir = File.new_for_path(dirname);
234 var file_enum = dir.enumerate_children(
235 GLib.FileAttribute.STANDARD_DISPLAY_NAME,
236 GLib.FileQueryInfoFlags.NONE,
242 while ((next_file = file_enum.next_file(null)) != null) {
243 var fn = next_file.get_display_name();
244 if (!Regex.match_simple("\\.vapi$", fn)) {
247 ret.add(Path.get_basename(fn).replace(".vapi", ""));
250 print("oops - something went wrong scanning the packages\n");
256 public override bool typeOptions(string fqn, string key, string type, out string[] opts)
259 print("get typeOptions %s (%s)%s", fqn, type, key);
260 if (type.up() == "BOOL" || type.up() == "BOOLEAN") {
261 opts = { "true", "false" };
264 var gir= Gir.factoryFqn(type) ;
266 print("could not find Gir data for %s\n", key);
269 print ("Got type %s", gir.asJSONString());
270 if (gir.nodetype != "Enum") {
274 var iter = gir.consts.map_iterator();
277 ret += (type + "." + iter.get_value().name);
280 if (ret.length > 0) {
290 public override List<SourceCompletionItem> suggestComplete(
291 JsRender.JsRender file,
295 string complete_string
298 var ret = new List<SourceCompletionItem>();
299 // completion rules??
301 // make sure data is loaded
306 // this. (based on the node type)
307 // this.xxx // Node and any determination...
309 if (complete_string.index_of(".",0) < 0) {
310 // string does not have a '.'
311 // offer up vala keywords... / _this .. / look for var string = .. in the code..
313 var max = (int)Vala.TokenType.YIELD +1;
314 for (var i =0; i < max;i++) {
315 var m = (Vala.TokenType)i;
316 var s = m.to_string();
317 var ss = s.slice(1,-1);
318 if (s[0] == '`' && GLib.Regex.match_simple("^[a-z]+$", ss) &&
319 complete_string != ss && ss.index_of(complete_string,0) == 0 ) {
320 ret.append(new SourceCompletionItem (ss, ss, null, "vala : " + ss));
323 var miter = Gir.cache.map_iterator();
324 while (miter.next()) {
325 var ss = miter.get_key();
327 if (complete_string != ss && ss.index_of(complete_string,0) == 0 ) {
328 ret.append(new SourceCompletionItem (ss, ss, null, "vala namespace : " + ss));
333 if (complete_string != "_this" && "_this".index_of(complete_string,0) == 0 ) { // should we ignore exact matches... ???
334 ret.append(new SourceCompletionItem ("_this - the top level element", "_this", null, "Top level element"));
345 // got at least one ".".
346 var parts = complete_string.split(".");
348 var cur_instance = false;
349 if (parts[0] == "this") {
350 // work out from the node, what the type is...
352 print("node is empty - no return\n");
353 return ret; // no idea..
355 curtype = "*" + node.fqn();
358 // all Gtk.... etc.. types...
361 //if (parts[0] == "Roo") {
363 // cur_instance = false;
366 var prevbits = parts[0] + ".";
367 for(var i =1; i < parts.length; i++) {
368 print("matching %d/%d\n", i, parts.length);
369 var is_last = i == parts.length -1;
371 // look up all the properties of the type...
372 var cls = Gir.factoryFqn(curtype);
373 if (cls == null && curtype[0] != '*') {
374 print("could not get class of curtype %s\n", curtype);
380 if (curtype[0] == '*' && parts[i] == "el") {
381 curtype = curtype.substring(1);
382 prevbits += parts[i] + ".";
386 // only exact matches from here on...
388 if (cls.props.has_key(parts[i])) {
389 var prop = cls.props.get(parts[i]);
390 if (prop.type.index_of(".",0) > -1) {
391 // type is another roo object..
393 prevbits += parts[i] + ".";
401 // check methods?? - we do not export that at present..
402 return ret; //no idea...
406 //look for child classes.
407 var citer = cls.classes.map_iterator();
409 while (citer.next()) {
410 var scls = citer.get_key();
411 print("checking against class %s\n",scls);
412 var look = prevbits + parts[i];
413 if (scls.index_of(look,0) != 0) {
416 // got a starting match..
418 cur_instance = false;
425 prevbits += parts[i] + ".";
430 // got to the last element..
431 print("Got last element\n");
432 if (curtype == "") { // should not happen.. we would have returned already..
435 print("Got last element type %s\n",curtype);
437 print("matching instance");
438 // it's a static reference..
439 var citer = this.classes.map_iterator();
440 while (citer.next()) {
441 var scls = citer.get_key();
442 var look = prevbits + parts[i];
443 if (parts[i].length > 0 && scls.index_of(look,0) != 0) {
446 // got a starting match..
447 ret.append(new SourceCompletionItem (
455 print("matching property");
459 var citer = cls.methods.map_iterator();
460 while (citer.next()) {
461 var prop = citer.get_value();
462 // does the name start with ...
463 if (parts[i].length > 0 && prop.name.index_of(parts[i],0) != 0) {
466 // got a matching property...
468 ret.append(new SourceCompletionItem (
469 prop.name + prop.sig + " : ("+ prop.propertyof + ")",
470 prevbits + prop.name + "(",
475 // get the properties / methods and subclasses.. of cls..
476 // we have cls.. - see if the string matches any of the properties..
477 citer = cls.props.map_iterator();
478 while (citer.next()) {
479 var prop = citer.get_value();
480 // does the name start with ...
481 if (parts[i].length > 0 && prop.name.index_of(parts[i],0) != 0) {
484 // got a matching property...
486 ret.append(new SourceCompletionItem (
487 prop.name + " : " + prop.type + " ("+ prop.propertyof + ")",
488 prevbits + prop.name,