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;
28 public Gtk.SourceSearchContext searchcontext;
29 public JsRender.JsRender file;
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.searchcontext = null;
53 this.el.homogeneous = false;
54 this.el.hexpand = true;
55 var child_0 = new Xcls_Box2( _this );
57 this.el.pack_start ( child_0.el , false,true );
58 var child_1 = new Xcls_RightEditor( _this );
60 this.el.pack_end ( child_1.el , true,true );
63 // user defined functions
64 public bool saveContents () {
67 if (_this.file == null) {
75 var str = _this.buffer.toString();
77 _this.buffer.checkSyntax();
81 // LeftPanel.model.changed( str , false);
83 _this.save_button.el.sensitive = false;
85 // find the text for the node..
86 if (_this.file.xtype != "PlainFile") {
87 if (ptype == "listener") {
88 this.node.listeners.set(key,str);
91 this.node.props.set(key,str);
94 _this.file.setSource( str );
103 public void scroll_to_line (int line) {
105 GLib.Timeout.add(500, () => {
107 var buf = this.view.el.get_buffer();
109 var sbuf = (Gtk.SourceBuffer) buf;
113 sbuf.get_iter_at_line(out iter, line);
114 this.view.el.scroll_to_iter(iter, 0.1f, true, 0.0f, 0.5f);
118 public int search (string txt) {
120 var s = new Gtk.SourceSearchSettings();
122 this.searchcontext = new Gtk.SourceSearchContext(this.buffer.el,s);
123 this.searchcontext .set_highlight(true);
124 s.set_search_text(txt);
125 Gtk.TextIter beg, st,en;
127 this.buffer.el.get_start_iter(out beg);
128 this.searchcontext.forward(beg, out st, out en);
131 return this.searchcontext.get_occurrences_count();
137 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
143 this.searchcontext = null;
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();
169 public void forwardSearch () {
172 public class Xcls_Box2 : Object
175 private Editor _this;
181 public Xcls_Box2(Editor _owner )
184 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
188 // set gobject values
189 this.el.homogeneous = false;
190 var child_0 = new Xcls_save_button( _this );
192 this.el.pack_start ( child_0.el , false,false );
193 var child_1 = new Xcls_key_edit( _this );
195 this.el.pack_end ( child_1.el , true,true );
198 // user defined functions
200 public class Xcls_save_button : Object
202 public Gtk.Button el;
203 private Editor _this;
209 public Xcls_save_button(Editor _owner )
212 _this.save_button = this;
213 this.el = new Gtk.Button();
217 // set gobject values
218 this.el.label = "Save";
221 this.el.clicked.connect( () => {
222 _this.saveContents();
226 // user defined functions
229 public class Xcls_key_edit : Object
232 private Editor _this;
238 public Xcls_key_edit(Editor _owner )
241 _this.key_edit = this;
242 this.el = new Gtk.Entry();
246 // set gobject values
249 // user defined functions
253 public class Xcls_RightEditor : Object
255 public Gtk.ScrolledWindow el;
256 private Editor _this;
262 public Xcls_RightEditor(Editor _owner )
265 _this.RightEditor = this;
266 this.el = new Gtk.ScrolledWindow( null, null );
270 // set gobject values
271 var child_0 = new Xcls_view( _this );
273 this.el.add ( child_0.el );
277 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
280 // user defined functions
282 public class Xcls_view : Object
284 public Gtk.SourceView el;
285 private Editor _this;
291 public Xcls_view(Editor _owner )
295 this.el = new Gtk.SourceView();
299 // set gobject values
300 this.el.auto_indent = true;
301 this.el.indent_width = 4;
302 this.el.show_line_marks = true;
303 this.el.insert_spaces_instead_of_tabs = true;
304 this.el.show_line_numbers = true;
305 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
306 this.el.tab_width = 4;
307 this.el.highlight_current_line = true;
308 var child_0 = new Xcls_buffer( _this );
310 this.el.set_buffer ( child_0.el );
314 var description = Pango.FontDescription.from_string("monospace");
315 description.set_size(8000);
316 this.el.override_font(description);
319 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
320 this.el.completion.unblock_interactive();
321 this.el.completion.select_on_show = true; // select
322 this.el.completion.show_headers = false;
323 this.el.completion.remember_info_visibility = true;
326 var attrs = new Gtk.SourceMarkAttributes();
327 var pink = Gdk.RGBA();
328 pink.parse ( "pink");
329 attrs.set_background ( pink);
330 attrs.set_icon_name ( "process-stop");
331 attrs.query_tooltip_text.connect(( mark) => {
332 //print("tooltip query? %s\n", mark.name);
336 this.el.set_mark_attributes ("ERR", attrs, 1);
338 var wattrs = new Gtk.SourceMarkAttributes();
339 var blue = Gdk.RGBA();
340 blue.parse ( "#ABF4EB");
341 wattrs.set_background ( blue);
342 wattrs.set_icon_name ( "process-stop");
343 wattrs.query_tooltip_text.connect(( mark) => {
344 //print("tooltip query? %s\n", mark.name);
348 this.el.set_mark_attributes ("WARN", wattrs, 1);
352 var dattrs = new Gtk.SourceMarkAttributes();
353 var purple = Gdk.RGBA();
354 purple.parse ( "#EEA9FF");
355 dattrs.set_background ( purple);
356 dattrs.set_icon_name ( "process-stop");
357 dattrs.query_tooltip_text.connect(( mark) => {
358 //print("tooltip query? %s\n", mark.name);
362 this.el.set_mark_attributes ("DEPR", dattrs, 1);
365 this.el.key_release_event.connect( (event) => {
367 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
368 print("SAVE: ctrl-S pressed");
369 _this.saveContents();
372 // print(event.key.keyval)
379 // user defined functions
380 public void load (string str) {
382 // show the help page for the active node..
383 //this.get('/Help').show();
386 // this.get('/BottomPane').el.set_current_page(0);
387 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
388 buf.set_text(str, str.length);
389 buf.set_undo_manager(null);
391 var lm = Gtk.SourceLanguageManager.get_default();
393 if (_this.file != null) {
394 lang = _this.file.language;
396 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
397 var lg = _this.file.content_type.length > 0 ?
398 lm.guess_language(_this.file.path, _this.file.content_type) :
399 lm.get_language(lang);
402 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
404 this.el.insert_spaces_instead_of_tabs = true;
406 print("sourcelanguage = %s\n", lg.name);
407 if (lg.name == "Vala") {
408 this.el.insert_spaces_instead_of_tabs = false;
412 this.el.grab_focus();
413 _this.save_button.el.sensitive = false;
416 public class Xcls_buffer : Object
418 public Gtk.SourceBuffer el;
419 private Editor _this;
423 public bool check_queued;
424 public int error_line;
425 public bool check_running;
428 public Xcls_buffer(Editor _owner )
432 this.el = new Gtk.SourceBuffer( null );
435 this.check_queued = false;
436 this.error_line = -1;
437 this.check_running = false;
439 // set gobject values
442 this.el.changed.connect( () => {
445 _this.save_button.el.sensitive = true;
446 print("EDITOR CHANGED");
451 // this.get('/LeftPanel.model').changed( str , false);
456 // user defined functions
457 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
459 this.error_line = validate_res.size;
461 if (this.error_line < 1) {
464 var tlines = this.el.get_line_count ();
466 var valiter = validate_res.map_iterator();
467 while (valiter.next()) {
469 // print("get inter\n");
470 var eline = valiter.get_key();
471 if (eline > tlines) {
474 this.el.get_iter_at_line( out iter, eline);
475 //print("mark line\n");
476 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
480 public string toString () {
484 this.el.get_start_iter(out s);
485 this.el.get_end_iter(out e);
486 var ret = this.el.get_text(s,e,true);
487 //print("TO STRING? " + ret);
490 public bool checkSyntax () {
492 if (this.check_running) {
493 print("Check is running\n");
494 if (this.check_queued) {
495 print("Check is already queued");
498 this.check_queued = true;
499 print("Adding queued Check ");
500 GLib.Timeout.add_seconds(1, () => {
501 this.check_queued = false;
510 var str = this.toString();
513 if (this.error_line > 0) {
516 this.el.get_bounds (out start, out end);
518 this.el.remove_source_marks (start, end, null);
520 if (str.length < 1) {
521 print("checkSyntax - empty string?\n");
525 if (_this.file.xtype == "PlainFile") {
527 // assume it's gtk...
528 this.check_running = true;
530 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
534 this.check_running = false;
540 if (_this.file == null) {
543 var p = Palete.factory(_this.file.xtype);
547 this.check_running = true;
550 if (_this.file.language == "js") {
551 this.check_running = false;
552 print("calling validate javascript\n");
553 return this.highlightErrors(p.validateJavascript(
564 print("calling validate vala\n");
565 // clear the buttons.
568 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
575 this.check_running = false;
580 //print("done mark line\n");
582 return true; // at present allow saving - even if it's invalid..
584 public bool highlightErrorsJson (string type, Json.Object obj) {
587 this.el.get_bounds (out start, out end);
589 this.el.remove_source_marks (start, end, type);
592 // we should highlight other types of errors..
594 if (!obj.has_member(type)) {
595 print("Return has no errors\n");
599 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
600 _this.window.windowstate.state != WindowState.State.CODE
606 var err = obj.get_object_member(type);
609 if (_this.file == null) {
613 var valafn = _this.file.path;
615 if (_this.file.xtype != "PlainFile") {
622 var regex = new Regex("\\.bjs$");
626 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
627 } catch (GLib.RegexError e) {
634 if (!err.has_member(valafn)) {
635 print("File path has no errors\n");
639 var lines = err.get_object_member(valafn);
642 if (obj.has_member("line_offset")) {
643 offset = (int)obj.get_int_member("line_offset") + 1;
649 var tlines = this.el.get_line_count () +1;
651 lines.foreach_member((obj, line, node) => {
654 // print("get inter\n");
655 var eline = int.parse(line) - offset;
656 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
659 if (eline > tlines || eline < 0) {
662 this.el.get_iter_at_line( out iter, eline);
663 //print("mark line\n");
664 var msg = "Line: %d".printf(eline+1);
665 var ar = lines.get_array_member(line);
666 for (var i = 0 ; i < ar.get_length(); i++) {
667 msg += (msg.length > 0) ? "\n" : "";
668 msg += ar.get_string_element(i);
672 this.el.create_source_mark(msg, type, iter);