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.expand = 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 bool saveContents () {
72 var str = _this.buffer.toString();
77 if (!_this.buffer.checkSyntax()) {
78 print("check syntax failed");
79 //this.get('/StandardErrorDialog').show("Fix errors in code and save..");
83 // LeftPanel.model.changed( str , false);
85 _this.save_button.el.sensitive = false;
90 // find the text for the node..
91 if (ptype == "listener") {
92 this.node.listeners.set(key,str);
95 this.node.props.set(key,str);
106 public void showPlainFile (string fname)
114 GLib.FileUtils.get_contents(fname, out str);
116 str = ""; // a tad dangerios... - perhaps we should block editing...
120 this.key_edit.el.text = "";
123 public void show (JsRender.JsRender file, JsRender.Node node, string ptype, string key)
131 // find the text for the node..
132 if (ptype == "listener") {
133 val = node.listeners.get(key);
136 val = node.props.get(key);
139 this.key_edit.el.text = key;
142 public class Xcls_Box2 : Object
145 private Editor _this;
151 public Xcls_Box2(Editor _owner )
154 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
158 // set gobject values
159 this.el.homogeneous = false;
160 var child_0 = new Xcls_save_button( _this );
162 this.el.pack_start ( child_0.el , false,false );
163 var child_1 = new Xcls_key_edit( _this );
165 this.el.pack_end ( child_1.el , true,true );
168 // user defined functions
170 public class Xcls_save_button : Object
172 public Gtk.Button el;
173 private Editor _this;
179 public Xcls_save_button(Editor _owner )
182 _this.save_button = this;
183 this.el = new Gtk.Button();
187 // set gobject values
188 this.el.label = "Save";
191 this.el.clicked.connect( () => {
192 _this.saveContents();
196 // user defined functions
199 public class Xcls_key_edit : Object
202 private Editor _this;
208 public Xcls_key_edit(Editor _owner )
211 _this.key_edit = this;
212 this.el = new Gtk.Entry();
216 // set gobject values
219 // user defined functions
223 public class Xcls_RightEditor : Object
225 public Gtk.ScrolledWindow el;
226 private Editor _this;
232 public Xcls_RightEditor(Editor _owner )
235 _this.RightEditor = this;
236 this.el = new Gtk.ScrolledWindow( null, null );
240 // set gobject values
241 var child_0 = new Xcls_view( _this );
243 this.el.add ( child_0.el );
247 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
250 // user defined functions
252 public class Xcls_view : Object
254 public Gtk.SourceView el;
255 private Editor _this;
261 public Xcls_view(Editor _owner )
265 this.el = new Gtk.SourceView();
269 // set gobject values
270 this.el.auto_indent = true;
271 this.el.indent_width = 4;
272 this.el.show_line_marks = true;
273 this.el.insert_spaces_instead_of_tabs = true;
274 this.el.show_line_numbers = true;
275 this.el.highlight_current_line = true;
276 var child_0 = new Xcls_buffer( _this );
278 this.el.set_buffer ( child_0.el );
282 var description = Pango.FontDescription.from_string("monospace");
283 description.set_size(8000);
284 this.el.override_font(description);
288 var attrs = new Gtk.SourceMarkAttributes();
289 var pink = Gdk.RGBA();
290 pink.parse ( "pink");
291 attrs.set_background ( pink);
292 attrs.set_icon_name ( "process-stop");
293 attrs.query_tooltip_text.connect(( mark) => {
294 //print("tooltip query? %s\n", mark.name);
298 this.el.set_mark_attributes ("ERR", attrs, 1);
300 var wattrs = new Gtk.SourceMarkAttributes();
301 var blue = Gdk.RGBA();
302 blue.parse ( "#ABF4EB");
303 wattrs.set_background ( blue);
304 wattrs.set_icon_name ( "process-stop");
305 wattrs.query_tooltip_text.connect(( mark) => {
306 //print("tooltip query? %s\n", mark.name);
310 this.el.set_mark_attributes ("WARN", wattrs, 1);
314 var dattrs = new Gtk.SourceMarkAttributes();
315 var purple = Gdk.RGBA();
316 purple.parse ( "#EEA9FF");
317 dattrs.set_background ( purple);
318 dattrs.set_icon_name ( "process-stop");
319 dattrs.query_tooltip_text.connect(( mark) => {
320 //print("tooltip query? %s\n", mark.name);
324 this.el.set_mark_attributes ("DEPR", dattrs, 1);
327 this.el.key_release_event.connect( (event) => {
329 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
330 print("SAVE: ctrl-S pressed");
331 _this.saveContents();
334 // print(event.key.keyval)
341 // user defined functions
342 public void load (string str) {
344 // show the help page for the active node..
345 //this.get('/Help').show();
348 // this.get('/BottomPane').el.set_current_page(0);
349 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
350 buf.set_text(str, str.length);
351 buf.set_undo_manager(null);
353 var lm = Gtk.SourceLanguageManager.get_default();
355 var lang = _this.file.language;
356 //?? is javascript going to work as js?
358 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
362 this.el.grab_focus();
363 _this.save_button.el.sensitive = false;
366 public class Xcls_buffer : Object
368 public Gtk.SourceBuffer el;
369 private Editor _this;
373 public bool check_queued;
374 public int error_line;
375 public bool check_running;
378 public Xcls_buffer(Editor _owner )
382 this.el = new Gtk.SourceBuffer( null );
385 this.check_queued = false;
386 this.error_line = -1;
387 this.check_running = false;
389 // set gobject values
392 this.el.changed.connect( () => {
395 _this.save_button.el.sensitive = true;
396 print("EDITOR CHANGED");
401 // this.get('/LeftPanel.model').changed( str , false);
406 // user defined functions
407 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
408 this.error_line = validate_res.size;
410 if (this.error_line < 1) {
413 var tlines = this.el.get_line_count ();
415 var valiter = validate_res.map_iterator();
416 while (valiter.next()) {
418 // print("get inter\n");
419 var eline = valiter.get_key();
420 if (eline > tlines) {
423 this.el.get_iter_at_line( out iter, eline);
424 //print("mark line\n");
425 this.el.create_source_mark(valiter.get_value(), "error", iter);
429 public string toString () {
433 this.el.get_start_iter(out s);
434 this.el.get_end_iter(out e);
435 var ret = this.el.get_text(s,e,true);
436 //print("TO STRING? " + ret);
439 public bool checkSyntax () {
441 if (this.check_running) {
442 print("Check is running\n");
443 if (this.check_queued) {
444 print("Check is already queued");
447 this.check_queued = true;
448 print("Adding queued Check ");
449 GLib.Timeout.add_seconds(1, () => {
450 this.check_queued = false;
461 var p = Palete.factory(_this.file.xtype);
463 var str = this.toString();
466 if (this.error_line > 0) {
469 this.el.get_bounds (out start, out end);
471 this.el.remove_source_marks (start, end, null);
474 if (str.length < 1) {
475 print("checkSyntax - empty string?\n");
478 this.check_running = true;
481 if (_this.file.language == "js") {
482 this.check_running = false;
483 print("calling validate javascript\n");
484 return this.highlightErrors(p.validateJavascript(
495 print("calling validate vala\n");
496 // clear the buttons.
500 _this.window.windowstate,
512 //print("done mark line\n");
514 return true; // at present allow saving - even if it's invalid..
516 public bool highlightErrorsJson (string type, Json.Object obj) {
519 this.el.get_bounds (out start, out end);
521 this.el.remove_source_marks (start, end, type);
524 // we should highlight other types of errors..
526 if (!obj.has_member(type)) {
527 print("Return has no errors\n");
531 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
532 _this.window.windowstate.state != WindowState.State.CODE
537 if (_this.file == null) {
541 var err = obj.get_object_member(type);
546 var regex = new Regex("\\.bjs$");
549 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
550 } catch (GLib.RegexError e) {
554 if (!err.has_member(valafn)) {
555 print("File path has no errors\n");
559 var lines = err.get_object_member(valafn);
562 if (obj.has_member("line_offset")) {
563 offset = (int)obj.get_int_member("line_offset") + 1;
567 var tlines = this.el.get_line_count () +1;
569 lines.foreach_member((obj, line, node) => {
572 // print("get inter\n");
573 var eline = int.parse(line) - offset;
574 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
577 if (eline > tlines || eline < 0) {
580 this.el.get_iter_at_line( out iter, eline);
581 //print("mark line\n");
582 var msg = "Line: %d".printf(eline+1);
583 var ar = lines.get_array_member(line);
584 for (var i = 0 ; i < ar.get_length(); i++) {
585 msg += (msg.length > 0) ? "\n" : "";
586 msg += ar.get_string_element(i);
590 this.el.create_source_mark(msg, type, iter);