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;
32 public signal void save ();
33 public JsRender.Node node;
39 this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
43 this.activeEditor = "";
52 this.el.homogeneous = false;
53 this.el.expand = true;
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 );
68 // user defined functions
69 public bool saveContents () {
79 var str = _this.buffer.toString();
84 if (!_this.buffer.checkSyntax()) {
85 print("check syntax failed");
86 //this.get('/StandardErrorDialog').show("Fix errors in code and save..");
90 // LeftPanel.model.changed( str , false);
92 _this.save_button.el.sensitive = false;
97 // find the text for the node..
98 if (ptype == "listener") {
99 this.node.listeners.set(key,str);
102 this.node.props.set(key,str);
113 public void showPlainFile (string fname)
122 GLib.FileUtils.get_contents(fname, out str);
124 str = ""; // a tad dangerios... - perhaps we should block editing...
128 this.key_edit.el.text = "";
131 public void show (JsRender.JsRender file, JsRender.Node node, string ptype, string key)
139 // find the text for the node..
140 if (ptype == "listener") {
141 val = node.listeners.get(key);
144 val = node.props.get(key);
147 this.key_edit.el.text = key;
150 public class Xcls_Box2 : Object
153 private Editor _this;
159 public Xcls_Box2(Editor _owner )
162 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
166 // set gobject values
167 this.el.homogeneous = false;
168 var child_0 = new Xcls_save_button( _this );
170 this.el.pack_start ( child_0.el , false,false );
171 var child_1 = new Xcls_key_edit( _this );
173 this.el.pack_end ( child_1.el , true,true );
176 // user defined functions
178 public class Xcls_save_button : Object
180 public Gtk.Button el;
181 private Editor _this;
187 public Xcls_save_button(Editor _owner )
190 _this.save_button = this;
191 this.el = new Gtk.Button();
195 // set gobject values
196 this.el.label = "Save";
199 this.el.clicked.connect( () => {
200 _this.saveContents();
204 // user defined functions
207 public class Xcls_key_edit : Object
210 private Editor _this;
216 public Xcls_key_edit(Editor _owner )
219 _this.key_edit = this;
220 this.el = new Gtk.Entry();
224 // set gobject values
227 // user defined functions
231 public class Xcls_RightEditor : Object
233 public Gtk.ScrolledWindow el;
234 private Editor _this;
240 public Xcls_RightEditor(Editor _owner )
243 _this.RightEditor = this;
244 this.el = new Gtk.ScrolledWindow( null, null );
248 // set gobject values
249 var child_0 = new Xcls_view( _this );
251 this.el.add ( child_0.el );
255 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
258 // user defined functions
260 public class Xcls_view : Object
262 public Gtk.SourceView el;
263 private Editor _this;
269 public Xcls_view(Editor _owner )
273 this.el = new Gtk.SourceView();
277 // set gobject values
278 this.el.auto_indent = true;
279 this.el.indent_width = 4;
280 this.el.show_line_marks = true;
281 this.el.insert_spaces_instead_of_tabs = true;
282 this.el.show_line_numbers = true;
283 this.el.highlight_current_line = true;
284 var child_0 = new Xcls_buffer( _this );
286 this.el.set_buffer ( child_0.el );
290 var description = Pango.FontDescription.from_string("monospace");
291 description.set_size(8000);
292 this.el.override_font(description);
296 var attrs = new Gtk.SourceMarkAttributes();
297 var pink = Gdk.RGBA();
298 pink.parse ( "pink");
299 attrs.set_background ( pink);
300 attrs.set_icon_name ( "process-stop");
301 attrs.query_tooltip_text.connect(( mark) => {
302 //print("tooltip query? %s\n", mark.name);
306 this.el.set_mark_attributes ("ERR", attrs, 1);
308 var wattrs = new Gtk.SourceMarkAttributes();
309 var blue = Gdk.RGBA();
310 blue.parse ( "#ABF4EB");
311 wattrs.set_background ( blue);
312 wattrs.set_icon_name ( "process-stop");
313 wattrs.query_tooltip_text.connect(( mark) => {
314 //print("tooltip query? %s\n", mark.name);
318 this.el.set_mark_attributes ("WARN", wattrs, 1);
322 var dattrs = new Gtk.SourceMarkAttributes();
323 var purple = Gdk.RGBA();
324 purple.parse ( "#EEA9FF");
325 dattrs.set_background ( purple);
326 dattrs.set_icon_name ( "process-stop");
327 dattrs.query_tooltip_text.connect(( mark) => {
328 //print("tooltip query? %s\n", mark.name);
332 this.el.set_mark_attributes ("DEPR", dattrs, 1);
335 this.el.key_release_event.connect( (event) => {
337 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
338 print("SAVE: ctrl-S pressed");
339 _this.saveContents();
342 // print(event.key.keyval)
349 // user defined functions
350 public void load (string str) {
352 // show the help page for the active node..
353 //this.get('/Help').show();
356 // this.get('/BottomPane').el.set_current_page(0);
357 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
358 buf.set_text(str, str.length);
359 buf.set_undo_manager(null);
361 var lm = Gtk.SourceLanguageManager.get_default();
363 var lang = _this.file.language;
364 //?? is javascript going to work as js?
366 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
370 this.el.grab_focus();
371 _this.save_button.el.sensitive = false;
374 public class Xcls_buffer : Object
376 public Gtk.SourceBuffer el;
377 private Editor _this;
381 public bool check_queued;
382 public int error_line;
383 public bool check_running;
386 public Xcls_buffer(Editor _owner )
390 this.el = new Gtk.SourceBuffer( null );
393 this.check_queued = false;
394 this.error_line = -1;
395 this.check_running = false;
397 // set gobject values
400 this.el.changed.connect( () => {
403 _this.save_button.el.sensitive = true;
404 print("EDITOR CHANGED");
409 // this.get('/LeftPanel.model').changed( str , false);
414 // user defined functions
415 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
416 this.error_line = validate_res.size;
418 if (this.error_line < 1) {
421 var tlines = this.el.get_line_count ();
423 var valiter = validate_res.map_iterator();
424 while (valiter.next()) {
426 // print("get inter\n");
427 var eline = valiter.get_key();
428 if (eline > tlines) {
431 this.el.get_iter_at_line( out iter, eline);
432 //print("mark line\n");
433 this.el.create_source_mark(valiter.get_value(), "error", iter);
437 public string toString () {
441 this.el.get_start_iter(out s);
442 this.el.get_end_iter(out e);
443 var ret = this.el.get_text(s,e,true);
444 //print("TO STRING? " + ret);
447 public bool checkSyntax () {
449 if (this.check_running) {
450 print("Check is running\n");
451 if (this.check_queued) {
452 print("Check is already queued");
455 this.check_queued = true;
456 print("Adding queued Check ");
457 GLib.Timeout.add_seconds(1, () => {
458 this.check_queued = false;
469 var p = Palete.factory(_this.file.xtype);
471 var str = this.toString();
474 if (this.error_line > 0) {
477 this.el.get_bounds (out start, out end);
479 this.el.remove_source_marks (start, end, null);
482 if (str.length < 1) {
483 print("checkSyntax - empty string?\n");
486 this.check_running = true;
489 if (_this.file.language == "js") {
490 this.check_running = false;
491 print("calling validate javascript\n");
492 return this.highlightErrors(p.validateJavascript(
503 print("calling validate vala\n");
504 // clear the buttons.
508 _this.window.windowstate,
520 //print("done mark line\n");
522 return true; // at present allow saving - even if it's invalid..
524 public bool highlightErrorsJson (string type, Json.Object obj) {
527 this.el.get_bounds (out start, out end);
529 this.el.remove_source_marks (start, end, type);
532 // we should highlight other types of errors..
534 if (!obj.has_member(type)) {
535 print("Return has no errors\n");
539 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
540 _this.window.windowstate.state != WindowState.State.CODE
545 if (_this.file == null) {
549 var err = obj.get_object_member(type);
554 var regex = new Regex("\\.bjs$");
557 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
558 } catch (GLib.RegexError e) {
562 if (!err.has_member(valafn)) {
563 print("File path has no errors\n");
567 var lines = err.get_object_member(valafn);
570 if (obj.has_member("line_offset")) {
571 offset = (int)obj.get_int_member("line_offset") + 1;
575 var tlines = this.el.get_line_count () +1;
577 lines.foreach_member((obj, line, node) => {
580 // print("get inter\n");
581 var eline = int.parse(line) - offset;
582 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
585 if (eline > tlines || eline < 0) {
588 this.el.get_iter_at_line( out iter, eline);
589 //print("mark line\n");
590 var msg = "Line: %d".printf(eline+1);
591 var ar = lines.get_array_member(line);
592 for (var i = 0 ; i < ar.get_length(); i++) {
593 msg += (msg.length > 0) ? "\n" : "";
594 msg += ar.get_string_element(i);
598 this.el.create_source_mark(msg, type, iter);