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)) {
151 this._icon = File.new_for_path(this.path).query_info("standard::icon",GLib.FileQueryInfoFlags.NONE).get_icon();
161 //public Xcls_Editor editor;
162 public GLib.ListStore childfiles; // used by directories..
165 //abstract JsRender(Project.Project project, string path);
167 protected JsRender(Project.Project project, string path)
170 //this.cn = new GLib.List<JsRender>();
171 GLib.debug("new jsrender %s", path);
173 this.project = project;
174 this.hasParent = false;
182 this.content_type = "";
183 this.build_module = "";
184 //this.loaded = false;
185 //print("JsRender.cto() - reset transStrings\n");
186 this.transStrings = new Gee.HashMap<string,string>();
187 this.namedStrings = new Gee.HashMap<string,string>();
188 // should use basename reallly...
190 var ar = this.path.split("/");
191 // name is in theory filename without .bjs (or .js eventually...)
193 Regex regex = new Regex ("\\.(bjs)$");
195 this.name = ar.length > 0 ? regex.replace(ar[ar.length-1],ar[ar.length-1].length, 0 , "") : "";
196 } catch (GLib.Error e) {
199 this.fullname = (this.parent.length > 0 ? (this.parent + ".") : "" ) + this.name;
201 this.doubleStringProps = new Gee.ArrayList<string>();
202 this.childfiles = new GLib.ListStore(typeof(JsRender));
203 //this.errorsByType = new Gee.HashMap<string, GLib.ListStore>();
204 this.errors = new Gee.ArrayList<Lsp.Diagnostic>((a,b) => { return a.equals(b); });
205 this.undo_json = new Gee.HashMap<int,string>();
210 public void renameTo(string name) throws Error
212 if (this.xtype == "PlainFile") {
215 var bjs = GLib.Path.get_dirname(this.path) +"/" + name + ".bjs";
216 if (FileUtils.test(bjs, FileTest.EXISTS)) {
217 throw new Error.RENAME_FILE_EXISTS("File exists %s\n",name);
219 GLib.FileUtils.remove(this.path);
221 // remove other files?
230 // not sure why xt is needed... -> project contains xtype..
232 public static JsRender factory(string xt, Project.Project project, string path) throws Error
237 return new Gtk(project, path);
240 return new Roo((Project.Roo) project, path);
242 // return new Flutter(project, path);
244 return new PlainFile(project, path);
246 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
252 public string nickType()
254 var ar = this.name.split(".");
256 for (var i =0; i < ar.length -1; i++) {
259 return string.joinv(".", ret);
262 public string nickName()
264 var ar = this.name.split(".");
265 return ar[ar.length-1];
269 public string nickNameSplit()
271 var n = this.nickName();
274 for (var i = 0; i < n.length; i++) {
275 if (i!=0 && n.get(i).isupper() && len > 10) {
279 ret += n.get(i).to_string();
289 Gdk.Pixbuf screenshot = null;
290 Gdk.Pixbuf screenshot92 = null;
291 Gdk.Pixbuf screenshot368 = null;
293 public Gdk.Pixbuf? getIcon(int size = 0) {
294 var fname = this.getIconFileName( );
295 if (!FileUtils.test(fname, FileTest.EXISTS)) {
296 GLib.debug("PIXBUF %s: %s does not exist?", this.name, fname);
302 if (this.screenshot == null) {
304 this.screenshot = new Gdk.Pixbuf.from_file(fname);
305 } catch (GLib.Error e) {}
307 return this.screenshot;
311 if (this.screenshot == null) {
313 if (this.screenshot == null) {
318 this.screenshot92 = this.screenshot.scale_simple(92, (int) (this.screenshot.height * 92.0 /this.screenshot.width * 1.0 )
319 , Gdk.InterpType.NEAREST) ;
320 return this.screenshot92;
323 if (this.screenshot == null) {
325 if (this.screenshot == null) {
330 this.screenshot368 = this.screenshot.scale_simple(368, (int) (this.screenshot.height * 368.0 /this.screenshot.width * 1.0 )
331 , Gdk.InterpType.NEAREST) ;
332 return this.screenshot368;
337 public void writeIcon(Gdk.Pixbuf pixbuf) {
339 this.screenshot92 = null;
340 this.screenshot368 = null;
341 this.screenshot = null;
343 GLib.debug("Wirte %s", this.getIconFileName( ));
344 pixbuf.save(this.getIconFileName( ),"png");
345 this.screenshot = pixbuf;
347 } catch (GLib.Error e) {
348 GLib.debug("failed to write pixbuf?");
356 public void widgetToIcon(global::Gtk.Widget widget) {
358 this.screenshot92 = null;
359 this.screenshot368 = null;
360 this.screenshot = null;
365 var filename = this.getIconFileName();
368 var p = new global::Gtk.WidgetPaintable(widget);
369 var s = new global::Gtk.Snapshot();
370 GLib.debug("Width %d, Height %d", widget.get_width(), widget.get_height());
371 p.snapshot(s, widget.get_width(), widget.get_height());
372 var n = s.free_to_node();
376 var r = new Gsk.CairoRenderer();
378 var t = r.render_texture(n,null);
379 GLib.debug("write to %s", filename);
380 t.save_to_png(filename);
384 } catch (GLib.Error e) {
385 GLib.debug("failed to write pixbuf?");
395 public string getIconFileName( )
398 var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path);
400 var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
402 if (!FileUtils.test(dir, FileTest.IS_DIR)) {
403 File.new_for_path(dir).make_directory();
405 } catch (GLib.Error e) {
406 // eakk.. what to do here...
408 var fname = dir + "/" + m5 + ".png";
415 public string toJsonString()
417 if (this.xtype == "PlainFile") {
420 var node = new Json.Node(Json.NodeType.OBJECT);
421 node.set_object(this.toJsonObject());
422 var generator = new JsonGen(node);
423 generator.indent = 1;
424 generator.pretty = true;
427 return generator.to_data();
430 public void saveBJS()
432 // if (!this.loaded) {
433 /// GLib.debug("saveBJS - skip - not loaded?");
436 if (this.xtype == "PlainFile") {
442 GLib.debug("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
444 this.writeFile(this.path, this.toJsonString());
446 } catch(GLib.Error e) {
447 print("Save failed");
451 bool in_undo = false;
452 protected void updateUndo()
457 if (this.xtype == "PlainFile") {
458 // handled by gtk sourceview buffer...
461 //GLib.debug("UNDO store %d", this.version);
462 this.undo_json.set(this.version, this.tree.toJsonString());
463 if (this.undo_json.has_key(this.version+1)) {
464 var n = this.version +1;
465 while (this.undo_json.has_key(n)) {
466 this.undo_json.unset(n++);
473 public bool undoStep(int step = -1) // undo back/next
476 if (!this.undo_json.has_key(this.version + step)) {
477 //GLib.debug("UNDO step %d failed - no version available", this.version + step);
480 var new_version = this.version + step;
481 var pa = new Json.Parser();
482 //GLib.debug("UNDO RESTORE : %d", this.version + step);
484 pa.load_from_data(this.undo_json.get(new_version));
485 var node = pa.get_root();
487 this.loadTree(node.get_object(),2);
488 this.tree.updated_count = new_version;
489 this.in_undo = false;
493 public string jsonHasOrEmpty(Json.Object obj, string key) {
494 return obj.has_member(key) ?
495 obj.get_string_member(key) : "";
499 public Json.Object toJsonObject ()
503 var ret = new Json.Object();
504 if (this.xtype == "PlainFile") {
508 //ret.set_string_member("id", this.id); // not relivant..
509 ret.set_string_member("name", this.name);
511 if (this.project.xtype == "Roo") {
512 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
513 ret.set_string_member("title", this.title == null ? "" : this.title);
514 //ret.set_string_member("path", this.path);
515 //ret.set_string_member("items", this.items);
516 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
517 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
519 if (this.project.xtype == "Gtk") {
521 ret.set_string_member("build_module", this.build_module );
523 ret.set_boolean_member("gen_extended", this.gen_extended);
525 if (this.transStrings.size > 0) {
526 var tr = new Json.Object();
527 var iter = this.transStrings.map_iterator();
528 while (iter.next()) {
529 tr.set_string_member(iter.get_value(), iter.get_key());
531 ret.set_object_member("strings", tr);
536 if (this.namedStrings.size > 0) {
537 var tr = new Json.Object();
538 var iter = this.namedStrings.map_iterator();
539 while (iter.next()) {
540 tr.set_string_member(iter.get_key(), iter.get_value());
542 ret.set_object_member("named_strings", tr);
545 var ar = new Json.Array();
546 // empty files do not have a tree.
547 if (this.tree != null) {
548 ar.add_object_element(this.tree.toJsonObject());
550 ret.set_array_member("items", ar);
557 public string getTitle ()
559 if (this.title == null) { // not sure why this happens..
562 if (this.title.length > 0) {
565 var a = this.path.split("/");
566 return a[a.length-1];
568 public string getTitleTip()
570 if (this.title.length > 0) {
571 return "<b>" + this.title + "</b> " + this.path;
578 this.cn.sort(function(a,b) {
579 return a.path > b.path;// ? 1 : -1;
583 // should be in palete provider really..
586 public Palete.Palete palete()
588 // error on plainfile?
589 return this.project.palete;
593 public string guessName(Node ar) // turns the object into full name.
595 // eg. xns: Roo, xtype: XXX -> Roo.xxx
596 if (!ar.hasXnsType()) {
600 return ar.get("xns") + "." + ar.get("xtype");
605 * non-atomic write (replacement for put contents, as it creates temporary files.
607 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
611 var f = GLib.File.new_for_path(path);
612 var data_out = new GLib.DataOutputStream(
613 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
615 data_out.put_string(contents, null);
616 data_out.close(null);
621 public Node? lineToNode(int line)
623 if (this.tree == null) {
626 return this.tree.lineToNode(line);
631 public GLib.ListStore toListStore()
633 var ret = new GLib.ListStore(typeof(Node));
634 ret.append(this.tree);
639 // used to handle list of files in project editor (really Gtk only)
640 public bool compile_group_selected {
642 var gproj = (Project.Gtk) this.project;
644 if (gproj.active_cg == null) {
647 if (this.xtype == "Dir") {
648 // show ticked if all ticked..
650 for(var i = 0; i < this.childfiles.n_items; i++ ) {
651 var f = (JsRender) this.childfiles.get_item(i);
652 if (!f.compile_group_selected) {
661 if (gproj.active_cg.sources == null) {
662 GLib.debug("compile_group_selected - sources is null? ");
666 return gproj.active_cg.sources.contains(this.relpath);
671 var gproj = (Project.Gtk) this.project;
673 if (gproj.active_cg == null) {
676 if (gproj.active_cg.loading_ui) {
680 if (this.xtype == "Dir") {
681 for(var i = 0; i < this.childfiles.n_items; i++ ) {
682 var f = (JsRender) this.childfiles.get_item(i);
683 f.compile_group_selected = value;
692 if (value == false) {
693 GLib.debug("REMOVE %s", this.relpath);
695 gproj.active_cg.sources.remove(this.relpath);
698 if (!gproj.active_cg.sources.contains(this.relpath)) {
699 GLib.debug("ADD %s", this.relpath);
700 gproj.active_cg.sources.add(this.relpath);
706 public bool compile_group_hidden {
708 var gproj = (Project.Gtk) this.project;
711 return gproj.hidden.contains(this.relpath);
716 var gproj = (Project.Gtk) this.project;
718 if (gproj.active_cg == null) {
721 if (gproj.active_cg.loading_ui) {
724 if (value == false) {
725 GLib.debug("REMOVE %s", this.relpath);
727 gproj.hidden.remove(this.relpath);
730 if (!gproj.hidden.contains(this.relpath)) {
731 gproj.hidden.add(this.relpath);
732 // hiding a project will auto clear it.
733 for(var i = 0; i < this.childfiles.n_items; i++ ) {
734 var f = (JsRender) this.childfiles.get_item(i);
735 f.compile_group_selected = false;
746 if (this.xtype == "Dir") {
749 // cleans up build (should really be in children..
750 this.removeFile(this.path);
751 if (this.path.has_suffix(".bjs") && this.project.xtype == "Roo") {
752 this.removeFile(this.path.substring(0, this.path.length-4) + ".js");
755 if (this.path.has_suffix(".bjs") && this.project.xtype == "Gtk") {
756 this.removeFile(this.path.substring(0, this.path.length-4) + ".vala");
757 this.removeFile(this.path.substring(0, this.path.length-4) + ".c");
758 this.removeFile(this.path.substring(0, this.path.length-4) + ".o");
760 if (this.path.has_suffix(".vala") && this.project.xtype == "Gtk") {
761 this.removeFile(this.path.substring(0, this.path.length-5) + ".c");
762 this.removeFile(this.path.substring(0, this.path.length-5) + ".o");
768 private void removeFile(string path)
771 if (GLib.FileUtils.test(path, GLib.FileTest.EXISTS)) {
772 GLib.FileUtils.unlink(path);
776 public string relTargetName()
778 return this.targetName().substring(this.project.path.length +1);
782 public string to_url()
784 return File.new_for_path (this.targetName()).get_uri ();
786 public Palete.LanguageClient? getLanguageServer()
789 return this.project.getLanguageServer(this.language_id());
793 public void updateErrors(Gee.ArrayList<Lsp.Diagnostic> new_errors)
795 var oc = this.error_counter;
796 var skip = new Gee.ArrayList<Lsp.Diagnostic>((a,b) => { return a.equals(b); });
797 var rem = new Gee.ArrayList<Lsp.Diagnostic>((a,b) => { return a.equals(b); });
798 foreach(var old in this.errors) {
799 if (new_errors.contains(old)) {
806 foreach(var old in rem) {
807 this.removeError(old);
809 foreach(var err in new_errors) {
810 if (skip.contains(err)) {
815 if (oc != this.error_counter) {
816 BuilderApplication.updateCompileResults();
823 public Gee.ArrayList<Lsp.Diagnostic> getErrors()
828 private void addError(Lsp.Diagnostic diag)
831 GLib.debug("ADD Error %s", diag.to_string());
832 this.errors.add(diag);
833 this.project.addError(this, diag);
835 this.error_counter++;
839 public void removeError(Lsp.Diagnostic diag)
841 GLib.debug("REMOVE Error %s", diag.to_string());
842 this.errors.remove(diag);
843 this.project.removeError(this, diag);
844 this.error_counter++;
846 public int getErrorsTotal(string category)
849 foreach(var diag in this.errors) {
850 if (diag.category == category) {
859 public void loadTree(Json.Object obj, int bjs_version = 2)
861 if (this.xtype == "PlainFile" ){
865 this.tree = new Node();
866 this.tree.loadFromJson(obj,bjs_version);
867 this.tree.version_changed.connect(() => {
875 public abstract string language_id();
876 public abstract void save();
877 public abstract void saveHTML(string html);
878 public abstract string toSource() ;
879 public abstract string toSourceCode() ; // used by commandline tester..
880 public abstract void setSource(string str);
881 public abstract string toSourcePreview() ;
882 public abstract void removeFiles() ;
883 public abstract void findTransStrings(Node? node );
884 public abstract string toGlade();
885 public abstract string targetName();
886 public abstract void loadItems() throws GLib.Error;