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