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;
27 public int last_search_end;
29 public Gtk.SourceSearchContext searchcontext;
30 public JsRender.JsRender file;
33 public signal void save ();
34 public JsRender.Node node;
40 this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
44 this.activeEditor = "";
46 this.last_search_end = 0;
48 this.searchcontext = null;
55 this.el.homogeneous = false;
56 this.el.hexpand = true;
57 this.el.vexpand = true;
58 var child_0 = new Xcls_Box2( _this );
60 this.el.pack_start ( child_0.el , false,true );
61 var child_1 = new Xcls_RightEditor( _this );
63 this.el.pack_end ( child_1.el , true,true );
66 // user defined functions
67 public bool saveContents () {
70 if (_this.file == null) {
78 var str = _this.buffer.toString();
80 _this.buffer.checkSyntax();
84 // LeftPanel.model.changed( str , false);
86 _this.save_button.el.sensitive = false;
88 // find the text for the node..
89 if (_this.file.xtype != "PlainFile") {
90 if (ptype == "listener") {
91 this.node.listeners.set(key,str);
94 this.node.props.set(key,str);
97 _this.file.setSource( str );
106 public void scroll_to_line (int line) {
108 GLib.Timeout.add(500, () => {
110 var buf = this.view.el.get_buffer();
112 var sbuf = (Gtk.SourceBuffer) buf;
116 sbuf.get_iter_at_line(out iter, line);
117 this.view.el.scroll_to_iter(iter, 0.1f, true, 0.0f, 0.5f);
121 public int search (string txt) {
123 var s = new Gtk.SourceSearchSettings();
125 this.searchcontext = new Gtk.SourceSearchContext(this.buffer.el,s);
126 this.searchcontext .set_highlight(true);
127 s.set_search_text(txt);
128 Gtk.TextIter beg, st,en;
130 this.buffer.el.get_start_iter(out beg);
131 this.searchcontext.forward(beg, out st, out en);
132 this.last_search_end = 0;
134 return this.searchcontext.get_occurrences_count();
140 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
146 this.searchcontext = null;
148 if (file.xtype != "PlainFile") {
154 // find the text for the node..
155 if (ptype == "listener") {
156 val = node.listeners.get(key);
159 val = node.props.get(key);
162 this.key_edit.el.show();
163 this.key_edit.el.text = key;
166 this.view.load( file.toSource() );
167 this.key_edit.el.hide();
171 public void forwardSearch (bool change_focus) {
173 if (this.searchcontext == null) {
177 Gtk.TextIter beg, st,en;
179 this.buffer.el.get_iter_at_offset(out beg, this.last_search_end);
180 if (!this.searchcontext.forward(beg, out st, out en)) {
182 this.last_search_end = 0;
184 this.last_search_end = en.get_offset();
186 this.view.el.grab_focus();
188 this.buffer.el.place_cursor(st);
189 this.view.el.scroll_to_iter(st, 0.1f, true, 0.0f, 0.5f);
193 public class Xcls_Box2 : Object
196 private Editor _this;
202 public Xcls_Box2(Editor _owner )
205 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
209 // set gobject values
210 this.el.homogeneous = false;
211 var child_0 = new Xcls_save_button( _this );
213 this.el.pack_start ( child_0.el , false,false );
214 var child_1 = new Xcls_key_edit( _this );
216 this.el.pack_start ( child_1.el , true,true );
217 var child_2 = new Xcls_HScale5( _this );
219 this.el.pack_end ( child_2.el , true,true );
222 // user defined functions
224 public class Xcls_save_button : Object
226 public Gtk.Button el;
227 private Editor _this;
233 public Xcls_save_button(Editor _owner )
236 _this.save_button = this;
237 this.el = new Gtk.Button();
241 // set gobject values
242 this.el.label = "Save";
245 this.el.clicked.connect( () => {
246 _this.saveContents();
250 // user defined functions
253 public class Xcls_key_edit : Object
256 private Editor _this;
262 public Xcls_key_edit(Editor _owner )
265 _this.key_edit = this;
266 this.el = new Gtk.Entry();
270 // set gobject values
271 this.el.width_request = 100;
272 this.el.editable = false;
275 // user defined functions
278 public class Xcls_HScale5 : Object
280 public Gtk.HScale el;
281 private Editor _this;
287 public Xcls_HScale5(Editor _owner )
290 this.el = new Gtk.HScale.with_range (6, 30, 1);
294 // set gobject values
295 this.el.has_origin = true;
296 this.el.draw_value = true;
298 this.el.sensitive = true;
303 this.el.set_range(6,30);
304 this.el.set_value(8);
308 this.el.change_value.connect( (st, val ) => {
310 var description = Pango.FontDescription.from_string("monospace");
311 print("resize to %d", (int)val*1000);
312 description.set_size((int)val*1000);
313 _this.view.el.override_font(description);
318 // user defined functions
322 public class Xcls_RightEditor : Object
324 public Gtk.ScrolledWindow el;
325 private Editor _this;
331 public Xcls_RightEditor(Editor _owner )
334 _this.RightEditor = this;
335 this.el = new Gtk.ScrolledWindow( null, null );
339 // set gobject values
340 var child_0 = new Xcls_view( _this );
342 this.el.add ( child_0.el );
346 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
349 // user defined functions
351 public class Xcls_view : Object
353 public Gtk.SourceView el;
354 private Editor _this;
360 public Xcls_view(Editor _owner )
364 this.el = new Gtk.SourceView();
368 // set gobject values
369 this.el.auto_indent = true;
370 this.el.indent_width = 4;
371 this.el.show_line_marks = true;
372 this.el.insert_spaces_instead_of_tabs = true;
373 this.el.show_line_numbers = true;
374 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
375 this.el.tab_width = 4;
376 this.el.highlight_current_line = true;
377 var child_0 = new Xcls_buffer( _this );
379 this.el.set_buffer ( child_0.el );
383 var description = Pango.FontDescription.from_string("monospace");
384 description.set_size(8000);
386 this.el.override_font(description);
389 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
390 } catch (GLib.Error e) {}
392 this.el.completion.unblock_interactive();
393 this.el.completion.select_on_show = true; // select
394 this.el.completion.show_headers = false;
395 this.el.completion.remember_info_visibility = true;
398 var attrs = new Gtk.SourceMarkAttributes();
399 var pink = Gdk.RGBA();
400 pink.parse ( "pink");
401 attrs.set_background ( pink);
402 attrs.set_icon_name ( "process-stop");
403 attrs.query_tooltip_text.connect(( mark) => {
404 //print("tooltip query? %s\n", mark.name);
408 this.el.set_mark_attributes ("ERR", attrs, 1);
410 var wattrs = new Gtk.SourceMarkAttributes();
411 var blue = Gdk.RGBA();
412 blue.parse ( "#ABF4EB");
413 wattrs.set_background ( blue);
414 wattrs.set_icon_name ( "process-stop");
415 wattrs.query_tooltip_text.connect(( mark) => {
416 //print("tooltip query? %s\n", mark.name);
420 this.el.set_mark_attributes ("WARN", wattrs, 1);
424 var dattrs = new Gtk.SourceMarkAttributes();
425 var purple = Gdk.RGBA();
426 purple.parse ( "#EEA9FF");
427 dattrs.set_background ( purple);
428 dattrs.set_icon_name ( "process-stop");
429 dattrs.query_tooltip_text.connect(( mark) => {
430 //print("tooltip query? %s\n", mark.name);
434 this.el.set_mark_attributes ("DEPR", dattrs, 1);
437 this.el.key_release_event.connect( (event) => {
439 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
440 print("SAVE: ctrl-S pressed");
441 _this.saveContents();
444 // print(event.key.keyval)
451 // user defined functions
452 public void load (string str) {
454 // show the help page for the active node..
455 //this.get('/Help').show();
458 // this.get('/BottomPane').el.set_current_page(0);
459 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
460 buf.set_text(str, str.length);
461 buf.set_undo_manager(null);
463 var lm = Gtk.SourceLanguageManager.get_default();
465 if (_this.file != null) {
466 lang = _this.file.language;
468 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
469 var lg = _this.file.content_type.length > 0 ?
470 lm.guess_language(_this.file.path, _this.file.content_type) :
471 lm.get_language(lang);
474 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
476 this.el.insert_spaces_instead_of_tabs = true;
478 print("sourcelanguage = %s\n", lg.name);
479 if (lg.name == "Vala") {
480 this.el.insert_spaces_instead_of_tabs = false;
484 this.el.grab_focus();
485 _this.save_button.el.sensitive = false;
488 public class Xcls_buffer : Object
490 public Gtk.SourceBuffer el;
491 private Editor _this;
495 public bool check_queued;
496 public int error_line;
497 public bool check_running;
500 public Xcls_buffer(Editor _owner )
504 this.el = new Gtk.SourceBuffer( null );
507 this.check_queued = false;
508 this.error_line = -1;
509 this.check_running = false;
511 // set gobject values
514 this.el.changed.connect( () => {
517 _this.save_button.el.sensitive = true;
518 print("EDITOR CHANGED");
523 // this.get('/LeftPanel.model').changed( str , false);
528 // user defined functions
529 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
531 this.error_line = validate_res.size;
533 if (this.error_line < 1) {
536 var tlines = this.el.get_line_count ();
538 var valiter = validate_res.map_iterator();
539 while (valiter.next()) {
541 // print("get inter\n");
542 var eline = valiter.get_key();
543 if (eline > tlines) {
546 this.el.get_iter_at_line( out iter, eline);
547 //print("mark line\n");
548 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
552 public bool checkSyntax () {
554 if (this.check_running) {
555 print("Check is running\n");
556 if (this.check_queued) {
557 print("Check is already queued");
560 this.check_queued = true;
561 print("Adding queued Check ");
562 GLib.Timeout.add_seconds(1, () => {
563 this.check_queued = false;
572 var str = this.toString();
575 if (this.error_line > 0) {
578 this.el.get_bounds (out start, out end);
580 this.el.remove_source_marks (start, end, null);
582 if (str.length < 1) {
583 print("checkSyntax - empty string?\n");
587 if (_this.file.xtype == "PlainFile") {
589 // assume it's gtk...
590 this.check_running = true;
592 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
596 this.check_running = false;
602 if (_this.file == null) {
605 var p = _this.file.project.palete;
609 this.check_running = true;
612 if (_this.file.language == "js") {
613 this.check_running = false;
614 print("calling validate javascript\n");
615 Gee.HashMap<int,string> errors;
616 p.javascriptHasErrors(
617 _this.window.windowstate,
625 return this.highlightErrors(errors);
630 print("calling validate vala\n");
631 // clear the buttons.
634 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
641 this.check_running = false;
646 //print("done mark line\n");
648 return true; // at present allow saving - even if it's invalid..
650 public string toString () {
654 this.el.get_start_iter(out s);
655 this.el.get_end_iter(out e);
656 var ret = this.el.get_text(s,e,true);
657 //print("TO STRING? " + ret);
660 public bool highlightErrorsJson (string type, Json.Object obj) {
663 this.el.get_bounds (out start, out end);
665 this.el.remove_source_marks (start, end, type);
668 // we should highlight other types of errors..
670 if (!obj.has_member(type)) {
671 print("Return has no errors\n");
675 if (_this.window.windowstate.state != WindowState.State.CODEONLY
682 var err = obj.get_object_member(type);
685 if (_this.file == null) {
689 var valafn = _this.file.path;
691 if (_this.file.xtype != "PlainFile") {
698 var regex = new Regex("\\.bjs$");
702 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
703 } catch (GLib.RegexError e) {
710 if (!err.has_member(valafn)) {
711 print("File path has no errors\n");
715 var lines = err.get_object_member(valafn);
718 if (obj.has_member("line_offset")) {
719 offset = (int)obj.get_int_member("line_offset") + 1;
725 var tlines = this.el.get_line_count () +1;
727 lines.foreach_member((obj, line, node) => {
730 // print("get inter\n");
731 var eline = int.parse(line) - offset;
732 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
735 if (eline > tlines || eline < 0) {
738 this.el.get_iter_at_line( out iter, eline);
739 //print("mark line\n");
740 var msg = "Line: %d".printf(eline+1);
741 var ar = lines.get_array_member(line);
742 for (var i = 0 ; i < ar.get_length(); i++) {
743 msg += (msg.length > 0) ? "\n" : "";
744 msg += ar.get_string_element(i);
748 this.el.create_source_mark(msg, type, iter);