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;
28 public JsRender.JsRender? file;
30 public Gtk.SourceSearchContext searchcontext;
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;
49 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)
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();
168 public void forwardSearch (bool change_focus) {
170 if (this.searchcontext == null) {
174 Gtk.TextIter beg, st,en;
176 this.buffer.el.get_iter_at_offset(out beg, this.last_search_end);
177 if (!this.searchcontext.forward(beg, out st, out en)) {
179 this.last_search_end = 0;
181 this.last_search_end = en.get_offset();
183 this.view.el.grab_focus();
185 this.buffer.el.place_cursor(st);
186 this.view.el.scroll_to_iter(st, 0.1f, true, 0.0f, 0.5f);
190 public void reset () {
195 this.searchcontext = null;
198 public class Xcls_Box2 : Object
201 private Editor _this;
207 public Xcls_Box2(Editor _owner )
210 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
214 // set gobject values
215 this.el.homogeneous = false;
216 var child_0 = new Xcls_save_button( _this );
218 this.el.pack_start ( child_0.el , false,false );
219 var child_1 = new Xcls_key_edit( _this );
221 this.el.pack_start ( child_1.el , true,true );
222 var child_2 = new Xcls_HScale5( _this );
224 this.el.pack_end ( child_2.el , true,true );
227 // user defined functions
229 public class Xcls_save_button : Object
231 public Gtk.Button el;
232 private Editor _this;
238 public Xcls_save_button(Editor _owner )
241 _this.save_button = this;
242 this.el = new Gtk.Button();
246 // set gobject values
247 this.el.label = "Save";
250 this.el.clicked.connect( () => {
251 _this.saveContents();
255 // user defined functions
258 public class Xcls_key_edit : Object
261 private Editor _this;
267 public Xcls_key_edit(Editor _owner )
270 _this.key_edit = this;
271 this.el = new Gtk.Entry();
275 // set gobject values
276 this.el.width_request = 100;
277 this.el.editable = false;
280 // user defined functions
283 public class Xcls_HScale5 : Object
285 public Gtk.HScale el;
286 private Editor _this;
292 public Xcls_HScale5(Editor _owner )
295 this.el = new Gtk.HScale.with_range (6, 30, 1);
299 // set gobject values
300 this.el.has_origin = true;
301 this.el.draw_value = true;
303 this.el.sensitive = true;
308 this.el.set_range(6,30);
309 this.el.set_value(8);
313 this.el.change_value.connect( (st, val ) => {
315 var description = Pango.FontDescription.from_string("monospace");
316 print("resize to %d", (int)val*1000);
317 description.set_size((int)val*1000);
318 _this.view.el.override_font(description);
323 // user defined functions
327 public class Xcls_RightEditor : Object
329 public Gtk.ScrolledWindow el;
330 private Editor _this;
336 public Xcls_RightEditor(Editor _owner )
339 _this.RightEditor = this;
340 this.el = new Gtk.ScrolledWindow( null, null );
344 // set gobject values
345 var child_0 = new Xcls_view( _this );
347 this.el.add ( child_0.el );
351 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
354 // user defined functions
356 public class Xcls_view : Object
358 public Gtk.SourceView el;
359 private Editor _this;
365 public Xcls_view(Editor _owner )
369 this.el = new Gtk.SourceView();
373 // set gobject values
374 this.el.auto_indent = true;
375 this.el.indent_width = 4;
376 this.el.show_line_marks = true;
377 this.el.insert_spaces_instead_of_tabs = true;
378 this.el.show_line_numbers = true;
379 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
380 this.el.tab_width = 4;
381 this.el.highlight_current_line = true;
382 var child_0 = new Xcls_buffer( _this );
384 this.el.set_buffer ( child_0.el );
388 var description = Pango.FontDescription.from_string("monospace");
389 description.set_size(8000);
391 this.el.override_font(description);
394 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
395 } catch (GLib.Error e) {}
397 this.el.completion.unblock_interactive();
398 this.el.completion.select_on_show = true; // select
399 this.el.completion.show_headers = false;
400 this.el.completion.remember_info_visibility = true;
403 var attrs = new Gtk.SourceMarkAttributes();
404 var pink = Gdk.RGBA();
405 pink.parse ( "pink");
406 attrs.set_background ( pink);
407 attrs.set_icon_name ( "process-stop");
408 attrs.query_tooltip_text.connect(( mark) => {
409 //print("tooltip query? %s\n", mark.name);
413 this.el.set_mark_attributes ("ERR", attrs, 1);
415 var wattrs = new Gtk.SourceMarkAttributes();
416 var blue = Gdk.RGBA();
417 blue.parse ( "#ABF4EB");
418 wattrs.set_background ( blue);
419 wattrs.set_icon_name ( "process-stop");
420 wattrs.query_tooltip_text.connect(( mark) => {
421 //print("tooltip query? %s\n", mark.name);
425 this.el.set_mark_attributes ("WARN", wattrs, 1);
429 var dattrs = new Gtk.SourceMarkAttributes();
430 var purple = Gdk.RGBA();
431 purple.parse ( "#EEA9FF");
432 dattrs.set_background ( purple);
433 dattrs.set_icon_name ( "process-stop");
434 dattrs.query_tooltip_text.connect(( mark) => {
435 //print("tooltip query? %s\n", mark.name);
439 this.el.set_mark_attributes ("DEPR", dattrs, 1);
442 this.el.key_release_event.connect( (event) => {
444 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
445 print("SAVE: ctrl-S pressed");
446 _this.saveContents();
449 // print(event.key.keyval)
456 // user defined functions
457 public void load (string str) {
459 // show the help page for the active node..
460 //this.get('/Help').show();
463 // this.get('/BottomPane').el.set_current_page(0);
464 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
465 buf.set_text(str, str.length);
466 buf.set_undo_manager(null);
468 var lm = Gtk.SourceLanguageManager.get_default();
470 if (_this.file != null) {
471 lang = _this.file.language;
473 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
474 var lg = _this.file.content_type.length > 0 ?
475 lm.guess_language(_this.file.path, _this.file.content_type) :
476 lm.get_language(lang);
479 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
481 this.el.insert_spaces_instead_of_tabs = true;
483 print("sourcelanguage = %s\n", lg.name);
484 if (lg.name == "Vala") {
485 this.el.insert_spaces_instead_of_tabs = false;
489 this.el.grab_focus();
490 _this.save_button.el.sensitive = false;
493 public class Xcls_buffer : Object
495 public Gtk.SourceBuffer el;
496 private Editor _this;
500 public bool check_queued;
501 public int error_line;
502 public bool check_running;
505 public Xcls_buffer(Editor _owner )
509 this.el = new Gtk.SourceBuffer( null );
512 this.check_queued = false;
513 this.error_line = -1;
514 this.check_running = false;
516 // set gobject values
519 this.el.changed.connect( () => {
522 _this.save_button.el.sensitive = true;
523 print("EDITOR CHANGED");
528 // this.get('/LeftPanel.model').changed( str , false);
533 // user defined functions
534 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
536 this.error_line = validate_res.size;
538 if (this.error_line < 1) {
541 var tlines = this.el.get_line_count ();
543 var valiter = validate_res.map_iterator();
544 while (valiter.next()) {
546 // print("get inter\n");
547 var eline = valiter.get_key();
548 if (eline > tlines) {
551 this.el.get_iter_at_line( out iter, eline);
552 //print("mark line\n");
553 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
557 public bool checkSyntax () {
559 if (this.check_running) {
560 print("Check is running\n");
561 if (this.check_queued) {
562 print("Check is already queued");
565 this.check_queued = true;
566 print("Adding queued Check ");
567 GLib.Timeout.add_seconds(1, () => {
568 this.check_queued = false;
577 var str = this.toString();
580 if (this.error_line > 0) {
583 this.el.get_bounds (out start, out end);
585 this.el.remove_source_marks (start, end, null);
587 if (str.length < 1) {
588 print("checkSyntax - empty string?\n");
592 if (_this.file.xtype == "PlainFile") {
594 // assume it's gtk...
595 this.check_running = true;
597 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
601 this.check_running = false;
607 if (_this.file == null) {
610 var p = _this.file.project.palete;
614 this.check_running = true;
617 if (_this.file.language == "js") {
618 this.check_running = false;
619 print("calling validate javascript\n");
620 Gee.HashMap<int,string> errors;
621 p.javascriptHasErrors(
622 _this.window.windowstate,
630 return this.highlightErrors(errors);
635 print("calling validate vala\n");
636 // clear the buttons.
639 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
646 this.check_running = false;
651 //print("done mark line\n");
653 return true; // at present allow saving - even if it's invalid..
655 public string toString () {
659 this.el.get_start_iter(out s);
660 this.el.get_end_iter(out e);
661 var ret = this.el.get_text(s,e,true);
662 //print("TO STRING? " + ret);
665 public bool highlightErrorsJson (string type, Json.Object obj) {
668 this.el.get_bounds (out start, out end);
670 this.el.remove_source_marks (start, end, type);
673 // we should highlight other types of errors..
675 if (!obj.has_member(type)) {
676 print("Return has no errors\n");
680 if (_this.window.windowstate.state != WindowState.State.CODEONLY
687 var err = obj.get_object_member(type);
690 if (_this.file == null) {
694 var valafn = _this.file.path;
696 if (_this.file.xtype != "PlainFile") {
703 var regex = new Regex("\\.bjs$");
707 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
708 } catch (GLib.RegexError e) {
715 if (!err.has_member(valafn)) {
716 print("File path has no errors\n");
720 var lines = err.get_object_member(valafn);
723 if (obj.has_member("line_offset")) {
724 offset = (int)obj.get_int_member("line_offset") + 1;
730 var tlines = this.el.get_line_count () +1;
732 lines.foreach_member((obj, line, node) => {
735 // print("get inter\n");
736 var eline = int.parse(line) - offset;
737 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
740 if (eline > tlines || eline < 0) {
743 this.el.get_iter_at_line( out iter, eline);
744 //print("mark line\n");
745 var msg = "Line: %d".printf(eline+1);
746 var ar = lines.get_array_member(line);
747 for (var i = 0 ; i < ar.get_length(); i++) {
748 msg += (msg.length > 0) ? "\n" : "";
749 msg += ar.get_string_element(i);
753 this.el.create_source_mark(msg, type, iter);