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 JsRender.JsRender file;
31 public signal void save ();
32 public JsRender.Node node;
38 this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
42 this.activeEditor = "";
51 this.el.homogeneous = false;
52 this.el.hexpand = true;
53 var child_0 = new Xcls_Box2( _this );
55 this.el.pack_start ( child_0.el , false,true );
56 var child_1 = new Xcls_RightEditor( _this );
58 this.el.pack_end ( child_1.el , true,true );
61 // user defined functions
62 public Gee.ArrayList<int> search (string txt) {
64 var s = new Gtk.SourceSearchSettings();
66 var sctx = new Gtk.SourceSearchContext(this.buffer.el,s);
67 sctx.set_highlight(true);
68 s.set_search_text(txt);
72 var ret = new Gee.ArrayList<int>();
74 var str = this.buffer.toString();
76 var lines = str.split("\n");
77 for (var i = 0 ; i < lines.length; i++) {
78 if (lines[i].contains(txt)) {
85 public bool saveContents () {
88 if (_this.file == null) {
96 var str = _this.buffer.toString();
98 _this.buffer.checkSyntax();
102 // LeftPanel.model.changed( str , false);
104 _this.save_button.el.sensitive = false;
106 // find the text for the node..
107 if (_this.file.xtype != "PlainFile") {
108 if (ptype == "listener") {
109 this.node.listeners.set(key,str);
112 this.node.props.set(key,str);
115 _this.file.setSource( str );
124 public void scroll_to_line (int line) {
126 GLib.Timeout.add(500, () => {
128 var buf = this.view.el.get_buffer();
130 var sbuf = (Gtk.SourceBuffer) buf;
134 sbuf.get_iter_at_line(out iter, line);
135 this.view.el.scroll_to_iter(iter, 0.1f, true, 0.0f, 0.5f);
139 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
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 class Xcls_Box2 : Object
174 private Editor _this;
180 public Xcls_Box2(Editor _owner )
183 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
187 // set gobject values
188 this.el.homogeneous = false;
189 var child_0 = new Xcls_save_button( _this );
191 this.el.pack_start ( child_0.el , false,false );
192 var child_1 = new Xcls_key_edit( _this );
194 this.el.pack_end ( child_1.el , true,true );
197 // user defined functions
199 public class Xcls_save_button : Object
201 public Gtk.Button el;
202 private Editor _this;
208 public Xcls_save_button(Editor _owner )
211 _this.save_button = this;
212 this.el = new Gtk.Button();
216 // set gobject values
217 this.el.label = "Save";
220 this.el.clicked.connect( () => {
221 _this.saveContents();
225 // user defined functions
228 public class Xcls_key_edit : Object
231 private Editor _this;
237 public Xcls_key_edit(Editor _owner )
240 _this.key_edit = this;
241 this.el = new Gtk.Entry();
245 // set gobject values
248 // user defined functions
252 public class Xcls_RightEditor : Object
254 public Gtk.ScrolledWindow el;
255 private Editor _this;
261 public Xcls_RightEditor(Editor _owner )
264 _this.RightEditor = this;
265 this.el = new Gtk.ScrolledWindow( null, null );
269 // set gobject values
270 var child_0 = new Xcls_view( _this );
272 this.el.add ( child_0.el );
276 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
279 // user defined functions
281 public class Xcls_view : Object
283 public Gtk.SourceView el;
284 private Editor _this;
290 public Xcls_view(Editor _owner )
294 this.el = new Gtk.SourceView();
298 // set gobject values
299 this.el.auto_indent = true;
300 this.el.indent_width = 4;
301 this.el.show_line_marks = true;
302 this.el.insert_spaces_instead_of_tabs = true;
303 this.el.show_line_numbers = true;
304 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
305 this.el.tab_width = 4;
306 this.el.highlight_current_line = true;
307 var child_0 = new Xcls_buffer( _this );
309 this.el.set_buffer ( child_0.el );
313 var description = Pango.FontDescription.from_string("monospace");
314 description.set_size(8000);
315 this.el.override_font(description);
318 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
319 this.el.completion.unblock_interactive();
320 this.el.completion.select_on_show = true; // select
321 this.el.completion.show_headers = false;
322 this.el.completion.remember_info_visibility = true;
325 var attrs = new Gtk.SourceMarkAttributes();
326 var pink = Gdk.RGBA();
327 pink.parse ( "pink");
328 attrs.set_background ( pink);
329 attrs.set_icon_name ( "process-stop");
330 attrs.query_tooltip_text.connect(( mark) => {
331 //print("tooltip query? %s\n", mark.name);
335 this.el.set_mark_attributes ("ERR", attrs, 1);
337 var wattrs = new Gtk.SourceMarkAttributes();
338 var blue = Gdk.RGBA();
339 blue.parse ( "#ABF4EB");
340 wattrs.set_background ( blue);
341 wattrs.set_icon_name ( "process-stop");
342 wattrs.query_tooltip_text.connect(( mark) => {
343 //print("tooltip query? %s\n", mark.name);
347 this.el.set_mark_attributes ("WARN", wattrs, 1);
351 var dattrs = new Gtk.SourceMarkAttributes();
352 var purple = Gdk.RGBA();
353 purple.parse ( "#EEA9FF");
354 dattrs.set_background ( purple);
355 dattrs.set_icon_name ( "process-stop");
356 dattrs.query_tooltip_text.connect(( mark) => {
357 //print("tooltip query? %s\n", mark.name);
361 this.el.set_mark_attributes ("DEPR", dattrs, 1);
364 this.el.key_release_event.connect( (event) => {
366 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
367 print("SAVE: ctrl-S pressed");
368 _this.saveContents();
371 // print(event.key.keyval)
378 // user defined functions
379 public void load (string str) {
381 // show the help page for the active node..
382 //this.get('/Help').show();
385 // this.get('/BottomPane').el.set_current_page(0);
386 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
387 buf.set_text(str, str.length);
388 buf.set_undo_manager(null);
390 var lm = Gtk.SourceLanguageManager.get_default();
392 if (_this.file != null) {
393 lang = _this.file.language;
395 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
396 var lg = _this.file.content_type.length > 0 ?
397 lm.guess_language(_this.file.path, _this.file.content_type) :
398 lm.get_language(lang);
401 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
403 this.el.insert_spaces_instead_of_tabs = true;
405 print("sourcelanguage = %s\n", lg.name);
406 if (lg.name == "Vala") {
407 this.el.insert_spaces_instead_of_tabs = false;
411 this.el.grab_focus();
412 _this.save_button.el.sensitive = false;
415 public class Xcls_buffer : Object
417 public Gtk.SourceBuffer el;
418 private Editor _this;
422 public bool check_queued;
423 public int error_line;
424 public bool check_running;
427 public Xcls_buffer(Editor _owner )
431 this.el = new Gtk.SourceBuffer( null );
434 this.check_queued = false;
435 this.error_line = -1;
436 this.check_running = false;
438 // set gobject values
441 this.el.changed.connect( () => {
444 _this.save_button.el.sensitive = true;
445 print("EDITOR CHANGED");
450 // this.get('/LeftPanel.model').changed( str , false);
455 // user defined functions
456 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
458 this.error_line = validate_res.size;
460 if (this.error_line < 1) {
463 var tlines = this.el.get_line_count ();
465 var valiter = validate_res.map_iterator();
466 while (valiter.next()) {
468 // print("get inter\n");
469 var eline = valiter.get_key();
470 if (eline > tlines) {
473 this.el.get_iter_at_line( out iter, eline);
474 //print("mark line\n");
475 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
479 public string toString () {
483 this.el.get_start_iter(out s);
484 this.el.get_end_iter(out e);
485 var ret = this.el.get_text(s,e,true);
486 //print("TO STRING? " + ret);
489 public bool checkSyntax () {
491 if (this.check_running) {
492 print("Check is running\n");
493 if (this.check_queued) {
494 print("Check is already queued");
497 this.check_queued = true;
498 print("Adding queued Check ");
499 GLib.Timeout.add_seconds(1, () => {
500 this.check_queued = false;
509 var str = this.toString();
512 if (this.error_line > 0) {
515 this.el.get_bounds (out start, out end);
517 this.el.remove_source_marks (start, end, null);
519 if (str.length < 1) {
520 print("checkSyntax - empty string?\n");
524 if (_this.file.xtype == "PlainFile") {
526 // assume it's gtk...
527 this.check_running = true;
529 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
533 this.check_running = false;
539 if (_this.file == null) {
542 var p = Palete.factory(_this.file.xtype);
546 this.check_running = true;
549 if (_this.file.language == "js") {
550 this.check_running = false;
551 print("calling validate javascript\n");
552 return this.highlightErrors(p.validateJavascript(
563 print("calling validate vala\n");
564 // clear the buttons.
567 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
574 this.check_running = false;
579 //print("done mark line\n");
581 return true; // at present allow saving - even if it's invalid..
583 public bool highlightErrorsJson (string type, Json.Object obj) {
586 this.el.get_bounds (out start, out end);
588 this.el.remove_source_marks (start, end, type);
591 // we should highlight other types of errors..
593 if (!obj.has_member(type)) {
594 print("Return has no errors\n");
598 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
599 _this.window.windowstate.state != WindowState.State.CODE
605 var err = obj.get_object_member(type);
608 if (_this.file == null) {
612 var valafn = _this.file.path;
614 if (_this.file.xtype != "PlainFile") {
621 var regex = new Regex("\\.bjs$");
625 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
626 } catch (GLib.RegexError e) {
633 if (!err.has_member(valafn)) {
634 print("File path has no errors\n");
638 var lines = err.get_object_member(valafn);
641 if (obj.has_member("line_offset")) {
642 offset = (int)obj.get_int_member("line_offset") + 1;
648 var tlines = this.el.get_line_count () +1;
650 lines.foreach_member((obj, line, node) => {
653 // print("get inter\n");
654 var eline = int.parse(line) - offset;
655 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
658 if (eline > tlines || eline < 0) {
661 this.el.get_iter_at_line( out iter, eline);
662 //print("mark line\n");
663 var msg = "Line: %d".printf(eline+1);
664 var ar = lines.get_array_member(line);
665 for (var i = 0 ; i < ar.get_length(); i++) {
666 msg += (msg.length > 0) ? "\n" : "";
667 msg += ar.get_string_element(i);
671 this.el.create_source_mark(msg, type, iter);