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();
364 if (_this.file != null) {
365 lang = _this.file.language;
367 //?? is javascript going to work as js?
369 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
373 this.el.grab_focus();
374 _this.save_button.el.sensitive = false;
377 public class Xcls_buffer : Object
379 public Gtk.SourceBuffer el;
380 private Editor _this;
384 public bool check_queued;
385 public int error_line;
386 public bool check_running;
389 public Xcls_buffer(Editor _owner )
393 this.el = new Gtk.SourceBuffer( null );
396 this.check_queued = false;
397 this.error_line = -1;
398 this.check_running = false;
400 // set gobject values
403 this.el.changed.connect( () => {
406 _this.save_button.el.sensitive = true;
407 print("EDITOR CHANGED");
412 // this.get('/LeftPanel.model').changed( str , false);
417 // user defined functions
418 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
419 this.error_line = validate_res.size;
421 if (this.error_line < 1) {
424 var tlines = this.el.get_line_count ();
426 var valiter = validate_res.map_iterator();
427 while (valiter.next()) {
429 // print("get inter\n");
430 var eline = valiter.get_key();
431 if (eline > tlines) {
434 this.el.get_iter_at_line( out iter, eline);
435 //print("mark line\n");
436 this.el.create_source_mark(valiter.get_value(), "error", iter);
440 public string toString () {
444 this.el.get_start_iter(out s);
445 this.el.get_end_iter(out e);
446 var ret = this.el.get_text(s,e,true);
447 //print("TO STRING? " + ret);
450 public bool checkSyntax () {
452 if (this.check_running) {
453 print("Check is running\n");
454 if (this.check_queued) {
455 print("Check is already queued");
458 this.check_queued = true;
459 print("Adding queued Check ");
460 GLib.Timeout.add_seconds(1, () => {
461 this.check_queued = false;
472 var p = Palete.factory(_this.file.xtype);
474 var str = this.toString();
477 if (this.error_line > 0) {
480 this.el.get_bounds (out start, out end);
482 this.el.remove_source_marks (start, end, null);
485 if (str.length < 1) {
486 print("checkSyntax - empty string?\n");
489 this.check_running = true;
492 if (_this.file.language == "js") {
493 this.check_running = false;
494 print("calling validate javascript\n");
495 return this.highlightErrors(p.validateJavascript(
506 print("calling validate vala\n");
507 // clear the buttons.
511 _this.window.windowstate,
523 //print("done mark line\n");
525 return true; // at present allow saving - even if it's invalid..
527 public bool highlightErrorsJson (string type, Json.Object obj) {
530 this.el.get_bounds (out start, out end);
532 this.el.remove_source_marks (start, end, type);
535 // we should highlight other types of errors..
537 if (!obj.has_member(type)) {
538 print("Return has no errors\n");
542 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
543 _this.window.windowstate.state != WindowState.State.CODE
548 if (_this.file == null) {
552 var err = obj.get_object_member(type);
557 var regex = new Regex("\\.bjs$");
560 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
561 } catch (GLib.RegexError e) {
565 if (!err.has_member(valafn)) {
566 print("File path has no errors\n");
570 var lines = err.get_object_member(valafn);
573 if (obj.has_member("line_offset")) {
574 offset = (int)obj.get_int_member("line_offset") + 1;
578 var tlines = this.el.get_line_count () +1;
580 lines.foreach_member((obj, line, node) => {
583 // print("get inter\n");
584 var eline = int.parse(line) - offset;
585 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
588 if (eline > tlines || eline < 0) {
591 this.el.get_iter_at_line( out iter, eline);
592 //print("mark line\n");
593 var msg = "Line: %d".printf(eline+1);
594 var ar = lines.get_array_member(line);
595 for (var i = 0 ; i < ar.get_length(); i++) {
596 msg += (msg.length > 0) ? "\n" : "";
597 msg += ar.get_string_element(i);
601 this.el.create_source_mark(msg, type, iter);