1 //<Script type="text/javascript">
6 public errordomain Error {
11 public abstract class JsRender : Object {
13 * @cfg {Array} doubleStringProps list of properties that can be double quoted.
15 public Gee.ArrayList<string> doubleStringProps;
17 public string id = "";
18 public string name { get; set; default = ""; } // is the JS name of the file.
19 public string fullname = "";
20 public string path = ""; // is the full path to the file.
22 public string relpath {
24 return this.project.path == this.path ? "" : this.path.substring(this.project.path.length+1);
28 public string reldir {
30 return this.project.path == this.dir ? "" : this. dir.substring(this.project.path.length+1);
37 return GLib.Path.get_dirname(this.path);
43 public string file_namespace {
45 if (!this.name.contains(".")) {
48 var bits = this.name.split(".");
53 public string file_without_namespace {
55 if (!this.name.contains(".")) {
58 var bits = this.name.split(".");
59 return this.name.substring(bits[0].length +1);
64 public string file_ext {
66 if (!this.path.contains(".")) {
69 var bits = this.name.split(".");
70 return bits[bits.length-1];
74 public string parent = ""; // JS parent.
75 public string region = ""; // RooJS - insert region.
77 public string title = ""; // a title.. ?? nickname.. ??? -
79 private int _version = 1; // should we increment this based on the node..?
82 if (this.tree != null) {
83 return this.tree.updated_count;
85 return ++this._version; // increased on every call? - bit of a kludge until we do real versioning
88 this._version = value;
93 public string permname;
94 public string language;
95 public string content_type;
96 public string modOrder;
98 public uint64 webkit_page_id; // set by webkit view - used to extract extension/etc..
99 public bool gen_extended = false; // nodetovala??
101 public Project.Project project;
105 public string build_module; // module to build if we compile (or are running tests...)
107 //Project : false, // link to container project!
109 public Node tree; // the tree of nodes.
111 //public GLib.List<JsRender> cn; // child files.. (used by project ... should move code here..)
113 public bool hasParent;
117 public Gee.HashMap<string,string> transStrings; // map of md5 -> string.
118 public Gee.HashMap<string,string> namedStrings;
119 //public Gee.HashMap<string, GLib.ListStore> errorsByType;
120 private Gee.ArrayList<Lsp.Diagnostic> errors;
121 public int error_counter {
122 get; private set; default = 0;
125 public signal void changed (Node? node, string source);
128 public signal void compile_notice(string type, string file, int line, string message);
130 private GLib.Icon? _icon = null;
132 public GLib.Icon? icon {
135 if (this._icon != null) {
139 if (this.path == "") {
142 if (!GLib.FileUtils.test(this.path, GLib.FileTest.EXISTS)) {
145 this._icon = File.new_for_path(this.path).query_info("standard::icon",GLib.FileQueryInfoFlags.NONE).get_icon();
154 //public Xcls_Editor editor;
155 public GLib.ListStore childfiles; // used by directories..
158 //abstract JsRender(Project.Project project, string path);
160 protected JsRender(Project.Project project, string path)
163 //this.cn = new GLib.List<JsRender>();
164 GLib.debug("new jsrender %s", path);
166 this.project = project;
167 this.hasParent = false;
175 this.content_type = "";
176 this.build_module = "";
177 //this.loaded = false;
178 //print("JsRender.cto() - reset transStrings\n");
179 this.transStrings = new Gee.HashMap<string,string>();
180 this.namedStrings = new Gee.HashMap<string,string>();
181 // should use basename reallly...
183 var ar = this.path.split("/");
184 // name is in theory filename without .bjs (or .js eventually...)
186 Regex regex = new Regex ("\\.(bjs)$");
188 this.name = ar.length > 0 ? regex.replace(ar[ar.length-1],ar[ar.length-1].length, 0 , "") : "";
189 } catch (GLib.Error e) {
192 this.fullname = (this.parent.length > 0 ? (this.parent + ".") : "" ) + this.name;
194 this.doubleStringProps = new Gee.ArrayList<string>();
195 this.childfiles = new GLib.ListStore(typeof(JsRender));
196 //this.errorsByType = new Gee.HashMap<string, GLib.ListStore>();
197 this.errors = new Gee.ArrayList<Lsp.Diagnostic>((a,b) => { return a.equals(b); });
203 public void renameTo(string name) throws Error
205 if (this.xtype == "PlainFile") {
208 var bjs = GLib.Path.get_dirname(this.path) +"/" + name + ".bjs";
209 if (FileUtils.test(bjs, FileTest.EXISTS)) {
210 throw new Error.RENAME_FILE_EXISTS("File exists %s\n",name);
212 GLib.FileUtils.remove(this.path);
214 // remove other files?
223 // not sure why xt is needed... -> project contains xtype..
225 public static JsRender factory(string xt, Project.Project project, string path) throws Error
230 return new Gtk(project, path);
233 return new Roo((Project.Roo) project, path);
235 // return new Flutter(project, path);
237 return new PlainFile(project, path);
239 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
245 public string nickType()
247 var ar = this.name.split(".");
249 for (var i =0; i < ar.length -1; i++) {
252 return string.joinv(".", ret);
255 public string nickName()
257 var ar = this.name.split(".");
258 return ar[ar.length-1];
262 public string nickNameSplit()
264 var n = this.nickName();
267 for (var i = 0; i < n.length; i++) {
268 if (i!=0 && n.get(i).isupper() && len > 10) {
272 ret += n.get(i).to_string();
282 Gdk.Pixbuf screenshot = null;
283 Gdk.Pixbuf screenshot92 = null;
284 Gdk.Pixbuf screenshot368 = null;
286 public Gdk.Pixbuf? getIcon(int size = 0) {
287 var fname = this.getIconFileName( );
288 if (!FileUtils.test(fname, FileTest.EXISTS)) {
289 GLib.debug("PIXBUF %s: %s does not exist?", this.name, fname);
295 if (this.screenshot == null) {
297 this.screenshot = new Gdk.Pixbuf.from_file(fname);
298 } catch (GLib.Error e) {}
300 return this.screenshot;
304 if (this.screenshot == null) {
306 if (this.screenshot == null) {
311 this.screenshot92 = this.screenshot.scale_simple(92, (int) (this.screenshot.height * 92.0 /this.screenshot.width * 1.0 )
312 , Gdk.InterpType.NEAREST) ;
313 return this.screenshot92;
316 if (this.screenshot == null) {
318 if (this.screenshot == null) {
323 this.screenshot368 = this.screenshot.scale_simple(368, (int) (this.screenshot.height * 368.0 /this.screenshot.width * 1.0 )
324 , Gdk.InterpType.NEAREST) ;
325 return this.screenshot368;
330 public void writeIcon(Gdk.Pixbuf pixbuf) {
332 this.screenshot92 = null;
333 this.screenshot368 = null;
334 this.screenshot = null;
336 GLib.debug("Wirte %s", this.getIconFileName( ));
337 pixbuf.save(this.getIconFileName( ),"png");
338 this.screenshot = pixbuf;
340 } catch (GLib.Error e) {
341 GLib.debug("failed to write pixbuf?");
349 public void widgetToIcon(global::Gtk.Widget widget) {
351 this.screenshot92 = null;
352 this.screenshot368 = null;
353 this.screenshot = null;
358 var filename = this.getIconFileName();
361 var p = new global::Gtk.WidgetPaintable(widget);
362 var s = new global::Gtk.Snapshot();
363 GLib.debug("Width %d, Height %d", widget.get_width(), widget.get_height());
364 p.snapshot(s, widget.get_width(), widget.get_height());
365 var n = s.free_to_node();
369 var r = new Gsk.CairoRenderer();
371 var t = r.render_texture(n,null);
372 GLib.debug("write to %s", filename);
373 t.save_to_png(filename);
377 } catch (GLib.Error e) {
378 GLib.debug("failed to write pixbuf?");
388 public string getIconFileName( )
391 var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path);
393 var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
395 if (!FileUtils.test(dir, FileTest.IS_DIR)) {
396 File.new_for_path(dir).make_directory();
398 } catch (GLib.Error e) {
399 // eakk.. what to do here...
401 var fname = dir + "/" + m5 + ".png";
408 public string toJsonString()
410 if (this.xtype == "PlainFile") {
413 var node = new Json.Node(Json.NodeType.OBJECT);
414 node.set_object(this.toJsonObject());
415 var generator = new JsonGen(node);
416 generator.indent = 1;
417 generator.pretty = true;
420 return generator.to_data();
423 public void saveBJS()
425 // if (!this.loaded) {
426 /// GLib.debug("saveBJS - skip - not loaded?");
429 if (this.xtype == "PlainFile") {
435 GLib.debug("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
437 this.writeFile(this.path, this.toJsonString());
439 } catch(GLib.Error e) {
440 print("Save failed");
451 public string jsonHasOrEmpty(Json.Object obj, string key) {
452 return obj.has_member(key) ?
453 obj.get_string_member(key) : "";
457 public Json.Object toJsonObject ()
461 var ret = new Json.Object();
462 if (this.xtype == "PlainFile") {
466 //ret.set_string_member("id", this.id); // not relivant..
467 ret.set_string_member("name", this.name);
469 if (this.project.xtype == "Roo") {
470 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
471 ret.set_string_member("title", this.title == null ? "" : this.title);
472 //ret.set_string_member("path", this.path);
473 //ret.set_string_member("items", this.items);
474 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
475 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
477 if (this.project.xtype == "Gtk") {
479 ret.set_string_member("build_module", this.build_module );
481 ret.set_boolean_member("gen_extended", this.gen_extended);
483 if (this.transStrings.size > 0) {
484 var tr = new Json.Object();
485 var iter = this.transStrings.map_iterator();
486 while (iter.next()) {
487 tr.set_string_member(iter.get_value(), iter.get_key());
489 ret.set_object_member("strings", tr);
494 if (this.namedStrings.size > 0) {
495 var tr = new Json.Object();
496 var iter = this.namedStrings.map_iterator();
497 while (iter.next()) {
498 tr.set_string_member(iter.get_key(), iter.get_value());
500 ret.set_object_member("named_strings", tr);
503 var ar = new Json.Array();
504 // empty files do not have a tree.
505 if (this.tree != null) {
506 ar.add_object_element(this.tree.toJsonObject());
508 ret.set_array_member("items", ar);
515 public string getTitle ()
517 if (this.title == null) { // not sure why this happens..
520 if (this.title.length > 0) {
523 var a = this.path.split("/");
524 return a[a.length-1];
526 public string getTitleTip()
528 if (this.title.length > 0) {
529 return "<b>" + this.title + "</b> " + this.path;
536 this.cn.sort(function(a,b) {
537 return a.path > b.path;// ? 1 : -1;
541 // should be in palete provider really..
544 public Palete.Palete palete()
546 // error on plainfile?
547 return this.project.palete;
551 public string guessName(Node ar) // turns the object into full name.
553 // eg. xns: Roo, xtype: XXX -> Roo.xxx
554 if (!ar.hasXnsType()) {
558 return ar.get("xns") + "." + ar.get("xtype");
563 * non-atomic write (replacement for put contents, as it creates temporary files.
565 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
569 var f = GLib.File.new_for_path(path);
570 var data_out = new GLib.DataOutputStream(
571 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
573 data_out.put_string(contents, null);
574 data_out.close(null);
579 public Node? lineToNode(int line)
581 if (this.tree == null) {
584 return this.tree.lineToNode(line);
589 public GLib.ListStore toListStore()
591 var ret = new GLib.ListStore(typeof(Node));
592 ret.append(this.tree);
597 // used to handle list of files in project editor (really Gtk only)
598 public bool compile_group_selected {
600 var gproj = (Project.Gtk) this.project;
602 if (gproj.active_cg == null) {
605 if (this.xtype == "Dir") {
606 // show ticked if all ticked..
608 for(var i = 0; i < this.childfiles.n_items; i++ ) {
609 var f = (JsRender) this.childfiles.get_item(i);
610 if (!f.compile_group_selected) {
619 if (gproj.active_cg.sources == null) {
620 GLib.debug("compile_group_selected - sources is null? ");
624 return gproj.active_cg.sources.contains(this.relpath);
629 var gproj = (Project.Gtk) this.project;
631 if (gproj.active_cg == null) {
634 if (gproj.active_cg.loading_ui) {
638 if (this.xtype == "Dir") {
639 for(var i = 0; i < this.childfiles.n_items; i++ ) {
640 var f = (JsRender) this.childfiles.get_item(i);
641 f.compile_group_selected = value;
650 if (value == false) {
651 GLib.debug("REMOVE %s", this.relpath);
653 gproj.active_cg.sources.remove(this.relpath);
656 if (!gproj.active_cg.sources.contains(this.relpath)) {
657 GLib.debug("ADD %s", this.relpath);
658 gproj.active_cg.sources.add(this.relpath);
664 public bool compile_group_hidden {
666 var gproj = (Project.Gtk) this.project;
669 return gproj.hidden.contains(this.relpath);
674 var gproj = (Project.Gtk) this.project;
676 if (gproj.active_cg == null) {
679 if (gproj.active_cg.loading_ui) {
682 if (value == false) {
683 GLib.debug("REMOVE %s", this.relpath);
685 gproj.hidden.remove(this.relpath);
688 if (!gproj.hidden.contains(this.relpath)) {
689 gproj.hidden.add(this.relpath);
690 // hiding a project will auto clear it.
691 for(var i = 0; i < this.childfiles.n_items; i++ ) {
692 var f = (JsRender) this.childfiles.get_item(i);
693 f.compile_group_selected = false;
704 if (this.xtype == "Dir") {
707 // cleans up build (should really be in children..
708 this.removeFile(this.path);
709 if (this.path.has_suffix(".bjs") && this.project.xtype == "Roo") {
710 this.removeFile(this.path.substring(0, this.path.length-4) + ".js");
713 if (this.path.has_suffix(".bjs") && this.project.xtype == "Gtk") {
714 this.removeFile(this.path.substring(0, this.path.length-4) + ".vala");
715 this.removeFile(this.path.substring(0, this.path.length-4) + ".c");
716 this.removeFile(this.path.substring(0, this.path.length-4) + ".o");
718 if (this.path.has_suffix(".vala") && this.project.xtype == "Gtk") {
719 this.removeFile(this.path.substring(0, this.path.length-5) + ".c");
720 this.removeFile(this.path.substring(0, this.path.length-5) + ".o");
726 private void removeFile(string path)
729 if (GLib.FileUtils.test(path, GLib.FileTest.EXISTS)) {
730 GLib.FileUtils.unlink(path);
734 public string relTargetName()
736 return this.targetName().substring(this.project.path.length +1);
740 public string to_url()
742 return File.new_for_path (this.targetName()).get_uri ();
744 public Palete.LanguageClient? getLanguageServer()
747 return this.project.getLanguageServer(this.language_id());
751 public void updateErrors(Gee.ArrayList<Lsp.Diagnostic> new_errors)
753 var oc = this.error_counter;
754 var skip = new Gee.ArrayList<Lsp.Diagnostic>((a,b) => { return a.equals(b); });
755 var rem = new Gee.ArrayList<Lsp.Diagnostic>((a,b) => { return a.equals(b); });
756 foreach(var old in this.errors) {
757 if (new_errors.contains(old)) {
764 foreach(var old in rem) {
765 this.removeError(old);
767 foreach(var err in new_errors) {
768 if (skip.contains(err)) {
773 if (oc != this.error_counter) {
774 BuilderApplication.updateCompileResults();
781 public Gee.ArrayList<Lsp.Diagnostic> getErrors()
786 private void addError(Lsp.Diagnostic diag)
789 GLib.debug("ADD Error %s", diag.to_string());
790 this.errors.add(diag);
791 this.project.addError(this, diag);
793 this.error_counter++;
797 public void removeError(Lsp.Diagnostic diag)
799 GLib.debug("REMOVE Error %s", diag.to_string());
800 this.errors.remove(diag);
801 this.project.removeError(this, diag);
802 this.error_counter++;
804 public int getErrorsTotal(string category)
807 foreach(var diag in this.errors) {
808 if (diag.category == category) {
818 public abstract string language_id();
819 public abstract void save();
820 public abstract void saveHTML(string html);
821 public abstract string toSource() ;
822 public abstract string toSourceCode() ; // used by commandline tester..
823 public abstract void setSource(string str);
824 public abstract string toSourcePreview() ;
825 public abstract void removeFiles() ;
826 public abstract void findTransStrings(Node? node );
827 public abstract string toGlade();
828 public abstract string targetName();
829 public abstract void loadItems() throws GLib.Error;