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 showPlainFile (string fname)
118 GLib.FileUtils.get_contents(fname, out str);
120 str = ""; // a tad dangerios... - perhaps we should block editing...
124 this.key_edit.el.text = "";
127 public void show (JsRender.JsRender file, JsRender.Node node, string ptype, string key)
135 if (file.xtype != "PlainFile") {
141 // find the text for the node..
142 if (ptype == "listener") {
143 val = node.listeners.get(key);
146 val = node.props.get(key);
149 this.key_edit.el.text = key;
152 this.view.load( file.toSource() );
159 public class Xcls_Box2 : Object
162 private Editor _this;
168 public Xcls_Box2(Editor _owner )
171 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
175 // set gobject values
176 this.el.homogeneous = false;
177 var child_0 = new Xcls_save_button( _this );
179 this.el.pack_start ( child_0.el , false,false );
180 var child_1 = new Xcls_key_edit( _this );
182 this.el.pack_end ( child_1.el , true,true );
185 // user defined functions
187 public class Xcls_save_button : Object
189 public Gtk.Button el;
190 private Editor _this;
196 public Xcls_save_button(Editor _owner )
199 _this.save_button = this;
200 this.el = new Gtk.Button();
204 // set gobject values
205 this.el.label = "Save";
208 this.el.clicked.connect( () => {
209 _this.saveContents();
213 // user defined functions
216 public class Xcls_key_edit : Object
219 private Editor _this;
225 public Xcls_key_edit(Editor _owner )
228 _this.key_edit = this;
229 this.el = new Gtk.Entry();
233 // set gobject values
236 // user defined functions
240 public class Xcls_RightEditor : Object
242 public Gtk.ScrolledWindow el;
243 private Editor _this;
249 public Xcls_RightEditor(Editor _owner )
252 _this.RightEditor = this;
253 this.el = new Gtk.ScrolledWindow( null, null );
257 // set gobject values
258 var child_0 = new Xcls_view( _this );
260 this.el.add ( child_0.el );
264 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
267 // user defined functions
269 public class Xcls_view : Object
271 public Gtk.SourceView el;
272 private Editor _this;
278 public Xcls_view(Editor _owner )
282 this.el = new Gtk.SourceView();
286 // set gobject values
287 this.el.auto_indent = true;
288 this.el.indent_width = 4;
289 this.el.show_line_marks = true;
290 this.el.insert_spaces_instead_of_tabs = true;
291 this.el.show_line_numbers = true;
292 this.el.highlight_current_line = true;
293 var child_0 = new Xcls_buffer( _this );
295 this.el.set_buffer ( child_0.el );
299 var description = Pango.FontDescription.from_string("monospace");
300 description.set_size(8000);
301 this.el.override_font(description);
305 var attrs = new Gtk.SourceMarkAttributes();
306 var pink = Gdk.RGBA();
307 pink.parse ( "pink");
308 attrs.set_background ( pink);
309 attrs.set_icon_name ( "process-stop");
310 attrs.query_tooltip_text.connect(( mark) => {
311 //print("tooltip query? %s\n", mark.name);
315 this.el.set_mark_attributes ("ERR", attrs, 1);
317 var wattrs = new Gtk.SourceMarkAttributes();
318 var blue = Gdk.RGBA();
319 blue.parse ( "#ABF4EB");
320 wattrs.set_background ( blue);
321 wattrs.set_icon_name ( "process-stop");
322 wattrs.query_tooltip_text.connect(( mark) => {
323 //print("tooltip query? %s\n", mark.name);
327 this.el.set_mark_attributes ("WARN", wattrs, 1);
331 var dattrs = new Gtk.SourceMarkAttributes();
332 var purple = Gdk.RGBA();
333 purple.parse ( "#EEA9FF");
334 dattrs.set_background ( purple);
335 dattrs.set_icon_name ( "process-stop");
336 dattrs.query_tooltip_text.connect(( mark) => {
337 //print("tooltip query? %s\n", mark.name);
341 this.el.set_mark_attributes ("DEPR", dattrs, 1);
344 this.el.key_release_event.connect( (event) => {
346 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
347 print("SAVE: ctrl-S pressed");
348 _this.saveContents();
351 // print(event.key.keyval)
358 // user defined functions
359 public void load (string str) {
361 // show the help page for the active node..
362 //this.get('/Help').show();
365 // this.get('/BottomPane').el.set_current_page(0);
366 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
367 buf.set_text(str, str.length);
368 buf.set_undo_manager(null);
370 var lm = Gtk.SourceLanguageManager.get_default();
372 if (_this.file != null) {
373 lang = _this.file.language;
375 //?? is javascript going to work as js?
377 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
381 this.el.grab_focus();
382 _this.save_button.el.sensitive = false;
385 public class Xcls_buffer : Object
387 public Gtk.SourceBuffer el;
388 private Editor _this;
392 public bool check_queued;
393 public int error_line;
394 public bool check_running;
397 public Xcls_buffer(Editor _owner )
401 this.el = new Gtk.SourceBuffer( null );
404 this.check_queued = false;
405 this.error_line = -1;
406 this.check_running = false;
408 // set gobject values
411 this.el.changed.connect( () => {
414 _this.save_button.el.sensitive = true;
415 print("EDITOR CHANGED");
420 // this.get('/LeftPanel.model').changed( str , false);
425 // user defined functions
426 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
428 this.error_line = validate_res.size;
430 if (this.error_line < 1) {
433 var tlines = this.el.get_line_count ();
435 var valiter = validate_res.map_iterator();
436 while (valiter.next()) {
438 // print("get inter\n");
439 var eline = valiter.get_key();
440 if (eline > tlines) {
443 this.el.get_iter_at_line( out iter, eline);
444 //print("mark line\n");
445 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
449 public string toString () {
453 this.el.get_start_iter(out s);
454 this.el.get_end_iter(out e);
455 var ret = this.el.get_text(s,e,true);
456 //print("TO STRING? " + ret);
459 public bool checkSyntax () {
461 if (this.check_running) {
462 print("Check is running\n");
463 if (this.check_queued) {
464 print("Check is already queued");
467 this.check_queued = true;
468 print("Adding queued Check ");
469 GLib.Timeout.add_seconds(1, () => {
470 this.check_queued = false;
479 var str = this.toString();
482 if (this.error_line > 0) {
485 this.el.get_bounds (out start, out end);
487 this.el.remove_source_marks (start, end, null);
489 if (str.length < 1) {
490 print("checkSyntax - empty string?\n");
494 if (_this.file == null && _this.fname.length > 0) {
496 // assume it's gtk...
497 this.check_running = true;
499 _this.window.windowstate.valasource.checkPlainFileSpawn(
500 _this.window.project,
507 if (_this.file == null) {
510 var p = Palete.factory(_this.file.xtype);
514 this.check_running = true;
517 if (_this.file.language == "js") {
518 this.check_running = false;
519 print("calling validate javascript\n");
520 return this.highlightErrors(p.validateJavascript(
531 print("calling validate vala\n");
532 // clear the buttons.
536 _this.window.windowstate,
548 //print("done mark line\n");
550 return true; // at present allow saving - even if it's invalid..
552 public bool highlightErrorsJson (string type, Json.Object obj) {
555 this.el.get_bounds (out start, out end);
557 this.el.remove_source_marks (start, end, type);
560 // we should highlight other types of errors..
562 if (!obj.has_member(type)) {
563 print("Return has no errors\n");
567 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
568 _this.window.windowstate.state != WindowState.State.CODE
574 var err = obj.get_object_member(type);
575 var valafn = _this.fname;
576 if (_this.file != null) {
583 var regex = new Regex("\\.bjs$");
586 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
587 } catch (GLib.RegexError e) {
594 if (!err.has_member(valafn)) {
595 print("File path has no errors\n");
599 var lines = err.get_object_member(valafn);
602 if (obj.has_member("line_offset")) {
603 offset = (int)obj.get_int_member("line_offset") + 1;
609 var tlines = this.el.get_line_count () +1;
611 lines.foreach_member((obj, line, node) => {
614 // print("get inter\n");
615 var eline = int.parse(line) - offset;
616 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
619 if (eline > tlines || eline < 0) {
622 this.el.get_iter_at_line( out iter, eline);
623 //print("mark line\n");
624 var msg = "Line: %d".printf(eline+1);
625 var ar = lines.get_array_member(line);
626 for (var i = 0 ; i < ar.get_length(); i++) {
627 msg += (msg.length > 0) ? "\n" : "";
628 msg += ar.get_string_element(i);
632 this.el.create_source_mark(msg, type, iter);