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.sourcecontext = new Gtk.SourceSearchContext(this.buffer.el,s);
123 this.sourcecontext .set_highlight(true);
124 s.set_search_text(txt);
125 return this.sourcecontext.get_occurences_count();
128 var s = new Gtk.SourceSearchSettings();
129 var buf = (Gtk.SourceBuffer) this.sourceview.el.get_buffer();
130 this.sourcecontext = new Gtk.SourceSearchContext(buf,s);
131 this.sourcecontext.set_highlight(true);
132 s.set_search_text(txt);
133 return this.sourcecontext.get_occurrences_count();
138 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
146 if (file.xtype != "PlainFile") {
152 // find the text for the node..
153 if (ptype == "listener") {
154 val = node.listeners.get(key);
157 val = node.props.get(key);
160 this.key_edit.el.show();
161 this.key_edit.el.text = key;
164 this.view.load( file.toSource() );
165 this.key_edit.el.hide();
170 public class Xcls_Box2 : Object
173 private Editor _this;
179 public Xcls_Box2(Editor _owner )
182 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
186 // set gobject values
187 this.el.homogeneous = false;
188 var child_0 = new Xcls_save_button( _this );
190 this.el.pack_start ( child_0.el , false,false );
191 var child_1 = new Xcls_key_edit( _this );
193 this.el.pack_end ( child_1.el , true,true );
196 // user defined functions
198 public class Xcls_save_button : Object
200 public Gtk.Button el;
201 private Editor _this;
207 public Xcls_save_button(Editor _owner )
210 _this.save_button = this;
211 this.el = new Gtk.Button();
215 // set gobject values
216 this.el.label = "Save";
219 this.el.clicked.connect( () => {
220 _this.saveContents();
224 // user defined functions
227 public class Xcls_key_edit : Object
230 private Editor _this;
236 public Xcls_key_edit(Editor _owner )
239 _this.key_edit = this;
240 this.el = new Gtk.Entry();
244 // set gobject values
247 // user defined functions
251 public class Xcls_RightEditor : Object
253 public Gtk.ScrolledWindow el;
254 private Editor _this;
260 public Xcls_RightEditor(Editor _owner )
263 _this.RightEditor = this;
264 this.el = new Gtk.ScrolledWindow( null, null );
268 // set gobject values
269 var child_0 = new Xcls_view( _this );
271 this.el.add ( child_0.el );
275 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
278 // user defined functions
280 public class Xcls_view : Object
282 public Gtk.SourceView el;
283 private Editor _this;
289 public Xcls_view(Editor _owner )
293 this.el = new Gtk.SourceView();
297 // set gobject values
298 this.el.auto_indent = true;
299 this.el.indent_width = 4;
300 this.el.show_line_marks = true;
301 this.el.insert_spaces_instead_of_tabs = true;
302 this.el.show_line_numbers = true;
303 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
304 this.el.tab_width = 4;
305 this.el.highlight_current_line = true;
306 var child_0 = new Xcls_buffer( _this );
308 this.el.set_buffer ( child_0.el );
312 var description = Pango.FontDescription.from_string("monospace");
313 description.set_size(8000);
314 this.el.override_font(description);
317 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
318 this.el.completion.unblock_interactive();
319 this.el.completion.select_on_show = true; // select
320 this.el.completion.show_headers = false;
321 this.el.completion.remember_info_visibility = true;
324 var attrs = new Gtk.SourceMarkAttributes();
325 var pink = Gdk.RGBA();
326 pink.parse ( "pink");
327 attrs.set_background ( pink);
328 attrs.set_icon_name ( "process-stop");
329 attrs.query_tooltip_text.connect(( mark) => {
330 //print("tooltip query? %s\n", mark.name);
334 this.el.set_mark_attributes ("ERR", attrs, 1);
336 var wattrs = new Gtk.SourceMarkAttributes();
337 var blue = Gdk.RGBA();
338 blue.parse ( "#ABF4EB");
339 wattrs.set_background ( blue);
340 wattrs.set_icon_name ( "process-stop");
341 wattrs.query_tooltip_text.connect(( mark) => {
342 //print("tooltip query? %s\n", mark.name);
346 this.el.set_mark_attributes ("WARN", wattrs, 1);
350 var dattrs = new Gtk.SourceMarkAttributes();
351 var purple = Gdk.RGBA();
352 purple.parse ( "#EEA9FF");
353 dattrs.set_background ( purple);
354 dattrs.set_icon_name ( "process-stop");
355 dattrs.query_tooltip_text.connect(( mark) => {
356 //print("tooltip query? %s\n", mark.name);
360 this.el.set_mark_attributes ("DEPR", dattrs, 1);
363 this.el.key_release_event.connect( (event) => {
365 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
366 print("SAVE: ctrl-S pressed");
367 _this.saveContents();
370 // print(event.key.keyval)
377 // user defined functions
378 public void load (string str) {
380 // show the help page for the active node..
381 //this.get('/Help').show();
384 // this.get('/BottomPane').el.set_current_page(0);
385 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
386 buf.set_text(str, str.length);
387 buf.set_undo_manager(null);
389 var lm = Gtk.SourceLanguageManager.get_default();
391 if (_this.file != null) {
392 lang = _this.file.language;
394 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
395 var lg = _this.file.content_type.length > 0 ?
396 lm.guess_language(_this.file.path, _this.file.content_type) :
397 lm.get_language(lang);
400 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
402 this.el.insert_spaces_instead_of_tabs = true;
404 print("sourcelanguage = %s\n", lg.name);
405 if (lg.name == "Vala") {
406 this.el.insert_spaces_instead_of_tabs = false;
410 this.el.grab_focus();
411 _this.save_button.el.sensitive = false;
414 public class Xcls_buffer : Object
416 public Gtk.SourceBuffer el;
417 private Editor _this;
421 public bool check_queued;
422 public int error_line;
423 public bool check_running;
426 public Xcls_buffer(Editor _owner )
430 this.el = new Gtk.SourceBuffer( null );
433 this.check_queued = false;
434 this.error_line = -1;
435 this.check_running = false;
437 // set gobject values
440 this.el.changed.connect( () => {
443 _this.save_button.el.sensitive = true;
444 print("EDITOR CHANGED");
449 // this.get('/LeftPanel.model').changed( str , false);
454 // user defined functions
455 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
457 this.error_line = validate_res.size;
459 if (this.error_line < 1) {
462 var tlines = this.el.get_line_count ();
464 var valiter = validate_res.map_iterator();
465 while (valiter.next()) {
467 // print("get inter\n");
468 var eline = valiter.get_key();
469 if (eline > tlines) {
472 this.el.get_iter_at_line( out iter, eline);
473 //print("mark line\n");
474 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
478 public string toString () {
482 this.el.get_start_iter(out s);
483 this.el.get_end_iter(out e);
484 var ret = this.el.get_text(s,e,true);
485 //print("TO STRING? " + ret);
488 public bool checkSyntax () {
490 if (this.check_running) {
491 print("Check is running\n");
492 if (this.check_queued) {
493 print("Check is already queued");
496 this.check_queued = true;
497 print("Adding queued Check ");
498 GLib.Timeout.add_seconds(1, () => {
499 this.check_queued = false;
508 var str = this.toString();
511 if (this.error_line > 0) {
514 this.el.get_bounds (out start, out end);
516 this.el.remove_source_marks (start, end, null);
518 if (str.length < 1) {
519 print("checkSyntax - empty string?\n");
523 if (_this.file.xtype == "PlainFile") {
525 // assume it's gtk...
526 this.check_running = true;
528 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
532 this.check_running = false;
538 if (_this.file == null) {
541 var p = Palete.factory(_this.file.xtype);
545 this.check_running = true;
548 if (_this.file.language == "js") {
549 this.check_running = false;
550 print("calling validate javascript\n");
551 return this.highlightErrors(p.validateJavascript(
562 print("calling validate vala\n");
563 // clear the buttons.
566 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
573 this.check_running = false;
578 //print("done mark line\n");
580 return true; // at present allow saving - even if it's invalid..
582 public bool highlightErrorsJson (string type, Json.Object obj) {
585 this.el.get_bounds (out start, out end);
587 this.el.remove_source_marks (start, end, type);
590 // we should highlight other types of errors..
592 if (!obj.has_member(type)) {
593 print("Return has no errors\n");
597 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
598 _this.window.windowstate.state != WindowState.State.CODE
604 var err = obj.get_object_member(type);
607 if (_this.file == null) {
611 var valafn = _this.file.path;
613 if (_this.file.xtype != "PlainFile") {
620 var regex = new Regex("\\.bjs$");
624 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
625 } catch (GLib.RegexError e) {
632 if (!err.has_member(valafn)) {
633 print("File path has no errors\n");
637 var lines = err.get_object_member(valafn);
640 if (obj.has_member("line_offset")) {
641 offset = (int)obj.get_int_member("line_offset") + 1;
647 var tlines = this.el.get_line_count () +1;
649 lines.foreach_member((obj, line, node) => {
652 // print("get inter\n");
653 var eline = int.parse(line) - offset;
654 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
657 if (eline > tlines || eline < 0) {
660 this.el.get_iter_at_line( out iter, eline);
661 //print("mark line\n");
662 var msg = "Line: %d".printf(eline+1);
663 var ar = lines.get_array_member(line);
664 for (var i = 0 ; i < ar.get_length(); i++) {
665 msg += (msg.length > 0) ? "\n" : "";
666 msg += ar.get_string_element(i);
670 this.el.create_source_mark(msg, type, iter);