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
89 this._version = value;
96 public string permname;
97 public string language;
98 public string content_type;
99 public string modOrder;
101 public uint64 webkit_page_id; // set by webkit view - used to extract extension/etc..
102 public bool gen_extended = false; // nodetovala??
104 public Project.Project project;
108 public string build_module; // module to build if we compile (or are running tests...)
110 //Project : false, // link to container project!
112 public Node tree; // the tree of nodes.
114 //public GLib.List<JsRender> cn; // child files.. (used by project ... should move code here..)
116 public bool hasParent;
120 public Gee.HashMap<string,string> transStrings; // map of md5 -> string.
121 public Gee.HashMap<string,string> namedStrings;
123 public Gee.HashMap<int,string> undo_json;
125 //public Gee.HashMap<string, GLib.ListStore> errorsByType;
126 private Gee.ArrayList<Lsp.Diagnostic> errors;
127 public int error_counter {
128 get; private set; default = 0;
131 //public signal void changed (Node? node, string source); (not used?)
134 public signal void compile_notice(string type, string file, int line, string message);
136 private GLib.Icon? _icon = null;
138 public GLib.Icon? icon {
141 if (this._icon != null) {
145 if (this.path == "") {
148 if (!GLib.FileUtils.test(this.path, GLib.FileTest.EXISTS)) {
152 this._icon = File.new_for_path(this.path).query_info("standard::icon",GLib.FileQueryInfoFlags.NONE).get_icon();
153 } catch(GLib.Error e) {
165 //public Xcls_Editor editor;
166 public GLib.ListStore childfiles; // used by directories..
169 //abstract JsRender(Project.Project project, string path);
171 protected JsRender(Project.Project project, string path)
174 //this.cn = new GLib.List<JsRender>();
175 GLib.debug("new jsrender %s", path);
177 this.project = project;
178 this.hasParent = false;
186 this.content_type = "";
187 this.build_module = "";
188 //this.loaded = false;
189 //print("JsRender.cto() - reset transStrings\n");
190 this.transStrings = new Gee.HashMap<string,string>();
191 this.namedStrings = new Gee.HashMap<string,string>();
192 // should use basename reallly...
194 var ar = this.path.split("/");
195 // name is in theory filename without .bjs (or .js eventually...)
197 Regex regex = new Regex ("\\.(bjs)$");
199 this.name = ar.length > 0 ? regex.replace(ar[ar.length-1],ar[ar.length-1].length, 0 , "") : "";
200 } catch (GLib.Error e) {
203 this.fullname = (this.parent.length > 0 ? (this.parent + ".") : "" ) + this.name;
205 this.doubleStringProps = new Gee.ArrayList<string>();
206 this.childfiles = new GLib.ListStore(typeof(JsRender));
207 //this.errorsByType = new Gee.HashMap<string, GLib.ListStore>();
208 this.errors = new Gee.ArrayList<Lsp.Diagnostic>((a,b) => { return a.equals(b); });
209 this.undo_json = new Gee.HashMap<int,string>();
214 public void renameTo(string name) throws Error
216 if (this.xtype == "PlainFile") {
219 var bjs = GLib.Path.get_dirname(this.path) +"/" + name + ".bjs";
220 if (FileUtils.test(bjs, FileTest.EXISTS)) {
221 throw new Error.RENAME_FILE_EXISTS("File exists %s\n",name);
223 GLib.FileUtils.remove(this.path);
225 // remove other files?
234 // not sure why xt is needed... -> project contains xtype..
236 public static JsRender factory(string xt, Project.Project project, string path) throws Error
241 return new Gtk(project, path);
244 return new Roo((Project.Roo) project, path);
246 // return new Flutter(project, path);
248 return new PlainFile(project, path);
250 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
256 public string nickType()
258 var ar = this.name.split(".");
260 for (var i =0; i < ar.length -1; i++) {
263 return string.joinv(".", ret);
266 public string nickName()
268 var ar = this.name.split(".");
269 return ar[ar.length-1];
273 public string nickNameSplit()
275 var n = this.nickName();
278 for (var i = 0; i < n.length; i++) {
279 if (i!=0 && n.get(i).isupper() && len > 10) {
283 ret += n.get(i).to_string();
293 Gdk.Pixbuf screenshot = null;
294 Gdk.Pixbuf screenshot92 = null;
295 Gdk.Pixbuf screenshot368 = null;
297 public Gdk.Pixbuf? getIcon(int size = 0) {
298 var fname = this.getIconFileName( );
299 if (!FileUtils.test(fname, FileTest.EXISTS)) {
300 GLib.debug("PIXBUF %s: %s does not exist?", this.name, fname);
306 if (this.screenshot == null) {
308 this.screenshot = new Gdk.Pixbuf.from_file(fname);
309 } catch (GLib.Error e) {}
311 return this.screenshot;
315 if (this.screenshot == null) {
317 if (this.screenshot == null) {
322 this.screenshot92 = this.screenshot.scale_simple(92, (int) (this.screenshot.height * 92.0 /this.screenshot.width * 1.0 )
323 , Gdk.InterpType.NEAREST) ;
324 return this.screenshot92;
327 if (this.screenshot == null) {
329 if (this.screenshot == null) {
334 this.screenshot368 = this.screenshot.scale_simple(368, (int) (this.screenshot.height * 368.0 /this.screenshot.width * 1.0 )
335 , Gdk.InterpType.NEAREST) ;
336 return this.screenshot368;
341 public void writeIcon(Gdk.Pixbuf pixbuf) {
343 this.screenshot92 = null;
344 this.screenshot368 = null;
345 this.screenshot = null;
347 GLib.debug("Wirte %s", this.getIconFileName( ));
348 pixbuf.save(this.getIconFileName( ),"png");
349 this.screenshot = pixbuf;
351 } catch (GLib.Error e) {
352 GLib.debug("failed to write pixbuf?");
360 public void widgetToIcon(global::Gtk.Widget widget) {
362 this.screenshot92 = null;
363 this.screenshot368 = null;
364 this.screenshot = null;
369 var filename = this.getIconFileName();
372 var p = new global::Gtk.WidgetPaintable(widget);
373 var s = new global::Gtk.Snapshot();
374 GLib.debug("Width %d, Height %d", widget.get_width(), widget.get_height());
375 p.snapshot(s, widget.get_width(), widget.get_height());
376 var n = s.free_to_node();
380 var r = new Gsk.CairoRenderer();
382 var t = r.render_texture(n,null);
383 GLib.debug("write to %s", filename);
384 t.save_to_png(filename);
388 } catch (GLib.Error e) {
389 GLib.debug("failed to write pixbuf?");
399 public string getIconFileName( )
402 var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path);
404 var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
406 if (!FileUtils.test(dir, FileTest.IS_DIR)) {
407 File.new_for_path(dir).make_directory();
409 } catch (GLib.Error e) {
410 // eakk.. what to do here...
412 var fname = dir + "/" + m5 + ".png";
419 public string toJsonString()
421 if (this.xtype == "PlainFile") {
424 var node = new Json.Node(Json.NodeType.OBJECT);
425 node.set_object(this.toJsonObject());
426 var generator = new JsonGen(node);
427 generator.indent = 1;
428 generator.pretty = true;
431 return generator.to_data();
434 public void saveBJS()
436 // if (!this.loaded) {
437 /// GLib.debug("saveBJS - skip - not loaded?");
440 if (this.xtype == "PlainFile") {
446 GLib.debug("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
448 this.writeFile(this.path, this.toJsonString());
450 } catch(GLib.Error e) {
451 print("Save failed");
455 bool in_undo = false;
456 protected void updateUndo()
461 if (this.xtype == "PlainFile") {
462 // handled by gtk sourceview buffer...
465 //GLib.debug("UNDO store %d", this.version);
466 this.undo_json.set(this.version, this.tree.toJsonString());
467 if (this.undo_json.has_key(this.version+1)) {
468 var n = this.version +1;
469 while (this.undo_json.has_key(n)) {
470 this.undo_json.unset(n++);
477 public bool undoStep(int step = -1) // undo back/next
480 if (!this.undo_json.has_key(this.version + step)) {
481 //GLib.debug("UNDO step %d failed - no version available", this.version + step);
484 var new_version = this.version + step;
485 var pa = new Json.Parser();
486 //GLib.debug("UNDO RESTORE : %d", this.version + step);
488 pa.load_from_data(this.undo_json.get(new_version));
489 } catch (GLib.Error e) {
492 var node = pa.get_root();
494 this.loadTree(node.get_object(),2);
495 this.tree.updated_count = new_version;
496 this.in_undo = false;
500 public string jsonHasOrEmpty(Json.Object obj, string key) {
501 return obj.has_member(key) ?
502 obj.get_string_member(key) : "";
506 public Json.Object toJsonObject ()
510 var ret = new Json.Object();
511 if (this.xtype == "PlainFile") {
515 //ret.set_string_member("id", this.id); // not relivant..
516 ret.set_string_member("name", this.name);
518 if (this.project.xtype == "Roo") {
519 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
520 ret.set_string_member("title", this.title == null ? "" : this.title);
521 //ret.set_string_member("path", this.path);
522 //ret.set_string_member("items", this.items);
523 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
524 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
526 if (this.project.xtype == "Gtk") {
528 ret.set_string_member("build_module", this.build_module );
530 ret.set_boolean_member("gen_extended", this.gen_extended);
532 if (this.transStrings.size > 0) {
533 var tr = new Json.Object();
534 var iter = this.transStrings.map_iterator();
535 while (iter.next()) {
536 tr.set_string_member(iter.get_value(), iter.get_key());
538 ret.set_object_member("strings", tr);
543 if (this.namedStrings.size > 0) {
544 var tr = new Json.Object();
545 var iter = this.namedStrings.map_iterator();
546 while (iter.next()) {
547 tr.set_string_member(iter.get_key(), iter.get_value());
549 ret.set_object_member("named_strings", tr);
552 var ar = new Json.Array();
553 // empty files do not have a tree.
554 if (this.tree != null) {
555 ar.add_object_element(this.tree.toJsonObject());
557 ret.set_array_member("items", ar);
564 public string getTitle ()
566 if (this.title == null) { // not sure why this happens..
569 if (this.title.length > 0) {
572 var a = this.path.split("/");
573 return a[a.length-1];
575 public string getTitleTip()
577 if (this.title.length > 0) {
578 return "<b>" + this.title + "</b> " + this.path;
585 this.cn.sort(function(a,b) {
586 return a.path > b.path;// ? 1 : -1;
590 // should be in palete provider really..
593 public Palete.Palete palete()
595 // error on plainfile?
596 return this.project.palete;
600 public string guessName(Node ar) // turns the object into full name.
602 // eg. xns: Roo, xtype: XXX -> Roo.xxx
603 if (!ar.hasXnsType()) {
607 return ar.get("xns") + "." + ar.get("xtype");
612 * non-atomic write (replacement for put contents, as it creates temporary files.
614 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
618 var f = GLib.File.new_for_path(path);
619 var data_out = new GLib.DataOutputStream(
620 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
622 data_out.put_string(contents, null);
623 data_out.close(null);
628 public Node? lineToNode(int line)
630 if (this.tree == null) {
633 return this.tree.lineToNode(line);
638 public GLib.ListStore toListStore()
640 var ret = new GLib.ListStore(typeof(Node));
641 ret.append(this.tree);
646 // used to handle list of files in project editor (really Gtk only)
647 public bool compile_group_selected {
649 var gproj = (Project.Gtk) this.project;
651 if (gproj.active_cg == null) {
654 if (this.xtype == "Dir") {
655 // show ticked if all ticked..
657 for(var i = 0; i < this.childfiles.n_items; i++ ) {
658 var f = (JsRender) this.childfiles.get_item(i);
659 if (!f.compile_group_selected) {
668 if (gproj.active_cg.sources == null) {
669 GLib.debug("compile_group_selected - sources is null? ");
673 return gproj.active_cg.sources.contains(this.relpath);
678 var gproj = (Project.Gtk) this.project;
680 if (gproj.active_cg == null) {
683 if (gproj.active_cg.loading_ui) {
687 if (this.xtype == "Dir") {
688 for(var i = 0; i < this.childfiles.n_items; i++ ) {
689 var f = (JsRender) this.childfiles.get_item(i);
690 f.compile_group_selected = value;
699 if (value == false) {
700 GLib.debug("REMOVE %s", this.relpath);
702 gproj.active_cg.sources.remove(this.relpath);
705 if (!gproj.active_cg.sources.contains(this.relpath)) {
706 GLib.debug("ADD %s", this.relpath);
707 gproj.active_cg.sources.add(this.relpath);
713 public bool compile_group_hidden {
715 var gproj = (Project.Gtk) this.project;
718 return gproj.hidden.contains(this.relpath);
723 var gproj = (Project.Gtk) this.project;
725 if (gproj.active_cg == null) {
728 if (gproj.active_cg.loading_ui) {
731 if (value == false) {
732 GLib.debug("REMOVE %s", this.relpath);
734 gproj.hidden.remove(this.relpath);
737 if (!gproj.hidden.contains(this.relpath)) {
738 gproj.hidden.add(this.relpath);
739 // hiding a project will auto clear it.
740 for(var i = 0; i < this.childfiles.n_items; i++ ) {
741 var f = (JsRender) this.childfiles.get_item(i);
742 f.compile_group_selected = false;
753 if (this.xtype == "Dir") {
756 // cleans up build (should really be in children..
757 this.removeFile(this.path);
758 if (this.path.has_suffix(".bjs") && this.project.xtype == "Roo") {
759 this.removeFile(this.path.substring(0, this.path.length-4) + ".js");
762 if (this.path.has_suffix(".bjs") && this.project.xtype == "Gtk") {
763 this.removeFile(this.path.substring(0, this.path.length-4) + ".vala");
764 this.removeFile(this.path.substring(0, this.path.length-4) + ".c");
765 this.removeFile(this.path.substring(0, this.path.length-4) + ".o");
767 if (this.path.has_suffix(".vala") && this.project.xtype == "Gtk") {
768 this.removeFile(this.path.substring(0, this.path.length-5) + ".c");
769 this.removeFile(this.path.substring(0, this.path.length-5) + ".o");
775 private void removeFile(string path)
778 if (GLib.FileUtils.test(path, GLib.FileTest.EXISTS)) {
779 GLib.FileUtils.unlink(path);
783 public string relTargetName()
785 return this.targetName().substring(this.project.path.length +1);
789 public string to_url()
791 return File.new_for_path (this.targetName()).get_uri ();
793 public Palete.LanguageClient? getLanguageServer()
796 return this.project.getLanguageServer(this.language_id());
800 public void updateErrors(Gee.ArrayList<Lsp.Diagnostic> new_errors)
802 var oc = this.error_counter;
803 var skip = new Gee.ArrayList<Lsp.Diagnostic>((a,b) => { return a.equals(b); });
804 var rem = new Gee.ArrayList<Lsp.Diagnostic>((a,b) => { return a.equals(b); });
805 foreach(var old in this.errors) {
806 if (new_errors.contains(old)) {
813 foreach(var old in rem) {
814 this.removeError(old);
816 foreach(var err in new_errors) {
817 if (skip.contains(err)) {
822 if (oc != this.error_counter) {
823 BuilderApplication.updateCompileResults();
830 public Gee.ArrayList<Lsp.Diagnostic> getErrors()
835 private void addError(Lsp.Diagnostic diag)
838 //GLib.debug("ADD Error %s", diag.to_string());
839 this.errors.add(diag);
840 this.project.addError(this, diag);
842 this.error_counter++;
846 public void removeError(Lsp.Diagnostic diag)
848 //GLib.debug("REMOVE Error %s", diag.to_string());
849 this.errors.remove(diag);
850 this.project.removeError(this, diag);
851 this.error_counter++;
853 public int getErrorsTotal(string category)
856 foreach(var diag in this.errors) {
857 if (diag.category == category) {
866 public void loadTree(Json.Object obj, int bjs_version = 2)
868 if (this.xtype == "PlainFile" ){
872 this.tree = new Node();
873 this.tree.loadFromJson(obj,bjs_version);
874 this.tree.version_changed.connect(() => {
882 public abstract string language_id();
883 public abstract void save();
884 public abstract void saveHTML(string html);
885 public abstract string toSource() ;
886 public abstract string toSourceCode() ; // used by commandline tester..
887 public abstract void setSource(string str);
888 public abstract string toSourcePreview() ;
889 public abstract void removeFiles() ;
890 public abstract void findTransStrings(Node? node );
891 public abstract string toGlade();
892 public abstract string targetName();
893 public abstract void loadItems() throws GLib.Error;