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 var child_0 = new Xcls_Box2( _this );
59 this.el.pack_start ( child_0.el , false,true );
60 var child_1 = new Xcls_RightEditor( _this );
62 this.el.pack_end ( child_1.el , true,true );
65 // user defined functions
66 public bool saveContents () {
69 if (_this.file == null) {
77 var str = _this.buffer.toString();
79 _this.buffer.checkSyntax();
83 // LeftPanel.model.changed( str , false);
85 _this.save_button.el.sensitive = false;
87 // find the text for the node..
88 if (_this.file.xtype != "PlainFile") {
89 if (ptype == "listener") {
90 this.node.listeners.set(key,str);
93 this.node.props.set(key,str);
96 _this.file.setSource( str );
105 public void scroll_to_line (int line) {
107 GLib.Timeout.add(500, () => {
109 var buf = this.view.el.get_buffer();
111 var sbuf = (Gtk.SourceBuffer) buf;
115 sbuf.get_iter_at_line(out iter, line);
116 this.view.el.scroll_to_iter(iter, 0.1f, true, 0.0f, 0.5f);
120 public int search (string txt) {
122 var s = new Gtk.SourceSearchSettings();
124 this.searchcontext = new Gtk.SourceSearchContext(this.buffer.el,s);
125 this.searchcontext .set_highlight(true);
126 s.set_search_text(txt);
127 Gtk.TextIter beg, st,en;
129 this.buffer.el.get_start_iter(out beg);
130 this.searchcontext.forward(beg, out st, out en);
131 this.last_search_end = 0;
133 return this.searchcontext.get_occurrences_count();
139 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
145 this.searchcontext = null;
147 if (file.xtype != "PlainFile") {
153 // find the text for the node..
154 if (ptype == "listener") {
155 val = node.listeners.get(key);
158 val = node.props.get(key);
161 this.key_edit.el.show();
162 this.key_edit.el.text = key;
165 this.view.load( file.toSource() );
166 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;
274 // user defined functions
277 public class Xcls_HScale5 : Object
279 public Gtk.HScale el;
280 private Editor _this;
286 public Xcls_HScale5(Editor _owner )
289 this.el = new Gtk.HScale.with_range (6, 30, 1);
293 // set gobject values
294 this.el.has_origin = true;
295 this.el.draw_value = true;
297 this.el.sensitive = true;
302 this.el.set_range(6,30);
303 this.el.set_value(8);
307 this.el.change_value.connect( (st, val ) => {
309 var description = Pango.FontDescription.from_string("monospace");
310 print("resize to %d", (int)val*1000);
311 description.set_size((int)val*1000);
312 _this.view.el.override_font(description);
317 // user defined functions
321 public class Xcls_RightEditor : Object
323 public Gtk.ScrolledWindow el;
324 private Editor _this;
330 public Xcls_RightEditor(Editor _owner )
333 _this.RightEditor = this;
334 this.el = new Gtk.ScrolledWindow( null, null );
338 // set gobject values
339 var child_0 = new Xcls_view( _this );
341 this.el.add ( child_0.el );
345 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
348 // user defined functions
350 public class Xcls_view : Object
352 public Gtk.SourceView el;
353 private Editor _this;
359 public Xcls_view(Editor _owner )
363 this.el = new Gtk.SourceView();
367 // set gobject values
368 this.el.auto_indent = true;
369 this.el.indent_width = 4;
370 this.el.show_line_marks = true;
371 this.el.insert_spaces_instead_of_tabs = true;
372 this.el.show_line_numbers = true;
373 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
374 this.el.tab_width = 4;
375 this.el.highlight_current_line = true;
376 var child_0 = new Xcls_buffer( _this );
378 this.el.set_buffer ( child_0.el );
382 var description = Pango.FontDescription.from_string("monospace");
383 description.set_size(8000);
385 this.el.override_font(description);
388 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
389 } catch (GLib.Error e) {}
391 this.el.completion.unblock_interactive();
392 this.el.completion.select_on_show = true; // select
393 this.el.completion.show_headers = false;
394 this.el.completion.remember_info_visibility = true;
397 var attrs = new Gtk.SourceMarkAttributes();
398 var pink = Gdk.RGBA();
399 pink.parse ( "pink");
400 attrs.set_background ( pink);
401 attrs.set_icon_name ( "process-stop");
402 attrs.query_tooltip_text.connect(( mark) => {
403 //print("tooltip query? %s\n", mark.name);
407 this.el.set_mark_attributes ("ERR", attrs, 1);
409 var wattrs = new Gtk.SourceMarkAttributes();
410 var blue = Gdk.RGBA();
411 blue.parse ( "#ABF4EB");
412 wattrs.set_background ( blue);
413 wattrs.set_icon_name ( "process-stop");
414 wattrs.query_tooltip_text.connect(( mark) => {
415 //print("tooltip query? %s\n", mark.name);
419 this.el.set_mark_attributes ("WARN", wattrs, 1);
423 var dattrs = new Gtk.SourceMarkAttributes();
424 var purple = Gdk.RGBA();
425 purple.parse ( "#EEA9FF");
426 dattrs.set_background ( purple);
427 dattrs.set_icon_name ( "process-stop");
428 dattrs.query_tooltip_text.connect(( mark) => {
429 //print("tooltip query? %s\n", mark.name);
433 this.el.set_mark_attributes ("DEPR", dattrs, 1);
436 this.el.key_release_event.connect( (event) => {
438 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
439 print("SAVE: ctrl-S pressed");
440 _this.saveContents();
443 // print(event.key.keyval)
450 // user defined functions
451 public void load (string str) {
453 // show the help page for the active node..
454 //this.get('/Help').show();
457 // this.get('/BottomPane').el.set_current_page(0);
458 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
459 buf.set_text(str, str.length);
460 buf.set_undo_manager(null);
462 var lm = Gtk.SourceLanguageManager.get_default();
464 if (_this.file != null) {
465 lang = _this.file.language;
467 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
468 var lg = _this.file.content_type.length > 0 ?
469 lm.guess_language(_this.file.path, _this.file.content_type) :
470 lm.get_language(lang);
473 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
475 this.el.insert_spaces_instead_of_tabs = true;
477 print("sourcelanguage = %s\n", lg.name);
478 if (lg.name == "Vala") {
479 this.el.insert_spaces_instead_of_tabs = false;
483 this.el.grab_focus();
484 _this.save_button.el.sensitive = false;
487 public class Xcls_buffer : Object
489 public Gtk.SourceBuffer el;
490 private Editor _this;
494 public bool check_queued;
495 public int error_line;
496 public bool check_running;
499 public Xcls_buffer(Editor _owner )
503 this.el = new Gtk.SourceBuffer( null );
506 this.check_queued = false;
507 this.error_line = -1;
508 this.check_running = false;
510 // set gobject values
513 this.el.changed.connect( () => {
516 _this.save_button.el.sensitive = true;
517 print("EDITOR CHANGED");
522 // this.get('/LeftPanel.model').changed( str , false);
527 // user defined functions
528 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
530 this.error_line = validate_res.size;
532 if (this.error_line < 1) {
535 var tlines = this.el.get_line_count ();
537 var valiter = validate_res.map_iterator();
538 while (valiter.next()) {
540 // print("get inter\n");
541 var eline = valiter.get_key();
542 if (eline > tlines) {
545 this.el.get_iter_at_line( out iter, eline);
546 //print("mark line\n");
547 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
551 public string toString () {
555 this.el.get_start_iter(out s);
556 this.el.get_end_iter(out e);
557 var ret = this.el.get_text(s,e,true);
558 //print("TO STRING? " + ret);
561 public bool checkSyntax () {
563 if (this.check_running) {
564 print("Check is running\n");
565 if (this.check_queued) {
566 print("Check is already queued");
569 this.check_queued = true;
570 print("Adding queued Check ");
571 GLib.Timeout.add_seconds(1, () => {
572 this.check_queued = false;
581 var str = this.toString();
584 if (this.error_line > 0) {
587 this.el.get_bounds (out start, out end);
589 this.el.remove_source_marks (start, end, null);
591 if (str.length < 1) {
592 print("checkSyntax - empty string?\n");
596 if (_this.file.xtype == "PlainFile") {
598 // assume it's gtk...
599 this.check_running = true;
601 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
605 this.check_running = false;
611 if (_this.file == null) {
614 var p = _this.file.project.palete;
618 this.check_running = true;
621 if (_this.file.language == "js") {
622 this.check_running = false;
623 print("calling validate javascript\n");
624 Gee.HashMap<int,string> errors;
625 p.javascriptHasErrors(
626 _this.window.windowstate,
634 return this.highlightErrors(errors);
639 print("calling validate vala\n");
640 // clear the buttons.
643 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
650 this.check_running = false;
655 //print("done mark line\n");
657 return true; // at present allow saving - even if it's invalid..
659 public bool highlightErrorsJson (string type, Json.Object obj) {
662 this.el.get_bounds (out start, out end);
664 this.el.remove_source_marks (start, end, type);
667 // we should highlight other types of errors..
669 if (!obj.has_member(type)) {
670 print("Return has no errors\n");
674 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
675 _this.window.windowstate.state != WindowState.State.CODE
681 var err = obj.get_object_member(type);
684 if (_this.file == null) {
688 var valafn = _this.file.path;
690 if (_this.file.xtype != "PlainFile") {
697 var regex = new Regex("\\.bjs$");
701 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
702 } catch (GLib.RegexError e) {
709 if (!err.has_member(valafn)) {
710 print("File path has no errors\n");
714 var lines = err.get_object_member(valafn);
717 if (obj.has_member("line_offset")) {
718 offset = (int)obj.get_int_member("line_offset") + 1;
724 var tlines = this.el.get_line_count () +1;
726 lines.foreach_member((obj, line, node) => {
729 // print("get inter\n");
730 var eline = int.parse(line) - offset;
731 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
734 if (eline > tlines || eline < 0) {
737 this.el.get_iter_at_line( out iter, eline);
738 //print("mark line\n");
739 var msg = "Line: %d".printf(eline+1);
740 var ar = lines.get_array_member(line);
741 for (var i = 0 ; i < ar.get_length(); i++) {
742 msg += (msg.length > 0) ? "\n" : "";
743 msg += ar.get_string_element(i);
747 this.el.create_source_mark(msg, type, iter);