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 JsRender.NodeProp? prop;
25 public int pos_root_x;
26 public JsRender.JsRender? file;
27 public int pos_root_y;
28 public int last_search_end;
29 public Gtk.SourceSearchContext searchcontext;
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.last_search_end = 0;
47 this.searchcontext = null;
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 void scroll_to_line (int line) {
67 GLib.Timeout.add(500, () => {
69 var buf = this.view.el.get_buffer();
71 var sbuf = (Gtk.SourceBuffer) buf;
75 sbuf.get_iter_at_line(out iter, line);
76 this.view.el.scroll_to_iter(iter, 0.1f, true, 0.0f, 0.5f);
80 public int search (string txt) {
82 var s = new Gtk.SourceSearchSettings();
84 this.searchcontext = new Gtk.SourceSearchContext(this.buffer.el,s);
85 this.searchcontext .set_highlight(true);
86 s.set_search_text(txt);
87 Gtk.TextIter beg, st,en;
89 this.buffer.el.get_start_iter(out beg);
90 this.searchcontext.forward(beg, out st, out en);
91 this.last_search_end = 0;
93 return this.searchcontext.get_occurrences_count();
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.key_edit.el.show();
111 this.key_edit.el.text = prop.rtype + " " + prop.name;
114 this.view.load( file.toSource() );
115 this.key_edit.el.hide();
119 public bool saveContents () {
122 if (_this.file == null) {
128 var str = _this.buffer.toString();
130 _this.buffer.checkSyntax();
134 // LeftPanel.model.changed( str , false);
136 _this.save_button.el.sensitive = false;
138 // find the text for the node..
139 if (_this.file.xtype != "PlainFile") {
140 // in theory these properties have to exist!?!
142 this.window.windowstate.left_props.reload();
144 _this.file.setSource( str );
153 public void forwardSearch (bool change_focus) {
155 if (this.searchcontext == null) {
159 Gtk.TextIter beg, st,en;
161 this.buffer.el.get_iter_at_offset(out beg, this.last_search_end);
162 if (!this.searchcontext.forward(beg, out st, out en)) {
164 this.last_search_end = 0;
166 this.last_search_end = en.get_offset();
168 this.view.el.grab_focus();
170 this.buffer.el.place_cursor(st);
171 this.view.el.scroll_to_iter(st, 0.1f, true, 0.0f, 0.5f);
175 public void reset () {
180 this.searchcontext = null;
183 public class Xcls_Box2 : Object
186 private Editor _this;
192 public Xcls_Box2(Editor _owner )
195 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
199 // set gobject values
200 this.el.homogeneous = false;
201 var child_0 = new Xcls_save_button( _this );
203 this.el.pack_start ( child_0.el , false,false );
204 var child_1 = new Xcls_key_edit( _this );
206 this.el.pack_start ( child_1.el , true,true );
207 var child_2 = new Xcls_HScale5( _this );
209 this.el.pack_end ( child_2.el , true,true );
212 // user defined functions
214 public class Xcls_save_button : Object
216 public Gtk.Button el;
217 private Editor _this;
223 public Xcls_save_button(Editor _owner )
226 _this.save_button = this;
227 this.el = new Gtk.Button();
231 // set gobject values
232 this.el.label = "Save";
235 this.el.clicked.connect( () => {
236 _this.saveContents();
240 // user defined functions
243 public class Xcls_key_edit : Object
246 private Editor _this;
252 public Xcls_key_edit(Editor _owner )
255 _this.key_edit = this;
256 this.el = new Gtk.Entry();
260 // set gobject values
261 this.el.width_request = 100;
262 this.el.editable = false;
265 // user defined functions
268 public class Xcls_HScale5 : Object
270 public Gtk.HScale el;
271 private Editor _this;
277 public Xcls_HScale5(Editor _owner )
280 this.el = new Gtk.HScale.with_range (6, 30, 1);
284 // set gobject values
285 this.el.has_origin = true;
286 this.el.draw_value = true;
288 this.el.sensitive = true;
293 this.el.set_range(6,30);
294 this.el.set_value(8);
298 this.el.change_value.connect( (st, val ) => {
300 var description = Pango.FontDescription.from_string("monospace");
301 print("resize to %d", (int)val*1000);
302 description.set_size((int)val*1000);
303 _this.view.el.override_font(description);
308 // user defined functions
312 public class Xcls_RightEditor : Object
314 public Gtk.ScrolledWindow el;
315 private Editor _this;
321 public Xcls_RightEditor(Editor _owner )
324 _this.RightEditor = this;
325 this.el = new Gtk.ScrolledWindow( null, null );
329 // set gobject values
330 var child_0 = new Xcls_view( _this );
332 this.el.add ( child_0.el );
336 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
339 // user defined functions
341 public class Xcls_view : Object
343 public Gtk.SourceView el;
344 private Editor _this;
350 public Xcls_view(Editor _owner )
354 this.el = new Gtk.SourceView();
358 // set gobject values
359 this.el.auto_indent = true;
360 this.el.indent_width = 4;
361 this.el.show_line_marks = true;
362 this.el.insert_spaces_instead_of_tabs = true;
363 this.el.show_line_numbers = true;
364 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
365 this.el.tab_width = 4;
366 this.el.highlight_current_line = true;
367 var child_0 = new Xcls_buffer( _this );
369 this.el.set_buffer ( child_0.el );
373 var description = Pango.FontDescription.from_string("monospace");
374 description.set_size(8000);
376 this.el.override_font(description);
379 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
380 } catch (GLib.Error e) {}
382 this.el.completion.unblock_interactive();
383 this.el.completion.select_on_show = true; // select
384 this.el.completion.show_headers = false;
385 this.el.completion.remember_info_visibility = true;
388 var attrs = new Gtk.SourceMarkAttributes();
389 var pink = Gdk.RGBA();
390 pink.parse ( "pink");
391 attrs.set_background ( pink);
392 attrs.set_icon_name ( "process-stop");
393 attrs.query_tooltip_text.connect(( mark) => {
394 //print("tooltip query? %s\n", mark.name);
398 this.el.set_mark_attributes ("ERR", attrs, 1);
400 var wattrs = new Gtk.SourceMarkAttributes();
401 var blue = Gdk.RGBA();
402 blue.parse ( "#ABF4EB");
403 wattrs.set_background ( blue);
404 wattrs.set_icon_name ( "process-stop");
405 wattrs.query_tooltip_text.connect(( mark) => {
406 //print("tooltip query? %s\n", mark.name);
410 this.el.set_mark_attributes ("WARN", wattrs, 1);
414 var dattrs = new Gtk.SourceMarkAttributes();
415 var purple = Gdk.RGBA();
416 purple.parse ( "#EEA9FF");
417 dattrs.set_background ( purple);
418 dattrs.set_icon_name ( "process-stop");
419 dattrs.query_tooltip_text.connect(( mark) => {
420 //print("tooltip query? %s\n", mark.name);
424 this.el.set_mark_attributes ("DEPR", dattrs, 1);
427 this.el.key_release_event.connect( (event) => {
429 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
430 print("SAVE: ctrl-S pressed");
431 _this.saveContents();
434 // print(event.key.keyval)
441 // user defined functions
442 public void load (string str) {
444 // show the help page for the active node..
445 //this.get('/Help').show();
448 // this.get('/BottomPane').el.set_current_page(0);
449 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
450 buf.set_text(str, str.length);
451 buf.set_undo_manager(null);
453 var lm = Gtk.SourceLanguageManager.get_default();
455 if (_this.file != null) {
456 lang = _this.file.language;
458 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
459 var lg = _this.file.content_type.length > 0 ?
460 lm.guess_language(_this.file.path, _this.file.content_type) :
461 lm.get_language(lang);
464 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
466 this.el.insert_spaces_instead_of_tabs = true;
468 print("sourcelanguage = %s\n", lg.name);
469 if (lg.name == "Vala") {
470 this.el.insert_spaces_instead_of_tabs = false;
474 this.el.grab_focus();
475 _this.save_button.el.sensitive = false;
478 public class Xcls_buffer : Object
480 public Gtk.SourceBuffer el;
481 private Editor _this;
485 public bool check_queued;
486 public int error_line;
487 public bool check_running;
490 public Xcls_buffer(Editor _owner )
494 this.el = new Gtk.SourceBuffer( null );
497 this.check_queued = false;
498 this.error_line = -1;
499 this.check_running = false;
501 // set gobject values
504 this.el.changed.connect( () => {
507 _this.save_button.el.sensitive = true;
508 print("EDITOR CHANGED");
513 // this.get('/LeftPanel.model').changed( str , false);
518 // user defined functions
519 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
521 this.error_line = validate_res.size;
523 if (this.error_line < 1) {
526 var tlines = this.el.get_line_count ();
528 var valiter = validate_res.map_iterator();
529 while (valiter.next()) {
531 // print("get inter\n");
532 var eline = valiter.get_key();
533 if (eline > tlines) {
536 this.el.get_iter_at_line( out iter, eline);
537 //print("mark line\n");
538 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
542 public bool checkSyntax () {
544 if (this.check_running) {
545 print("Check is running\n");
546 if (this.check_queued) {
547 print("Check is already queued");
550 this.check_queued = true;
551 print("Adding queued Check ");
552 GLib.Timeout.add_seconds(1, () => {
553 this.check_queued = false;
562 var str = this.toString();
565 if (this.error_line > 0) {
568 this.el.get_bounds (out start, out end);
570 this.el.remove_source_marks (start, end, null);
572 if (str.length < 1) {
573 print("checkSyntax - empty string?\n");
577 if (_this.file.xtype == "PlainFile") {
579 // assume it's gtk...
580 this.check_running = true;
582 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
586 this.check_running = false;
592 if (_this.file == null) {
595 var p = _this.file.project.palete;
599 this.check_running = true;
602 if (_this.file.language == "js") {
603 this.check_running = false;
604 print("calling validate javascript\n");
605 Gee.HashMap<int,string> errors;
606 p.javascriptHasErrors(
607 _this.window.windowstate,
610 _this.file, // no reference not node?
613 return this.highlightErrors(errors);
618 print("calling validate vala\n");
619 // clear the buttons.
622 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
628 this.check_running = false;
633 //print("done mark line\n");
635 return true; // at present allow saving - even if it's invalid..
637 public string toString () {
641 this.el.get_start_iter(out s);
642 this.el.get_end_iter(out e);
643 var ret = this.el.get_text(s,e,true);
644 //print("TO STRING? " + ret);
647 public bool highlightErrorsJson (string type, Json.Object obj) {
650 this.el.get_bounds (out start, out end);
652 this.el.remove_source_marks (start, end, type);
655 // we should highlight other types of errors..
657 if (!obj.has_member(type)) {
658 print("Return has no errors\n");
662 if (_this.window.windowstate.state != WindowState.State.CODEONLY
669 var err = obj.get_object_member(type);
672 if (_this.file == null) {
676 var valafn = _this.file.path;
678 if (_this.file.xtype != "PlainFile") {
685 var regex = new Regex("\\.bjs$");
689 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
690 } catch (GLib.RegexError e) {
697 if (!err.has_member(valafn)) {
698 print("File path has no errors\n");
702 var lines = err.get_object_member(valafn);
705 if (obj.has_member("line_offset")) {
706 offset = (int)obj.get_int_member("line_offset") + 1;
712 var tlines = this.el.get_line_count () +1;
714 lines.foreach_member((obj, line, node) => {
717 // print("get inter\n");
718 var eline = int.parse(line) - offset;
719 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
722 if (eline > tlines || eline < 0) {
725 this.el.get_iter_at_line( out iter, eline);
726 //print("mark line\n");
727 var msg = "Line: %d".printf(eline+1);
728 var ar = lines.get_array_member(line);
729 for (var i = 0 ; i < ar.get_length(); i++) {
730 msg += (msg.length > 0) ? "\n" : "";
731 msg += ar.get_string_element(i);
735 this.el.create_source_mark(msg, type, iter);