Merge branch 'master' of http://git.roojs.com/roobuilder
[roobuilder] / src / Builder4 / Editor.bjs
1 {
2  "build_module" : "builder",
3  "items" : [
4   {
5    "# JsRender.JsRender file" : "null",
6    "# JsRender.Node node" : "null",
7    "# Xcls_MainWindow window" : "null",
8    "# bool dirty" : false,
9    "# bool pos" : false,
10    "# int pos_root_x" : "",
11    "# int pos_root_y" : "",
12    "# string activeEditor" : "\"\"",
13    "# string key" : "\"\"",
14    "# string ptype" : "\"\"",
15    "$ homogeneous" : false,
16    "$ xns" : "Gtk",
17    "* pack" : "add",
18    "@ void save" : "()",
19    "Gtk.Orientation orientation" : "Gtk.Orientation.VERTICAL",
20    "Gtk.SourceSearchContext searchcontext" : "null",
21    "bool hexpand" : true,
22    "bool vexpand" : true,
23    "int last_search_end" : 0,
24    "items" : [
25     {
26      "$ homogeneous" : false,
27      "$ xns" : "Gtk",
28      "* pack" : "pack_start,false,true",
29      "Gtk.Orientation orientation" : "Gtk.Orientation.HORIZONTAL",
30      "items" : [
31       {
32        "$ xns" : "Gtk",
33        "* pack" : "pack_start,false,false",
34        "id" : "save_button",
35        "label" : "Save",
36        "listeners" : {
37         "clicked" : [
38          " () => { ",
39          "    _this.saveContents();",
40          "}",
41          "  "
42         ]
43        },
44        "xtype" : "Button"
45       },
46       {
47        "$ xns" : "Gtk",
48        "* pack" : "pack_start,true,true",
49        "bool editable" : false,
50        "id" : "key_edit",
51        "int width_request" : 100,
52        "xtype" : "Entry"
53       },
54       {
55        "$ xns" : "Gtk",
56        "* ctor" : "new Gtk.HScale.with_range (6, 30, 1)",
57        "* init" : [
58         "{",
59         "\tthis.el.set_range(6,30);",
60         "\tthis.el.set_value(8);",
61         "}",
62         ""
63        ],
64        "* pack" : "pack_end,true,true",
65        "bool draw_value" : true,
66        "bool has_origin" : true,
67        "bool sensitive" : true,
68        "int digits" : 0,
69        "listeners" : {
70         "change_value" : [
71          "(st, val ) => {",
72          "\t ",
73          "\t  var description =   Pango.FontDescription.from_string(\"monospace\");",
74          "\t  print(\"resize to %d\", (int)val*1000);",
75          "      description.set_size((int)val*1000);",
76          "      _this.view.el.override_font(description);",
77          "      return false;",
78          "}"
79         ]
80        },
81        "xtype" : "HScale"
82       }
83      ],
84      "xtype" : "Box"
85     },
86     {
87      "$ xns" : "Gtk",
88      "* init" : "this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);",
89      "* pack" : "pack_end,true,true",
90      "id" : "RightEditor",
91      "items" : [
92       {
93        "$ Gtk.SourceDrawSpacesFlags draw_spaces" : "Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE",
94        "$ gboolean auto_indent" : true,
95        "$ show_line_numbers" : true,
96        "$ xns" : "Gtk",
97        "* init" : [
98         " ",
99         "",
100         "\t\tvar description =   Pango.FontDescription.from_string(\"monospace\");",
101         "\t\tdescription.set_size(8000);",
102         "",
103         "\t\t this.el.override_font(description);",
104         "",
105         "\ttry {        ",
106         "\t\tthis.el.completion.add_provider(new Palete.CompletionProvider(_this));",
107         "    } catch (GLib.Error  e) {}",
108         "    ",
109         "\tthis.el.completion.unblock_interactive();",
110         "\tthis.el.completion.select_on_show\t\t\t= true; // select",
111         "\tthis.el.completion.show_headers\t\t\t= false;",
112         "\tthis.el.completion.remember_info_visibility\t\t= true;",
113         "    ",
114         "  ",
115         "    var attrs = new Gtk.SourceMarkAttributes();",
116         "    var  pink =   Gdk.RGBA();",
117         "    pink.parse ( \"pink\");",
118         "    attrs.set_background ( pink);",
119         "    attrs.set_icon_name ( \"process-stop\");    ",
120         "    attrs.query_tooltip_text.connect(( mark) => {",
121         "        //print(\"tooltip query? %s\\n\", mark.name);",
122         "        return mark.name;",
123         "    });",
124         "    ",
125         "    this.el.set_mark_attributes (\"ERR\", attrs, 1);",
126         "    ",
127         "     var wattrs = new Gtk.SourceMarkAttributes();",
128         "    var  blue =   Gdk.RGBA();",
129         "    blue.parse ( \"#ABF4EB\");",
130         "    wattrs.set_background ( blue);",
131         "    wattrs.set_icon_name ( \"process-stop\");    ",
132         "    wattrs.query_tooltip_text.connect(( mark) => {",
133         "        //print(\"tooltip query? %s\\n\", mark.name);",
134         "        return mark.name;",
135         "    });",
136         "    ",
137         "    this.el.set_mark_attributes (\"WARN\", wattrs, 1);",
138         "    ",
139         " ",
140         "    ",
141         "     var dattrs = new Gtk.SourceMarkAttributes();",
142         "    var  purple =   Gdk.RGBA();",
143         "    purple.parse ( \"#EEA9FF\");",
144         "    dattrs.set_background ( purple);",
145         "    dattrs.set_icon_name ( \"process-stop\");    ",
146         "    dattrs.query_tooltip_text.connect(( mark) => {",
147         "        //print(\"tooltip query? %s\\n\", mark.name);",
148         "        return mark.name;",
149         "    });",
150         "    ",
151         "    this.el.set_mark_attributes (\"DEPR\", dattrs, 1);",
152         "    ",
153         "    "
154        ],
155        "* pack" : "add",
156        "bool insert_spaces_instead_of_tabs" : true,
157        "gboolean highlight_current_line" : true,
158        "gboolean show_line_marks" : true,
159        "id" : "view",
160        "indent_width" : 4,
161        "items" : [
162         {
163          "$ xns" : "Gtk",
164          "* pack" : "set_buffer",
165          "bool check_queued" : false,
166          "bool check_running" : false,
167          "id" : "buffer",
168          "int error_line" : "-1",
169          "listeners" : {
170           "changed" : [
171            " () => {",
172            "    // check syntax??",
173            "    // ??needed..??",
174            "    _this.save_button.el.sensitive = true;",
175            "    print(\"EDITOR CHANGED\");",
176            "    this.checkSyntax();",
177            "   ",
178            "    _this.dirty = true;",
179            "",
180            "    // this.get('/LeftPanel.model').changed(  str , false);",
181            "    return ;",
182            "}",
183            "",
184            " ",
185            ""
186           ]
187          },
188          "xtype" : "SourceBuffer",
189          "|   bool checkSyntax" : [
190           " () {",
191           " ",
192           "    if (this.check_running) {",
193           "        print(\"Check is running\\n\");",
194           "        if (this.check_queued) { ",
195           "            print(\"Check is already queued\");",
196           "            return true;",
197           "        }",
198           "        this.check_queued = true;",
199           "        print(\"Adding queued Check \");",
200           "        GLib.Timeout.add_seconds(1, () => {",
201           "            this.check_queued = false;",
202           "            ",
203           "            this.checkSyntax();",
204           "            return false;",
205           "        });",
206           "    ",
207           "",
208           "        return true;",
209           "    }",
210           "    var str = this.toString();",
211           "    ",
212           "    // needed???",
213           "    if (this.error_line > 0) {",
214           "         Gtk.TextIter start;",
215           "         Gtk.TextIter end;     ",
216           "        this.el.get_bounds (out start, out end);",
217           "",
218           "        this.el.remove_source_marks (start, end, null);",
219           "    }",
220           "    if (str.length < 1) {",
221           "        print(\"checkSyntax - empty string?\\n\");",
222           "        return true;",
223           "    }",
224           "    ",
225           "    if (_this.file.xtype == \"PlainFile\") {",
226           "    ",
227           "        // assume it's gtk...",
228           "           this.check_running = true;",
229           " ",
230           "         if (!_this.window.windowstate.valasource.checkPlainFileSpawn(",
231           "\t   _this.file,",
232           "\t    str",
233           "\t )) {",
234           "            this.check_running = false;",
235           "        }",
236           "\t",
237           "        return true;",
238           "    ",
239           "    }",
240           "   if (_this.file == null) {",
241           "       return true;",
242           "   }",
243           "    var p = _this.file.project.palete;",
244           "    ",
245           "",
246           "     ",
247           "    this.check_running = true;",
248           "    ",
249           "    ",
250           "    if (_this.file.language == \"js\") {",
251           "        this.check_running = false;",
252           "        print(\"calling validate javascript\\n\"); ",
253           "        Gee.HashMap<int,string> errors;",
254           "        p.javascriptHasErrors(",
255           "    \t\t_this.window.windowstate,",
256           "            str, ",
257           "             _this.key, ",
258           "            _this.ptype,",
259           "            _this.file,",
260           " ",
261           "            out errors",
262           "        );",
263           "        return this.highlightErrors(errors);    ",
264           "        ",
265           "    }",
266           "        ",
267           "        ",
268           "    print(\"calling validate vala\\n\");    ",
269           "    // clear the buttons.",
270           " ",
271           "    ",
272           "   if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(",
273           "        _this.file,",
274           "        _this.node,",
275           "         _this.key,        ",
276           "         _this.ptype,",
277           "            str",
278           "        )) {",
279           "        this.check_running = false;",
280           "    } ",
281           "     ",
282           "    ",
283           "    ",
284           "    //print(\"done mark line\\n\");",
285           "     ",
286           "    return true; // at present allow saving - even if it's invalid..",
287           "}",
288           ""
289          ],
290          "|   string toString" : [
291           "  () {",
292           "    ",
293           "    Gtk.TextIter s;",
294           "    Gtk.TextIter e;",
295           "    this.el.get_start_iter(out s);",
296           "    this.el.get_end_iter(out e);",
297           "    var ret = this.el.get_text(s,e,true);",
298           "    //print(\"TO STRING? \" + ret);",
299           "    return ret;",
300           "}",
301           " "
302          ],
303          "| bool highlightErrors" : [
304           "( Gee.HashMap<int,string> validate_res) {",
305           "         ",
306           "        this.error_line = validate_res.size;",
307           "",
308           "        if (this.error_line < 1) {",
309           "              return true;",
310           "        }",
311           "        var tlines = this.el.get_line_count ();",
312           "        Gtk.TextIter iter;",
313           "        var valiter = validate_res.map_iterator();",
314           "        while (valiter.next()) {",
315           "        ",
316           "    //        print(\"get inter\\n\");",
317           "            var eline = valiter.get_key();",
318           "            if (eline > tlines) {",
319           "                continue;",
320           "            }",
321           "            this.el.get_iter_at_line( out iter, eline);",
322           "            //print(\"mark line\\n\");",
323           "            this.el.create_source_mark(valiter.get_value(), \"ERR\", iter);",
324           "        }   ",
325           "        return false;",
326           "    }"
327          ],
328          "| bool highlightErrorsJson" : [
329           "(string type, Json.Object obj) {",
330           "      Gtk.TextIter start;",
331           "     Gtk.TextIter end;     ",
332           "        this.el.get_bounds (out start, out end);",
333           "        ",
334           "        this.el.remove_source_marks (start, end, type);",
335           "                 ",
336           "     ",
337           "     // we should highlight other types of errors..",
338           "    ",
339           "    if (!obj.has_member(type)) {",
340           "        print(\"Return has no errors\\n\");",
341           "        return true;",
342           "    }",
343           "    ",
344           "    if (_this.window.windowstate.state != WindowState.State.CODEONLY ",
345           "      ",
346           "        ) {",
347           "        return true;",
348           "    } ",
349           "    ",
350           "    ",
351           "    var err = obj.get_object_member(type);",
352           "    ",
353           "    ",
354           "    if (_this.file == null) {",
355           "        return true;",
356           "    ",
357           "    }",
358           "    var valafn = _this.file.path;",
359           " ",
360           "    if (_this.file.xtype != \"PlainFile\") {",
361           "",
362           "",
363           "        ",
364           "        ",
365           "         valafn = \"\";",
366           "          try {             ",
367           "               var  regex = new Regex(\"\\\\.bjs$\");",
368           "               // should not happen",
369           "              ",
370           "             ",
371           "                valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , \".vala\");",
372           "             } catch (GLib.RegexError e) {",
373           "                return true;",
374           "            }   ",
375           "",
376           "",
377           "",
378           "      }",
379           "       if (!err.has_member(valafn)) {",
380           "            print(\"File path has no errors\\n\");",
381           "            return  true;",
382           "        }",
383           "",
384           "        var lines = err.get_object_member(valafn);",
385           "        ",
386           "        var offset = 1;",
387           "        if (obj.has_member(\"line_offset\")) {",
388           "            offset = (int)obj.get_int_member(\"line_offset\") + 1;",
389           "        }",
390           "    ",
391           "",
392           "     ",
393           "    ",
394           "    var tlines = this.el.get_line_count () +1;",
395           "    ",
396           "    lines.foreach_member((obj, line, node) => {",
397           "        ",
398           "             Gtk.TextIter iter;",
399           "    //        print(\"get inter\\n\");",
400           "            var eline = int.parse(line) - offset;",
401           "            print(\"GOT ERROR on line %s -- converted to %d\\n\", line,eline);",
402           "            ",
403           "            ",
404           "            if (eline > tlines || eline < 0) {",
405           "                return;",
406           "            }",
407           "            this.el.get_iter_at_line( out iter, eline);",
408           "            //print(\"mark line\\n\");",
409           "            var msg  = \"Line: %d\".printf(eline+1);",
410           "            var ar = lines.get_array_member(line);",
411           "            for (var i = 0 ; i < ar.get_length(); i++) {",
412           "\t\t    msg += (msg.length > 0) ? \"\\n\" : \"\";",
413           "\t\t    msg += ar.get_string_element(i);",
414           "\t    }",
415           "            ",
416           "            ",
417           "            this.el.create_source_mark(msg, type, iter);",
418           "        } );",
419           "        return false;",
420           "    ",
421           "",
422           "",
423           "",
424           "",
425           "}",
426           ""
427          ]
428         }
429        ],
430        "listeners" : {
431         "key_release_event" : [
432          " (event) => {",
433          "    ",
434          "    if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {",
435          "        print(\"SAVE: ctrl-S  pressed\");",
436          "        _this.saveContents();",
437          "        return false;",
438          "    }",
439          "   // print(event.key.keyval)",
440          "    ",
441          "    return false;",
442          "",
443          "} ",
444          "",
445          " "
446         ]
447        },
448        "uint tab_width" : 4,
449        "xtype" : "SourceView",
450        "|   void load" : [
451         " (string str) {",
452         "",
453         "// show the help page for the active node..",
454         "   //this.get('/Help').show();",
455         "",
456         "",
457         "  // this.get('/BottomPane').el.set_current_page(0);",
458         "    var buf = (Gtk.SourceBuffer)this.el.get_buffer();",
459         "    buf.set_text(str, str.length);",
460         "    buf.set_undo_manager(null);",
461         "    ",
462         "    var lm = Gtk.SourceLanguageManager.get_default();",
463         "    var lang = \"vala\";",
464         "    if (_this.file != null) {",
465         "         lang = _this.file.language;",
466         "    }",
467         "    print(\"lang=%s, content_type = %s\\n\", lang, _this.file.content_type);",
468         "    var lg = _this.file.content_type.length > 0  ?",
469         "            lm.guess_language(_this.file.path, _this.file.content_type) :",
470         "            lm.get_language(lang);",
471         "     ",
472         "   ",
473         "    ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg); ",
474         "",
475         "    this.el.insert_spaces_instead_of_tabs = true;",
476         "    if (lg != null) {",
477         "\t\tprint(\"sourcelanguage  = %s\\n\", lg.name);",
478         "\t\tif (lg.name == \"Vala\") {",
479         "\t\t    this.el.insert_spaces_instead_of_tabs = false;",
480         "\t\t}",
481         "     }",
482         "    _this.dirty = false;",
483         "    this.el.grab_focus();",
484         "    _this.save_button.el.sensitive = false;",
485         "}"
486        ]
487       }
488      ],
489      "xtype" : "ScrolledWindow"
490     }
491    ],
492    "string id" : "Editor",
493    "xtype" : "Box",
494    "|   bool saveContents" : [
495     "  ()  {",
496     "    ",
497     "    ",
498     "    if (_this.file == null) {",
499     "        return true;",
500     "    }",
501     "    ",
502     "    ",
503     "   ",
504     "   ",
505     "     ",
506     "     var str = _this.buffer.toString();",
507     "     ",
508     "     _this.buffer.checkSyntax();",
509     "     ",
510     "     ",
511     "     ",
512     "     // LeftPanel.model.changed(  str , false);",
513     "     _this.dirty = false;",
514     "     _this.save_button.el.sensitive = false;",
515     "     ",
516     "    // find the text for the node..",
517     "    if (_this.file.xtype != \"PlainFile\") {",
518     "        if (ptype == \"listener\") {",
519     "            this.node.listeners.set(key,str);",
520     "        ",
521     "        } else {",
522     "             this.node.props.set(key,str);",
523     "        }",
524     "    } else {",
525     "        _this.file.setSource(  str );",
526     "     }",
527     "    ",
528     "    // call the signal..",
529     "    this.save();",
530     "    ",
531     "    return true;",
532     "",
533     "} "
534    ],
535    "|   void show" : [
536     "(JsRender.JsRender file, JsRender.Node? node, string ptype, string key)",
537     "{",
538     "    this.file = file;    ",
539     "    this.ptype = \"\";",
540     "    this.key  = \"\";",
541     "    this.node = null;",
542     "\tthis.searchcontext = null;",
543     "    ",
544     "    if (file.xtype != \"PlainFile\") {",
545     "    ",
546     "        this.ptype = ptype;",
547     "        this.key  = key;",
548     "        this.node = node;",
549     "         string val = \"\";",
550     "        // find the text for the node..",
551     "        if (ptype == \"listener\") {",
552     "            val = node.listeners.get(key);",
553     "        ",
554     "        } else {",
555     "            val = node.props.get(key);",
556     "        }",
557     "        this.view.load(val);",
558     "        this.key_edit.el.show();",
559     "        this.key_edit.el.text = key;  ",
560     "    ",
561     "    } else {",
562     "        this.view.load(        file.toSource() );",
563     "        this.key_edit.el.hide();",
564     "    }",
565     " ",
566     "}"
567    ],
568    "| int search" : [
569     "(string txt) {",
570     "",
571     "\tvar s = new Gtk.SourceSearchSettings();",
572     "\t",
573     "\tthis.searchcontext = new Gtk.SourceSearchContext(this.buffer.el,s);",
574     "\tthis.searchcontext .set_highlight(true);",
575     "\ts.set_search_text(txt);",
576     "\tGtk.TextIter beg, st,en;",
577     "\t ",
578     "\tthis.buffer.el.get_start_iter(out beg);",
579     "\tthis.searchcontext.forward(beg, out st, out en);",
580     "\tthis.last_search_end = 0;",
581     "\t",
582     "\treturn this.searchcontext.get_occurrences_count();",
583     "",
584     " ",
585     "   ",
586     "",
587     "}",
588     ""
589    ],
590    "| void forwardSearch" : [
591     "(bool change_focus) {",
592     "",
593     "\tif (this.searchcontext == null) {",
594     "\t\treturn;",
595     "\t}",
596     "\t",
597     "\tGtk.TextIter beg, st,en;",
598     "\t ",
599     "\tthis.buffer.el.get_iter_at_offset(out beg, this.last_search_end);",
600     "\tif (!this.searchcontext.forward(beg, out st, out en)) {",
601     "\t",
602     "\t\tthis.last_search_end = 0;",
603     "\t} else {",
604     "\t\tthis.last_search_end = en.get_offset();",
605     "\t\tif (change_focus) {",
606     "\t\t\tthis.view.el.grab_focus();",
607     "\t\t}",
608     "\t\tthis.buffer.el.place_cursor(st);",
609     "\t\tthis.view.el.scroll_to_iter(st,  0.1f, true, 0.0f, 0.5f);",
610     "\t}",
611     " ",
612     "}",
613     ""
614    ],
615    "| void scroll_to_line" : [
616     "(int line) {",
617     "",
618     "\tGLib.Timeout.add(500, () => {",
619     "   ",
620     "\t\tvar buf = this.view.el.get_buffer();",
621     "",
622     "\t\tvar sbuf = (Gtk.SourceBuffer) buf;",
623     "",
624     "",
625     "\t\tGtk.TextIter iter;   ",
626     "\t\tsbuf.get_iter_at_line(out iter,  line);",
627     "\t\tthis.view.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.5f);",
628     "\t\treturn false;",
629     "\t});   ",
630     "}",
631     ""
632    ]
633   }
634  ],
635  "modOrder" : "",
636  "name" : "Editor",
637  "parent" : "",
638  "path" : "/home/alan/gitlive/roobuilder/src/Builder4/Editor.bjs",
639  "permname" : "",
640  "title" : ""
641 }