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 () {
173 if (this.searchcontext == null) {
179 public class Xcls_Box2 : Object
182 private Editor _this;
188 public Xcls_Box2(Editor _owner )
191 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
195 // set gobject values
196 this.el.homogeneous = false;
197 var child_0 = new Xcls_save_button( _this );
199 this.el.pack_start ( child_0.el , false,false );
200 var child_1 = new Xcls_key_edit( _this );
202 this.el.pack_end ( child_1.el , true,true );
205 // user defined functions
207 public class Xcls_save_button : Object
209 public Gtk.Button el;
210 private Editor _this;
216 public Xcls_save_button(Editor _owner )
219 _this.save_button = this;
220 this.el = new Gtk.Button();
224 // set gobject values
225 this.el.label = "Save";
228 this.el.clicked.connect( () => {
229 _this.saveContents();
233 // user defined functions
236 public class Xcls_key_edit : Object
239 private Editor _this;
245 public Xcls_key_edit(Editor _owner )
248 _this.key_edit = this;
249 this.el = new Gtk.Entry();
253 // set gobject values
256 // user defined functions
260 public class Xcls_RightEditor : Object
262 public Gtk.ScrolledWindow el;
263 private Editor _this;
269 public Xcls_RightEditor(Editor _owner )
272 _this.RightEditor = this;
273 this.el = new Gtk.ScrolledWindow( null, null );
277 // set gobject values
278 var child_0 = new Xcls_view( _this );
280 this.el.add ( child_0.el );
284 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
287 // user defined functions
289 public class Xcls_view : Object
291 public Gtk.SourceView el;
292 private Editor _this;
298 public Xcls_view(Editor _owner )
302 this.el = new Gtk.SourceView();
306 // set gobject values
307 this.el.auto_indent = true;
308 this.el.indent_width = 4;
309 this.el.show_line_marks = true;
310 this.el.insert_spaces_instead_of_tabs = true;
311 this.el.show_line_numbers = true;
312 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
313 this.el.tab_width = 4;
314 this.el.highlight_current_line = true;
315 var child_0 = new Xcls_buffer( _this );
317 this.el.set_buffer ( child_0.el );
321 var description = Pango.FontDescription.from_string("monospace");
322 description.set_size(8000);
323 this.el.override_font(description);
326 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
327 this.el.completion.unblock_interactive();
328 this.el.completion.select_on_show = true; // select
329 this.el.completion.show_headers = false;
330 this.el.completion.remember_info_visibility = true;
333 var attrs = new Gtk.SourceMarkAttributes();
334 var pink = Gdk.RGBA();
335 pink.parse ( "pink");
336 attrs.set_background ( pink);
337 attrs.set_icon_name ( "process-stop");
338 attrs.query_tooltip_text.connect(( mark) => {
339 //print("tooltip query? %s\n", mark.name);
343 this.el.set_mark_attributes ("ERR", attrs, 1);
345 var wattrs = new Gtk.SourceMarkAttributes();
346 var blue = Gdk.RGBA();
347 blue.parse ( "#ABF4EB");
348 wattrs.set_background ( blue);
349 wattrs.set_icon_name ( "process-stop");
350 wattrs.query_tooltip_text.connect(( mark) => {
351 //print("tooltip query? %s\n", mark.name);
355 this.el.set_mark_attributes ("WARN", wattrs, 1);
359 var dattrs = new Gtk.SourceMarkAttributes();
360 var purple = Gdk.RGBA();
361 purple.parse ( "#EEA9FF");
362 dattrs.set_background ( purple);
363 dattrs.set_icon_name ( "process-stop");
364 dattrs.query_tooltip_text.connect(( mark) => {
365 //print("tooltip query? %s\n", mark.name);
369 this.el.set_mark_attributes ("DEPR", dattrs, 1);
372 this.el.key_release_event.connect( (event) => {
374 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
375 print("SAVE: ctrl-S pressed");
376 _this.saveContents();
379 // print(event.key.keyval)
386 // user defined functions
387 public void load (string str) {
389 // show the help page for the active node..
390 //this.get('/Help').show();
393 // this.get('/BottomPane').el.set_current_page(0);
394 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
395 buf.set_text(str, str.length);
396 buf.set_undo_manager(null);
398 var lm = Gtk.SourceLanguageManager.get_default();
400 if (_this.file != null) {
401 lang = _this.file.language;
403 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
404 var lg = _this.file.content_type.length > 0 ?
405 lm.guess_language(_this.file.path, _this.file.content_type) :
406 lm.get_language(lang);
409 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
411 this.el.insert_spaces_instead_of_tabs = true;
413 print("sourcelanguage = %s\n", lg.name);
414 if (lg.name == "Vala") {
415 this.el.insert_spaces_instead_of_tabs = false;
419 this.el.grab_focus();
420 _this.save_button.el.sensitive = false;
423 public class Xcls_buffer : Object
425 public Gtk.SourceBuffer el;
426 private Editor _this;
430 public bool check_queued;
431 public int error_line;
432 public bool check_running;
435 public Xcls_buffer(Editor _owner )
439 this.el = new Gtk.SourceBuffer( null );
442 this.check_queued = false;
443 this.error_line = -1;
444 this.check_running = false;
446 // set gobject values
449 this.el.changed.connect( () => {
452 _this.save_button.el.sensitive = true;
453 print("EDITOR CHANGED");
458 // this.get('/LeftPanel.model').changed( str , false);
463 // user defined functions
464 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
466 this.error_line = validate_res.size;
468 if (this.error_line < 1) {
471 var tlines = this.el.get_line_count ();
473 var valiter = validate_res.map_iterator();
474 while (valiter.next()) {
476 // print("get inter\n");
477 var eline = valiter.get_key();
478 if (eline > tlines) {
481 this.el.get_iter_at_line( out iter, eline);
482 //print("mark line\n");
483 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
487 public string toString () {
491 this.el.get_start_iter(out s);
492 this.el.get_end_iter(out e);
493 var ret = this.el.get_text(s,e,true);
494 //print("TO STRING? " + ret);
497 public bool checkSyntax () {
499 if (this.check_running) {
500 print("Check is running\n");
501 if (this.check_queued) {
502 print("Check is already queued");
505 this.check_queued = true;
506 print("Adding queued Check ");
507 GLib.Timeout.add_seconds(1, () => {
508 this.check_queued = false;
517 var str = this.toString();
520 if (this.error_line > 0) {
523 this.el.get_bounds (out start, out end);
525 this.el.remove_source_marks (start, end, null);
527 if (str.length < 1) {
528 print("checkSyntax - empty string?\n");
532 if (_this.file.xtype == "PlainFile") {
534 // assume it's gtk...
535 this.check_running = true;
537 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
541 this.check_running = false;
547 if (_this.file == null) {
550 var p = Palete.factory(_this.file.xtype);
554 this.check_running = true;
557 if (_this.file.language == "js") {
558 this.check_running = false;
559 print("calling validate javascript\n");
560 return this.highlightErrors(p.validateJavascript(
571 print("calling validate vala\n");
572 // clear the buttons.
575 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
582 this.check_running = false;
587 //print("done mark line\n");
589 return true; // at present allow saving - even if it's invalid..
591 public bool highlightErrorsJson (string type, Json.Object obj) {
594 this.el.get_bounds (out start, out end);
596 this.el.remove_source_marks (start, end, type);
599 // we should highlight other types of errors..
601 if (!obj.has_member(type)) {
602 print("Return has no errors\n");
606 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
607 _this.window.windowstate.state != WindowState.State.CODE
613 var err = obj.get_object_member(type);
616 if (_this.file == null) {
620 var valafn = _this.file.path;
622 if (_this.file.xtype != "PlainFile") {
629 var regex = new Regex("\\.bjs$");
633 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
634 } catch (GLib.RegexError e) {
641 if (!err.has_member(valafn)) {
642 print("File path has no errors\n");
646 var lines = err.get_object_member(valafn);
649 if (obj.has_member("line_offset")) {
650 offset = (int)obj.get_int_member("line_offset") + 1;
656 var tlines = this.el.get_line_count () +1;
658 lines.foreach_member((obj, line, node) => {
661 // print("get inter\n");
662 var eline = int.parse(line) - offset;
663 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
666 if (eline > tlines || eline < 0) {
669 this.el.get_iter_at_line( out iter, eline);
670 //print("mark line\n");
671 var msg = "Line: %d".printf(eline+1);
672 var ar = lines.get_array_member(line);
673 for (var i = 0 ; i < ar.get_length(); i++) {
674 msg += (msg.length > 0) ? "\n" : "";
675 msg += ar.get_string_element(i);
679 this.el.create_source_mark(msg, type, iter);