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;
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 = "";
53 this.el.homogeneous = false;
54 var child_0 = new Xcls_Box2( _this );
56 this.el.pack_start ( child_0.el , false,true );
57 var child_1 = new Xcls_RightEditor( _this );
59 this.el.pack_end ( child_1.el , true,true );
62 // user defined functions
63 public bool saveContents () {
66 if (_this.file == null) {
75 var str = _this.buffer.toString();
80 if (!_this.buffer.checkSyntax()) {
81 print("check syntax failed");
82 //this.get('/StandardErrorDialog').show("Fix errors in code and save..");
86 // LeftPanel.model.changed( str , false);
88 _this.save_button.el.sensitive = false;
93 // find the text for the node..
94 if (ptype == "listener") {
95 this.node.listeners.set(key,str);
98 this.node.props.set(key,str);
109 public void show (JsRender.JsRender file, JsRender.Node node, string ptype, string key)
117 if (file.xtype != "PlainFile") {
123 // find the text for the node..
124 if (ptype == "listener") {
125 val = node.listeners.get(key);
128 val = node.props.get(key);
134 this.view.load( file.toSource() );
137 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 if (_this.file != null) {
356 lang = _this.file.language;
358 //?? is javascript going to work as js?
360 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
364 this.el.grab_focus();
365 _this.save_button.el.sensitive = false;
368 public class Xcls_buffer : Object
370 public Gtk.SourceBuffer el;
371 private Editor _this;
375 public bool check_queued;
376 public int error_line;
377 public bool check_running;
380 public Xcls_buffer(Editor _owner )
384 this.el = new Gtk.SourceBuffer( null );
387 this.check_queued = false;
388 this.error_line = -1;
389 this.check_running = false;
391 // set gobject values
394 this.el.changed.connect( () => {
397 _this.save_button.el.sensitive = true;
398 print("EDITOR CHANGED");
403 // this.get('/LeftPanel.model').changed( str , false);
408 // user defined functions
409 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
411 this.error_line = validate_res.size;
413 if (this.error_line < 1) {
416 var tlines = this.el.get_line_count ();
418 var valiter = validate_res.map_iterator();
419 while (valiter.next()) {
421 // print("get inter\n");
422 var eline = valiter.get_key();
423 if (eline > tlines) {
426 this.el.get_iter_at_line( out iter, eline);
427 //print("mark line\n");
428 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
432 public string toString () {
436 this.el.get_start_iter(out s);
437 this.el.get_end_iter(out e);
438 var ret = this.el.get_text(s,e,true);
439 //print("TO STRING? " + ret);
442 public bool checkSyntax () {
444 if (this.check_running) {
445 print("Check is running\n");
446 if (this.check_queued) {
447 print("Check is already queued");
450 this.check_queued = true;
451 print("Adding queued Check ");
452 GLib.Timeout.add_seconds(1, () => {
453 this.check_queued = false;
462 var str = this.toString();
465 if (this.error_line > 0) {
468 this.el.get_bounds (out start, out end);
470 this.el.remove_source_marks (start, end, null);
472 if (str.length < 1) {
473 print("checkSyntax - empty string?\n");
477 if (_this.file == null && _this.fname.length > 0) {
479 // assume it's gtk...
480 this.check_running = true;
482 _this.window.windowstate.valasource.checkPlainFileSpawn(
483 _this.window.project,
490 if (_this.file == null) {
493 var p = Palete.factory(_this.file.xtype);
497 this.check_running = true;
500 if (_this.file.language == "js") {
501 this.check_running = false;
502 print("calling validate javascript\n");
503 return this.highlightErrors(p.validateJavascript(
514 print("calling validate vala\n");
515 // clear the buttons.
519 _this.window.windowstate,
531 //print("done mark line\n");
533 return true; // at present allow saving - even if it's invalid..
535 public bool highlightErrorsJson (string type, Json.Object obj) {
538 this.el.get_bounds (out start, out end);
540 this.el.remove_source_marks (start, end, type);
543 // we should highlight other types of errors..
545 if (!obj.has_member(type)) {
546 print("Return has no errors\n");
550 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
551 _this.window.windowstate.state != WindowState.State.CODE
557 var err = obj.get_object_member(type);
558 var valafn = _this.fname;
559 if (_this.file != null) {
566 var regex = new Regex("\\.bjs$");
569 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
570 } catch (GLib.RegexError e) {
577 if (!err.has_member(valafn)) {
578 print("File path has no errors\n");
582 var lines = err.get_object_member(valafn);
585 if (obj.has_member("line_offset")) {
586 offset = (int)obj.get_int_member("line_offset") + 1;
592 var tlines = this.el.get_line_count () +1;
594 lines.foreach_member((obj, line, node) => {
597 // print("get inter\n");
598 var eline = int.parse(line) - offset;
599 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
602 if (eline > tlines || eline < 0) {
605 this.el.get_iter_at_line( out iter, eline);
606 //print("mark line\n");
607 var msg = "Line: %d".printf(eline+1);
608 var ar = lines.get_array_member(line);
609 for (var i = 0 ; i < ar.get_length(); i++) {
610 msg += (msg.length > 0) ? "\n" : "";
611 msg += ar.get_string_element(i);
615 this.el.create_source_mark(msg, type, iter);