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();
77 _this.buffer.checkSyntax();
81 // LeftPanel.model.changed( str , false);
83 _this.save_button.el.sensitive = false;
85 // find the text for the node..
86 if (_this.file.xtype != "PlainFile") {
87 if (ptype == "listener") {
88 this.node.listeners.set(key,str);
91 this.node.props.set(key,str);
102 public void show (JsRender.JsRender file, JsRender.Node node, string ptype, string key)
110 if (file.xtype != "PlainFile") {
116 // find the text for the node..
117 if (ptype == "listener") {
118 val = node.listeners.get(key);
121 val = node.props.get(key);
127 this.view.load( file.toSource() );
130 this.key_edit.el.text = key;
135 public class Xcls_Box2 : Object
138 private Editor _this;
144 public Xcls_Box2(Editor _owner )
147 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
151 // set gobject values
152 this.el.homogeneous = false;
153 var child_0 = new Xcls_save_button( _this );
155 this.el.pack_start ( child_0.el , false,false );
156 var child_1 = new Xcls_key_edit( _this );
158 this.el.pack_end ( child_1.el , true,true );
161 // user defined functions
163 public class Xcls_save_button : Object
165 public Gtk.Button el;
166 private Editor _this;
172 public Xcls_save_button(Editor _owner )
175 _this.save_button = this;
176 this.el = new Gtk.Button();
180 // set gobject values
181 this.el.label = "Save";
184 this.el.clicked.connect( () => {
185 _this.saveContents();
189 // user defined functions
192 public class Xcls_key_edit : Object
195 private Editor _this;
201 public Xcls_key_edit(Editor _owner )
204 _this.key_edit = this;
205 this.el = new Gtk.Entry();
209 // set gobject values
212 // user defined functions
216 public class Xcls_RightEditor : Object
218 public Gtk.ScrolledWindow el;
219 private Editor _this;
225 public Xcls_RightEditor(Editor _owner )
228 _this.RightEditor = this;
229 this.el = new Gtk.ScrolledWindow( null, null );
233 // set gobject values
234 var child_0 = new Xcls_view( _this );
236 this.el.add ( child_0.el );
240 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
243 // user defined functions
245 public class Xcls_view : Object
247 public Gtk.SourceView el;
248 private Editor _this;
254 public Xcls_view(Editor _owner )
258 this.el = new Gtk.SourceView();
262 // set gobject values
263 this.el.auto_indent = true;
264 this.el.indent_width = 4;
265 this.el.show_line_marks = true;
266 this.el.insert_spaces_instead_of_tabs = true;
267 this.el.show_line_numbers = true;
268 this.el.highlight_current_line = true;
269 var child_0 = new Xcls_buffer( _this );
271 this.el.set_buffer ( child_0.el );
275 var description = Pango.FontDescription.from_string("monospace");
276 description.set_size(8000);
277 this.el.override_font(description);
281 var attrs = new Gtk.SourceMarkAttributes();
282 var pink = Gdk.RGBA();
283 pink.parse ( "pink");
284 attrs.set_background ( pink);
285 attrs.set_icon_name ( "process-stop");
286 attrs.query_tooltip_text.connect(( mark) => {
287 //print("tooltip query? %s\n", mark.name);
291 this.el.set_mark_attributes ("ERR", attrs, 1);
293 var wattrs = new Gtk.SourceMarkAttributes();
294 var blue = Gdk.RGBA();
295 blue.parse ( "#ABF4EB");
296 wattrs.set_background ( blue);
297 wattrs.set_icon_name ( "process-stop");
298 wattrs.query_tooltip_text.connect(( mark) => {
299 //print("tooltip query? %s\n", mark.name);
303 this.el.set_mark_attributes ("WARN", wattrs, 1);
307 var dattrs = new Gtk.SourceMarkAttributes();
308 var purple = Gdk.RGBA();
309 purple.parse ( "#EEA9FF");
310 dattrs.set_background ( purple);
311 dattrs.set_icon_name ( "process-stop");
312 dattrs.query_tooltip_text.connect(( mark) => {
313 //print("tooltip query? %s\n", mark.name);
317 this.el.set_mark_attributes ("DEPR", dattrs, 1);
320 this.el.key_release_event.connect( (event) => {
322 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
323 print("SAVE: ctrl-S pressed");
324 _this.saveContents();
327 // print(event.key.keyval)
334 // user defined functions
335 public void load (string str) {
337 // show the help page for the active node..
338 //this.get('/Help').show();
341 // this.get('/BottomPane').el.set_current_page(0);
342 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
343 buf.set_text(str, str.length);
344 buf.set_undo_manager(null);
346 var lm = Gtk.SourceLanguageManager.get_default();
348 if (_this.file != null) {
349 lang = _this.file.language;
351 //?? is javascript going to work as js?
353 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
357 this.el.grab_focus();
358 _this.save_button.el.sensitive = false;
361 public class Xcls_buffer : Object
363 public Gtk.SourceBuffer el;
364 private Editor _this;
368 public bool check_queued;
369 public int error_line;
370 public bool check_running;
373 public Xcls_buffer(Editor _owner )
377 this.el = new Gtk.SourceBuffer( null );
380 this.check_queued = false;
381 this.error_line = -1;
382 this.check_running = false;
384 // set gobject values
387 this.el.changed.connect( () => {
390 _this.save_button.el.sensitive = true;
391 print("EDITOR CHANGED");
396 // this.get('/LeftPanel.model').changed( str , false);
401 // user defined functions
402 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
404 this.error_line = validate_res.size;
406 if (this.error_line < 1) {
409 var tlines = this.el.get_line_count ();
411 var valiter = validate_res.map_iterator();
412 while (valiter.next()) {
414 // print("get inter\n");
415 var eline = valiter.get_key();
416 if (eline > tlines) {
419 this.el.get_iter_at_line( out iter, eline);
420 //print("mark line\n");
421 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
425 public string toString () {
429 this.el.get_start_iter(out s);
430 this.el.get_end_iter(out e);
431 var ret = this.el.get_text(s,e,true);
432 //print("TO STRING? " + ret);
435 public bool checkSyntax () {
437 if (this.check_running) {
438 print("Check is running\n");
439 if (this.check_queued) {
440 print("Check is already queued");
443 this.check_queued = true;
444 print("Adding queued Check ");
445 GLib.Timeout.add_seconds(1, () => {
446 this.check_queued = false;
455 var str = this.toString();
458 if (this.error_line > 0) {
461 this.el.get_bounds (out start, out end);
463 this.el.remove_source_marks (start, end, null);
465 if (str.length < 1) {
466 print("checkSyntax - empty string?\n");
470 if (_this.file == null && _this.fname.length > 0) {
472 // assume it's gtk...
473 this.check_running = true;
475 _this.window.windowstate.valasource.checkPlainFileSpawn(
476 _this.window.project,
483 if (_this.file == null) {
486 var p = Palete.factory(_this.file.xtype);
490 this.check_running = true;
493 if (_this.file.language == "js") {
494 this.check_running = false;
495 print("calling validate javascript\n");
496 return this.highlightErrors(p.validateJavascript(
507 print("calling validate vala\n");
508 // clear the buttons.
512 _this.window.windowstate,
524 //print("done mark line\n");
526 return true; // at present allow saving - even if it's invalid..
528 public bool highlightErrorsJson (string type, Json.Object obj) {
531 this.el.get_bounds (out start, out end);
533 this.el.remove_source_marks (start, end, type);
536 // we should highlight other types of errors..
538 if (!obj.has_member(type)) {
539 print("Return has no errors\n");
543 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
544 _this.window.windowstate.state != WindowState.State.CODE
550 var err = obj.get_object_member(type);
551 var valafn = _this.fname;
552 if (_this.file != null) {
559 var regex = new Regex("\\.bjs$");
562 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
563 } catch (GLib.RegexError e) {
570 if (!err.has_member(valafn)) {
571 print("File path has no errors\n");
575 var lines = err.get_object_member(valafn);
578 if (obj.has_member("line_offset")) {
579 offset = (int)obj.get_int_member("line_offset") + 1;
585 var tlines = this.el.get_line_count () +1;
587 lines.foreach_member((obj, line, node) => {
590 // print("get inter\n");
591 var eline = int.parse(line) - offset;
592 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
595 if (eline > tlines || eline < 0) {
598 this.el.get_iter_at_line( out iter, eline);
599 //print("mark line\n");
600 var msg = "Line: %d".printf(eline+1);
601 var ar = lines.get_array_member(line);
602 for (var i = 0 ; i < ar.get_length(); i++) {
603 msg += (msg.length > 0) ? "\n" : "";
604 msg += ar.get_string_element(i);
608 this.el.create_source_mark(msg, type, iter);