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_end ( child_1.el , true,true );
219 // user defined functions
221 public class Xcls_save_button : Object
223 public Gtk.Button el;
224 private Editor _this;
230 public Xcls_save_button(Editor _owner )
233 _this.save_button = this;
234 this.el = new Gtk.Button();
238 // set gobject values
239 this.el.label = "Save";
242 this.el.clicked.connect( () => {
243 _this.saveContents();
247 // user defined functions
250 public class Xcls_key_edit : Object
253 private Editor _this;
259 public Xcls_key_edit(Editor _owner )
262 _this.key_edit = this;
263 this.el = new Gtk.Entry();
267 // set gobject values
270 // user defined functions
274 public class Xcls_RightEditor : Object
276 public Gtk.ScrolledWindow el;
277 private Editor _this;
283 public Xcls_RightEditor(Editor _owner )
286 _this.RightEditor = this;
287 this.el = new Gtk.ScrolledWindow( null, null );
291 // set gobject values
292 var child_0 = new Xcls_view( _this );
294 this.el.add ( child_0.el );
298 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
301 // user defined functions
303 public class Xcls_view : Object
305 public Gtk.SourceView el;
306 private Editor _this;
312 public Xcls_view(Editor _owner )
316 this.el = new Gtk.SourceView();
320 // set gobject values
321 this.el.auto_indent = true;
322 this.el.indent_width = 4;
323 this.el.show_line_marks = true;
324 this.el.insert_spaces_instead_of_tabs = true;
325 this.el.show_line_numbers = true;
326 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
327 this.el.tab_width = 4;
328 this.el.highlight_current_line = true;
329 var child_0 = new Xcls_buffer( _this );
331 this.el.set_buffer ( child_0.el );
335 var description = Pango.FontDescription.from_string("monospace");
336 description.set_size(8000);
338 this.el.override_font(description);
341 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
342 } catch (GLib.Error e) {}
344 this.el.completion.unblock_interactive();
345 this.el.completion.select_on_show = true; // select
346 this.el.completion.show_headers = false;
347 this.el.completion.remember_info_visibility = true;
350 var attrs = new Gtk.SourceMarkAttributes();
351 var pink = Gdk.RGBA();
352 pink.parse ( "pink");
353 attrs.set_background ( pink);
354 attrs.set_icon_name ( "process-stop");
355 attrs.query_tooltip_text.connect(( mark) => {
356 //print("tooltip query? %s\n", mark.name);
360 this.el.set_mark_attributes ("ERR", attrs, 1);
362 var wattrs = new Gtk.SourceMarkAttributes();
363 var blue = Gdk.RGBA();
364 blue.parse ( "#ABF4EB");
365 wattrs.set_background ( blue);
366 wattrs.set_icon_name ( "process-stop");
367 wattrs.query_tooltip_text.connect(( mark) => {
368 //print("tooltip query? %s\n", mark.name);
372 this.el.set_mark_attributes ("WARN", wattrs, 1);
376 var dattrs = new Gtk.SourceMarkAttributes();
377 var purple = Gdk.RGBA();
378 purple.parse ( "#EEA9FF");
379 dattrs.set_background ( purple);
380 dattrs.set_icon_name ( "process-stop");
381 dattrs.query_tooltip_text.connect(( mark) => {
382 //print("tooltip query? %s\n", mark.name);
386 this.el.set_mark_attributes ("DEPR", dattrs, 1);
389 this.el.key_release_event.connect( (event) => {
391 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
392 print("SAVE: ctrl-S pressed");
393 _this.saveContents();
396 // print(event.key.keyval)
403 // user defined functions
404 public void load (string str) {
406 // show the help page for the active node..
407 //this.get('/Help').show();
410 // this.get('/BottomPane').el.set_current_page(0);
411 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
412 buf.set_text(str, str.length);
413 buf.set_undo_manager(null);
415 var lm = Gtk.SourceLanguageManager.get_default();
417 if (_this.file != null) {
418 lang = _this.file.language;
420 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
421 var lg = _this.file.content_type.length > 0 ?
422 lm.guess_language(_this.file.path, _this.file.content_type) :
423 lm.get_language(lang);
426 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
428 this.el.insert_spaces_instead_of_tabs = true;
430 print("sourcelanguage = %s\n", lg.name);
431 if (lg.name == "Vala") {
432 this.el.insert_spaces_instead_of_tabs = false;
436 this.el.grab_focus();
437 _this.save_button.el.sensitive = false;
440 public class Xcls_buffer : Object
442 public Gtk.SourceBuffer el;
443 private Editor _this;
447 public bool check_queued;
448 public int error_line;
449 public bool check_running;
452 public Xcls_buffer(Editor _owner )
456 this.el = new Gtk.SourceBuffer( null );
459 this.check_queued = false;
460 this.error_line = -1;
461 this.check_running = false;
463 // set gobject values
466 this.el.changed.connect( () => {
469 _this.save_button.el.sensitive = true;
470 print("EDITOR CHANGED");
475 // this.get('/LeftPanel.model').changed( str , false);
480 // user defined functions
481 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
483 this.error_line = validate_res.size;
485 if (this.error_line < 1) {
488 var tlines = this.el.get_line_count ();
490 var valiter = validate_res.map_iterator();
491 while (valiter.next()) {
493 // print("get inter\n");
494 var eline = valiter.get_key();
495 if (eline > tlines) {
498 this.el.get_iter_at_line( out iter, eline);
499 //print("mark line\n");
500 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
504 public string toString () {
508 this.el.get_start_iter(out s);
509 this.el.get_end_iter(out e);
510 var ret = this.el.get_text(s,e,true);
511 //print("TO STRING? " + ret);
514 public bool checkSyntax () {
516 if (this.check_running) {
517 print("Check is running\n");
518 if (this.check_queued) {
519 print("Check is already queued");
522 this.check_queued = true;
523 print("Adding queued Check ");
524 GLib.Timeout.add_seconds(1, () => {
525 this.check_queued = false;
534 var str = this.toString();
537 if (this.error_line > 0) {
540 this.el.get_bounds (out start, out end);
542 this.el.remove_source_marks (start, end, null);
544 if (str.length < 1) {
545 print("checkSyntax - empty string?\n");
549 if (_this.file.xtype == "PlainFile") {
551 // assume it's gtk...
552 this.check_running = true;
554 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
558 this.check_running = false;
564 if (_this.file == null) {
567 var p = Palete.factory(_this.file.xtype);
571 this.check_running = true;
574 if (_this.file.language == "js") {
575 this.check_running = false;
576 print("calling validate javascript\n");
577 return this.highlightErrors(p.validateJavascript(
588 print("calling validate vala\n");
589 // clear the buttons.
592 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
599 this.check_running = false;
604 //print("done mark line\n");
606 return true; // at present allow saving - even if it's invalid..
608 public bool highlightErrorsJson (string type, Json.Object obj) {
611 this.el.get_bounds (out start, out end);
613 this.el.remove_source_marks (start, end, type);
616 // we should highlight other types of errors..
618 if (!obj.has_member(type)) {
619 print("Return has no errors\n");
623 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
624 _this.window.windowstate.state != WindowState.State.CODE
630 var err = obj.get_object_member(type);
633 if (_this.file == null) {
637 var valafn = _this.file.path;
639 if (_this.file.xtype != "PlainFile") {
646 var regex = new Regex("\\.bjs$");
650 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
651 } catch (GLib.RegexError e) {
658 if (!err.has_member(valafn)) {
659 print("File path has no errors\n");
663 var lines = err.get_object_member(valafn);
666 if (obj.has_member("line_offset")) {
667 offset = (int)obj.get_int_member("line_offset") + 1;
673 var tlines = this.el.get_line_count () +1;
675 lines.foreach_member((obj, line, node) => {
678 // print("get inter\n");
679 var eline = int.parse(line) - offset;
680 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
683 if (eline > tlines || eline < 0) {
686 this.el.get_iter_at_line( out iter, eline);
687 //print("mark line\n");
688 var msg = "Line: %d".printf(eline+1);
689 var ar = lines.get_array_member(line);
690 for (var i = 0 ; i < ar.get_length(); i++) {
691 msg += (msg.length > 0) ? "\n" : "";
692 msg += ar.get_string_element(i);
696 this.el.create_source_mark(msg, type, iter);