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 // find the text for the node..
136 if (ptype == "listener") {
137 val = node.listeners.get(key);
140 val = node.props.get(key);
143 this.key_edit.el.text = key;
146 public class Xcls_Box2 : Object
149 private Editor _this;
155 public Xcls_Box2(Editor _owner )
158 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
162 // set gobject values
163 this.el.homogeneous = false;
164 var child_0 = new Xcls_save_button( _this );
166 this.el.pack_start ( child_0.el , false,false );
167 var child_1 = new Xcls_key_edit( _this );
169 this.el.pack_end ( child_1.el , true,true );
172 // user defined functions
174 public class Xcls_save_button : Object
176 public Gtk.Button el;
177 private Editor _this;
183 public Xcls_save_button(Editor _owner )
186 _this.save_button = this;
187 this.el = new Gtk.Button();
191 // set gobject values
192 this.el.label = "Save";
195 this.el.clicked.connect( () => {
196 _this.saveContents();
200 // user defined functions
203 public class Xcls_key_edit : Object
206 private Editor _this;
212 public Xcls_key_edit(Editor _owner )
215 _this.key_edit = this;
216 this.el = new Gtk.Entry();
220 // set gobject values
223 // user defined functions
227 public class Xcls_RightEditor : Object
229 public Gtk.ScrolledWindow el;
230 private Editor _this;
236 public Xcls_RightEditor(Editor _owner )
239 _this.RightEditor = this;
240 this.el = new Gtk.ScrolledWindow( null, null );
244 // set gobject values
245 var child_0 = new Xcls_view( _this );
247 this.el.add ( child_0.el );
251 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
254 // user defined functions
256 public class Xcls_view : Object
258 public Gtk.SourceView el;
259 private Editor _this;
265 public Xcls_view(Editor _owner )
269 this.el = new Gtk.SourceView();
273 // set gobject values
274 this.el.auto_indent = true;
275 this.el.indent_width = 4;
276 this.el.show_line_marks = true;
277 this.el.insert_spaces_instead_of_tabs = true;
278 this.el.show_line_numbers = true;
279 this.el.highlight_current_line = true;
280 var child_0 = new Xcls_buffer( _this );
282 this.el.set_buffer ( child_0.el );
286 var description = Pango.FontDescription.from_string("monospace");
287 description.set_size(8000);
288 this.el.override_font(description);
292 var attrs = new Gtk.SourceMarkAttributes();
293 var pink = Gdk.RGBA();
294 pink.parse ( "pink");
295 attrs.set_background ( pink);
296 attrs.set_icon_name ( "process-stop");
297 attrs.query_tooltip_text.connect(( mark) => {
298 //print("tooltip query? %s\n", mark.name);
302 this.el.set_mark_attributes ("ERR", attrs, 1);
304 var wattrs = new Gtk.SourceMarkAttributes();
305 var blue = Gdk.RGBA();
306 blue.parse ( "#ABF4EB");
307 wattrs.set_background ( blue);
308 wattrs.set_icon_name ( "process-stop");
309 wattrs.query_tooltip_text.connect(( mark) => {
310 //print("tooltip query? %s\n", mark.name);
314 this.el.set_mark_attributes ("WARN", wattrs, 1);
318 var dattrs = new Gtk.SourceMarkAttributes();
319 var purple = Gdk.RGBA();
320 purple.parse ( "#EEA9FF");
321 dattrs.set_background ( purple);
322 dattrs.set_icon_name ( "process-stop");
323 dattrs.query_tooltip_text.connect(( mark) => {
324 //print("tooltip query? %s\n", mark.name);
328 this.el.set_mark_attributes ("DEPR", dattrs, 1);
331 this.el.key_release_event.connect( (event) => {
333 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
334 print("SAVE: ctrl-S pressed");
335 _this.saveContents();
338 // print(event.key.keyval)
345 // user defined functions
346 public void load (string str) {
348 // show the help page for the active node..
349 //this.get('/Help').show();
352 // this.get('/BottomPane').el.set_current_page(0);
353 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
354 buf.set_text(str, str.length);
355 buf.set_undo_manager(null);
357 var lm = Gtk.SourceLanguageManager.get_default();
359 if (_this.file != null) {
360 lang = _this.file.language;
362 //?? is javascript going to work as js?
364 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
368 this.el.grab_focus();
369 _this.save_button.el.sensitive = false;
372 public class Xcls_buffer : Object
374 public Gtk.SourceBuffer el;
375 private Editor _this;
379 public bool check_queued;
380 public int error_line;
381 public bool check_running;
384 public Xcls_buffer(Editor _owner )
388 this.el = new Gtk.SourceBuffer( null );
391 this.check_queued = false;
392 this.error_line = -1;
393 this.check_running = false;
395 // set gobject values
398 this.el.changed.connect( () => {
401 _this.save_button.el.sensitive = true;
402 print("EDITOR CHANGED");
407 // this.get('/LeftPanel.model').changed( str , false);
412 // user defined functions
413 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
414 while (Gtk.events_pending()) {
415 Gtk.main_iteration();
418 this.error_line = validate_res.size;
420 if (this.error_line < 1) {
423 var tlines = this.el.get_line_count ();
425 var valiter = validate_res.map_iterator();
426 while (valiter.next()) {
428 // print("get inter\n");
429 var eline = valiter.get_key();
430 if (eline > tlines) {
433 this.el.get_iter_at_line( out iter, eline);
434 //print("mark line\n");
435 this.el.create_source_mark(valiter.get_value(), "error", iter);
439 public string toString () {
443 this.el.get_start_iter(out s);
444 this.el.get_end_iter(out e);
445 var ret = this.el.get_text(s,e,true);
446 //print("TO STRING? " + ret);
449 public bool checkSyntax () {
451 if (this.check_running) {
452 print("Check is running\n");
453 if (this.check_queued) {
454 print("Check is already queued");
457 this.check_queued = true;
458 print("Adding queued Check ");
459 GLib.Timeout.add_seconds(1, () => {
460 this.check_queued = false;
469 var str = this.toString();
472 if (this.error_line > 0) {
475 this.el.get_bounds (out start, out end);
477 this.el.remove_source_marks (start, end, null);
479 if (str.length < 1) {
480 print("checkSyntax - empty string?\n");
484 if (_this.file == null && _this.fname.length > 0) {
486 // assume it's gtk...
487 this.check_running = true;
489 _this.window.windowstate.valasource.checkPlainFileSpawn(
490 _this.window.project,
497 if (_this.file == null) {
500 var p = Palete.factory(_this.file.xtype);
504 this.check_running = true;
507 if (_this.file.language == "js") {
508 this.check_running = false;
509 print("calling validate javascript\n");
510 return this.highlightErrors(p.validateJavascript(
521 print("calling validate vala\n");
522 // clear the buttons.
526 _this.window.windowstate,
538 //print("done mark line\n");
540 return true; // at present allow saving - even if it's invalid..
542 public bool highlightErrorsJson (string type, Json.Object obj) {
545 this.el.get_bounds (out start, out end);
547 this.el.remove_source_marks (start, end, type);
550 // we should highlight other types of errors..
552 if (!obj.has_member(type)) {
553 print("Return has no errors\n");
557 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
558 _this.window.windowstate.state != WindowState.State.CODE
564 var err = obj.get_object_member(type);
565 var valafn = _this.fname;
566 if (_this.file != null) {
573 var regex = new Regex("\\.bjs$");
576 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
577 } catch (GLib.RegexError e) {
584 if (!err.has_member(valafn)) {
585 print("File path has no errors\n");
589 var lines = err.get_object_member(valafn);
592 if (obj.has_member("line_offset")) {
593 offset = (int)obj.get_int_member("line_offset") + 1;
599 var tlines = this.el.get_line_count () +1;
601 lines.foreach_member((obj, line, node) => {
604 // print("get inter\n");
605 var eline = int.parse(line) - offset;
606 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
609 if (eline > tlines || eline < 0) {
612 this.el.get_iter_at_line( out iter, eline);
613 //print("mark line\n");
614 var msg = "Line: %d".printf(eline+1);
615 var ar = lines.get_array_member(line);
616 for (var i = 0 ; i < ar.get_length(); i++) {
617 msg += (msg.length > 0) ? "\n" : "";
618 msg += ar.get_string_element(i);
622 this.el.create_source_mark(msg, type, iter);