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_close_btn close_btn;
17 public Xcls_RightEditor RightEditor;
18 public Xcls_view view;
19 public Xcls_buffer buffer;
22 public Xcls_MainWindow window;
23 public int pos_root_x;
25 public int pos_root_y;
27 public Gtk.SourceSearchContext searchcontext;
28 public int last_search_end;
29 public JsRender.NodeProp? prop;
30 public JsRender.JsRender? file;
31 public JsRender.Node node;
32 public signal void save ();
33 public string activeEditor;
39 this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
45 this.searchcontext = null;
46 this.last_search_end = 0;
50 this.activeEditor = "";
53 this.el.homogeneous = false;
54 this.el.hexpand = true;
55 this.el.vexpand = true;
56 var child_0 = new Xcls_Box2( _this );
58 this.el.pack_start ( child_0.el , false,true );
59 var child_1 = new Xcls_RightEditor( _this );
61 this.el.pack_end ( child_1.el , true,true );
64 // user defined functions
65 public bool saveContents () {
68 if (_this.file == null) {
74 var str = _this.buffer.toString();
76 _this.buffer.checkSyntax();
80 // LeftPanel.model.changed( str , false);
82 _this.save_button.el.sensitive = false;
84 // find the text for the node..
85 if (_this.file.xtype != "PlainFile") {
86 // in theory these properties have to exist!?!
88 this.window.windowstate.left_props.reload();
90 _this.file.setSource( str );
99 public void show (JsRender.JsRender file, JsRender.Node? node, JsRender.NodeProp? prop)
104 if (file.xtype != "PlainFile") {
108 // find the text for the node..
109 this.view.load( prop.val );
110 this.close_btn.el.show();
113 this.view.load( file.toSource() );
114 this.close_btn.el.hide();
118 public void forwardSearch (bool change_focus) {
120 if (this.searchcontext == null) {
124 Gtk.TextIter beg, st,en;
126 this.buffer.el.get_iter_at_offset(out beg, this.last_search_end);
127 if (!this.searchcontext.forward(beg, out st, out en)) {
129 this.last_search_end = 0;
131 this.last_search_end = en.get_offset();
133 this.view.el.grab_focus();
135 this.buffer.el.place_cursor(st);
136 this.view.el.scroll_to_iter(st, 0.1f, true, 0.0f, 0.5f);
140 public int search (string txt) {
142 var s = new Gtk.SourceSearchSettings();
144 this.searchcontext = new Gtk.SourceSearchContext(this.buffer.el,s);
145 this.searchcontext .set_highlight(true);
146 s.set_search_text(txt);
147 Gtk.TextIter beg, st,en;
149 this.buffer.el.get_start_iter(out beg);
150 this.searchcontext.forward(beg, out st, out en);
151 this.last_search_end = 0;
153 return this.searchcontext.get_occurrences_count();
159 public void reset () {
164 this.searchcontext = null;
167 public void scroll_to_line (int line) {
169 GLib.Timeout.add(500, () => {
171 var buf = this.view.el.get_buffer();
173 var sbuf = (Gtk.SourceBuffer) buf;
177 sbuf.get_iter_at_line(out iter, line);
178 this.view.el.scroll_to_iter(iter, 0.1f, true, 0.0f, 0.5f);
182 public class Xcls_Box2 : Object
185 private Editor _this;
191 public Xcls_Box2(Editor _owner )
194 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
198 // set gobject values
199 this.el.homogeneous = false;
200 var child_0 = new Xcls_save_button( _this );
202 this.el.add ( child_0.el );
203 var child_1 = new Xcls_Label5( _this );
205 this.el.add ( child_1.el );
206 var child_2 = new Xcls_HScale6( _this );
208 this.el.add ( child_2.el );
209 var child_3 = new Xcls_close_btn( _this );
211 this.el.add ( child_3.el );
214 // user defined functions
216 public class Xcls_save_button : Object
218 public Gtk.Button el;
219 private Editor _this;
225 public Xcls_save_button(Editor _owner )
228 _this.save_button = this;
229 this.el = new Gtk.Button();
233 // set gobject values
234 this.el.always_show_image = true;
235 this.el.label = "Save";
236 var child_0 = new Xcls_Image4( _this );
238 this.el.image = child_0.el;
241 this.el.clicked.connect( () => {
242 _this.saveContents();
246 // user defined functions
248 public class Xcls_Image4 : Object
251 private Editor _this;
257 public Xcls_Image4(Editor _owner )
260 this.el = new Gtk.Image();
264 // set gobject values
265 this.el.icon_name = "document-save";
268 // user defined functions
272 public class Xcls_Label5 : Object
275 private Editor _this;
281 public Xcls_Label5(Editor _owner )
284 this.el = new Gtk.Label( null );
288 // set gobject values
289 this.el.hexpand = true;
292 // user defined functions
295 public class Xcls_HScale6 : Object
297 public Gtk.HScale el;
298 private Editor _this;
304 public Xcls_HScale6(Editor _owner )
307 this.el = new Gtk.HScale.with_range (6, 30, 1);
311 // set gobject values
312 this.el.width_request = 200;
313 this.el.has_origin = true;
314 this.el.draw_value = true;
316 this.el.sensitive = true;
321 this.el.set_range(6,30);
322 this.el.set_value(8);
326 this.el.change_value.connect( (st, val ) => {
328 var description = Pango.FontDescription.from_string("monospace");
329 print("resize to %d", (int)val*1000);
330 description.set_size((int)val*1000);
331 _this.view.el.override_font(description);
336 // user defined functions
339 public class Xcls_close_btn : Object
341 public Gtk.Button el;
342 private Editor _this;
348 public Xcls_close_btn(Editor _owner )
351 _this.close_btn = this;
352 this.el = new Gtk.Button();
356 // set gobject values
357 this.el.always_show_image = true;
358 var child_0 = new Xcls_Image8( _this );
360 this.el.image = child_0.el;
363 this.el.clicked.connect( () => {
364 _this.saveContents();
365 _this.window.windowstate.switchState(WindowState.State.PREVIEW);
369 // user defined functions
371 public class Xcls_Image8 : Object
374 private Editor _this;
380 public Xcls_Image8(Editor _owner )
383 this.el = new Gtk.Image();
387 // set gobject values
388 this.el.icon_name = "window-close";
391 // user defined functions
396 public class Xcls_RightEditor : Object
398 public Gtk.ScrolledWindow el;
399 private Editor _this;
405 public Xcls_RightEditor(Editor _owner )
408 _this.RightEditor = this;
409 this.el = new Gtk.ScrolledWindow( null, null );
413 // set gobject values
414 var child_0 = new Xcls_view( _this );
416 this.el.add ( child_0.el );
420 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
423 // user defined functions
425 public class Xcls_view : Object
427 public Gtk.SourceView el;
428 private Editor _this;
434 public Xcls_view(Editor _owner )
438 this.el = new Gtk.SourceView();
442 // set gobject values
443 this.el.auto_indent = true;
444 this.el.indent_width = 4;
445 this.el.show_line_marks = true;
446 this.el.insert_spaces_instead_of_tabs = true;
447 this.el.show_line_numbers = true;
448 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
449 this.el.tab_width = 4;
450 this.el.highlight_current_line = true;
451 var child_0 = new Xcls_buffer( _this );
453 this.el.set_buffer ( child_0.el );
457 var description = Pango.FontDescription.from_string("monospace");
458 description.set_size(8000);
460 this.el.override_font(description);
463 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
464 } catch (GLib.Error e) {}
466 this.el.completion.unblock_interactive();
467 this.el.completion.select_on_show = true; // select
468 this.el.completion.show_headers = false;
469 this.el.completion.remember_info_visibility = true;
472 var attrs = new Gtk.SourceMarkAttributes();
473 var pink = Gdk.RGBA();
474 pink.parse ( "pink");
475 attrs.set_background ( pink);
476 attrs.set_icon_name ( "process-stop");
477 attrs.query_tooltip_text.connect(( mark) => {
478 //print("tooltip query? %s\n", mark.name);
482 this.el.set_mark_attributes ("ERR", attrs, 1);
484 var wattrs = new Gtk.SourceMarkAttributes();
485 var blue = Gdk.RGBA();
486 blue.parse ( "#ABF4EB");
487 wattrs.set_background ( blue);
488 wattrs.set_icon_name ( "process-stop");
489 wattrs.query_tooltip_text.connect(( mark) => {
490 //print("tooltip query? %s\n", mark.name);
494 this.el.set_mark_attributes ("WARN", wattrs, 1);
498 var dattrs = new Gtk.SourceMarkAttributes();
499 var purple = Gdk.RGBA();
500 purple.parse ( "#EEA9FF");
501 dattrs.set_background ( purple);
502 dattrs.set_icon_name ( "process-stop");
503 dattrs.query_tooltip_text.connect(( mark) => {
504 //print("tooltip query? %s\n", mark.name);
508 this.el.set_mark_attributes ("DEPR", dattrs, 1);
511 this.el.key_release_event.connect( (event) => {
513 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
514 print("SAVE: ctrl-S pressed");
515 _this.saveContents();
518 // print(event.key.keyval)
525 // user defined functions
526 public void load (string str) {
528 // show the help page for the active node..
529 //this.get('/Help').show();
532 // this.get('/BottomPane').el.set_current_page(0);
533 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
534 buf.set_text(str, str.length);
535 buf.set_undo_manager(null);
537 var lm = Gtk.SourceLanguageManager.get_default();
539 if (_this.file != null) {
540 lang = _this.file.language;
542 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
543 var lg = _this.file.content_type.length > 0 ?
544 lm.guess_language(_this.file.path, _this.file.content_type) :
545 lm.get_language(lang);
548 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
550 this.el.insert_spaces_instead_of_tabs = true;
552 print("sourcelanguage = %s\n", lg.name);
553 if (lg.name == "Vala") {
554 this.el.insert_spaces_instead_of_tabs = false;
558 this.el.grab_focus();
559 _this.save_button.el.sensitive = false;
562 public class Xcls_buffer : Object
564 public Gtk.SourceBuffer el;
565 private Editor _this;
569 public int error_line;
570 public bool check_queued;
571 public bool check_running;
574 public Xcls_buffer(Editor _owner )
578 this.el = new Gtk.SourceBuffer( null );
581 this.error_line = -1;
582 this.check_queued = false;
583 this.check_running = false;
585 // set gobject values
588 this.el.changed.connect( () => {
591 _this.save_button.el.sensitive = true;
592 print("EDITOR CHANGED");
597 // this.get('/LeftPanel.model').changed( str , false);
602 // user defined functions
603 public bool checkSyntax () {
605 if (this.check_running) {
606 print("Check is running\n");
607 if (this.check_queued) {
608 print("Check is already queued");
611 this.check_queued = true;
612 print("Adding queued Check ");
613 GLib.Timeout.add_seconds(1, () => {
614 this.check_queued = false;
623 var str = this.toString();
626 if (this.error_line > 0) {
629 this.el.get_bounds (out start, out end);
631 this.el.remove_source_marks (start, end, null);
633 if (str.length < 1) {
634 print("checkSyntax - empty string?\n");
638 if (_this.file.xtype == "PlainFile") {
640 // assume it's gtk...
641 this.check_running = true;
643 if (!BuilderApplication.valasource.checkPlainFileSpawn(
647 this.check_running = false;
653 if (_this.file == null) {
656 var p = _this.file.project.palete;
660 this.check_running = true;
663 if (_this.file.language == "js") {
664 this.check_running = false;
665 print("calling validate javascript\n");
666 Gee.HashMap<int,string> errors;
667 p.javascriptHasErrors(
668 _this.window.windowstate,
671 _this.file, // no reference not node?
674 return this.highlightErrors(errors);
679 print("calling validate vala\n");
680 // clear the buttons.
683 if (! BuilderApplication.valasource.checkFileWithNodePropChange(
689 this.check_running = false;
694 //print("done mark line\n");
696 return true; // at present allow saving - even if it's invalid..
698 public bool highlightErrorsJson (string type, Json.Object obj) {
701 this.el.get_bounds (out start, out end);
703 this.el.remove_source_marks (start, end, type);
706 // we should highlight other types of errors..
708 if (!obj.has_member(type)) {
709 print("Return has no errors\n");
713 if (_this.window.windowstate.state != WindowState.State.CODEONLY
720 var err = obj.get_object_member(type);
723 if (_this.file == null) {
727 var valafn = _this.file.path;
729 if (_this.file.xtype != "PlainFile") {
736 var regex = new Regex("\\.bjs$");
740 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
741 } catch (GLib.RegexError e) {
748 if (!err.has_member(valafn)) {
749 print("File path has no errors\n");
753 var lines = err.get_object_member(valafn);
756 if (obj.has_member("line_offset")) {
757 offset = (int)obj.get_int_member("line_offset") + 1;
763 var tlines = this.el.get_line_count () +1;
765 lines.foreach_member((obj, line, node) => {
768 // print("get inter\n");
769 var eline = int.parse(line) - offset;
770 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
773 if (eline > tlines || eline < 0) {
776 this.el.get_iter_at_line( out iter, eline);
777 //print("mark line\n");
778 var msg = "Line: %d".printf(eline+1);
779 var ar = lines.get_array_member(line);
780 for (var i = 0 ; i < ar.get_length(); i++) {
781 msg += (msg.length > 0) ? "\n" : "";
782 msg += ar.get_string_element(i);
786 this.el.create_source_mark(msg, type, iter);
795 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
797 this.error_line = validate_res.size;
799 if (this.error_line < 1) {
802 var tlines = this.el.get_line_count ();
804 var valiter = validate_res.map_iterator();
805 while (valiter.next()) {
807 // print("get inter\n");
808 var eline = valiter.get_key();
809 if (eline > tlines) {
812 this.el.get_iter_at_line( out iter, eline);
813 //print("mark line\n");
814 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
818 public string toString () {
822 this.el.get_start_iter(out s);
823 this.el.get_end_iter(out e);
824 var ret = this.el.get_text(s,e,true);
825 //print("TO STRING? " + ret);