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 );
62 // user defined functions
63 public bool saveContents () {
73 var str = _this.buffer.toString();
78 if (!_this.buffer.checkSyntax()) {
79 print("check syntax failed");
80 //this.get('/StandardErrorDialog').show("Fix errors in code and save..");
84 // LeftPanel.model.changed( str , false);
86 _this.save_button.el.sensitive = false;
91 // find the text for the node..
92 if (ptype == "listener") {
93 this.node.listeners.set(key,str);
96 this.node.props.set(key,str);
107 public void showPlainFile (string fname)
115 GLib.FileUtils.get_contents(fname, out str);
117 str = ""; // a tad dangerios... - perhaps we should block editing...
121 this.key_edit.el.text = "";
124 public void show (JsRender.JsRender file, JsRender.Node node, string ptype, string key)
132 // find the text for the node..
133 if (ptype == "listener") {
134 val = node.listeners.get(key);
137 val = node.props.get(key);
140 this.key_edit.el.text = key;
143 public class Xcls_Box2 : Object
146 private Editor _this;
152 public Xcls_Box2(Editor _owner )
155 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
159 // set gobject values
160 this.el.homogeneous = false;
161 var child_0 = new Xcls_save_button( _this );
163 this.el.pack_start ( child_0.el , false,false );
164 var child_1 = new Xcls_key_edit( _this );
166 this.el.pack_end ( child_1.el , true,true );
169 // user defined functions
171 public class Xcls_save_button : Object
173 public Gtk.Button el;
174 private Editor _this;
180 public Xcls_save_button(Editor _owner )
183 _this.save_button = this;
184 this.el = new Gtk.Button();
188 // set gobject values
189 this.el.label = "Save";
192 this.el.clicked.connect( () => {
193 _this.saveContents();
197 // user defined functions
200 public class Xcls_key_edit : Object
203 private Editor _this;
209 public Xcls_key_edit(Editor _owner )
212 _this.key_edit = this;
213 this.el = new Gtk.Entry();
217 // set gobject values
220 // user defined functions
224 public class Xcls_RightEditor : Object
226 public Gtk.ScrolledWindow el;
227 private Editor _this;
233 public Xcls_RightEditor(Editor _owner )
236 _this.RightEditor = this;
237 this.el = new Gtk.ScrolledWindow( null, null );
241 // set gobject values
242 var child_0 = new Xcls_view( _this );
244 this.el.add ( child_0.el );
248 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
251 // user defined functions
253 public class Xcls_view : Object
255 public Gtk.SourceView el;
256 private Editor _this;
262 public Xcls_view(Editor _owner )
266 this.el = new Gtk.SourceView();
270 // set gobject values
271 this.el.auto_indent = true;
272 this.el.indent_width = 4;
273 this.el.show_line_marks = true;
274 this.el.insert_spaces_instead_of_tabs = true;
275 this.el.show_line_numbers = true;
276 this.el.highlight_current_line = true;
277 var child_0 = new Xcls_buffer( _this );
279 this.el.set_buffer ( child_0.el );
283 var description = Pango.FontDescription.from_string("monospace");
284 description.set_size(8000);
285 this.el.override_font(description);
289 var attrs = new Gtk.SourceMarkAttributes();
290 var pink = Gdk.RGBA();
291 pink.parse ( "pink");
292 attrs.set_background ( pink);
293 attrs.set_icon_name ( "process-stop");
294 attrs.query_tooltip_text.connect(( mark) => {
295 //print("tooltip query? %s\n", mark.name);
299 this.el.set_mark_attributes ("ERR", attrs, 1);
301 var wattrs = new Gtk.SourceMarkAttributes();
302 var blue = Gdk.RGBA();
303 blue.parse ( "#ABF4EB");
304 wattrs.set_background ( blue);
305 wattrs.set_icon_name ( "process-stop");
306 wattrs.query_tooltip_text.connect(( mark) => {
307 //print("tooltip query? %s\n", mark.name);
311 this.el.set_mark_attributes ("WARN", wattrs, 1);
315 var dattrs = new Gtk.SourceMarkAttributes();
316 var purple = Gdk.RGBA();
317 purple.parse ( "#EEA9FF");
318 dattrs.set_background ( purple);
319 dattrs.set_icon_name ( "process-stop");
320 dattrs.query_tooltip_text.connect(( mark) => {
321 //print("tooltip query? %s\n", mark.name);
325 this.el.set_mark_attributes ("DEPR", dattrs, 1);
328 this.el.key_release_event.connect( (event) => {
330 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
331 print("SAVE: ctrl-S pressed");
332 _this.saveContents();
335 // print(event.key.keyval)
342 // user defined functions
343 public void load (string str) {
345 // show the help page for the active node..
346 //this.get('/Help').show();
349 // this.get('/BottomPane').el.set_current_page(0);
350 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
351 buf.set_text(str, str.length);
352 buf.set_undo_manager(null);
354 var lm = Gtk.SourceLanguageManager.get_default();
356 var lang = _this.file.language;
357 //?? is javascript going to work as js?
359 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
363 this.el.grab_focus();
364 _this.save_button.el.sensitive = false;
367 public class Xcls_buffer : Object
369 public Gtk.SourceBuffer el;
370 private Editor _this;
374 public bool check_queued;
375 public int error_line;
376 public bool check_running;
379 public Xcls_buffer(Editor _owner )
383 this.el = new Gtk.SourceBuffer( null );
386 this.check_queued = false;
387 this.error_line = -1;
388 this.check_running = false;
390 // set gobject values
393 this.el.changed.connect( () => {
396 _this.save_button.el.sensitive = true;
397 print("EDITOR CHANGED");
402 // this.get('/LeftPanel.model').changed( str , false);
407 // user defined functions
408 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
409 this.error_line = validate_res.size;
411 if (this.error_line < 1) {
414 var tlines = this.el.get_line_count ();
416 var valiter = validate_res.map_iterator();
417 while (valiter.next()) {
419 // print("get inter\n");
420 var eline = valiter.get_key();
421 if (eline > tlines) {
424 this.el.get_iter_at_line( out iter, eline);
425 //print("mark line\n");
426 this.el.create_source_mark(valiter.get_value(), "error", iter);
430 public string toString () {
434 this.el.get_start_iter(out s);
435 this.el.get_end_iter(out e);
436 var ret = this.el.get_text(s,e,true);
437 //print("TO STRING? " + ret);
440 public bool checkSyntax () {
442 if (this.check_running) {
443 print("Check is running\n");
444 if (this.check_queued) {
445 print("Check is already queued");
448 this.check_queued = true;
449 print("Adding queued Check ");
450 GLib.Timeout.add_seconds(1, () => {
451 this.check_queued = false;
462 var p = Palete.factory(_this.file.xtype);
464 var str = this.toString();
467 if (this.error_line > 0) {
470 this.el.get_bounds (out start, out end);
472 this.el.remove_source_marks (start, end, null);
475 if (str.length < 1) {
476 print("checkSyntax - empty string?\n");
479 this.check_running = true;
482 if (_this.file.language == "js") {
483 this.check_running = false;
484 print("calling validate javascript\n");
485 return this.highlightErrors(p.validateJavascript(
496 print("calling validate vala\n");
497 // clear the buttons.
501 _this.window.windowstate,
513 //print("done mark line\n");
515 return true; // at present allow saving - even if it's invalid..
517 public bool highlightErrorsJson (string type, Json.Object obj) {
520 this.el.get_bounds (out start, out end);
522 this.el.remove_source_marks (start, end, type);
525 // we should highlight other types of errors..
527 if (!obj.has_member(type)) {
528 print("Return has no errors\n");
532 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
533 _this.window.windowstate.state != WindowState.State.CODE
538 if (_this.file == null) {
542 var err = obj.get_object_member(type);
547 var regex = new Regex("\\.bjs$");
550 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
551 } catch (GLib.RegexError e) {
555 if (!err.has_member(valafn)) {
556 print("File path has no errors\n");
560 var lines = err.get_object_member(valafn);
563 if (obj.has_member("line_offset")) {
564 offset = (int)obj.get_int_member("line_offset") + 1;
568 var tlines = this.el.get_line_count () +1;
570 lines.foreach_member((obj, line, node) => {
573 // print("get inter\n");
574 var eline = int.parse(line) - offset;
575 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
578 if (eline > tlines || eline < 0) {
581 this.el.get_iter_at_line( out iter, eline);
582 //print("mark line\n");
583 var msg = "Line: %d".printf(eline+1);
584 var ar = lines.get_array_member(line);
585 for (var i = 0 ; i < ar.get_length(); i++) {
586 msg += (msg.length > 0) ? "\n" : "";
587 msg += ar.get_string_element(i);
591 this.el.create_source_mark(msg, type, iter);