3 public class Editor : Object
8 public static Editor singleton()
10 if (_Editor == null) {
11 _Editor= new Editor();
15 public Xcls_save_button save_button;
16 public Xcls_key_edit key_edit;
17 public Xcls_RightEditor RightEditor;
18 public Xcls_view view;
19 public Xcls_buffer buffer;
22 public Xcls_MainWindow window;
23 public string activeEditor;
24 public int pos_root_x;
25 public int pos_root_y;
28 public Gtk.SourceSearchContext searchcontext;
29 public JsRender.JsRender file;
32 public signal void save ();
33 public JsRender.Node node;
39 this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
43 this.activeEditor = "";
46 this.searchcontext = null;
53 this.el.homogeneous = false;
54 this.el.hexpand = true;
55 var child_0 = new Xcls_Box2( _this );
57 this.el.pack_start ( child_0.el , false,true );
58 var child_1 = new Xcls_RightEditor( _this );
60 this.el.pack_end ( child_1.el , true,true );
63 // user defined functions
64 public bool saveContents () {
67 if (_this.file == null) {
75 var str = _this.buffer.toString();
77 _this.buffer.checkSyntax();
81 // LeftPanel.model.changed( str , false);
83 _this.save_button.el.sensitive = false;
85 // find the text for the node..
86 if (_this.file.xtype != "PlainFile") {
87 if (ptype == "listener") {
88 this.node.listeners.set(key,str);
91 this.node.props.set(key,str);
94 _this.file.setSource( str );
103 public void scroll_to_line (int line) {
105 GLib.Timeout.add(500, () => {
107 var buf = this.view.el.get_buffer();
109 var sbuf = (Gtk.SourceBuffer) buf;
113 sbuf.get_iter_at_line(out iter, line);
114 this.view.el.scroll_to_iter(iter, 0.1f, true, 0.0f, 0.5f);
118 public int search (string txt) {
120 var s = new Gtk.SourceSearchSettings();
122 this.searchcontext = new Gtk.SourceSearchContext(this.buffer.el,s);
123 this.searchcontext .set_highlight(true);
124 s.set_search_text(txt);
125 Gtk.TextIter beg, st,en;
127 this.buffer.el.get_start_iter(out beg);
128 this.searchcontext.forward(beg, out st, out en);
131 return this.searchcontext.get_occurrences_count();
137 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
145 if (file.xtype != "PlainFile") {
151 // find the text for the node..
152 if (ptype == "listener") {
153 val = node.listeners.get(key);
156 val = node.props.get(key);
159 this.key_edit.el.show();
160 this.key_edit.el.text = key;
163 this.view.load( file.toSource() );
164 this.key_edit.el.hide();
169 public class Xcls_Box2 : Object
172 private Editor _this;
178 public Xcls_Box2(Editor _owner )
181 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
185 // set gobject values
186 this.el.homogeneous = false;
187 var child_0 = new Xcls_save_button( _this );
189 this.el.pack_start ( child_0.el , false,false );
190 var child_1 = new Xcls_key_edit( _this );
192 this.el.pack_end ( child_1.el , true,true );
195 // user defined functions
197 public class Xcls_save_button : Object
199 public Gtk.Button el;
200 private Editor _this;
206 public Xcls_save_button(Editor _owner )
209 _this.save_button = this;
210 this.el = new Gtk.Button();
214 // set gobject values
215 this.el.label = "Save";
218 this.el.clicked.connect( () => {
219 _this.saveContents();
223 // user defined functions
226 public class Xcls_key_edit : Object
229 private Editor _this;
235 public Xcls_key_edit(Editor _owner )
238 _this.key_edit = this;
239 this.el = new Gtk.Entry();
243 // set gobject values
246 // user defined functions
250 public class Xcls_RightEditor : Object
252 public Gtk.ScrolledWindow el;
253 private Editor _this;
259 public Xcls_RightEditor(Editor _owner )
262 _this.RightEditor = this;
263 this.el = new Gtk.ScrolledWindow( null, null );
267 // set gobject values
268 var child_0 = new Xcls_view( _this );
270 this.el.add ( child_0.el );
274 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
277 // user defined functions
279 public class Xcls_view : Object
281 public Gtk.SourceView el;
282 private Editor _this;
288 public Xcls_view(Editor _owner )
292 this.el = new Gtk.SourceView();
296 // set gobject values
297 this.el.auto_indent = true;
298 this.el.indent_width = 4;
299 this.el.show_line_marks = true;
300 this.el.insert_spaces_instead_of_tabs = true;
301 this.el.show_line_numbers = true;
302 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
303 this.el.tab_width = 4;
304 this.el.highlight_current_line = true;
305 var child_0 = new Xcls_buffer( _this );
307 this.el.set_buffer ( child_0.el );
311 var description = Pango.FontDescription.from_string("monospace");
312 description.set_size(8000);
313 this.el.override_font(description);
316 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
317 this.el.completion.unblock_interactive();
318 this.el.completion.select_on_show = true; // select
319 this.el.completion.show_headers = false;
320 this.el.completion.remember_info_visibility = true;
323 var attrs = new Gtk.SourceMarkAttributes();
324 var pink = Gdk.RGBA();
325 pink.parse ( "pink");
326 attrs.set_background ( pink);
327 attrs.set_icon_name ( "process-stop");
328 attrs.query_tooltip_text.connect(( mark) => {
329 //print("tooltip query? %s\n", mark.name);
333 this.el.set_mark_attributes ("ERR", attrs, 1);
335 var wattrs = new Gtk.SourceMarkAttributes();
336 var blue = Gdk.RGBA();
337 blue.parse ( "#ABF4EB");
338 wattrs.set_background ( blue);
339 wattrs.set_icon_name ( "process-stop");
340 wattrs.query_tooltip_text.connect(( mark) => {
341 //print("tooltip query? %s\n", mark.name);
345 this.el.set_mark_attributes ("WARN", wattrs, 1);
349 var dattrs = new Gtk.SourceMarkAttributes();
350 var purple = Gdk.RGBA();
351 purple.parse ( "#EEA9FF");
352 dattrs.set_background ( purple);
353 dattrs.set_icon_name ( "process-stop");
354 dattrs.query_tooltip_text.connect(( mark) => {
355 //print("tooltip query? %s\n", mark.name);
359 this.el.set_mark_attributes ("DEPR", dattrs, 1);
362 this.el.key_release_event.connect( (event) => {
364 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
365 print("SAVE: ctrl-S pressed");
366 _this.saveContents();
369 // print(event.key.keyval)
376 // user defined functions
377 public void load (string str) {
379 // show the help page for the active node..
380 //this.get('/Help').show();
383 // this.get('/BottomPane').el.set_current_page(0);
384 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
385 buf.set_text(str, str.length);
386 buf.set_undo_manager(null);
388 var lm = Gtk.SourceLanguageManager.get_default();
390 if (_this.file != null) {
391 lang = _this.file.language;
393 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
394 var lg = _this.file.content_type.length > 0 ?
395 lm.guess_language(_this.file.path, _this.file.content_type) :
396 lm.get_language(lang);
399 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
401 this.el.insert_spaces_instead_of_tabs = true;
403 print("sourcelanguage = %s\n", lg.name);
404 if (lg.name == "Vala") {
405 this.el.insert_spaces_instead_of_tabs = false;
409 this.el.grab_focus();
410 _this.save_button.el.sensitive = false;
413 public class Xcls_buffer : Object
415 public Gtk.SourceBuffer el;
416 private Editor _this;
420 public bool check_queued;
421 public int error_line;
422 public bool check_running;
425 public Xcls_buffer(Editor _owner )
429 this.el = new Gtk.SourceBuffer( null );
432 this.check_queued = false;
433 this.error_line = -1;
434 this.check_running = false;
436 // set gobject values
439 this.el.changed.connect( () => {
442 _this.save_button.el.sensitive = true;
443 print("EDITOR CHANGED");
448 // this.get('/LeftPanel.model').changed( str , false);
453 // user defined functions
454 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
456 this.error_line = validate_res.size;
458 if (this.error_line < 1) {
461 var tlines = this.el.get_line_count ();
463 var valiter = validate_res.map_iterator();
464 while (valiter.next()) {
466 // print("get inter\n");
467 var eline = valiter.get_key();
468 if (eline > tlines) {
471 this.el.get_iter_at_line( out iter, eline);
472 //print("mark line\n");
473 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
477 public string toString () {
481 this.el.get_start_iter(out s);
482 this.el.get_end_iter(out e);
483 var ret = this.el.get_text(s,e,true);
484 //print("TO STRING? " + ret);
487 public bool checkSyntax () {
489 if (this.check_running) {
490 print("Check is running\n");
491 if (this.check_queued) {
492 print("Check is already queued");
495 this.check_queued = true;
496 print("Adding queued Check ");
497 GLib.Timeout.add_seconds(1, () => {
498 this.check_queued = false;
507 var str = this.toString();
510 if (this.error_line > 0) {
513 this.el.get_bounds (out start, out end);
515 this.el.remove_source_marks (start, end, null);
517 if (str.length < 1) {
518 print("checkSyntax - empty string?\n");
522 if (_this.file.xtype == "PlainFile") {
524 // assume it's gtk...
525 this.check_running = true;
527 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
531 this.check_running = false;
537 if (_this.file == null) {
540 var p = Palete.factory(_this.file.xtype);
544 this.check_running = true;
547 if (_this.file.language == "js") {
548 this.check_running = false;
549 print("calling validate javascript\n");
550 return this.highlightErrors(p.validateJavascript(
561 print("calling validate vala\n");
562 // clear the buttons.
565 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
572 this.check_running = false;
577 //print("done mark line\n");
579 return true; // at present allow saving - even if it's invalid..
581 public bool highlightErrorsJson (string type, Json.Object obj) {
584 this.el.get_bounds (out start, out end);
586 this.el.remove_source_marks (start, end, type);
589 // we should highlight other types of errors..
591 if (!obj.has_member(type)) {
592 print("Return has no errors\n");
596 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
597 _this.window.windowstate.state != WindowState.State.CODE
603 var err = obj.get_object_member(type);
606 if (_this.file == null) {
610 var valafn = _this.file.path;
612 if (_this.file.xtype != "PlainFile") {
619 var regex = new Regex("\\.bjs$");
623 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
624 } catch (GLib.RegexError e) {
631 if (!err.has_member(valafn)) {
632 print("File path has no errors\n");
636 var lines = err.get_object_member(valafn);
639 if (obj.has_member("line_offset")) {
640 offset = (int)obj.get_int_member("line_offset") + 1;
646 var tlines = this.el.get_line_count () +1;
648 lines.foreach_member((obj, line, node) => {
651 // print("get inter\n");
652 var eline = int.parse(line) - offset;
653 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
656 if (eline > tlines || eline < 0) {
659 this.el.get_iter_at_line( out iter, eline);
660 //print("mark line\n");
661 var msg = "Line: %d".printf(eline+1);
662 var ar = lines.get_array_member(line);
663 for (var i = 0 ; i < ar.get_length(); i++) {
664 msg += (msg.length > 0) ? "\n" : "";
665 msg += ar.get_string_element(i);
669 this.el.create_source_mark(msg, type, iter);