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