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 Gee.ArrayList<int> search (string txt) {
66 var s = new Gtk.SourceSearchSettings();
68 var sctx = new Gtk.SourceSearchContext(this.buffer.el,s);
69 sctx.set_highlight(true);
70 s.set_search_text(txt);
71 while (sctx.get_occurence_position(st, en)> -1) {
74 var ret = new Gee.ArrayList<int>();
76 var str = this.buffer.toString();
78 var lines = str.split("\n");
79 for (var i = 0 ; i < lines.length; i++) {
80 if (lines[i].contains(txt)) {
87 public bool saveContents () {
90 if (_this.file == null) {
98 var str = _this.buffer.toString();
100 _this.buffer.checkSyntax();
104 // LeftPanel.model.changed( str , false);
106 _this.save_button.el.sensitive = false;
108 // find the text for the node..
109 if (_this.file.xtype != "PlainFile") {
110 if (ptype == "listener") {
111 this.node.listeners.set(key,str);
114 this.node.props.set(key,str);
117 _this.file.setSource( str );
126 public void scroll_to_line (int line) {
128 GLib.Timeout.add(500, () => {
130 var buf = this.view.el.get_buffer();
132 var sbuf = (Gtk.SourceBuffer) buf;
136 sbuf.get_iter_at_line(out iter, line);
137 this.view.el.scroll_to_iter(iter, 0.1f, true, 0.0f, 0.5f);
141 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
149 if (file.xtype != "PlainFile") {
155 // find the text for the node..
156 if (ptype == "listener") {
157 val = node.listeners.get(key);
160 val = node.props.get(key);
163 this.key_edit.el.show();
164 this.key_edit.el.text = key;
167 this.view.load( file.toSource() );
168 this.key_edit.el.hide();
173 public class Xcls_Box2 : Object
176 private Editor _this;
182 public Xcls_Box2(Editor _owner )
185 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
189 // set gobject values
190 this.el.homogeneous = false;
191 var child_0 = new Xcls_save_button( _this );
193 this.el.pack_start ( child_0.el , false,false );
194 var child_1 = new Xcls_key_edit( _this );
196 this.el.pack_end ( child_1.el , true,true );
199 // user defined functions
201 public class Xcls_save_button : Object
203 public Gtk.Button el;
204 private Editor _this;
210 public Xcls_save_button(Editor _owner )
213 _this.save_button = this;
214 this.el = new Gtk.Button();
218 // set gobject values
219 this.el.label = "Save";
222 this.el.clicked.connect( () => {
223 _this.saveContents();
227 // user defined functions
230 public class Xcls_key_edit : Object
233 private Editor _this;
239 public Xcls_key_edit(Editor _owner )
242 _this.key_edit = this;
243 this.el = new Gtk.Entry();
247 // set gobject values
250 // user defined functions
254 public class Xcls_RightEditor : Object
256 public Gtk.ScrolledWindow el;
257 private Editor _this;
263 public Xcls_RightEditor(Editor _owner )
266 _this.RightEditor = this;
267 this.el = new Gtk.ScrolledWindow( null, null );
271 // set gobject values
272 var child_0 = new Xcls_view( _this );
274 this.el.add ( child_0.el );
278 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
281 // user defined functions
283 public class Xcls_view : Object
285 public Gtk.SourceView el;
286 private Editor _this;
292 public Xcls_view(Editor _owner )
296 this.el = new Gtk.SourceView();
300 // set gobject values
301 this.el.auto_indent = true;
302 this.el.indent_width = 4;
303 this.el.show_line_marks = true;
304 this.el.insert_spaces_instead_of_tabs = true;
305 this.el.show_line_numbers = true;
306 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
307 this.el.tab_width = 4;
308 this.el.highlight_current_line = true;
309 var child_0 = new Xcls_buffer( _this );
311 this.el.set_buffer ( child_0.el );
315 var description = Pango.FontDescription.from_string("monospace");
316 description.set_size(8000);
317 this.el.override_font(description);
320 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
321 this.el.completion.unblock_interactive();
322 this.el.completion.select_on_show = true; // select
323 this.el.completion.show_headers = false;
324 this.el.completion.remember_info_visibility = true;
327 var attrs = new Gtk.SourceMarkAttributes();
328 var pink = Gdk.RGBA();
329 pink.parse ( "pink");
330 attrs.set_background ( pink);
331 attrs.set_icon_name ( "process-stop");
332 attrs.query_tooltip_text.connect(( mark) => {
333 //print("tooltip query? %s\n", mark.name);
337 this.el.set_mark_attributes ("ERR", attrs, 1);
339 var wattrs = new Gtk.SourceMarkAttributes();
340 var blue = Gdk.RGBA();
341 blue.parse ( "#ABF4EB");
342 wattrs.set_background ( blue);
343 wattrs.set_icon_name ( "process-stop");
344 wattrs.query_tooltip_text.connect(( mark) => {
345 //print("tooltip query? %s\n", mark.name);
349 this.el.set_mark_attributes ("WARN", wattrs, 1);
353 var dattrs = new Gtk.SourceMarkAttributes();
354 var purple = Gdk.RGBA();
355 purple.parse ( "#EEA9FF");
356 dattrs.set_background ( purple);
357 dattrs.set_icon_name ( "process-stop");
358 dattrs.query_tooltip_text.connect(( mark) => {
359 //print("tooltip query? %s\n", mark.name);
363 this.el.set_mark_attributes ("DEPR", dattrs, 1);
366 this.el.key_release_event.connect( (event) => {
368 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
369 print("SAVE: ctrl-S pressed");
370 _this.saveContents();
373 // print(event.key.keyval)
380 // user defined functions
381 public void load (string str) {
383 // show the help page for the active node..
384 //this.get('/Help').show();
387 // this.get('/BottomPane').el.set_current_page(0);
388 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
389 buf.set_text(str, str.length);
390 buf.set_undo_manager(null);
392 var lm = Gtk.SourceLanguageManager.get_default();
394 if (_this.file != null) {
395 lang = _this.file.language;
397 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
398 var lg = _this.file.content_type.length > 0 ?
399 lm.guess_language(_this.file.path, _this.file.content_type) :
400 lm.get_language(lang);
403 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
405 this.el.insert_spaces_instead_of_tabs = true;
407 print("sourcelanguage = %s\n", lg.name);
408 if (lg.name == "Vala") {
409 this.el.insert_spaces_instead_of_tabs = false;
413 this.el.grab_focus();
414 _this.save_button.el.sensitive = false;
417 public class Xcls_buffer : Object
419 public Gtk.SourceBuffer el;
420 private Editor _this;
424 public bool check_queued;
425 public int error_line;
426 public bool check_running;
429 public Xcls_buffer(Editor _owner )
433 this.el = new Gtk.SourceBuffer( null );
436 this.check_queued = false;
437 this.error_line = -1;
438 this.check_running = false;
440 // set gobject values
443 this.el.changed.connect( () => {
446 _this.save_button.el.sensitive = true;
447 print("EDITOR CHANGED");
452 // this.get('/LeftPanel.model').changed( str , false);
457 // user defined functions
458 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
460 this.error_line = validate_res.size;
462 if (this.error_line < 1) {
465 var tlines = this.el.get_line_count ();
467 var valiter = validate_res.map_iterator();
468 while (valiter.next()) {
470 // print("get inter\n");
471 var eline = valiter.get_key();
472 if (eline > tlines) {
475 this.el.get_iter_at_line( out iter, eline);
476 //print("mark line\n");
477 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
481 public string toString () {
485 this.el.get_start_iter(out s);
486 this.el.get_end_iter(out e);
487 var ret = this.el.get_text(s,e,true);
488 //print("TO STRING? " + ret);
491 public bool checkSyntax () {
493 if (this.check_running) {
494 print("Check is running\n");
495 if (this.check_queued) {
496 print("Check is already queued");
499 this.check_queued = true;
500 print("Adding queued Check ");
501 GLib.Timeout.add_seconds(1, () => {
502 this.check_queued = false;
511 var str = this.toString();
514 if (this.error_line > 0) {
517 this.el.get_bounds (out start, out end);
519 this.el.remove_source_marks (start, end, null);
521 if (str.length < 1) {
522 print("checkSyntax - empty string?\n");
526 if (_this.file.xtype == "PlainFile") {
528 // assume it's gtk...
529 this.check_running = true;
531 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
535 this.check_running = false;
541 if (_this.file == null) {
544 var p = Palete.factory(_this.file.xtype);
548 this.check_running = true;
551 if (_this.file.language == "js") {
552 this.check_running = false;
553 print("calling validate javascript\n");
554 return this.highlightErrors(p.validateJavascript(
565 print("calling validate vala\n");
566 // clear the buttons.
569 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
576 this.check_running = false;
581 //print("done mark line\n");
583 return true; // at present allow saving - even if it's invalid..
585 public bool highlightErrorsJson (string type, Json.Object obj) {
588 this.el.get_bounds (out start, out end);
590 this.el.remove_source_marks (start, end, type);
593 // we should highlight other types of errors..
595 if (!obj.has_member(type)) {
596 print("Return has no errors\n");
600 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
601 _this.window.windowstate.state != WindowState.State.CODE
607 var err = obj.get_object_member(type);
610 if (_this.file == null) {
614 var valafn = _this.file.path;
616 if (_this.file.xtype != "PlainFile") {
623 var regex = new Regex("\\.bjs$");
627 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
628 } catch (GLib.RegexError e) {
635 if (!err.has_member(valafn)) {
636 print("File path has no errors\n");
640 var lines = err.get_object_member(valafn);
643 if (obj.has_member("line_offset")) {
644 offset = (int)obj.get_int_member("line_offset") + 1;
650 var tlines = this.el.get_line_count () +1;
652 lines.foreach_member((obj, line, node) => {
655 // print("get inter\n");
656 var eline = int.parse(line) - offset;
657 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
660 if (eline > tlines || eline < 0) {
663 this.el.get_iter_at_line( out iter, eline);
664 //print("mark line\n");
665 var msg = "Line: %d".printf(eline+1);
666 var ar = lines.get_array_member(line);
667 for (var i = 0 ; i < ar.get_length(); i++) {
668 msg += (msg.length > 0) ? "\n" : "";
669 msg += ar.get_string_element(i);
673 this.el.create_source_mark(msg, type, iter);